-
Notifications
You must be signed in to change notification settings - Fork 628
/
update.go
102 lines (83 loc) · 3.25 KB
/
update.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
package solomachine
import (
"context"
errorsmod "cosmossdk.io/errors"
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/codec"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
)
// VerifyClientMessage introspects the provided ClientMessage and checks its validity
// A Solomachine Header is considered valid if the currently registered public key has signed over the new public key with the correct sequence
// A Solomachine Misbehaviour is considered valid if duplicate signatures of the current public key are found on two different messages at a given sequence
func (cs ClientState) VerifyClientMessage(ctx context.Context, cdc codec.BinaryCodec, clientStore storetypes.KVStore, clientMsg exported.ClientMessage) error {
switch msg := clientMsg.(type) {
case *Header:
return cs.verifyHeader(cdc, msg)
case *Misbehaviour:
return cs.verifyMisbehaviour(cdc, msg)
default:
return errorsmod.Wrapf(clienttypes.ErrInvalidClientType, "expected type of %T or %T, got type %T", Header{}, Misbehaviour{}, msg)
}
}
func (cs ClientState) verifyHeader(cdc codec.BinaryCodec, header *Header) error {
// assert update timestamp is not less than current consensus state timestamp
if header.Timestamp < cs.ConsensusState.Timestamp {
return errorsmod.Wrapf(
clienttypes.ErrInvalidHeader,
"header timestamp is less than to the consensus state timestamp (%d < %d)", header.Timestamp, cs.ConsensusState.Timestamp,
)
}
// assert currently registered public key signed over the new public key with correct sequence
headerData := &HeaderData{
NewPubKey: header.NewPublicKey,
NewDiversifier: header.NewDiversifier,
}
dataBz, err := cdc.Marshal(headerData)
if err != nil {
return err
}
signBytes := &SignBytes{
Sequence: cs.Sequence,
Timestamp: header.Timestamp,
Diversifier: cs.ConsensusState.Diversifier,
Path: []byte(SentinelHeaderPath),
Data: dataBz,
}
data, err := cdc.Marshal(signBytes)
if err != nil {
return err
}
sigData, err := UnmarshalSignatureData(cdc, header.Signature)
if err != nil {
return err
}
publicKey, err := cs.ConsensusState.GetPubKey()
if err != nil {
return err
}
if err := VerifySignature(publicKey, data, sigData); err != nil {
return errorsmod.Wrap(ErrInvalidHeader, err.Error())
}
return nil
}
// UpdateState updates the consensus state to the new public key and an incremented sequence.
// A list containing the updated consensus height is returned.
// If the provided clientMsg is not of type Header, the handler will no-op and return an empty slice.
func (cs ClientState) UpdateState(ctx context.Context, cdc codec.BinaryCodec, clientStore storetypes.KVStore, clientMsg exported.ClientMessage) []exported.Height {
smHeader, ok := clientMsg.(*Header)
if !ok {
// clientMsg is invalid Misbehaviour, no update necessary
return []exported.Height{}
}
// create new solomachine ConsensusState
consensusState := &ConsensusState{
PublicKey: smHeader.NewPublicKey,
Diversifier: smHeader.NewDiversifier,
Timestamp: smHeader.Timestamp,
}
cs.Sequence++
cs.ConsensusState = consensusState
setClientState(clientStore, cdc, &cs)
return []exported.Height{clienttypes.NewHeight(0, cs.Sequence)}
}