testing.gno
4.09 Kb · 154 lines
1package testing
2
3import (
4 "crypto/sha256"
5 "encoding/base64"
6 "time"
7
8 "gno.land/p/aib/ibc/lightclient/tendermint"
9 "gno.land/p/aib/ibc/types"
10 "gno.land/p/aib/ics23"
11)
12
13func NewMsgHeader(chainID string, timestamp time.Time,
14 apphash []byte, height uint64, trustedHeight types.Height,
15 valset, nextValset, trustedValset *tendermint.ValidatorSet,
16 signatures []tendermint.CommitSig) *tendermint.MsgHeader {
17 header := &tendermint.Header{
18 Version: tendermint.Consensus{
19 Block: tendermint.BlockProtocol,
20 App: 0,
21 },
22 ChainID: chainID,
23 Height: height,
24 Time: timestamp,
25 LastBlockID: tendermint.BlockID{
26 Hash: Hash("last_block_hash"),
27 PartSetHeader: tendermint.PartSetHeader{
28 Total: 1,
29 Hash: Hash("last_block_partset_hash"),
30 },
31 },
32 LastCommitHash: Hash("last_commit_hash"),
33 DataHash: Hash("data_hash"),
34 ValidatorsHash: valset.Hash(),
35 NextValidatorsHash: nextValset.Hash(),
36 ConsensusHash: Hash("consensus_hash"),
37 AppHash: apphash,
38 LastResultsHash: Hash("last_results_hash"),
39 EvidenceHash: Hash("evidence_hash"),
40 ProposerAddress: valset.Validators[0].Address,
41 }
42
43 return &tendermint.MsgHeader{
44 Header: header,
45 Commit: &tendermint.Commit{
46 Height: height,
47 Round: 0,
48 BlockID: tendermint.BlockID{
49 Hash: header.Hash(), // compute expected block hash
50 PartSetHeader: tendermint.PartSetHeader{
51 Total: 1,
52 Hash: Hash("block_partset_hash"),
53 },
54 },
55 Signatures: signatures,
56 },
57 ValidatorSet: valset,
58 TrustedHeight: trustedHeight,
59 TrustedValidators: trustedValset,
60 }
61}
62
63func NewClientState(chainID string, height types.Height) tendermint.ClientState {
64 return tendermint.ClientState{
65 ChainID: chainID,
66 TrustLevel: tendermint.DefaultTrustLevel,
67 UnbondingPeriod: time.Hour * 3,
68 TrustingPeriod: time.Hour,
69 MaxClockDrift: time.Hour,
70 LatestHeight: height,
71 ProofSpecs: ics23.GetSDKProofSpecs(),
72 }
73}
74
75func GenValset() *tendermint.ValidatorSet {
76 var (
77 val1 = &tendermint.Validator{
78 Address: GenAddr("val1"),
79 PubKey: GenPubkey("val1_pubkey"),
80 VotingPower: 2,
81 }
82 val2 = &tendermint.Validator{
83 Address: GenAddr("val2"),
84 PubKey: GenPubkey("val2_pubkey"),
85 VotingPower: 3,
86 }
87 )
88 return tendermint.NewValset(val1, val2)
89}
90
91func GenConsensusState(timestamp time.Time, apphash, valsethash []byte) tendermint.ConsensusState {
92 return tendermint.ConsensusState{
93 Timestamp: timestamp,
94 Root: tendermint.MerkleRoot{Hash: apphash},
95 NextValidatorsHash: valsethash,
96 }
97}
98
99func GenMsgHeader(chainID string, timestamp time.Time, apphash []byte,
100 height uint64, trustedHeight types.Height,
101 valset, nextValset, trustedValset *tendermint.ValidatorSet) *tendermint.MsgHeader {
102 var (
103 signatures = []tendermint.CommitSig{
104 {
105 BlockIDFlag: tendermint.BlockIDFlagCommit,
106 ValidatorAddress: valset.Validators[0].Address,
107 Timestamp: ToTime("2025-09-25T07:55:57.306746166Z"),
108 Signature: B64Dec("qtv1z4S2Q6T87vGQo0lrjRZqv9PrHIji4pTyviMnVyGx9td6eySdzwQwCthwmihU48ebNlFiMlFJ0CT891UmDg=="),
109 },
110 {
111 BlockIDFlag: tendermint.BlockIDFlagCommit,
112 ValidatorAddress: valset.Validators[1].Address,
113 Timestamp: ToTime("2025-09-25T07:55:57.310583641Z"),
114 Signature: B64Dec("Q5E6Kjma00n/T98rC9qJmoB6JTGFX/IB+mDVs4Wd1h0eJ8fabY/6oI8zdoU6/7W6VR6wjpHyWBsJrpGT6C0LCg=="),
115 },
116 }
117 )
118 return NewMsgHeader(chainID, timestamp, apphash, height, trustedHeight,
119 valset, nextValset, trustedValset, signatures)
120}
121
122func GenAddr(seed string) []byte {
123 return Hash(seed)[:tendermint.AddressSize]
124}
125
126func GenSignature(seed string) []byte {
127 h := Hash(seed)
128 return append(h, Hash(string(h))...) // 32x2=64 bytes
129}
130
131func GenPubkey(seed string) []byte {
132 return Hash(seed) //pubkey len is 32 bytes
133}
134
135func Hash(s string) []byte {
136 bz := sha256.Sum256([]byte(s))
137 return bz[:]
138}
139
140func B64Dec(s string) []byte {
141 bz, err := base64.StdEncoding.DecodeString(s)
142 if err != nil {
143 panic(err)
144 }
145 return bz
146}
147
148func ToTime(s string) time.Time {
149 t, err := time.Parse(time.RFC3339Nano, s)
150 if err != nil {
151 panic(err)
152 }
153 return t
154}