-
Notifications
You must be signed in to change notification settings - Fork 580
/
update.go
107 lines (87 loc) · 3.55 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
103
104
105
106
107
package solomachine
import (
"fmt"
"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/v7/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v7/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 sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) error {
switch msg := clientMsg.(type) {
case *Header:
return cs.verifyHeader(ctx, cdc, clientStore, msg)
case *Misbehaviour:
return cs.verifyMisbehaviour(ctx, cdc, clientStore, msg)
default:
return sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "expected type of %T or %T, got type %T", Header{}, Misbehaviour{}, msg)
}
}
func (cs ClientState) verifyHeader(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, header *Header) error {
// assert update timestamp is not less than current consensus state timestamp
if header.Timestamp < cs.ConsensusState.Timestamp {
return sdkerrors.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 sdkerrors.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.
func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, clientMsg exported.ClientMessage) []exported.Height {
smHeader, ok := clientMsg.(*Header)
if !ok {
panic(fmt.Errorf("unsupported ClientMessage: %T", clientMsg))
}
// 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)}
}
// UpdateStateOnMisbehaviour updates state upon misbehaviour. This method should only be called on misbehaviour
// as it does not perform any misbehaviour checks.
func (cs ClientState) UpdateStateOnMisbehaviour(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, _ exported.ClientMessage) {
cs.IsFrozen = true
setClientState(clientStore, cdc, &cs)
}