-
Notifications
You must be signed in to change notification settings - Fork 750
Expand file tree
/
Copy pathclient_state.go
More file actions
194 lines (161 loc) · 6.12 KB
/
client_state.go
File metadata and controls
194 lines (161 loc) · 6.12 KB
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
package solomachine
import (
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
commitmenttypesv2 "github.com/cosmos/ibc-go/v9/modules/core/23-commitment/types/v2"
host "github.com/cosmos/ibc-go/v9/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)
var _ exported.ClientState = (*ClientState)(nil)
// NewClientState creates a new ClientState instance.
func NewClientState(latestSequence uint64, consensusState *ConsensusState) *ClientState {
return &ClientState{
Sequence: latestSequence,
IsFrozen: false,
ConsensusState: consensusState,
}
}
// ClientType is Solo Machine.
func (ClientState) ClientType() string {
return exported.Solomachine
}
// Validate performs basic validation of the client state fields.
func (cs ClientState) Validate() error {
if cs.Sequence == 0 {
return errorsmod.Wrap(clienttypes.ErrInvalidClient, "sequence cannot be 0")
}
if cs.ConsensusState == nil {
return errorsmod.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be nil")
}
return cs.ConsensusState.ValidateBasic()
}
// verifyMembership is a generic proof verification method which verifies a proof of the existence of a value at a given CommitmentPath at the latest sequence.
// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24).
func (cs *ClientState) verifyMembership(
clientStore storetypes.KVStore,
cdc codec.BinaryCodec,
proof []byte,
path exported.Path,
value []byte,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, proof)
if err != nil {
return err
}
merklePath, ok := path.(commitmenttypesv2.MerklePath)
if !ok {
return errorsmod.Wrapf(ibcerrors.ErrInvalidType, "expected %T, got %T", commitmenttypesv2.MerklePath{}, path)
}
if len(merklePath.GetKeyPath()) != 2 {
return errorsmod.Wrapf(host.ErrInvalidPath, "path must be of length 2: %s", merklePath.GetKeyPath())
}
// in a multistore context: index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
key, err := merklePath.GetKey(1)
if err != nil {
return errorsmod.Wrapf(host.ErrInvalidPath, "key not found at index 1: %v", err)
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: cs.ConsensusState.Diversifier,
Path: key,
Data: value,
}
signBz, err := cdc.Marshal(signBytes)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(clientStore, cdc, cs)
return nil
}
// verifyNonMembership is a generic proof verification method which verifies the absence of a given CommitmentPath at the latest sequence.
// The caller is expected to construct the full CommitmentPath from a CommitmentPrefix and a standardized path (as defined in ICS 24).
func (cs *ClientState) verifyNonMembership(
clientStore storetypes.KVStore,
cdc codec.BinaryCodec,
proof []byte,
path exported.Path,
) error {
publicKey, sigData, timestamp, sequence, err := produceVerificationArgs(cdc, cs, proof)
if err != nil {
return err
}
merklePath, ok := path.(commitmenttypesv2.MerklePath)
if !ok {
return errorsmod.Wrapf(ibcerrors.ErrInvalidType, "expected %T, got %T", commitmenttypesv2.MerklePath{}, path)
}
if len(merklePath.GetKeyPath()) != 2 {
return errorsmod.Wrapf(host.ErrInvalidPath, "path must be of length 2: %s", merklePath.GetKeyPath())
}
// in a multistore context: index 0 is the key for the IBC store in the multistore, index 1 is the key in the IBC store
key, err := merklePath.GetKey(1)
if err != nil {
return errorsmod.Wrapf(host.ErrInvalidPath, "key not found at index 1: %v", err)
}
signBytes := &SignBytes{
Sequence: sequence,
Timestamp: timestamp,
Diversifier: cs.ConsensusState.Diversifier,
Path: key,
Data: nil,
}
signBz, err := cdc.Marshal(signBytes)
if err != nil {
return err
}
if err := VerifySignature(publicKey, signBz, sigData); err != nil {
return err
}
cs.Sequence++
cs.ConsensusState.Timestamp = timestamp
setClientState(clientStore, cdc, cs)
return nil
}
// produceVerificationArgs performs 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.
func produceVerificationArgs(
cdc codec.BinaryCodec,
cs *ClientState,
proof []byte,
) (cryptotypes.PubKey, signing.SignatureData, uint64, uint64, error) {
if proof == nil {
return nil, nil, 0, 0, errorsmod.Wrap(ErrInvalidProof, "proof cannot be empty")
}
var timestampedSigData TimestampedSignatureData
if err := cdc.Unmarshal(proof, ×tampedSigData); err != nil {
return nil, nil, 0, 0, errorsmod.Wrapf(err, "failed to unmarshal proof into type %T", timestampedSigData)
}
timestamp := timestampedSigData.Timestamp
if len(timestampedSigData.SignatureData) == 0 {
return nil, nil, 0, 0, errorsmod.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.GetTimestamp() > timestamp {
return nil, nil, 0, 0, errorsmod.Wrapf(ErrInvalidProof, "the consensus state timestamp is greater than the signature timestamp (%d >= %d)", cs.ConsensusState.GetTimestamp(), timestamp)
}
sequence := cs.Sequence
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 storetypes.KVStore, cdc codec.BinaryCodec, clientState exported.ClientState) {
bz := clienttypes.MustMarshalClientState(cdc, clientState)
store.Set(host.ClientStateKey(), bz)
}