z9aaa_recover_client_filetest.gno
10.61 Kb · 257 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "time"
6
7 "gno.land/p/aib/ibc/lightclient/tendermint"
8 tmtesting "gno.land/p/aib/ibc/lightclient/tendermint/testing"
9 "gno.land/p/aib/ibc/types"
10 "gno.land/r/aib/ibc/core"
11)
12
13// RecoverClient success with authorized relayer
14func main(cur realm) {
15 core.SetAdmin(cross(cur), "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm")
16
17 // Subject client setup: create + freeze via misbehaviour (pattern from z8a).
18 var (
19 chainID = "atomone-1"
20 height = uint64(2)
21 clientState = tmtesting.NewClientState(chainID, types.NewHeight(1, height))
22 apphash = tmtesting.Hash("apphash-2")
23 // priv=8a6cAbQSpDbebmcTEhCMPhhr/SkL/2pizo60yzHRkN9Uyk7RHOZm7g4xW+yeJh147/Z4/6HXF6gBwcFNkLsZ/A==
24 val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=",
25 "VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 1)
26 // priv=nWg6ETc62tyxd94lh8fFaQnZKaAW6vlS0L/4lfseJuI14ZXUKp7AZROkflLFVF+SBg4wJVfzgzIKyWq3D066+g==
27 val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=",
28 "NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 1)
29 trustedValset = tendermint.NewValset(val1, val2)
30 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
31 )
32 subjectID := core.CreateClient(cross(cur), clientState, consensusState)
33
34 // Build valid misbehavior to freeze the subject.
35 var header1, header2 *tendermint.MsgHeader
36 {
37 var (
38 apphash = tmtesting.Hash("apphash-4")
39 val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=", "VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 10)
40 val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=", "NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 10)
41 valset = tendermint.NewValset(val1, val2)
42 commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
43 newHeight = uint64(4)
44 newTimestamp = consensusState.Timestamp.Add(time.Minute * time.Duration(0))
45 nextValset = tendermint.NewValset(val1, val2)
46 trustedHeight = clientState.LatestHeight
47
48 signatures = []tendermint.CommitSig{
49 {
50 BlockIDFlag: tendermint.BlockIDFlagCommit,
51 ValidatorAddress: valset.Validators[0].Address,
52 Timestamp: commitTimestamp,
53 Signature: []byte("\x2e\xba\x21\xb0\x2f\xd2\x85\xb9\xef\x82\x68\xdc\xef\xd1\xd1\x12\x70\x88\x94\x10\x7e\x4d\x49\xac\x46\x3d\x86\xe2\xf2\xae\x38\xb4\xa5\xab\x0c\xc1\x8f\x8a\x59\xda\x36\x17\x01\xe4\x16\x49\xbf\x03\x86\xf0\x31\x3f\x30\x37\x9e\x47\x28\x72\x3b\x0c\x89\xb9\x94\x05"),
54 },
55 {
56 BlockIDFlag: tendermint.BlockIDFlagCommit,
57 ValidatorAddress: valset.Validators[1].Address,
58 Timestamp: commitTimestamp,
59 Signature: []byte("\x67\xcc\xc6\xc4\x20\xa0\xcb\x36\xaf\x33\x5c\xf8\xe4\xad\x47\x49\x38\x9d\x9e\xa3\x7c\xe6\x88\x81\x62\x51\x7f\xcc\xa5\x92\x0d\x98\xe9\xbe\x71\x2d\x22\xcd\x41\x30\x28\x83\x03\xcb\xf5\xd7\x28\xde\x7b\x92\x85\x9e\xa6\xe5\xde\x50\x89\x2d\x2b\xc5\x9d\x9a\x33\x00"),
60 },
61 }
62 )
63 header1 = tmtesting.NewMsgHeader(chainID, newTimestamp, apphash, newHeight,
64 trustedHeight, valset, nextValset, trustedValset, signatures)
65 }
66 {
67 var (
68 apphash = tmtesting.Hash("apphash-3")
69 val1 = tendermint.NewValidator("9DIBYr64rywKO3Kk6+743xDHcEU=", "VMpO0RzmZu4OMVvsniYdeO/2eP+h1xeoAcHBTZC7Gfw=", 10)
70 val2 = tendermint.NewValidator("y+naL3ubs9q1bXrY9+uRxY9c+J8=", "NeGV1CqewGUTpH5SxVRfkgYOMCVX84MyCslqtw9Ouvo=", 10)
71 valset = tendermint.NewValset(val1, val2)
72 commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
73 newHeight = uint64(3)
74 newTimestamp = consensusState.Timestamp.Add(time.Minute * time.Duration(0))
75 nextValset = tendermint.NewValset(val1, val2)
76 trustedHeight = clientState.LatestHeight
77
78 signatures = []tendermint.CommitSig{
79 {
80 BlockIDFlag: tendermint.BlockIDFlagCommit,
81 ValidatorAddress: valset.Validators[0].Address,
82 Timestamp: commitTimestamp,
83 Signature: []byte("\x51\xa4\x8f\x78\x42\x4e\x6e\x0d\xc3\x2b\xe6\xcb\x09\x5c\xe5\x7d\x35\x84\xcf\xb1\x0f\x53\x72\x0c\x41\xde\xd3\x6d\xbc\x81\x28\x64\x41\xf7\xe9\x1e\xa9\x93\xd0\xa4\x84\x16\xca\xa8\xa3\x8e\x56\x45\xec\xae\x1d\x24\xb4\xa8\xf4\x29\x85\x04\x06\xe4\x18\x6b\x7d\x08"),
84 },
85 {
86 BlockIDFlag: tendermint.BlockIDFlagCommit,
87 ValidatorAddress: valset.Validators[1].Address,
88 Timestamp: commitTimestamp,
89 Signature: []byte("\xd9\xd6\x82\x83\x78\x75\xfa\x2e\xd7\x1b\xa5\x3d\xfe\xb3\x80\xb6\xc7\x59\x20\x45\x10\xdf\x0c\x19\xd1\xcc\xfc\x37\xd5\x59\x79\x95\x93\x60\x08\xfb\x5f\xa0\x87\x95\x51\x1e\x29\xed\x94\xd1\x31\x5e\xda\x90\x86\x11\xbe\xb2\x88\x68\xe1\xba\xa7\x31\xc7\xa2\xda\x07"),
90 },
91 }
92 )
93 header2 = tmtesting.NewMsgHeader(chainID, newTimestamp, apphash, newHeight,
94 trustedHeight, valset, nextValset, trustedValset, signatures)
95 }
96 core.UpdateClient(cross(cur), subjectID, &tendermint.Misbehaviour{Header1: header1, Header2: header2})
97
98 // Substitute client: fresh Active client with matching parameters (same
99 // chainID for IBC correctness, but the implementation also supports a
100 // different one — the subject will adopt the substitute's chainID).
101 substituteConsState := tmtesting.GenConsensusState(time.Now(), tmtesting.Hash("apphash-5"), trustedValset.Hash())
102 substituteID := core.CreateClient(cross(cur), clientState, substituteConsState)
103
104 // Advance the substitute to height (1,3) so recovery must actually adopt
105 // the substitute's LatestHeight (the subject starts at (1,2)). Signatures
106 // reused verbatim from z2a (chainID=atomone-1, height=3, apphash-3,
107 // trusted=(1,2), time-shift=1m).
108 {
109 var (
110 apphash = tmtesting.Hash("apphash-3")
111 commitTimestamp = tmtesting.ToTime("2025-09-25T07:55:57.306746166Z")
112 newHeight = uint64(3)
113 newTimestamp = substituteConsState.Timestamp.Add(time.Minute * time.Duration(1))
114 valset = tendermint.NewValset(val1, val2)
115 nextValset = tendermint.NewValset(val1, val2)
116 trustedHeight = clientState.LatestHeight
117
118 signatures = []tendermint.CommitSig{
119 {
120 BlockIDFlag: tendermint.BlockIDFlagCommit,
121 ValidatorAddress: valset.Validators[0].Address,
122 Timestamp: commitTimestamp,
123 Signature: []byte("\x5c\xd2\x8b\xe3\x4b\x60\x3e\xaa\x75\x3c\xce\x24\xfe\x15\x75\x55\x84\xd4\xa3\xce\xbe\x0f\x94\xe9\xf7\x27\xb3\x7a\xdd\x02\x2d\xa0\x0b\xa7\x83\x7f\x50\xc3\xde\x3d\x95\x59\xb3\xad\xed\xd0\xdd\x23\x1d\x39\x9a\x8e\x1f\xc3\xcf\xdb\x1d\xa9\x93\xf5\x9a\xc0\x2b\x05"),
124 },
125 {
126 BlockIDFlag: tendermint.BlockIDFlagCommit,
127 ValidatorAddress: valset.Validators[1].Address,
128 Timestamp: commitTimestamp,
129 Signature: []byte("\x34\x41\x2e\x78\x7f\xbf\x70\xef\x14\x48\xe3\x14\xd5\x83\xdc\x42\xff\x40\xf6\x5b\x71\x62\x09\xf9\x6f\x54\x63\x3e\xdb\xc5\x98\xc0\x9e\xa7\xde\x33\xac\xa7\x5f\xbb\xd6\x63\x49\xe1\xe9\x98\x86\x03\x46\x6a\x7e\xb6\x5d\xe5\x71\xe5\x1e\x5d\x4d\xd8\x8d\xbe\x2f\x01"),
130 },
131 }
132
133 msgHeader = tmtesting.NewMsgHeader(
134 "atomone-1", newTimestamp, apphash, newHeight, trustedHeight, valset,
135 nextValset, trustedValset, signatures,
136 )
137 )
138 core.UpdateClient(cross(cur), substituteID, msgHeader)
139 }
140
141 println("----------- before recover: subject status")
142 println(core.Render("clients/" + subjectID + "/status"))
143 println("----------- before recover: substitute status")
144 println(core.Render("clients/" + substituteID + "/status"))
145
146 core.RecoverClient(cross(cur), subjectID, substituteID)
147
148 println("----------- after recover: subject status")
149 println(core.Render("clients/" + subjectID + "/status"))
150 println("----------- after recover: substitute status")
151 println(core.Render("clients/" + substituteID + "/status"))
152 println("----------- after recover: subject frozen_height (must be 0/0)")
153 println(core.Render("clients/" + subjectID))
154}
155
156// Output:
157// ----------- before recover: subject status
158// {"status":"Frozen"}
159// ----------- before recover: substitute status
160// {"status":"Active"}
161// ----------- after recover: subject status
162// {"status":"Active"}
163// ----------- after recover: substitute status
164// {"status":"Active"}
165// ----------- after recover: subject frozen_height (must be 0/0)
166// {"id":"07-tendermint-1","type":"07-tendermint","creator":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","status":"Active","counterparty_client_id":"","counterparty_merke_prefix":[],"client_state":{"chain_id":"atomone-1","latest_height":{"revision_number":1,"revision_height":3},"frozen_height":{"revision_number":0,"revision_height":0},"trust_level":{"numerator":1,"denominator":3},"trusting_period":3600,"unbonding_period":10800,"max_clock_drift":3600,"upgrade_path":[]},"last_consensus_state":{"height":{"revision_number":1,"revision_height":3},"timestamp":1234567950,"root":"m9afpc+PXaYRHdGC7FkrzuzRNn8ONC1xRCzYYAPRCU8=","next_validators_hash":"OJfJuH6dfiZI2d0uAxqamqfIVqV4c4oEAZigvJ8UqFQ="}}
167
168// Events:
169// [
170// {
171// "type": "create_client",
172// "attrs": [
173// {
174// "key": "client_id",
175// "value": "07-tendermint-1"
176// },
177// {
178// "key": "client_type",
179// "value": "07-tendermint"
180// },
181// {
182// "key": "consensus_heights",
183// "value": "1/2"
184// }
185// ],
186// "pkg_path": "gno.land/r/aib/ibc/core"
187// },
188// {
189// "type": "client_misbehaviour",
190// "attrs": [
191// {
192// "key": "client_id",
193// "value": "07-tendermint-1"
194// },
195// {
196// "key": "client_type",
197// "value": "07-tendermint"
198// }
199// ],
200// "pkg_path": "gno.land/r/aib/ibc/core"
201// },
202// {
203// "type": "create_client",
204// "attrs": [
205// {
206// "key": "client_id",
207// "value": "07-tendermint-2"
208// },
209// {
210// "key": "client_type",
211// "value": "07-tendermint"
212// },
213// {
214// "key": "consensus_heights",
215// "value": "1/2"
216// }
217// ],
218// "pkg_path": "gno.land/r/aib/ibc/core"
219// },
220// {
221// "type": "update_client",
222// "attrs": [
223// {
224// "key": "client_id",
225// "value": "07-tendermint-2"
226// },
227// {
228// "key": "client_type",
229// "value": "07-tendermint"
230// },
231// {
232// "key": "consensus_heights",
233// "value": "1/3"
234// }
235// ],
236// "pkg_path": "gno.land/r/aib/ibc/core"
237// },
238// {
239// "type": "recover_client",
240// "attrs": [
241// {
242// "key": "subject_client_id",
243// "value": "07-tendermint-1"
244// },
245// {
246// "key": "substitute_client_id",
247// "value": "07-tendermint-2"
248// },
249// {
250// "key": "client_type",
251// "value": "07-tendermint"
252// }
253// ],
254// "pkg_path": "gno.land/r/aib/ibc/core"
255// }
256// ]
257