z3a_on_timeout_filetest.gno
11.95 Kb · 340 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "chain"
6 "encoding/hex"
7 "testing"
8 "time"
9
10 "gno.land/p/aib/ibc/lightclient/tendermint"
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/r/aib/ibc/apps/transfer"
15 "gno.land/r/aib/ibc/core"
16)
17
18// OnTimeout: success with native coin
19func main(cur realm) {
20 var (
21 chainID = "atomone-1"
22 trustedHeight = types.NewHeight(1, 2)
23 clientState = tmtesting.NewClientState(chainID, trustedHeight)
24 apphash, _ = hex.DecodeString("a119217258843545bc79dd7df4c050c6c2a21b1de7c132dd065fd7463f743e2d")
25 // priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
26 val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=",
27 "VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 1)
28 // priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
29 val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=",
30 "NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 1)
31 trustedValset = tendermint.NewValset(val1, val2)
32 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
33 counterpartyID = "07-tendermint-42"
34 )
35 clientID := core.CreateClient(cross(cur), clientState, consensusState)
36 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
37
38 // Transfer the 100ugnot we want to ack
39 coins := chain.NewCoins(chain.NewCoin("ugnot", 100))
40 testing.SetRealm(testing.NewUserRealm("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"))
41 testing.SetOriginSend(coins)
42 println("----------- assert render total_escrow/ugnot before Transfer")
43 println(transfer.Render("total_escrow/ugnot"))
44 sendPacket, sequence := transfer.Transfer(cross(cur), clientID, "atone1user", "ugnot", 100, uint64(time.Now().Add(10*time.Minute).Unix()), "memo")
45 println("----------- assert render total_escrow/ugnot after Transfer")
46 println(transfer.Render("total_escrow/ugnot"))
47
48 // Change block time to after the timeout
49 ctx := testing.GetContext()
50 ctx.Time = time.Now().Add(12 * time.Minute)
51 testing.SetContext(ctx)
52 // Update client with a block created after the timeout
53 // NOTE code generated by:
54 // go run -C ./cmd/gen-block-signatures . -apphash-hex=82ea91f259352674360c621c4b0a1975cc076e3792b6d22a8bfbe8e7f7d0d2d7 -chainid=atomone-1 -header-time-shift=12 -height=12 -privkeys=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==,nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
55 // NOTE apphash generated by the gen-proof command below
56 apphash, _ = hex.DecodeString("82ea91f259352674360c621c4b0a1975cc076e3792b6d22a8bfbe8e7f7d0d2d7")
57 trustedHeight = clientState.LatestHeight
58 var (
59 commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
60 newHeight = uint64(12)
61 newTimestamp = consensusState.Timestamp.Add(time.Minute * time.Duration(12))
62 valset = tendermint.NewValset(val1, val2)
63 nextValset = tendermint.NewValset(val1, val2)
64
65 signatures = []tendermint.CommitSig{
66 {
67 BlockIDFlag: tendermint.BlockIDFlagCommit,
68 ValidatorAddress: valset.Validators[0].Address,
69 Timestamp: commitTimestamp,
70 Signature: []byte("\x90\x76\xf4\x27\xaf\x23\xd0\xcb\x97\xb1\x6d\xfa\x86\xae\x6d\x46\xe9\xe4\x65\x76\xf2\x6a\x1d\x5f\x83\xa6\x9e\x4a\x28\x55\x6e\x74\x84\x02\xff\x15\xc0\x41\xed\x15\xb7\xef\x3a\x3f\xe1\xe6\xa0\x8f\x34\x83\x9c\x2e\x27\x26\x69\x00\x73\x45\x87\xd2\x3c\xe6\x12\x07"),
71 },
72 {
73 BlockIDFlag: tendermint.BlockIDFlagCommit,
74 ValidatorAddress: valset.Validators[1].Address,
75 Timestamp: commitTimestamp,
76 Signature: []byte("\x45\x61\x3b\x34\x58\xa1\xf5\x75\x16\x6f\xb3\xad\xda\x12\xa8\xef\xb6\x64\x3f\x6b\x80\x73\x74\xe3\x15\x6f\xe1\x3b\x3f\x50\xed\x0d\xbc\xa7\x54\x11\x72\xe3\x71\x22\x95\xec\x77\x93\x45\x03\x67\xa5\x00\x4c\x84\x3b\x1c\xa4\xf7\x0e\x95\xd1\xe2\x04\xb6\x91\x63\x09"),
77 },
78 }
79
80 msgHeader = tmtesting.NewMsgHeader(
81 chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
82 nextValset, trustedValset, signatures,
83 )
84 )
85 core.UpdateClient(cross(cur), clientID, msgHeader)
86
87 // Timeout the packet
88 // Generate the proof that no RecvPacket have been received by the
89 // counterparty chain.
90 specs := ics23.IavlSpec()
91 // NOTE code generated by:
92 // go run -C ./cmd/gen-proof . 'prefix2' '07-tendermint-42' 'receipt'
93 proof := []ics23.CommitmentProof{
94
95 // iavl proof
96 ics23.CommitmentProof_Nonexist{
97 Nonexist: &ics23.NonExistenceProof{
98 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\x02\x00\x00\x00\x00\x00\x00\x00\x01"),
99 Left: &ics23.ExistenceProof{
100 Key: []byte("\x50"),
101 Value: []byte("\x50"),
102 Leaf: &ics23.LeafOp{
103 Hash: specs.LeafSpec.Hash,
104 PrehashKey: specs.LeafSpec.PrehashKey,
105 PrehashValue: specs.LeafSpec.PrehashValue,
106 Length: specs.LeafSpec.Length,
107 Prefix: []byte("\x00\x02\x02"),
108 },
109 Path: []*ics23.InnerOp{
110 {
111 Hash: specs.InnerSpec.Hash,
112 Prefix: []byte("\x04\x06\x02\x20"),
113 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"),
114 },
115 {
116 Hash: specs.InnerSpec.Hash,
117 Prefix: []byte("\x06\x0a\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"),
118 Suffix: []byte(""),
119 },
120 },
121 },
122 Right: &ics23.ExistenceProof{
123 Key: []byte("\x72"),
124 Value: []byte("\x72"),
125 Leaf: &ics23.LeafOp{
126 Hash: specs.LeafSpec.Hash,
127 PrehashKey: specs.LeafSpec.PrehashKey,
128 PrehashValue: specs.LeafSpec.PrehashValue,
129 Length: specs.LeafSpec.Length,
130 Prefix: []byte("\x00\x02\x02"),
131 },
132 Path: []*ics23.InnerOp{
133 {
134 Hash: specs.InnerSpec.Hash,
135 Prefix: []byte("\x02\x04\x02\x20"),
136 Suffix: []byte("\x20\xa3\x03\x93\x0c\xa8\x83\x16\x18\xac\x7e\x4d\xdd\x10\x54\x6c\xfc\x36\x6f\xb7\x30\xd6\x63\x0c\x03\x0a\x97\x22\x6b\xbe\xfc\x69\x35"),
137 },
138 {
139 Hash: specs.InnerSpec.Hash,
140 Prefix: []byte("\x04\x06\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"),
141 Suffix: []byte(""),
142 },
143 {
144 Hash: specs.InnerSpec.Hash,
145 Prefix: []byte("\x06\x0a\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"),
146 Suffix: []byte(""),
147 },
148 },
149 },
150 },
151 },
152
153 // rootmulti proof
154 ics23.CommitmentProof_Exist{
155 Exist: &ics23.ExistenceProof{
156 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
157 Value: []byte("\x38\x22\xf3\x2f\x21\x66\x53\x63\x29\x4f\x96\xed\xda\x15\xa7\x81\x1a\x67\x6c\x2b\xa9\xdd\xcc\xec\x46\x63\x64\xf1\x00\x69\x82\x7d"),
158 Leaf: &ics23.LeafOp{
159 Hash: specs.LeafSpec.Hash,
160 PrehashKey: specs.LeafSpec.PrehashKey,
161 PrehashValue: specs.LeafSpec.PrehashValue,
162 Length: specs.LeafSpec.Length,
163 Prefix: []byte("\x00"),
164 },
165 Path: []*ics23.InnerOp{},
166 },
167 },
168 }
169
170 timeoutPacket := types.MsgTimeout{
171 Packet: types.Packet{
172 Sequence: sequence,
173 SourceClient: clientID,
174 DestinationClient: counterpartyID,
175 TimeoutTimestamp: sendPacket.TimeoutTimestamp,
176 Payloads: sendPacket.Payloads,
177 },
178 ProofUnreceived: proof,
179 ProofHeight: msgHeader.GetHeight(),
180 }
181 // For the refund to work we have to fill the realm account
182 transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
183 testing.IssueCoins(transferAppAddr, coins)
184
185 res := core.Timeout(cross(cur), timeoutPacket)
186
187 println("\ntimeout res:", res)
188 println("----------- assert render total_escrow/ugnot after Ack")
189 println(transfer.Render("total_escrow/ugnot"))
190}
191
192// Output:
193// ----------- assert render total_escrow/ugnot before Transfer
194// {"denom":"ugnot","amount":0}
195// ----------- assert render total_escrow/ugnot after Transfer
196// {"denom":"ugnot","amount":100}
197//
198// timeout res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
199// ----------- assert render total_escrow/ugnot after Ack
200// {"denom":"ugnot","amount":0}
201
202// Events:
203// [
204// {
205// "type": "create_client",
206// "attrs": [
207// {
208// "key": "client_id",
209// "value": "07-tendermint-1"
210// },
211// {
212// "key": "client_type",
213// "value": "07-tendermint"
214// },
215// {
216// "key": "consensus_heights",
217// "value": "1/2"
218// }
219// ],
220// "pkg_path": "gno.land/r/aib/ibc/core"
221// },
222// {
223// "type": "send_packet",
224// "attrs": [
225// {
226// "key": "packet_source_client",
227// "value": "07-tendermint-1"
228// },
229// {
230// "key": "packet_dest_client",
231// "value": "07-tendermint-42"
232// },
233// {
234// "key": "packet_sequence",
235// "value": "1"
236// },
237// {
238// "key": "packet_timeout_timestamp",
239// "value": "1234568490"
240// },
241// {
242// "key": "encoded_packet_hex",
243// "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220aa8ad8cc042a7f0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a480a0575676e6f7412033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e6531757365722a046d656d6f"
244// }
245// ],
246// "pkg_path": "gno.land/r/aib/ibc/core"
247// },
248// {
249// "type": "ibc_transfer",
250// "attrs": [
251// {
252// "key": "sender",
253// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
254// },
255// {
256// "key": "receiver",
257// "value": "atone1user"
258// },
259// {
260// "key": "denom",
261// "value": "ugnot"
262// },
263// {
264// "key": "amount",
265// "value": "100"
266// },
267// {
268// "key": "memo",
269// "value": "memo"
270// }
271// ],
272// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
273// },
274// {
275// "type": "update_client",
276// "attrs": [
277// {
278// "key": "client_id",
279// "value": "07-tendermint-1"
280// },
281// {
282// "key": "client_type",
283// "value": "07-tendermint"
284// },
285// {
286// "key": "consensus_heights",
287// "value": "1/12"
288// }
289// ],
290// "pkg_path": "gno.land/r/aib/ibc/core"
291// },
292// {
293// "type": "timeout_packet",
294// "attrs": [
295// {
296// "key": "packet_source_client",
297// "value": "07-tendermint-1"
298// },
299// {
300// "key": "packet_dest_client",
301// "value": "07-tendermint-42"
302// },
303// {
304// "key": "packet_sequence",
305// "value": "1"
306// },
307// {
308// "key": "packet_timeout_timestamp",
309// "value": "1234568490"
310// },
311// {
312// "key": "encoded_packet_hex",
313// "value": "0801120f30372d74656e6465726d696e742d311a1030372d74656e6465726d696e742d343220aa8ad8cc042a7f0a087472616e7366657212087472616e736665721a0769637332302d3122166170706c69636174696f6e2f782d70726f746f6275662a480a0575676e6f7412033130301a28673177796d75343764726872306b7571323039386d3739326c797467746a326e797837377972736d220a61746f6e6531757365722a046d656d6f"
314// }
315// ],
316// "pkg_path": "gno.land/r/aib/ibc/core"
317// },
318// {
319// "type": "timeout",
320// "attrs": [
321// {
322// "key": "receiver",
323// "value": "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm"
324// },
325// {
326// "key": "denom",
327// "value": "ugnot"
328// },
329// {
330// "key": "amount",
331// "value": "100"
332// },
333// {
334// "key": "memo",
335// "value": "memo"
336// }
337// ],
338// "pkg_path": "gno.land/r/aib/ibc/apps/transfer"
339// }
340// ]