z3aaa_on_timeout_filetest.gno
7.70 Kb · 196 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/testing/grc20test"
15 "gno.land/r/aib/ibc/apps/transfer"
16 "gno.land/r/aib/ibc/core"
17)
18
19// OnTimeout: success w/ non-IBC GRC20 token
20func main(cur realm) {
21 var (
22 chainID = "chain-id-2"
23 trustedHeight = types.NewHeight(2, 2)
24 clientState = tmtesting.NewClientState(chainID, trustedHeight)
25 apphash, _ = hex.DecodeString("ec53f57875853302c68a69e9b5c1353a16dbce77cdf7ec564301ddb0be0d9ec5")
26 // priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
27 val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=",
28 "VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 10)
29 // priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
30 val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=",
31 "NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 10)
32 trustedValset = tendermint.NewValset(val1, val2)
33 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
34 counterpartyID = "07-tendermint-2"
35 )
36 clientID := core.CreateClient(cross(cur), clientState, consensusState)
37 core.RegisterCounterparty(cross(cur), clientID, [][]byte{[]byte("iavlStoreKey"), []byte("prefix2")}, counterpartyID)
38 relayer := cur.Previous().Address()
39
40 signer := cur.Address()
41
42 // Mint GRC20 tokens to signer and approve transfer app
43 grc20test.Mint(cross(cur), signer, 1000)
44 transferAppAddr := chain.PackageAddress("gno.land/r/aib/ibc/apps/transfer")
45 grc20test.Approve(cross(cur), signer, transferAppAddr, 100)
46
47 // Step 1: Transfer to escrow GRC20 tokens (chain A → chain B)
48 grc20regKey := "gno.land/r/aib/ibc/apps/testing/grc20test." + grc20test.Slug
49 testing.SetOriginCaller(signer)
50 println("signer grc20 balance before Transfer:", grc20test.BalanceOf(signer))
51 sendPacket, sequence := transfer.Transfer(cross(cur), clientID, "atone1user", grc20regKey, 100, uint64(time.Now().Add(10*time.Minute).Unix()), "")
52 println("\nsigner grc20 balance after Transfer:", grc20test.BalanceOf(signer))
53
54 // Change block time to after the timeout
55 ctx := testing.GetContext()
56 ctx.Time = time.Now().Add(12 * time.Minute)
57 testing.SetContext(ctx)
58 // Update client with a block created after the timeout
59 testing.SetOriginCaller(relayer)
60 apphash, _ = hex.DecodeString("82ea91f259352674360c621c4b0a1975cc076e3792b6d22a8bfbe8e7f7d0d2d7")
61 trustedHeight = clientState.LatestHeight
62 specs := ics23.IavlSpec()
63 var (
64 commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
65 newHeight = uint64(12)
66 newTimestamp = consensusState.Timestamp.Add(time.Minute * time.Duration(12))
67 valset = tendermint.NewValset(val1, val2)
68 nextValset = tendermint.NewValset(val1, val2)
69
70 signatures = []tendermint.CommitSig{
71 {
72 BlockIDFlag: tendermint.BlockIDFlagCommit,
73 ValidatorAddress: valset.Validators[0].Address,
74 Timestamp: commitTimestamp,
75 Signature: []byte("\x7a\xa8\x84\xac\xac\x99\x9b\xd8\x07\xd4\x8d\xb1\x0b\x58\x87\xda\x78\xa9\x76\xcf\x8f\x01\x9f\xe8\x60\xf1\xf8\x09\x73\x91\xd7\x07\x48\x62\x87\xa5\x7e\xa1\x35\xe3\x47\x08\x3d\xef\x03\x99\x02\xcf\x33\x6e\x88\x63\x06\x29\x49\x26\x9a\x67\xdd\xc2\x97\x83\xb5\x0b"),
76 },
77 {
78 BlockIDFlag: tendermint.BlockIDFlagCommit,
79 ValidatorAddress: valset.Validators[1].Address,
80 Timestamp: commitTimestamp,
81 Signature: []byte("\x5c\x24\x40\x0c\x93\xe3\x60\x30\x40\x00\x8f\xe6\xb4\x5f\x90\x2b\x82\x82\xcd\x14\x0f\x0a\xcf\x29\xe7\xa3\x05\xf4\x23\x03\xf0\x84\x7a\x59\x46\x5e\xca\x3f\xb0\x60\xf2\xe7\xe7\x8c\x85\xb8\x2d\x32\x8e\xdc\x07\xbb\x0b\x6b\xe7\x01\x71\x1d\xa2\x94\x47\xb6\x78\x03"),
82 },
83 }
84
85 msgHeader = tmtesting.NewMsgHeader(
86 chainID, newTimestamp, apphash, newHeight, trustedHeight, valset,
87 nextValset, trustedValset, signatures,
88 )
89 )
90 core.UpdateClient(cross(cur), clientID, msgHeader)
91
92 // Timeout the packet
93 // Generate the proof that no RecvPacket has been received by the counterparty chain.
94 proof := []ics23.CommitmentProof{
95
96 // iavl proof
97 ics23.CommitmentProof_Nonexist{
98 Nonexist: &ics23.NonExistenceProof{
99 Key: []byte("\x70\x72\x65\x66\x69\x78\x32\x30\x37\x2d\x74\x65\x6e\x64\x65\x72\x6d\x69\x6e\x74\x2d\x32\x02\x00\x00\x00\x00\x00\x00\x00\x01"),
100 Left: &ics23.ExistenceProof{
101 Key: []byte("\x50"),
102 Value: []byte("\x50"),
103 Leaf: &ics23.LeafOp{
104 Hash: specs.LeafSpec.Hash,
105 PrehashKey: specs.LeafSpec.PrehashKey,
106 PrehashValue: specs.LeafSpec.PrehashValue,
107 Length: specs.LeafSpec.Length,
108 Prefix: []byte("\x00\x02\x02"),
109 },
110 Path: []*ics23.InnerOp{
111 {
112 Hash: specs.InnerSpec.Hash,
113 Prefix: []byte("\x04\x06\x02\x20"),
114 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"),
115 },
116 {
117 Hash: specs.InnerSpec.Hash,
118 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"),
119 Suffix: []byte(""),
120 },
121 },
122 },
123 Right: &ics23.ExistenceProof{
124 Key: []byte("\x72"),
125 Value: []byte("\x72"),
126 Leaf: &ics23.LeafOp{
127 Hash: specs.LeafSpec.Hash,
128 PrehashKey: specs.LeafSpec.PrehashKey,
129 PrehashValue: specs.LeafSpec.PrehashValue,
130 Length: specs.LeafSpec.Length,
131 Prefix: []byte("\x00\x02\x02"),
132 },
133 Path: []*ics23.InnerOp{
134 {
135 Hash: specs.InnerSpec.Hash,
136 Prefix: []byte("\x02\x04\x02\x20"),
137 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"),
138 },
139 {
140 Hash: specs.InnerSpec.Hash,
141 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"),
142 Suffix: []byte(""),
143 },
144 {
145 Hash: specs.InnerSpec.Hash,
146 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"),
147 Suffix: []byte(""),
148 },
149 },
150 },
151 },
152 },
153
154 // rootmulti proof
155 ics23.CommitmentProof_Exist{
156 Exist: &ics23.ExistenceProof{
157 Key: []byte("\x69\x61\x76\x6c\x53\x74\x6f\x72\x65\x4b\x65\x79"),
158 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"),
159 Leaf: &ics23.LeafOp{
160 Hash: specs.LeafSpec.Hash,
161 PrehashKey: specs.LeafSpec.PrehashKey,
162 PrehashValue: specs.LeafSpec.PrehashValue,
163 Length: specs.LeafSpec.Length,
164 Prefix: []byte("\x00"),
165 },
166 Path: []*ics23.InnerOp{},
167 },
168 },
169 }
170
171 timeoutPacket := types.MsgTimeout{
172 Packet: types.Packet{
173 Sequence: sequence,
174 SourceClient: clientID,
175 DestinationClient: counterpartyID,
176 TimeoutTimestamp: sendPacket.TimeoutTimestamp,
177 Payloads: sendPacket.Payloads,
178 },
179 ProofUnreceived: proof,
180 ProofHeight: msgHeader.GetHeight(),
181 }
182
183 res := core.Timeout(cross(cur), timeoutPacket)
184
185 println("\ntimeout res:", res)
186 println("\nsigner grc20 balance after Timeout:", grc20test.BalanceOf(signer))
187}
188
189// Output:
190// signer grc20 balance before Transfer: 1000
191//
192// signer grc20 balance after Transfer: 900
193//
194// timeout res: (2 gno.land/p/aib/ibc/types.ResponseResultType)
195//
196// signer grc20 balance after Timeout: 1000