-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
248 lines (202 loc) · 6.83 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package utils
import (
"context"
clictx "github.com/brc20-collab/brczero/libs/cosmos-sdk/client/context"
sdkerrors "github.com/brc20-collab/brczero/libs/cosmos-sdk/types/errors"
"github.com/brc20-collab/brczero/libs/ibc-go/modules/core/02-client/types"
commitmenttypes "github.com/brc20-collab/brczero/libs/ibc-go/modules/core/23-commitment/types"
host "github.com/brc20-collab/brczero/libs/ibc-go/modules/core/24-host"
ibcclient "github.com/brc20-collab/brczero/libs/ibc-go/modules/core/client"
"github.com/brc20-collab/brczero/libs/ibc-go/modules/core/exported"
ibctmtypes "github.com/brc20-collab/brczero/libs/ibc-go/modules/light-clients/07-tendermint/types"
tmtypes "github.com/brc20-collab/brczero/libs/tendermint/types"
)
// QueryClientState returns a client state. If prove is true, it performs an ABCI store query
// in order to retrieve the merkle proof. Otherwise, it uses the gRPC query client.
func QueryClientState(
clientCtx clictx.CLIContext, clientID string, prove bool,
) (*types.QueryClientStateResponse, error) {
if prove {
return QueryClientStateABCI(clientCtx, clientID)
}
queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryClientStateRequest{
ClientId: clientID,
}
return queryClient.ClientState(context.Background(), req)
}
// QueryClientStateABCI queries the store to get the light client state and a merkle proof.
func QueryClientStateABCI(
clientCtx clictx.CLIContext, clientID string,
) (*types.QueryClientStateResponse, error) {
key := host.FullClientStateKey(clientID)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
return nil, err
}
// check if client exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(types.ErrClientNotFound, clientID)
}
cdc := clientCtx.CodecProy
clientState, err := types.UnmarshalClientState(cdc, value)
if err != nil {
return nil, err
}
anyClientState, err := types.PackClientState(clientState)
if err != nil {
return nil, err
}
clientStateRes := types.NewQueryClientStateResponse(anyClientState, proofBz, proofHeight)
return clientStateRes, nil
}
// QueryConsensusState returns a consensus state. If prove is true, it performs an ABCI store
// query in order to retrieve the merkle proof. Otherwise, it uses the gRPC query client.
func QueryConsensusState(
clientCtx clictx.CLIContext, clientID string, height exported.Height, prove, latestHeight bool,
) (*types.QueryConsensusStateResponse, error) {
if prove {
return QueryConsensusStateABCI(clientCtx, clientID, height)
}
queryClient := types.NewQueryClient(clientCtx)
req := &types.QueryConsensusStateRequest{
ClientId: clientID,
RevisionNumber: height.GetRevisionNumber(),
RevisionHeight: height.GetRevisionHeight(),
LatestHeight: latestHeight,
}
return queryClient.ConsensusState(context.Background(), req)
}
// QueryConsensusStateABCI queries the store to get the consensus state of a light client and a
// merkle proof of its existence or non-existence.
func QueryConsensusStateABCI(
clientCtx clictx.CLIContext, clientID string, height exported.Height,
) (*types.QueryConsensusStateResponse, error) {
key := host.FullConsensusStateKey(clientID, height)
value, proofBz, proofHeight, err := ibcclient.QueryTendermintProof(clientCtx, key)
if err != nil {
return nil, err
}
// check if consensus state exists
if len(value) == 0 {
return nil, sdkerrors.Wrap(types.ErrConsensusStateNotFound, clientID)
}
cdc := clientCtx.CodecProy
cs, err := types.UnmarshalConsensusState(cdc, value)
if err != nil {
return nil, err
}
anyConsensusState, err := types.PackConsensusState(cs)
if err != nil {
return nil, err
}
return types.NewQueryConsensusStateResponse(anyConsensusState, proofBz, proofHeight), nil
}
// QueryTendermintHeader takes a client context and returns the appropriate
// tendermint header
func QueryTendermintHeader(clientCtx clictx.CLIContext) (ibctmtypes.Header, int64, error) {
node, err := clientCtx.GetNode()
if err != nil {
return ibctmtypes.Header{}, 0, err
}
info, err := node.ABCIInfo()
if err != nil {
return ibctmtypes.Header{}, 0, err
}
var height int64
if clientCtx.Height != 0 {
height = clientCtx.Height
} else {
height = info.Response.LastBlockHeight
}
commit, err := node.Commit(&height)
if err != nil {
return ibctmtypes.Header{}, 0, err
}
page := 1
count := 10_000
validators, err := node.Validators(&height, page, count)
if err != nil {
return ibctmtypes.Header{}, 0, err
}
protoCommit := commit.SignedHeader.ToProto()
protoValset, err := tmtypes.NewValidatorSet(validators.Validators).ToProto()
if err != nil {
return ibctmtypes.Header{}, 0, err
}
header := ibctmtypes.Header{
SignedHeader: protoCommit,
ValidatorSet: protoValset,
}
return header, height, nil
}
// QueryNodeConsensusState takes a client context and returns the appropriate
// tendermint consensus state
func QueryNodeConsensusState(clientCtx clictx.CLIContext) (*ibctmtypes.ConsensusState, int64, error) {
node, err := clientCtx.GetNode()
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
info, err := node.ABCIInfo()
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
var height int64
if clientCtx.Height != 0 {
height = clientCtx.Height
} else {
height = info.Response.LastBlockHeight
}
commit, err := node.Commit(&height)
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
page := 1
count := 10_000
nextHeight := height + 1
nextVals, err := node.Validators(&nextHeight, page, count)
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
state := &ibctmtypes.ConsensusState{
Timestamp: commit.Time,
Root: commitmenttypes.NewMerkleRoot(commit.AppHash),
NextValidatorsHash: tmtypes.NewValidatorSet(nextVals.Validators).Hash(height),
}
return state, height, nil
}
// QuerySelfConsensusState takes a client context and returns the appropriate
// tendermint consensus state
func QuerySelfConsensusState(clientCtx clictx.CLIContext) (*ibctmtypes.ConsensusState, int64, error) {
node, err := clientCtx.GetNode()
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
info, err := node.ABCIInfo()
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
var height int64
if clientCtx.Height != 0 {
height = clientCtx.Height
} else {
height = info.Response.LastBlockHeight
}
commit, err := node.Commit(&height)
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
page := 1
count := 10_000
nextHeight := height + 1
nextVals, err := node.Validators(&nextHeight, page, count)
if err != nil {
return &ibctmtypes.ConsensusState{}, 0, err
}
state := &ibctmtypes.ConsensusState{
Timestamp: commit.Time,
Root: commitmenttypes.NewMerkleRoot(commit.AppHash),
NextValidatorsHash: tmtypes.NewValidatorSet(nextVals.Validators).Hash(height),
}
return state, height, nil
}