z7aa_recv_packet_filetest.gno
9.01 Kb · 271 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 authorized relayer
16func main(cur realm) {
17 core.SetAdmin(cross(cur), "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm")
18 core.AddRelayer(cross(cur), "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm")
19 var (
20 chainID = "chain-id-2"
21 trustedHeight = types.NewHeight(2, 2)
22 clientState = tmtesting.NewClientState(chainID, trustedHeight)
23 // NOTE apphash generated by the gen-proof command below
24 apphash, _ = hex.DecodeString("8da82bb625ee946f4b7aa03a82010cb115f9ce3879c7caad8d92e71eb821996e")
25 trustedValset = tmtesting.GenValset()
26 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
27 counterpartyID = "07-tendermint-42"
28 )
29 clientID := core.CreateClient(cross(cur), clientState, consensusState)
30 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
31
32 // Register app
33 var (
34 app = appstesting.NewApp(cross(cur))
35 appPortID = "appID"
36 )
37 app.SetOnRecvPacketReturn(types.RecvPacketResult{
38 Status: types.PacketStatus_Success,
39 Acknowledgement: []byte{0x01},
40 })
41 core.RegisterApp(cross(cur), appPortID, app)
42
43 // Receive the packet
44 // Generate the proof of existence of the packet commitment for the
45 // counterparty chain.
46 specs := ics23.IavlSpec()
47 // NOTE code generated by:
48 // 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"}]}'
49 proof := []ics23.CommitmentProof{
50
51 // iavl proof
52 ics23.CommitmentProof_Exist{
53 Exist: &ics23.ExistenceProof{
54 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"),
55 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"),
56 Leaf: &ics23.LeafOp{
57 Hash: specs.LeafSpec.Hash,
58 PrehashKey: specs.LeafSpec.PrehashKey,
59 PrehashValue: specs.LeafSpec.PrehashValue,
60 Length: specs.LeafSpec.Length,
61 Prefix: []byte("\x00\x02\x02"),
62 },
63 Path: []*ics23.InnerOp{
64 {
65 Hash: specs.InnerSpec.Hash,
66 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"),
67 Suffix: []byte(""),
68 },
69 {
70 Hash: specs.InnerSpec.Hash,
71 Prefix: []byte("\x04\x08\x02\x20"),
72 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"),
73 },
74 {
75 Hash: specs.InnerSpec.Hash,
76 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"),
77 Suffix: []byte(""),
78 },
79 },
80 },
81 },
82
83 // rootmulti proof
84 ics23.CommitmentProof_Exist{
85 Exist: &ics23.ExistenceProof{
86 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
87 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"),
88 Leaf: &ics23.LeafOp{
89 Hash: specs.LeafSpec.Hash,
90 PrehashKey: specs.LeafSpec.PrehashKey,
91 PrehashValue: specs.LeafSpec.PrehashValue,
92 Length: specs.LeafSpec.Length,
93 Prefix: []byte("\x00"),
94 },
95 Path: []*ics23.InnerOp{},
96 },
97 },
98 }
99
100 recvPacket := types.MsgRecvPacket{
101 Packet: types.Packet{
102 Sequence: 1,
103 SourceClient: counterpartyID,
104 DestinationClient: clientID,
105 TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()),
106 Payloads: []types.Payload{{
107 SourcePort: "appID",
108 DestinationPort: "appID",
109 Encoding: "application/json",
110 Value: []byte("{}"),
111 Version: "v1",
112 }},
113 },
114 ProofCommitment: proof,
115 ProofHeight: trustedHeight,
116 }
117
118 res := core.RecvPacket(cross(cur), recvPacket)
119
120 println("res:", res)
121 println("\n----------- assert render clients/07-tendermint-1/packet_receipts")
122 println(core.Render("clients/" + clientID + "/packet_receipts"))
123 println("----------- assert render clients/07-tendermint-1/packet_receipts/1")
124 println(core.Render("clients/" + clientID + "/packet_receipts/1"))
125 println("\n----------- assert render clients/07-tendermint-1/packet_acknowledgements")
126 println(core.Render("clients/" + clientID + "/packet_acknowledgements"))
127 println("----------- assert render clients/07-tendermint-1/packet_acknowledgements/1")
128 println(core.Render("clients/" + clientID + "/packet_acknowledgements/1"))
129 println("\n----------- app report")
130 println(app.Report())
131
132 // assert NOOP response (res=1) if ack is resubmitted
133 res = core.RecvPacket(cross(cur), recvPacket)
134
135 println("res:", res)
136 println("\n----------- app report")
137 println(app.Report())
138
139 println("\n----------- assert render clients/07-tendermint-1/unreceived_packets")
140 println(core.Render("clients/" + clientID + "/unreceived_packets?sequences=1,2,3"))
141}
142
143// Output:
144// res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
145//
146// ----------- assert render clients/07-tendermint-1/packet_receipts
147// {"items":[{"sequence":"1","data":"Ag=="}],"page":1,"total":1}
148// ----------- assert render clients/07-tendermint-1/packet_receipts/1
149// {"sequence":"1","data":"Ag=="}
150//
151// ----------- assert render clients/07-tendermint-1/packet_acknowledgements
152// {"items":[{"sequence":"1","data":"1VweDPBIAzwFFDq7yxEn1x+nFqDTbfioZ8D2GCwn5EU="}],"page":1,"total":1}
153// ----------- assert render clients/07-tendermint-1/packet_acknowledgements/1
154// {"sequence":"1","data":"1VweDPBIAzwFFDq7yxEn1x+nFqDTbfioZ8D2GCwn5EU="}
155//
156// ----------- app report
157// OnSendPacket (0)
158// OnRecvPacket (1)
159// - sourceClient: 07-tendermint-42
160// - destinationClient: 07-tendermint-1
161// - sequence: 1
162// - payload:
163// - sourcePort: appID
164// - destinationPort: appID
165// - version: v1
166// - encoding: application/json
167// - value: {}
168//
169// OnTimeoutPacket (0)
170// OnAcknowledgementPacket (0)
171//
172// res: (1 gno.land/p/aib/ibc/types.ResponseResultType)
173//
174// ----------- app report
175// OnSendPacket (0)
176// OnRecvPacket (1)
177// - sourceClient: 07-tendermint-42
178// - destinationClient: 07-tendermint-1
179// - sequence: 1
180// - payload:
181// - sourcePort: appID
182// - destinationPort: appID
183// - version: v1
184// - encoding: application/json
185// - value: {}
186//
187// OnTimeoutPacket (0)
188// OnAcknowledgementPacket (0)
189//
190//
191// ----------- assert render clients/07-tendermint-1/unreceived_packets
192// {"height":{"revision_number":0,"revision_height":123},"unreceived_sequences":["2","3"]}
193
194// Events:
195// [
196// {
197// "type": "create_client",
198// "attrs": [
199// {
200// "key": "client_id",
201// "value": "07-tendermint-1"
202// },
203// {
204// "key": "client_type",
205// "value": "07-tendermint"
206// },
207// {
208// "key": "consensus_heights",
209// "value": "2/2"
210// }
211// ],
212// "pkg_path": "gno.land/r/aib/ibc/core"
213// },
214// {
215// "type": "recv_packet",
216// "attrs": [
217// {
218// "key": "packet_source_client",
219// "value": "07-tendermint-42"
220// },
221// {
222// "key": "packet_dest_client",
223// "value": "07-tendermint-1"
224// },
225// {
226// "key": "packet_sequence",
227// "value": "1"
228// },
229// {
230// "key": "packet_timeout_timestamp",
231// "value": "1234571490"
232// },
233// {
234// "key": "encoded_packet_hex",
235// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a280a056170704944120561707049441a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
236// }
237// ],
238// "pkg_path": "gno.land/r/aib/ibc/core"
239// },
240// {
241// "type": "write_acknowledgement",
242// "attrs": [
243// {
244// "key": "packet_source_client",
245// "value": "07-tendermint-42"
246// },
247// {
248// "key": "packet_dest_client",
249// "value": "07-tendermint-1"
250// },
251// {
252// "key": "packet_sequence",
253// "value": "1"
254// },
255// {
256// "key": "packet_timeout_timestamp",
257// "value": "1234571490"
258// },
259// {
260// "key": "encoded_packet_hex",
261// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a280a056170704944120561707049441a02763122106170706c69636174696f6e2f6a736f6e2a027b7d"
262// },
263// {
264// "key": "encoded_acknowledgement_hex",
265// "value": "0a0101"
266// }
267// ],
268// "pkg_path": "gno.land/r/aib/ibc/core"
269// }
270// ]
271