-
Notifications
You must be signed in to change notification settings - Fork 580
/
client_state.go
340 lines (296 loc) · 10.1 KB
/
client_state.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package types
import (
"bytes"
"encoding/binary"
"reflect"
"strings"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v4/modules/core/02-client/types"
connectiontypes "github.com/cosmos/ibc-go/v4/modules/core/03-connection/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v4/modules/core/24-host"
"github.com/cosmos/ibc-go/v4/modules/core/exported"
)
var _ exported.ClientState = (*ClientState)(nil)
// NewClientState creates a new ClientState instance
func NewClientState(chainID string, height clienttypes.Height) *ClientState {
return &ClientState{
ChainId: chainID,
Height: height,
}
}
// GetChainID returns an empty string
func (cs ClientState) GetChainID() string {
return cs.ChainId
}
// ClientType is localhost.
func (cs ClientState) ClientType() string {
return exported.Localhost
}
// GetLatestHeight returns the latest height stored.
func (cs ClientState) GetLatestHeight() exported.Height {
return cs.Height
}
// Status always returns Active. The localhost status cannot be changed.
func (cs ClientState) Status(_ sdk.Context, _ sdk.KVStore, _ codec.BinaryCodec,
) exported.Status {
return exported.Active
}
// Validate performs a basic validation of the client state fields.
func (cs ClientState) Validate() error {
if strings.TrimSpace(cs.ChainId) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidChainID, "chain id cannot be blank")
}
if cs.Height.RevisionHeight == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "local revision height cannot be zero")
}
return nil
}
// ZeroCustomFields returns the same client state since there are no custom fields in localhost
func (cs ClientState) ZeroCustomFields() exported.ClientState {
return &cs
}
// Initialize ensures that initial consensus state for localhost is nil
func (cs ClientState) Initialize(_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, consState exported.ConsensusState) error {
if consState != nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "initial consensus state for localhost must be nil.")
}
return nil
}
// ExportMetadata is a no-op for localhost client
func (cs ClientState) ExportMetadata(_ sdk.KVStore) []exported.GenesisMetadata {
return nil
}
// CheckHeaderAndUpdateState updates the localhost client. It only needs access to the context
func (cs *ClientState) CheckHeaderAndUpdateState(
ctx sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, _ exported.Header,
) (exported.ClientState, exported.ConsensusState, error) {
// use the chain ID from context since the localhost client is from the running chain (i.e self).
cs.ChainId = ctx.ChainID()
revision := clienttypes.ParseChainID(cs.ChainId)
cs.Height = clienttypes.NewHeight(revision, uint64(ctx.BlockHeight()))
return cs, nil, nil
}
// CheckMisbehaviourAndUpdateState implements ClientState
// Since localhost is the client of the running chain, misbehaviour cannot be submitted to it
// Thus, CheckMisbehaviourAndUpdateState returns an error for localhost
func (cs ClientState) CheckMisbehaviourAndUpdateState(
_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, _ exported.Misbehaviour,
) (exported.ClientState, error) {
return nil, sdkerrors.Wrap(clienttypes.ErrInvalidMisbehaviour, "cannot submit misbehaviour to localhost client")
}
// CheckSubstituteAndUpdateState returns an error. The localhost cannot be modified by
// proposals.
func (cs ClientState) CheckSubstituteAndUpdateState(
ctx sdk.Context, _ codec.BinaryCodec, _, _ sdk.KVStore,
_ exported.ClientState,
) (exported.ClientState, error) {
return nil, sdkerrors.Wrap(clienttypes.ErrUpdateClientFailed, "cannot update localhost client with a proposal")
}
// VerifyUpgradeAndUpdateState returns an error since localhost cannot be upgraded
func (cs ClientState) VerifyUpgradeAndUpdateState(
_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore,
_ exported.ClientState, _ exported.ConsensusState, _, _ []byte,
) (exported.ClientState, exported.ConsensusState, error) {
return nil, nil, sdkerrors.Wrap(clienttypes.ErrInvalidUpgradeClient, "cannot upgrade localhost client")
}
// VerifyClientState verifies that the localhost client state is stored locally
func (cs ClientState) VerifyClientState(
store sdk.KVStore, cdc codec.BinaryCodec,
_ exported.Height, _ exported.Prefix, _ string, _ []byte, clientState exported.ClientState,
) error {
path := host.KeyClientState
bz := store.Get([]byte(path))
if bz == nil {
return sdkerrors.Wrapf(clienttypes.ErrFailedClientStateVerification,
"not found for path: %s", path)
}
selfClient := clienttypes.MustUnmarshalClientState(cdc, bz)
if !reflect.DeepEqual(selfClient, clientState) {
return sdkerrors.Wrapf(clienttypes.ErrFailedClientStateVerification,
"stored clientState != provided clientState: \n%v\n≠\n%v",
selfClient, clientState,
)
}
return nil
}
// VerifyClientConsensusState returns nil since a local host client does not store consensus
// states.
func (cs ClientState) VerifyClientConsensusState(
sdk.KVStore, codec.BinaryCodec,
exported.Height, string, exported.Height, exported.Prefix,
[]byte, exported.ConsensusState,
) error {
return nil
}
// VerifyConnectionState verifies a proof of the connection state of the
// specified connection end stored locally.
func (cs ClientState) VerifyConnectionState(
store sdk.KVStore,
cdc codec.BinaryCodec,
_ exported.Height,
_ exported.Prefix,
_ []byte,
connectionID string,
connectionEnd exported.ConnectionI,
) error {
path := host.ConnectionKey(connectionID)
bz := store.Get(path)
if bz == nil {
return sdkerrors.Wrapf(clienttypes.ErrFailedConnectionStateVerification, "not found for path %s", path)
}
var prevConnection connectiontypes.ConnectionEnd
err := cdc.Unmarshal(bz, &prevConnection)
if err != nil {
return err
}
if !reflect.DeepEqual(&prevConnection, connectionEnd) {
return sdkerrors.Wrapf(
clienttypes.ErrFailedConnectionStateVerification,
"connection end ≠ previous stored connection: \n%v\n≠\n%v", connectionEnd, prevConnection,
)
}
return nil
}
// VerifyChannelState verifies a proof of the channel state of the specified
// channel end, under the specified port, stored on the local machine.
func (cs ClientState) VerifyChannelState(
store sdk.KVStore,
cdc codec.BinaryCodec,
_ exported.Height,
prefix exported.Prefix,
_ []byte,
portID,
channelID string,
channel exported.ChannelI,
) error {
path := host.ChannelKey(portID, channelID)
bz := store.Get(path)
if bz == nil {
return sdkerrors.Wrapf(clienttypes.ErrFailedChannelStateVerification, "not found for path %s", path)
}
var prevChannel channeltypes.Channel
err := cdc.Unmarshal(bz, &prevChannel)
if err != nil {
return err
}
if !reflect.DeepEqual(&prevChannel, channel) {
return sdkerrors.Wrapf(
clienttypes.ErrFailedChannelStateVerification,
"channel end ≠ previous stored channel: \n%v\n≠\n%v", channel, prevChannel,
)
}
return nil
}
// VerifyPacketCommitment verifies a proof of an outgoing packet commitment at
// the specified port, specified channel, and specified sequence.
func (cs ClientState) VerifyPacketCommitment(
ctx sdk.Context,
store sdk.KVStore,
_ codec.BinaryCodec,
_ exported.Height,
_ uint64,
_ uint64,
_ exported.Prefix,
_ []byte,
portID,
channelID string,
sequence uint64,
commitmentBytes []byte,
) error {
path := host.PacketCommitmentKey(portID, channelID, sequence)
data := store.Get(path)
if len(data) == 0 {
return sdkerrors.Wrapf(clienttypes.ErrFailedPacketCommitmentVerification, "not found for path %s", path)
}
if !bytes.Equal(data, commitmentBytes) {
return sdkerrors.Wrapf(
clienttypes.ErrFailedPacketCommitmentVerification,
"commitment ≠ previous commitment: \n%X\n≠\n%X", commitmentBytes, data,
)
}
return nil
}
// VerifyPacketAcknowledgement verifies a proof of an incoming packet
// acknowledgement at the specified port, specified channel, and specified sequence.
func (cs ClientState) VerifyPacketAcknowledgement(
ctx sdk.Context,
store sdk.KVStore,
_ codec.BinaryCodec,
_ exported.Height,
_ uint64,
_ uint64,
_ exported.Prefix,
_ []byte,
portID,
channelID string,
sequence uint64,
acknowledgement []byte,
) error {
path := host.PacketAcknowledgementKey(portID, channelID, sequence)
data := store.Get(path)
if len(data) == 0 {
return sdkerrors.Wrapf(clienttypes.ErrFailedPacketAckVerification, "not found for path %s", path)
}
if !bytes.Equal(data, acknowledgement) {
return sdkerrors.Wrapf(
clienttypes.ErrFailedPacketAckVerification,
"ak bytes ≠ previous ack: \n%X\n≠\n%X", acknowledgement, data,
)
}
return nil
}
// VerifyPacketReceiptAbsence verifies a proof of the absence of an
// incoming packet receipt at the specified port, specified channel, and
// specified sequence.
func (cs ClientState) VerifyPacketReceiptAbsence(
ctx sdk.Context,
store sdk.KVStore,
_ codec.BinaryCodec,
_ exported.Height,
_ uint64,
_ uint64,
_ exported.Prefix,
_ []byte,
portID,
channelID string,
sequence uint64,
) error {
path := host.PacketReceiptKey(portID, channelID, sequence)
data := store.Get(path)
if data != nil {
return sdkerrors.Wrap(clienttypes.ErrFailedPacketReceiptVerification, "expected no packet receipt")
}
return nil
}
// VerifyNextSequenceRecv verifies a proof of the next sequence number to be
// received of the specified channel at the specified port.
func (cs ClientState) VerifyNextSequenceRecv(
ctx sdk.Context,
store sdk.KVStore,
_ codec.BinaryCodec,
_ exported.Height,
_ uint64,
_ uint64,
_ exported.Prefix,
_ []byte,
portID,
channelID string,
nextSequenceRecv uint64,
) error {
path := host.NextSequenceRecvKey(portID, channelID)
data := store.Get(path)
if len(data) == 0 {
return sdkerrors.Wrapf(clienttypes.ErrFailedNextSeqRecvVerification, "not found for path %s", path)
}
prevSequenceRecv := binary.BigEndian.Uint64(data)
if prevSequenceRecv != nextSequenceRecv {
return sdkerrors.Wrapf(
clienttypes.ErrFailedNextSeqRecvVerification,
"next sequence receive ≠ previous stored sequence (%d ≠ %d)", nextSequenceRecv, prevSequenceRecv,
)
}
return nil
}