-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
82 lines (71 loc) · 2.57 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 celestia_da_light_client
import (
"bytes"
"fmt"
"time"
cmttypes "github.com/cometbft/cometbft/types"
)
type ClientMessage interface {
ClientType() string
ValidateBasic() error
}
var _ ClientMessage = (*Header)(nil)
// ConsensusState returns the updated consensus state associated with the header
func (h Header) ConsensusState() *ConsensusState {
return &ConsensusState{
Timestamp: h.GetTime(),
Root: h.Header.GetDataHash(),
NextValidatorsHash: h.Header.NextValidatorsHash,
}
}
// ClientType defines that the Header is a Tendermint consensus algorithm
func (Header) ClientType() string {
return ModuleName
}
// 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() Height {
revision := ParseChainID(h.Header.ChainID)
return 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 fmt.Errorf("err invalid header, tendermint signed header cannot be nil")
}
if h.Header == nil {
return fmt.Errorf("err invalid header, tendermint header cannot be nil")
}
tmSignedHeader, err := cmttypes.SignedHeaderFromProto(h.SignedHeader)
if err != nil {
return fmt.Errorf("err, header is not a tendermint header, %v", err)
}
if err := tmSignedHeader.ValidateBasic(h.Header.GetChainID()); err != nil {
return fmt.Errorf("err, header failed basic validation, %v", err)
}
// TrustedHeight is less than Header for updates and misbehaviour
if h.TrustedHeight.GTE(h.GetHeight()) {
return fmt.Errorf("invalid header height, TrustedHeight %d must be less than header height %d", h.TrustedHeight, h.Commit.GetHeight())
}
if h.ValidatorSet == nil {
return fmt.Errorf("invalid header, validator set is nil")
}
tmValset, err := cmttypes.ValidatorSetFromProto(h.ValidatorSet)
if err != nil {
return fmt.Errorf("validator set is not tendermint validator set, %v", err)
}
if !bytes.Equal(h.Header.ValidatorsHash, tmValset.Hash()) {
return fmt.Errorf("invalid header, validator set does not match hash")
}
return nil
}