z9aa_recover_client_filetest.gno
3.17 Kb · 115 lines
1// PKGPATH: gno.land/r/aib/main
2package main
3
4import (
5 "testing"
6 "time"
7
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: an Expired subject client is recovered using an
14// Active substitute. Expiry is simulated by advancing block time past the
15// subject's TrustingPeriod via testing.SetContext.
16func main(cur realm) {
17 core.SetAdmin(cross(cur), "g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm")
18
19 var (
20 chainID = "chain-id-2"
21 clientState = tmtesting.NewClientState(chainID, types.NewHeight(2, 2))
22 apphash = tmtesting.Hash("apphash")
23 trustedValset = tmtesting.GenValset()
24 consensusState = tmtesting.GenConsensusState(time.Now(), apphash, trustedValset.Hash())
25 )
26 subjectID := core.CreateClient(cross(cur), clientState, consensusState)
27
28 // Advance block time past the subject's TrustingPeriod so its status
29 // becomes Expired.
30 ctx := testing.GetContext()
31 ctx.Time = time.Now().Add(clientState.TrustingPeriod)
32 testing.SetContext(ctx)
33
34 // Substitute is created AFTER the time jump, so its consensus state
35 // timestamp (time.Now() = ctx.Time) is fresh relative to the new clock.
36 substituteConsState := tmtesting.GenConsensusState(time.Now(), tmtesting.Hash("apphash-sub"), trustedValset.Hash())
37 substituteID := core.CreateClient(cross(cur), clientState, substituteConsState)
38
39 println("----------- before recover: subject status")
40 println(core.Render("clients/" + subjectID + "/status"))
41 println("----------- before recover: substitute status")
42 println(core.Render("clients/" + substituteID + "/status"))
43
44 core.RecoverClient(cross(cur), subjectID, substituteID)
45
46 println("----------- after recover: subject status")
47 println(core.Render("clients/" + subjectID + "/status"))
48}
49
50// Output:
51// ----------- before recover: subject status
52// {"status":"Expired"}
53// ----------- before recover: substitute status
54// {"status":"Active"}
55// ----------- after recover: subject status
56// {"status":"Active"}
57
58// Events:
59// [
60// {
61// "type": "create_client",
62// "attrs": [
63// {
64// "key": "client_id",
65// "value": "07-tendermint-1"
66// },
67// {
68// "key": "client_type",
69// "value": "07-tendermint"
70// },
71// {
72// "key": "consensus_heights",
73// "value": "2/2"
74// }
75// ],
76// "pkg_path": "gno.land/r/aib/ibc/core"
77// },
78// {
79// "type": "create_client",
80// "attrs": [
81// {
82// "key": "client_id",
83// "value": "07-tendermint-2"
84// },
85// {
86// "key": "client_type",
87// "value": "07-tendermint"
88// },
89// {
90// "key": "consensus_heights",
91// "value": "2/2"
92// }
93// ],
94// "pkg_path": "gno.land/r/aib/ibc/core"
95// },
96// {
97// "type": "recover_client",
98// "attrs": [
99// {
100// "key": "subject_client_id",
101// "value": "07-tendermint-1"
102// },
103// {
104// "key": "substitute_client_id",
105// "value": "07-tendermint-2"
106// },
107// {
108// "key": "client_type",
109// "value": "07-tendermint"
110// }
111// ],
112// "pkg_path": "gno.land/r/aib/ibc/core"
113// }
114// ]
115