-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus_state.go
67 lines (60 loc) · 2.44 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
62
63
64
65
66
67
package types
import (
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
clienttypes "github.com/ci123chain/ci123chain/pkg/ibc/core/clients/types"
commitmenttypes "github.com/ci123chain/ci123chain/pkg/ibc/core/commitment/types"
"github.com/ci123chain/ci123chain/pkg/ibc/core/exported"
tmbytes "github.com/tendermint/tendermint/libs/bytes"
tmtypes "github.com/tendermint/tendermint/types"
"time"
)
////ConsensusState defines the consensus state from Tendermint.
//type ConsensusState struct {
// // timestamp that corresponds to the block height in which the ConsensusState
// // was stored.
// Timestamp time.Time `json:"timestamp"`
// // commitment root (i.e app hash)
// Root commitmenttypes.MerkleRoot `json:"root"`
// NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash,omitempty" yaml:"next_validators_hash"`
//}
//
//func (cs *ConsensusState) Reset() { *cs = ConsensusState{} }
//func (cs *ConsensusState) String() string { return proto.CompactTextString(cs) }
//func (*ConsensusState) ProtoMessage() {}
// NewConsensusState creates a new ConsensusState instance.
func NewConsensusState(
timestamp time.Time, root commitmenttypes.MerkleRoot, nextValsHash tmbytes.HexBytes,
) *ConsensusState {
return &ConsensusState{
Timestamp: timestamp,
Root: root,
NextValidatorsHash: nextValsHash,
}
}
// ClientType returns Tendermint
func (ConsensusState) ClientType() string {
return exported.Tendermint
}
// GetRoot returns the commitment Root for the specific
func (cs ConsensusState) GetRoot() exported.Root {
return cs.Root
}
// GetTimestamp returns block time in nanoseconds of the header that created consensus state
func (cs ConsensusState) GetTimestamp() uint64 {
return uint64(cs.Timestamp.UnixNano())
}
// ValidateBasic defines a basic validation for the tendermint consensus state.
// NOTE: ProcessedTimestamp may be zero if this is an initial consensus state passed in by collator
// as opposed to a consensus state constructed by the chain.
func (cs ConsensusState) ValidateBasic() error {
if cs.Root.Empty() {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "root cannot be empty")
}
if err := tmtypes.ValidateHash(cs.NextValidatorsHash); err != nil {
return sdkerrors.Wrap(err, "next validators hash is invalid")
}
if cs.Timestamp.Unix() <= 0 {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "timestamp must be a positive Unix time")
}
return nil
}