-
Notifications
You must be signed in to change notification settings - Fork 586
/
header.go
82 lines (71 loc) · 2.85 KB
/
header.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
package tendermint
import (
"bytes"
"time"
tmtypes "github.com/cometbft/cometbft/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"
)
var _ exported.ClientMessage = &Header{}
// ConsensusState returns the updated consensus state associated with the header
func (h Header) ConsensusState() *ConsensusState {
return &ConsensusState{
Timestamp: h.GetTime(),
Root: commitmenttypes.NewMerkleRoot(h.Header.GetAppHash()),
NextValidatorsHash: h.Header.NextValidatorsHash,
}
}
// ClientType defines that the Header is a Tendermint consensus algorithm
func (h Header) ClientType() string {
return exported.Tendermint
}
// GetHeight returns the current height. It returns 0 if the tendermint
// header is nil.
// NOTE: the header.Header is checked to be non nil in ValidateBasic.
func (h Header) GetHeight() exported.Height {
revision := clienttypes.ParseChainID(h.Header.ChainID)
return clienttypes.NewHeight(revision, uint64(h.Header.Height))
}
// GetTime returns the current block timestamp. It returns a zero time if
// the tendermint header is nil.
// NOTE: the header.Header is checked to be non nil in ValidateBasic.
func (h Header) GetTime() time.Time {
return h.Header.Time
}
// ValidateBasic calls the SignedHeader ValidateBasic function and checks
// that validatorsets are not nil.
// NOTE: TrustedHeight and TrustedValidators may be empty when creating client
// with MsgCreateClient
func (h Header) ValidateBasic() error {
if h.SignedHeader == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "tendermint signed header cannot be nil")
}
if h.Header == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "tendermint header cannot be nil")
}
tmSignedHeader, err := tmtypes.SignedHeaderFromProto(h.SignedHeader)
if err != nil {
return sdkerrors.Wrap(err, "header is not a tendermint header")
}
if err := tmSignedHeader.ValidateBasic(h.Header.GetChainID()); err != nil {
return sdkerrors.Wrap(err, "header failed basic validation")
}
// TrustedHeight is less than Header for updates and misbehaviour
if h.TrustedHeight.GTE(h.GetHeight()) {
return sdkerrors.Wrapf(ErrInvalidHeaderHeight, "TrustedHeight %d must be less than header height %d",
h.TrustedHeight, h.GetHeight())
}
if h.ValidatorSet == nil {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "validator set is nil")
}
tmValset, err := tmtypes.ValidatorSetFromProto(h.ValidatorSet)
if err != nil {
return sdkerrors.Wrap(err, "validator set is not tendermint validator set")
}
if !bytes.Equal(h.Header.ValidatorsHash, tmValset.Hash()) {
return sdkerrors.Wrap(clienttypes.ErrInvalidHeader, "validator set does not match hash")
}
return nil
}