-
Notifications
You must be signed in to change notification settings - Fork 573
/
consensus_state.go
61 lines (48 loc) · 1.84 KB
/
consensus_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
package types
import (
"strings"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v3/modules/core/exported"
)
var _ exported.ConsensusState = &ConsensusState{}
// ClientType returns Solo Machine type.
func (ConsensusState) ClientType() string {
return exported.Solomachine
}
// GetTimestamp returns zero.
func (cs ConsensusState) GetTimestamp() uint64 {
return cs.Timestamp
}
// GetRoot returns nil since solo machines do not have roots.
func (cs ConsensusState) GetRoot() exported.Root {
return nil
}
// GetPubKey unmarshals the public key into a cryptotypes.PubKey type.
// An error is returned if the public key is nil or the cached value
// is not a PubKey.
func (cs ConsensusState) GetPubKey() (cryptotypes.PubKey, error) {
if cs.PublicKey == nil {
return nil, sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state PublicKey cannot be nil")
}
publicKey, ok := cs.PublicKey.GetCachedValue().(cryptotypes.PubKey)
if !ok {
return nil, sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state PublicKey is not cryptotypes.PubKey")
}
return publicKey, nil
}
// ValidateBasic defines basic validation for the solo machine consensus state.
func (cs ConsensusState) ValidateBasic() error {
if cs.Timestamp == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "timestamp cannot be 0")
}
if cs.Diversifier != "" && strings.TrimSpace(cs.Diversifier) == "" {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "diversifier cannot contain only spaces")
}
publicKey, err := cs.GetPubKey()
if err != nil || publicKey == nil || len(publicKey.Bytes()) == 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "public key cannot be empty")
}
return nil
}