z2aa_on_ack_packet_filetest.gno
8.80 Kb · 272 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "chain"
6 "encoding/hex"
7 "testing"
8 "time"
9
10 tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
11 "gno.land/p/aib/ibc/types"
12 "gno.land/p/aib/ics23"
13 "gno.land/r/aib/ibc/apps/transfer"
14 "gno.land/r/aib/ibc/core"
15)
16
17// OnAcknowledgementPacket: success with native coin refund
18func main(cur realm) {
19 var (
20 chainID = "chain-id-2"
21 trustedHeight = types.NewHeight(2, 2)
22 clientState = tmtesting.NewClientState(chainID, trustedHeight)
23 apphash, _ = hex.DecodeString("5467ca2d19a7b74dbcd20e33bda91932f912d76595872ec856945af96e743b82")
24 trustedValset = tmtesting.GenValset()
25 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
26 counterpartyID = "07-tendermint-42"
27 )
28 clientID := core.CreateClient(cross(cur), clientState, consensusState)
29 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
30
31 // Transfer the 100ugnot we want to ack
32 coins := chain.NewCoins(chain.NewCoin("ugnot", 100))
33 testing.SetRealm(testing.NewUserRealm("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"))
34 testing.SetOriginSend(coins)
35 println("----------- assert render total_escrow/ugnot before Transfer")
36 println(transfer.Render("total_escrow/ugnot"))
37 sendPacket, sequence := transfer.Transfer(cross(cur), clientID, "atone1user", "ugnot", 100, uint64(time.Now().Add(time.Hour).Unix()), "")
38 println("----------- assert render total_escrow/ugnot after Transfer")
39 println(transfer.Render("total_escrow/ugnot"))
40
41 // Acknowledge the packet
42 specs := ics23.IavlSpec()
43 // Generate the proof of acknowledgement written during the RecvPacket of the
44 // counterparty client.
45 // NOTE proof generated by:
46 // ./cmd/gen-proof prefix2 07-tendermint-42 acknowledgement $(echo -n UNIVERSAL_ERROR_ACKNOWLEDGEMENT | sha256sum | cut -d' ' -f1)
47 proofAcked := []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\x03\x00\x00\x00\x00\x00\x00\x00\x01"),
53 Value: []byte("\xe2\xfb\x30\xdf\xbf\x7a\xbd\xea\xca\x82\xd4\x26\x53\x4d\x2b\x3a\x9d\x54\x44\xdd\x2a\x87\xfa\x16\xd3\x8b\x77\xba\x1a\x13\xce\xd7"),
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("\x91\x57\xa5\x00\xd6\xc8\xde\x17\x14\xba\xdf\x44\x6d\x17\xb6\xb8\xd3\xa5\x8b\x30\xf8\x54\x83\x18\x66\xd4\x33\x53\x6f\xd2\xea\xb9"),
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 ackPacket := types.MsgAcknowledgement{
99 Packet: types.Packet{
100 Sequence: sequence,
101 SourceClient: clientID,
102 DestinationClient: counterpartyID,
103 TimeoutTimestamp: sendPacket.TimeoutTimestamp,
104 Payloads: sendPacket.Payloads,
105 },
106 Acknowledgement: types.Acknowledgement{
107 // This UNIVERSAL_ERROR_ACKNOWLEDGEMENT error should trigger a refund
108 AppAcknowledgements: [][]byte{types.UniversalErrorAcknowledgement()},
109 },
110 ProofAcked: proofAcked,
111 ProofHeight: trustedHeight,
112 }
113 // For the refund to work we have to fill the realm account
114 transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
115 testing.IssueCoins(transferAppAddr, coins)
116
117 res := core.Acknowledgement(cross(cur), ackPacket)
118
119 println("\nack res:", res)
120 println("----------- assert render total_escrow/ugnot after Ack")
121 println(transfer.Render("total_escrow/ugnot"))
122}
123
124// Output:
125// ----------- assert render total_escrow/ugnot before Transfer
126// {"denom":"ugnot","amount":0}
127// ----------- assert render total_escrow/ugnot after Transfer
128// {"denom":"ugnot","amount":100}
129//
130// ack res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
131// ----------- assert render total_escrow/ugnot after Ack
132// {"denom":"ugnot","amount":0}
133
134// Events:
135// [
136// {
137// "type": "create_client",
138// "attrs": [
139// {
140// "key": "client_id",
141// "value": "07-tendermint-1"
142// },
143// {
144// "key": "client_type",
145// "value": "07-tendermint"
146// },
147// {
148// "key": "consensus_heights",
149// "value": "2/2"
150// }
151// ],
152// "pkg_path": "gno.land/r/aib/ibc/core"
153// },
154// {
155// "type": "send_packet",
156// "attrs": [
157// {
158// "key": "packet_source_client",
159// "value": "07-tendermint-1"
160// },
161// {
162// "key": "packet_dest_client",
163// "value": "07-tendermint-42"
164// },
165// {
166// "key": "packet_sequence",
167// "value": "1"
168// },
169// {
170// "key": "packet_timeout_timestamp",
171// "value": "1234571490"
172// },
173// {
174// "key": "encoded_packet_hex",
175// "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a790a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a420a0575676e6f7412033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e653175736572"
176// }
177// ],
178// "pkg_path": "gno.land/r/aib/ibc/core"
179// },
180// {
181// "type": "ibc_transfer",
182// "attrs": [
183// {
184// "key": "sender",
185// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
186// },
187// {
188// "key": "receiver",
189// "value": "atone1user"
190// },
191// {
192// "key": "denom",
193// "value": "ugnot"
194// },
195// {
196// "key": "amount",
197// "value": "100"
198// },
199// {
200// "key": "memo",
201// "value": ""
202// }
203// ],
204// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
205// },
206// {
207// "type": "acknowledge_packet",
208// "attrs": [
209// {
210// "key": "packet_source_client",
211// "value": "07-tendermint-1"
212// },
213// {
214// "key": "packet_dest_client",
215// "value": "07-tendermint-42"
216// },
217// {
218// "key": "packet_sequence",
219// "value": "1"
220// },
221// {
222// "key": "packet_timeout_timestamp",
223// "value": "1234571490"
224// },
225// {
226// "key": "encoded_packet_hex",
227// "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220e2a1d8cc042a790a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a420a0575676e6f7412033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e653175736572"
228// }
229// ],
230// "pkg_path": "gno.land/r/aib/ibc/core"
231// },
232// {
233// "type": "fungible_token_packet",
234// "attrs": [
235// {
236// "key": "sender",
237// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
238// },
239// {
240// "key": "receiver",
241// "value": "atone1user"
242// },
243// {
244// "key": "denom",
245// "value": "ugnot"
246// },
247// {
248// "key": "amount",
249// "value": "100"
250// },
251// {
252// "key": "memo",
253// "value": ""
254// },
255// {
256// "key": "acknowledgement",
257// "value": "Gtԥu\ufffd?\ufffd;\u001c\u0006W76azEz\ufffd\ufffdX\ufffdxۍ\u0010\ufffdKJ\ufffd\u0011\ufffd"
258// }
259// ],
260// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
261// },
262// {
263// "type": "fungible_token_packet",
264// "attrs": [
265// {
266// "key": "error",
267// "value": "receive packet failed"
268// }
269// ],
270// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
271// }
272// ]