-
Notifications
You must be signed in to change notification settings - Fork 556
/
client_state.go
491 lines (422 loc) · 15 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package types
import (
"reflect"
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v3/modules/core/23-commitment/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
)
var _ exported.ClientState = (*ClientState)(nil)
// NewClientState creates a new ClientState instance.
func NewClientState(latestSequence uint64, consensusState *ConsensusState, allowUpdateAfterProposal bool) *ClientState {
return &ClientState{
Sequence: latestSequence,
IsFrozen: false,
ConsensusState: consensusState,
AllowUpdateAfterProposal: allowUpdateAfterProposal,
}
}
// ClientType is Solo Machine.
func (cs ClientState) ClientType() string {
return exported.Solomachine
}
// GetLatestHeight returns the latest sequence number.
// Return exported.Height to satisfy ClientState interface
// Revision number is always 0 for a solo-machine.
func (cs ClientState) GetLatestHeight() exported.Height {
return clienttypes.NewHeight(0, cs.Sequence)
}
// Status returns the status of the solo machine client.
// The client may be:
// - Active: if frozen sequence is 0
// - Frozen: otherwise solo machine is frozen
func (cs ClientState) Status(_ sdk.Context, _ sdk.KVStore, _ codec.BinaryCodec) exported.Status {
if cs.IsFrozen {
return exported.Frozen
}
return exported.Active
}
// Validate performs basic validation of the client state fields.
func (cs ClientState) Validate() error {
if cs.Sequence == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidClient, "sequence cannot be 0")
}
if cs.ConsensusState == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be nil")
}
return cs.ConsensusState.ValidateBasic()
}
// ZeroCustomFields returns solomachine client state with client-specific fields FrozenSequence,
// and AllowUpdateAfterProposal zeroed out
func (cs ClientState) ZeroCustomFields() exported.ClientState {
return NewClientState(
cs.Sequence, cs.ConsensusState, false,
)
}
// Initialize will check that initial consensus state is equal to the latest consensus state of the initial client.
func (cs ClientState) Initialize(_ sdk.Context, _ codec.BinaryCodec, _ sdk.KVStore, consState exported.ConsensusState) error {
if !reflect.DeepEqual(cs.ConsensusState, consState) {
return sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "consensus state in initial client does not equal initial consensus state. expected: %s, got: %s",
cs.ConsensusState, consState)
}
return nil
}
// ExportMetadata is a no-op since solomachine does not store any metadata in client store
func (cs ClientState) ExportMetadata(_ sdk.KVStore) []exported.GenesisMetadata {
return nil
}
// VerifyUpgradeAndUpdateState returns an error since solomachine client does not support upgrades
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 solomachine client")
}
// VerifyClientState verifies a proof of the client state of the running chain
// stored on the solo machine.
func (cs *ClientState) VerifyClientState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
prefix exported.Prefix,
counterpartyClientIdentifier string,
proof []byte,
clientState exported.ClientState,
) error {
// NOTE: the proof height sequence is incremented by one due to the connection handshake verification ordering
height = clienttypes.NewHeight(height.GetRevisionNumber(), height.GetRevisionHeight()+1)
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
clientPrefixedPath := commitmenttypes.NewMerklePath(host.FullClientStatePath(counterpartyClientIdentifier))
path, err := commitmenttypes.ApplyPrefix(prefix, clientPrefixedPath)
if err != nil {
return err
}
signBz, err := ClientStateSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, clientState)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
return nil
}
// VerifyClientConsensusState verifies a proof of the consensus state of the
// running chain stored on the solo machine.
func (cs *ClientState) VerifyClientConsensusState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
counterpartyClientIdentifier string,
consensusHeight exported.Height,
prefix exported.Prefix,
proof []byte,
consensusState exported.ConsensusState,
) error {
// NOTE: the proof height sequence is incremented by two due to the connection handshake verification ordering
height = clienttypes.NewHeight(height.GetRevisionNumber(), height.GetRevisionHeight()+2)
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
clientPrefixedPath := commitmenttypes.NewMerklePath(host.FullConsensusStatePath(counterpartyClientIdentifier, consensusHeight))
path, err := commitmenttypes.ApplyPrefix(prefix, clientPrefixedPath)
if err != nil {
return err
}
signBz, err := ConsensusStateSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, consensusState)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
return nil
}
// VerifyConnectionState verifies a proof of the connection state of the
// specified connection end stored on the target machine.
func (cs *ClientState) VerifyConnectionState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
prefix exported.Prefix,
proof []byte,
connectionID string,
connectionEnd exported.ConnectionI,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
connectionPath := commitmenttypes.NewMerklePath(host.ConnectionPath(connectionID))
path, err := commitmenttypes.ApplyPrefix(prefix, connectionPath)
if err != nil {
return err
}
signBz, err := ConnectionStateSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, connectionEnd)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
return nil
}
// VerifyChannelState verifies a proof of the channel state of the specified
// channel end, under the specified port, stored on the target machine.
func (cs *ClientState) VerifyChannelState(
store sdk.KVStore,
cdc codec.BinaryCodec,
height exported.Height,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
channel exported.ChannelI,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
channelPath := commitmenttypes.NewMerklePath(host.ChannelPath(portID, channelID))
path, err := commitmenttypes.ApplyPrefix(prefix, channelPath)
if err != nil {
return err
}
signBz, err := ChannelStateSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, channel)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
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,
cdc codec.BinaryCodec,
height exported.Height,
_ uint64,
_ uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
packetSequence uint64,
commitmentBytes []byte,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
commitmentPath := commitmenttypes.NewMerklePath(host.PacketCommitmentPath(portID, channelID, packetSequence))
path, err := commitmenttypes.ApplyPrefix(prefix, commitmentPath)
if err != nil {
return err
}
signBz, err := PacketCommitmentSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, commitmentBytes)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
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,
cdc codec.BinaryCodec,
height exported.Height,
_ uint64,
_ uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
packetSequence uint64,
acknowledgement []byte,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
ackPath := commitmenttypes.NewMerklePath(host.PacketAcknowledgementPath(portID, channelID, packetSequence))
path, err := commitmenttypes.ApplyPrefix(prefix, ackPath)
if err != nil {
return err
}
signBz, err := PacketAcknowledgementSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, acknowledgement)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
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,
cdc codec.BinaryCodec,
height exported.Height,
_ uint64,
_ uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
packetSequence uint64,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
receiptPath := commitmenttypes.NewMerklePath(host.PacketReceiptPath(portID, channelID, packetSequence))
path, err := commitmenttypes.ApplyPrefix(prefix, receiptPath)
if err != nil {
return err
}
signBz, err := PacketReceiptAbsenceSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
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,
cdc codec.BinaryCodec,
height exported.Height,
_ uint64,
_ uint64,
prefix exported.Prefix,
proof []byte,
portID,
channelID string,
nextSequenceRecv uint64,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, height, prefix, proof)
if err != nil {
return err
}
nextSequenceRecvPath := commitmenttypes.NewMerklePath(host.NextSequenceRecvPath(portID, channelID))
path, err := commitmenttypes.ApplyPrefix(prefix, nextSequenceRecvPath)
if err != nil {
return err
}
signBz, err := NextSequenceRecvSignBytes(cdc, sequence, timestamp, cs.ConsensusState.Diversifier, path, nextSequenceRecv)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(store, cdc, cs)
return nil
}
// produceVerificationArgs perfoms the basic checks on the arguments that are
// shared between the verification functions and returns the public key of the
// consensus state, the unmarshalled proof representing the signature and timestamp
// along with the solo-machine sequence encoded in the proofHeight.
func produceVerificationArgs(
cdc codec.BinaryCodec,
cs *ClientState,
height exported.Height,
prefix exported.Prefix,
proof []byte,
) (cryptotypes.PubKey, signing.SignatureData, uint64, uint64, error) {
if revision := height.GetRevisionNumber(); revision != 0 {
return nil, nil, 0, 0, sdkerrors.Wrapf(sdkerrors.ErrInvalidHeight, "revision must be 0 for solomachine, got revision-number: %d", revision)
}
// sequence is encoded in the revision height of height struct
sequence := height.GetRevisionHeight()
if prefix == nil {
return nil, nil, 0, 0, sdkerrors.Wrap(commitmenttypes.ErrInvalidPrefix, "prefix cannot be empty")
}
_, ok := prefix.(*commitmenttypes.MerklePrefix)
if !ok {
return nil, nil, 0, 0, sdkerrors.Wrapf(commitmenttypes.ErrInvalidPrefix, "invalid prefix type %T, expected MerklePrefix", prefix)
}
if proof == nil {
return nil, nil, 0, 0, sdkerrors.Wrap(ErrInvalidProof, "proof cannot be empty")
}
timestampedSigData := &TimestampedSignatureData{}
if err := cdc.Unmarshal(proof, timestampedSigData); err != nil {
return nil, nil, 0, 0, sdkerrors.Wrapf(err, "failed to unmarshal proof into type %T", timestampedSigData)
}
timestamp := timestampedSigData.Timestamp
if len(timestampedSigData.SignatureData) == 0 {
return nil, nil, 0, 0, sdkerrors.Wrap(ErrInvalidProof, "signature data cannot be empty")
}
sigData, err := UnmarshalSignatureData(cdc, timestampedSigData.SignatureData)
if err != nil {
return nil, nil, 0, 0, err
}
if cs.ConsensusState == nil {
return nil, nil, 0, 0, sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be empty")
}
latestSequence := cs.GetLatestHeight().GetRevisionHeight()
if latestSequence != sequence {
return nil, nil, 0, 0, sdkerrors.Wrapf(
sdkerrors.ErrInvalidHeight,
"client state sequence != proof sequence (%d != %d)", latestSequence, sequence,
)
}
if cs.ConsensusState.GetTimestamp() > timestamp {
return nil, nil, 0, 0, sdkerrors.Wrapf(ErrInvalidProof, "the consensus state timestamp is greater than the signature timestamp (%d >= %d)", cs.ConsensusState.GetTimestamp(), timestamp)
}
publicKey, err := cs.ConsensusState.GetPubKey()
if err != nil {
return nil, nil, 0, 0, err
}
return publicKey, sigData, timestamp, sequence, nil
}
// sets the client state to the store
func setClientState(store sdk.KVStore, cdc codec.BinaryCodec, clientState exported.ClientState) {
bz := clienttypes.MustMarshalClientState(cdc, clientState)
store.Set([]byte(host.KeyClientState), bz)
}