z4aa_on_recv_packet_filetest.gno
11.11 Kb · 316 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "chain"
6 "encoding/base64"
7 "encoding/hex"
8 "testing"
9 "time"
10
11 tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
12 "gno.land/p/aib/ibc/types"
13 "gno.land/p/aib/ics23"
14 "gno.land/p/nt/testutils/v0"
15 "gno.land/r/aib/ibc/apps/transfer"
16 "gno.land/r/aib/ibc/core"
17)
18
19// OnRecvPacket: success w/ IBC token (returning native ugnot).
20//
21// Scenario: chain A sent ugnot to chain B over clientID (SendPacket escrows
22// under clientID), then chain B sends it back (RecvPacket unescrows). The
23// per-client escrow accounting must debit clientID (the packet's
24// destinationClient), not sourceClient.
25func main(cur realm) {
26 var (
27 chainID = "chain-id-2"
28 trustedHeight = types.NewHeight(2, 2)
29 clientState = tmtesting.NewClientState(chainID, trustedHeight)
30 // NOTE this apphash was provided by the gen-proof command below.
31 apphash, _ = hex.DecodeString("efb8069a70149ec2985f044957bcb3a9aefa81122db1c3dbc286f6f4c6709821")
32 trustedValset = tmtesting.GenValset()
33 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
34 counterpartyID = "07-tendermint-42"
35 )
36 clientID := core.CreateClient(cross(cur), clientState, consensusState)
37 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
38
39 // Step 1: a real native Transfer escrows 100ugnot under clientID. This is
40 // what establishes the per-client escrow that RecvPacket will release.
41 // IssueCoins funds the transfer realm so the later unescrow can pay out;
42 // OriginSend is the user-call claim that Transfer's native branch verifies.
43 transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
44 testing.IssueCoins(transferAppAddr, chain.NewCoins(chain.NewCoin("ugnot", 100)))
45 sender := testutils.TestAddress("sender")
46 testing.SetRealm(testing.NewUserRealm(sender))
47 testing.SetOriginSend(chain.NewCoins(chain.NewCoin("ugnot", 100)))
48 transfer.Transfer(cross(cur), clientID, "atone1user", "ugnot", 100, uint64(time.Now().Add(time.Hour).Unix()), "")
49 testing.SetOriginSend(nil)
50
51 receiver := testutils.TestAddress("receiver")
52 payload := transfer.NewFungibleTokenPacketData(
53 "transfer/"+counterpartyID+"/ugnot", // expect transfer/07-tendermint-42/ugnot
54 "100",
55 "atone1user",
56 receiver.String(),
57 "",
58 )
59 payloadBz := payload.ProtoMarshal()
60 // NOTE this base64 value is used in payload.value in the gen-proof command below.
61 println("Payload proto:", base64.StdEncoding.EncodeToString(payloadBz))
62
63 // Generate the proof of existence of the packet commitment for the
64 // counterparty chain.
65 specs := ics23.IavlSpec()
66 // NOTE code generated by:
67 // 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":"transfer","destination_port":"transfer","encoding":"application/x-protobuf", "value":"Ch90cmFuc2Zlci8wNy10ZW5kZXJtaW50LTQyL3Vnbm90EgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=","version":"ics20-1"}]}'
68 proof := []ics23.CommitmentProof{
69
70 // iavl proof
71 ics23.CommitmentProof_Exist{
72 Exist: &ics23.ExistenceProof{
73 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"),
74 Value: []byte("\x0d\x99\x21\xf7\xdb\xea\xfe\x29\x2d\x78\x15\xd3\x20\x61\x12\xbf\x5f\xce\x28\x0d\x26\x23\x62\xb6\xfb\xe0\x4f\x9b\x28\x24\x92\xf7"),
75 Leaf: &ics23.LeafOp{
76 Hash: specs.LeafSpec.Hash,
77 PrehashKey: specs.LeafSpec.PrehashKey,
78 PrehashValue: specs.LeafSpec.PrehashValue,
79 Length: specs.LeafSpec.Length,
80 Prefix: []byte("\x00\x02\x02"),
81 },
82 Path: []*ics23.InnerOp{
83 {
84 Hash: specs.InnerSpec.Hash,
85 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"),
86 Suffix: []byte(""),
87 },
88 {
89 Hash: specs.InnerSpec.Hash,
90 Prefix: []byte("\x04\x08\x02\x20"),
91 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"),
92 },
93 {
94 Hash: specs.InnerSpec.Hash,
95 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"),
96 Suffix: []byte(""),
97 },
98 },
99 },
100 },
101
102 // rootmulti proof
103 ics23.CommitmentProof_Exist{
104 Exist: &ics23.ExistenceProof{
105 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
106 Value: []byte("\xdc\x5b\xb5\x1b\x61\xfe\xee\x3e\x93\x07\x16\x1a\xf3\xa3\x89\xd6\x6a\x3c\xf1\x44\x1f\xef\x41\x85\xd1\xb0\x47\x55\xa5\x61\x19\xa6"),
107 Leaf: &ics23.LeafOp{
108 Hash: specs.LeafSpec.Hash,
109 PrehashKey: specs.LeafSpec.PrehashKey,
110 PrehashValue: specs.LeafSpec.PrehashValue,
111 Length: specs.LeafSpec.Length,
112 Prefix: []byte("\x00"),
113 },
114 Path: []*ics23.InnerOp{},
115 },
116 },
117 }
118
119 recvPacket := types.MsgRecvPacket{
120 Packet: types.Packet{
121 Sequence: 1,
122 SourceClient: counterpartyID,
123 DestinationClient: clientID,
124 TimeoutTimestamp: uint64(time.Now().Add(time.Hour).Unix()),
125 Payloads: []types.Payload{{
126 SourcePort: transfer.PortID,
127 DestinationPort: transfer.PortID,
128 Encoding: transfer.EncodingProtobuf,
129 Value: payloadBz,
130 Version: transfer.V1,
131 }},
132 },
133 ProofCommitment: proof,
134 ProofHeight: trustedHeight,
135 }
136 // NOTE the following will show 100ugnot escrowed under clientID from the
137 // Transfer above.
138 println("----------- assert render total_escrow/ugnot before RecvPacket")
139 println(transfer.Render("total_escrow/ugnot"))
140
141 res := core.RecvPacket(cross(cur), recvPacket)
142
143 println("\nack res:", res)
144 // The returning packet released the 100ugnot escrowed under clientID.
145 println("----------- assert render total_escrow/ugnot after RecvPacket")
146 println(transfer.Render("total_escrow/ugnot"))
147}
148
149// Output:
150// Payload proto: Ch90cmFuc2Zlci8wNy10ZW5kZXJtaW50LTQyL3Vnbm90EgMxMDAaCmF0b25lMXVzZXIiKGcxd2Zqa3hldGZ3ZWpoeWg2bHRhMDQ3aDZsdGEwNDdoNmxsbTgyeWw=
151// ----------- assert render total_escrow/ugnot before RecvPacket
152// {"denom":"ugnot","amount":100,"clients":[{"client":"07-tendermint-1","amount":100}]}
153//
154// ack res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
155// ----------- assert render total_escrow/ugnot after RecvPacket
156// {"denom":"ugnot","amount":0,"clients":[{"client":"07-tendermint-1","amount":0}]}
157
158// Events:
159// [
160// {
161// "type": "create_client",
162// "attrs": [
163// {
164// "key": "client_id",
165// "value": "07-tendermint-1"
166// },
167// {
168// "key": "client_type",
169// "value": "07-tendermint"
170// },
171// {
172// "key": "consensus_heights",
173// "value": "2/2"
174// }
175// ],
176// "pkg_path": "gno.land/r/aib/ibc/core"
177// },
178// {
179// "type": "send_packet",
180// "attrs": [
181// {
182// "key": "packet_source_client",
183// "value": "07-tendermint-1"
184// },
185// {
186// "key": "packet_dest_client",
187// "value": "07-tendermint-42"
188// },
189// {
190// "key": "packet_sequence",
191// "value": "1"
192// },
193// {
194// "key": "packet_timeout_timestamp",
195// "value": "1234571490"
196// },
197// {
198// "key": "encoded_packet_hex",
199// "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a790a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a420a0575676e6f7412033130301a28673177646a6b75657239776630343768366c746130343768366c746130343768366c647978356178220a61746f6e653175736572"
200// }
201// ],
202// "pkg_path": "gno.land/r/aib/ibc/core"
203// },
204// {
205// "type": "ibc_transfer",
206// "attrs": [
207// {
208// "key": "sender",
209// "value": "g1wdjkuer9wf047h6lta047h6lta047h6ldyx5ax"
210// },
211// {
212// "key": "receiver",
213// "value": "atone1user"
214// },
215// {
216// "key": "denom",
217// "value": "ugnot"
218// },
219// {
220// "key": "amount",
221// "value": "100"
222// },
223// {
224// "key": "memo",
225// "value": ""
226// }
227// ],
228// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
229// },
230// {
231// "type": "recv_packet",
232// "attrs": [
233// {
234// "key": "packet_source_client",
235// "value": "07-tendermint-42"
236// },
237// {
238// "key": "packet_dest_client",
239// "value": "07-tendermint-1"
240// },
241// {
242// "key": "packet_sequence",
243// "value": "1"
244// },
245// {
246// "key": "packet_timeout_timestamp",
247// "value": "1234571490"
248// },
249// {
250// "key": "encoded_packet_hex",
251// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a93010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a5c0a1f7472616e736665722f30372d74656e6465726d696e742d34322f75676e6f7412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
252// }
253// ],
254// "pkg_path": "gno.land/r/aib/ibc/core"
255// },
256// {
257// "type": "fungible_token_packet",
258// "attrs": [
259// {
260// "key": "sender",
261// "value": "atone1user"
262// },
263// {
264// "key": "receiver",
265// "value": "g1wfjkxetfwejhyh6lta047h6lta047h6llm82yl"
266// },
267// {
268// "key": "denom",
269// "value": "ugnot"
270// },
271// {
272// "key": "amount",
273// "value": "100"
274// },
275// {
276// "key": "memo",
277// "value": ""
278// },
279// {
280// "key": "success",
281// "value": "true"
282// }
283// ],
284// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
285// },
286// {
287// "type": "write_acknowledgement",
288// "attrs": [
289// {
290// "key": "packet_source_client",
291// "value": "07-tendermint-42"
292// },
293// {
294// "key": "packet_dest_client",
295// "value": "07-tendermint-1"
296// },
297// {
298// "key": "packet_sequence",
299// "value": "1"
300// },
301// {
302// "key": "packet_timeout_timestamp",
303// "value": "1234571490"
304// },
305// {
306// "key": "encoded_packet_hex",
307// "value": "0801121030372d74656e6465726d696e742d34321a0f30372d74656e6465726d696e742d3120e2a1d8cc042a93010a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a5c0a1f7472616e736665722f30372d74656e6465726d696e742d34322f75676e6f7412033130301a0a61746f6e6531757365722228673177666a6b7865746677656a687968366c746130343768366c746130343768366c6c6d3832796c"
308// },
309// {
310// "key": "encoded_acknowledgement_hex",
311// "value": "0a117b22726573756c74223a2241513d3d227d"
312// }
313// ],
314// "pkg_path": "gno.land/r/aib/ibc/core"
315// }
316// ]