Search Apps Documentation Source Content File Folder Download Copy Actions Download

z7o_recv_packet_filetest.gno

8.60 Kb · 262 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 with OnRecvPacket error
 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_Failure,
 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
138// Output:
139// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
140//
141// ----------- assert render clients/07-tendermint-1/packet_receipts
142// {"items":[{"sequence":"1","data":"Ag=="}],"page":1,"total":1}
143// ----------- assert render clients/07-tendermint-1/packet_receipts/1
144// {"sequence":"1","data":"Ag=="}
145//
146// ----------- assert render clients/07-tendermint-1/packet_acknowledgements
147// {"items":[{"sequence":"1","data":"4vsw3796verKgtQmU00rOp1URN0qh/oW04t3uhoTztc="}],"page":1,"total":1}
148// ----------- assert render clients/07-tendermint-1/packet_acknowledgements/1
149// {"sequence":"1","data":"4vsw3796verKgtQmU00rOp1URN0qh/oW04t3uhoTztc="}
150//
151// ----------- app report
152// OnSendPacket (0)
153// OnRecvPacket (1)
154// - sourceClient: 07-tendermint-42
155// - destinationClient: 07-tendermint-1
156// - sequence: 1
157// - payload:
158//   - sourcePort: appID
159//   - destinationPort: appID
160//   - version: v1
161//   - encoding: application/json
162//   - value: {}
163//
164// OnTimeoutPacket (0)
165// OnAcknowledgementPacket (0)
166//
167// res: (1 gno.land/p/aib/ibc/types.ResponseResultType)
168//
169// ----------- app report
170// OnSendPacket (0)
171// OnRecvPacket (1)
172// - sourceClient: 07-tendermint-42
173// - destinationClient: 07-tendermint-1
174// - sequence: 1
175// - payload:
176//   - sourcePort: appID
177//   - destinationPort: appID
178//   - version: v1
179//   - encoding: application/json
180//   - value: {}
181//
182// OnTimeoutPacket (0)
183// OnAcknowledgementPacket (0)
184
185// Events:
186// [
187//   {
188//     "type": "create_client",
189//     "attrs": [
190//       {
191//         "key": "client_id",
192//         "value": "07-tendermint-1"
193//       },
194//       {
195//         "key": "client_type",
196//         "value": "07-tendermint"
197//       },
198//       {
199//         "key": "consensus_heights",
200//         "value": "2/2"
201//       }
202//     ],
203//     "pkg_path": "gno.land/r/aib/ibc/core"
204//   },
205//   {
206//     "type": "recv_packet",
207//     "attrs": [
208//       {
209//         "key": "packet_source_client",
210//         "value": "07-tendermint-42"
211//       },
212//       {
213//         "key": "packet_dest_client",
214//         "value": "07-tendermint-1"
215//       },
216//       {
217//         "key": "packet_sequence",
218//         "value": "1"
219//       },
220//       {
221//         "key": "packet_timeout_timestamp",
222//         "value": "1234571490"
223//       },
224//       {
225//         "key": "encoded_packet_hex",
226//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a280a056170704944120561707049441a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
227//       }
228//     ],
229//     "pkg_path": "gno.land/r/aib/ibc/core"
230//   },
231//   {
232//     "type": "write_acknowledgement",
233//     "attrs": [
234//       {
235//         "key": "packet_source_client",
236//         "value": "07-tendermint-42"
237//       },
238//       {
239//         "key": "packet_dest_client",
240//         "value": "07-tendermint-1"
241//       },
242//       {
243//         "key": "packet_sequence",
244//         "value": "1"
245//       },
246//       {
247//         "key": "packet_timeout_timestamp",
248//         "value": "1234571490"
249//       },
250//       {
251//         "key": "encoded_packet_hex",
252//         "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a280a056170704944120561707049441a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
253//       },
254//       {
255//         "key": "encoded_acknowledgement_hex",
256//         "value": "0a204774d4a575993f963b1c06573736617a457abef8589178db8d10c94b4ab511ab"
257//       }
258//     ],
259//     "pkg_path": "gno.land/r/aib/ibc/core"
260//   }
261// ]
262