Search Apps Documentation Source Content File Folder Download Copy Actions Download

z7a_recv_packet_filetest.gno

8.84 Kb · 269 lines
  1// PKGPATH: gno.land/r/aib/main
  2package main
  3
  4import (
  5	"encoding/hex"
  6	"time"
  7
  8	tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
  9	"gno.land/p/aib/ibc/types"
 10	"gno.land/p/aib/ics23"
 11	appstesting "gno.land/r/aib/ibc/apps/testing"
 12	"gno.land/r/aib/ibc/core"
 13)
 14
 15// RecvPacket success
 16func main(cur realm) {
 17	var (
 18		chainID       = "chain-id-2"
 19		trustedHeight = types.NewHeight(2, 2)
 20		clientState   = tmtesting.NewClientState(chainID, trustedHeight)
 21		// NOTE apphash generated by the gen-proof command below
 22		apphash, _     = hex.DecodeString("8da82bb625ee946f4b7aa03a82010cb115f9ce3879c7caad8d92e71eb821996e")
 23		trustedValset  = tmtesting.GenValset()
 24		consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
 25		counterpartyID = "07-tendermint-42"
 26	)
 27	clientID := core.CreateClient(cross(cur), clientState, consensusState)
 28	core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
 29
 30	// Register app
 31	var (
 32		app       = appstesting.NewApp(cross(cur))
 33		appPortID = "appID"
 34	)
 35	app.SetOnRecvPacketReturn(types.RecvPacketResult{
 36		Status:          types.PacketStatus_Success,
 37		Acknowledgement: []byte{0x01},
 38	})
 39	core.RegisterApp(cross(cur), appPortID, app)
 40
 41	// Receive the packet
 42	// Generate the proof of existence of the packet commitment for the
 43	// counterparty chain.
 44	specs := ics23.IavlSpec()
 45	// NOTE code generated by:
 46	// go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'packet' '{"sequence":1,"source_client":"07-tendermint-42","destination_client":"07-tendermint-1","timeout_timestamp":1234571490,"payloads":[{"source_port":"appID","destination_port":"appID","encoding":"application/json","value":"e30=","version":"v1"}]}'
 47	proof := []ics23.CommitmentProof{
 48
 49		// iavl proof
 50		ics23.CommitmentProof_Exist{
 51			Exist: &ics23.ExistenceProof{
 52				Key:   []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x34\x32\x01\x00\x00\x00\x00\x00\x00\x00\x01"),
 53				Value: []byte("\x23\x99\xf6\x84\x16\xb7\xd0\x09\x3b\xe4\x9e\x5f\x8e\xe1\xbe\x1c\x8e\x07\xa0\x93\xc5\x67\x09\x03\x46\xce\x36\xc9\x7d\x11\x71\x71"),
 54				Leaf: &ics23.LeafOp{
 55					Hash:         specs.LeafSpec.Hash,
 56					PrehashKey:   specs.LeafSpec.PrehashKey,
 57					PrehashValue: specs.LeafSpec.PrehashValue,
 58					Length:       specs.LeafSpec.Length,
 59					Prefix:       []byte("\x00\x02\x02"),
 60				},
 61				Path: []*ics23.InnerOp{
 62					{
 63						Hash:   specs.InnerSpec.Hash,
 64						Prefix: []byte("\x02\x04\x02\x20\x35\xf8\xea\x80\x53\x90\xe0\x84\x85\x4f\x39\x9b\x42\xcc\xde\xae\xa3\x3a\x1d\xed\xc1\x15\x63\x8a\xc4\x8d\x06\x00\x63\x7d\xba\x1f\x20"),
 65						Suffix: []byte(""),
 66					},
 67					{
 68						Hash:   specs.InnerSpec.Hash,
 69						Prefix: []byte("\x04\x08\x02\x20"),
 70						Suffix: []byte("\x20\x79\x8e\x2c\xaa\x96\xfd\xfb\xa3\x76\xdd\xeb\x47\x99\x99\x54\xd2\xf4\x7e\x65\x16\x22\x64\xb0\x53\x6a\xb5\xdf\xf7\xfc\x0a\x2e\x07"),
 71					},
 72					{
 73						Hash:   specs.InnerSpec.Hash,
 74						Prefix: []byte("\x06\x0c\x02\x20\x9a\xf3\x7d\xd5\x95\xa0\x19\x08\x03\xb5\xe0\x5a\xae\xf4\x2a\xe3\xfa\xd4\x99\xe4\xfb\xe3\x7f\x7c\xd3\x1c\xad\xff\x22\xa9\xee\x74\x20"),
 75						Suffix: []byte(""),
 76					},
 77				},
 78			},
 79		},
 80
 81		// rootmulti proof
 82		ics23.CommitmentProof_Exist{
 83			Exist: &ics23.ExistenceProof{
 84				Key:   []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
 85				Value: []byte("\xeb\xfd\x02\x8a\x5d\xe1\xcf\x93\xde\x5d\x0b\xa8\xcf\x6e\x0d\x5e\x29\xf8\x80\x1a\x08\x0b\x07\x20\x3c\xf1\xca\xf2\xf4\xdd\xd3\x10"),
 86				Leaf: &ics23.LeafOp{
 87					Hash:         specs.LeafSpec.Hash,
 88					PrehashKey:   specs.LeafSpec.PrehashKey,
 89					PrehashValue: specs.LeafSpec.PrehashValue,
 90					Length:       specs.LeafSpec.Length,
 91					Prefix:       []byte("\x00"),
 92				},
 93				Path: []*ics23.InnerOp{},
 94			},
 95		},
 96	}
 97
 98	recvPacket := types.MsgRecvPacket{
 99		Packet: types.Packet{
100			Sequence:          1,
101			SourceClient:      counterpartyID,
102			DestinationClient: clientID,
103			TimeoutTimestamp:  uint64(time.Now().Add(time.Hour).Unix()),
104			Payloads: []types.Payload{{
105				SourcePort:      "appID",
106				DestinationPort: "appID",
107				Encoding:        "application/json",
108				Value:           []byte("{}"),
109				Version:         "v1",
110			}},
111		},
112		ProofCommitment: proof,
113		ProofHeight:     trustedHeight,
114	}
115
116	res := core.RecvPacket(cross(cur), recvPacket)
117
118	println("res:", res)
119	println("\n----------- assert render clients/07-tendermint-1/packet_receipts")
120	println(core.Render("clients/" + clientID + "/packet_receipts"))
121	println("----------- assert render clients/07-tendermint-1/packet_receipts/1")
122	println(core.Render("clients/" + clientID + "/packet_receipts/1"))
123	println("\n----------- assert render clients/07-tendermint-1/packet_acknowledgements")
124	println(core.Render("clients/" + clientID + "/packet_acknowledgements"))
125	println("----------- assert render clients/07-tendermint-1/packet_acknowledgements/1")
126	println(core.Render("clients/" + clientID + "/packet_acknowledgements/1"))
127	println("\n----------- app report")
128	println(app.Report())
129
130	// assert NOOP response (res=1) if ack is resubmitted
131	res = core.RecvPacket(cross(cur), recvPacket)
132
133	println("res:", res)
134	println("\n----------- app report")
135	println(app.Report())
136
137	println("\n----------- assert render clients/07-tendermint-1/unreceived_packets")
138	println(core.Render("clients/" + clientID + "/unreceived_packets?sequences=1,2,3"))
139}
140
141// Output:
142// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
143//
144// ----------- assert render clients/07-tendermint-1/packet_receipts
145// {"items":[{"sequence":"1","data":"Ag=="}],"page":1,"total":1}
146// ----------- assert render clients/07-tendermint-1/packet_receipts/1
147// {"sequence":"1","data":"Ag=="}
148//
149// ----------- assert render clients/07-tendermint-1/packet_acknowledgements
150// {"items":[{"sequence":"1","data":"1VweDPBIAzwFFDq7yxEn1x+nFqDTbfioZ8D2GCwn5EU="}],"page":1,"total":1}
151// ----------- assert render clients/07-tendermint-1/packet_acknowledgements/1
152// {"sequence":"1","data":"1VweDPBIAzwFFDq7yxEn1x+nFqDTbfioZ8D2GCwn5EU="}
153//
154// ----------- app report
155// OnSendPacket (0)
156// OnRecvPacket (1)
157// - sourceClient: 07-tendermint-42
158// - destinationClient: 07-tendermint-1
159// - sequence: 1
160// - payload:
161//   - sourcePort: appID
162//   - destinationPort: appID
163//   - version: v1
164//   - encoding: application/json
165//   - value: {}
166//
167// OnTimeoutPacket (0)
168// OnAcknowledgementPacket (0)
169//
170// res: (1 gno.land/p/aib/ibc/types.ResponseResultType)
171//
172// ----------- app report
173// OnSendPacket (0)
174// OnRecvPacket (1)
175// - sourceClient: 07-tendermint-42
176// - destinationClient: 07-tendermint-1
177// - sequence: 1
178// - payload:
179//   - sourcePort: appID
180//   - destinationPort: appID
181//   - version: v1
182//   - encoding: application/json
183//   - value: {}
184//
185// OnTimeoutPacket (0)
186// OnAcknowledgementPacket (0)
187//
188//
189// ----------- assert render clients/07-tendermint-1/unreceived_packets
190// {"height":{"revision_number":0,"revision_height":123},"unreceived_sequences":["2","3"]}
191
192// Events:
193// [
194//   {
195//     "type": "create_client",
196//     "attrs": [
197//       {
198//         "key": "client_id",
199//         "value": "07-tendermint-1"
200//       },
201//       {
202//         "key": "client_type",
203//         "value": "07-tendermint"
204//       },
205//       {
206//         "key": "consensus_heights",
207//         "value": "2/2"
208//       }
209//     ],
210//     "pkg_path": "gno.land/r/aib/ibc/core"
211//   },
212//   {
213//     "type": "recv_packet",
214//     "attrs": [
215//       {
216//         "key": "packet_source_client",
217//         "value": "07-tendermint-42"
218//       },
219//       {
220//         "key": "packet_dest_client",
221//         "value": "07-tendermint-1"
222//       },
223//       {
224//         "key": "packet_sequence",
225//         "value": "1"
226//       },
227//       {
228//         "key": "packet_timeout_timestamp",
229//         "value": "1234571490"
230//       },
231//       {
232//         "key": "encoded_packet_hex",
233//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a280a056170704944120561707049441a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
234//       }
235//     ],
236//     "pkg_path": "gno.land/r/aib/ibc/core"
237//   },
238//   {
239//     "type": "write_acknowledgement",
240//     "attrs": [
241//       {
242//         "key": "packet_source_client",
243//         "value": "07-tendermint-42"
244//       },
245//       {
246//         "key": "packet_dest_client",
247//         "value": "07-tendermint-1"
248//       },
249//       {
250//         "key": "packet_sequence",
251//         "value": "1"
252//       },
253//       {
254//         "key": "packet_timeout_timestamp",
255//         "value": "1234571490"
256//       },
257//       {
258//         "key": "encoded_packet_hex",
259//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a280a056170704944120561707049441a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
260//       },
261//       {
262//         "key": "encoded_acknowledgement_hex",
263//         "value": "0a0101"
264//       }
265//     ],
266//     "pkg_path": "gno.land/r/aib/ibc/core"
267//   }
268// ]
269