Skip to content

Commit

Permalink
Migrate tendermint consensus state to proto (cosmos#6957)
Browse files Browse the repository at this point in the history
* migrate tm consensus state to proto

* update con state

* register con state

* add confio to proto lint exclude

* Update proto/ibc/tendermint/tendermint.proto

* casttype to hexbytes

Co-authored-by: Federico Kunze <[email protected]>
  • Loading branch information
colin-axner and fedekunze authored Aug 6, 2020
1 parent ba140c9 commit 3076a8b
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 47 deletions.
1 change: 1 addition & 0 deletions buf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lint:
- tendermint
- gogoproto
- cosmos_proto
- confio
breaking:
use:
- FILE
Expand Down
20 changes: 20 additions & 0 deletions proto/ibc/tendermint/tendermint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types";

import "confio/proofs.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";
import "ibc/commitment/commitment.proto";
import "gogoproto/gogo.proto";

// ClientState from Tendermint tracks the current validator set, latest height,
Expand Down Expand Up @@ -45,6 +47,24 @@ message ClientState {
[(gogoproto.moretags) = "yaml:\"proof_specs\""];
}

// ConsensusState defines the consensus state from Tendermint.
message ConsensusState {
option (gogoproto.goproto_getters) = false;

// timestamp that corresponds to the block height in which the ConsensusState
// was stored.
google.protobuf.Timestamp timestamp = 1
[(gogoproto.nullable) = false, (gogoproto.stdtime) = true];
// commitment root (i.e app hash)
ibc.commitment.MerkleRoot root = 2 [(gogoproto.nullable) = false];
// height at which the consensus state was stored.
uint64 height = 3;
bytes next_validators_hash = 4 [
(gogoproto.casttype) = "github.com/tendermint/tendermint/libs/bytes.HexBytes",
(gogoproto.moretags) = "yaml:\"next_validators_hash\""
];
}

// Fraction defines the protobuf message type for tmmath.Fraction
message Fraction {
int64 numerator = 1;
Expand Down
4 changes: 4 additions & 0 deletions x/ibc/07-tendermint/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
(*clientexported.ClientState)(nil),
&ClientState{},
)
registry.RegisterImplementations(
(*clientexported.ConsensusState)(nil),
&ConsensusState{},
)
}

var (
Expand Down
13 changes: 3 additions & 10 deletions x/ibc/07-tendermint/types/consensus_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@ import (
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
commitmentexported "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types"
)

// ConsensusState defines a Tendermint consensus state
type ConsensusState struct {
Timestamp time.Time `json:"timestamp" yaml:"timestamp"`
Root commitmentexported.Root `json:"root" yaml:"root"`
Height uint64 `json:"height" yaml:"height"`
NextValidatorsHash tmbytes.HexBytes `json:"next_validators_hash"` // validators hash for the next block
}

// NewConsensusState creates a new ConsensusState instance.
func NewConsensusState(
timestamp time.Time, root commitmentexported.Root, height uint64,
timestamp time.Time, root commitmenttypes.MerkleRoot, height uint64,
nextValsHash tmbytes.HexBytes,
) ConsensusState {
return ConsensusState{
Expand Down Expand Up @@ -55,7 +48,7 @@ func (cs ConsensusState) GetTimestamp() uint64 {

// ValidateBasic defines a basic validation for the tendermint consensus state.
func (cs ConsensusState) ValidateBasic() error {
if cs.Root == nil || cs.Root.Empty() {
if cs.Root.Empty() {
return sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "root cannot be empty")
}
if err := tmtypes.ValidateHash(cs.NextValidatorsHash); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/07-tendermint/types/consensus_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() {
ibctmtypes.ConsensusState{
Timestamp: suite.now,
Height: height,
Root: nil,
Root: commitmenttypes.MerkleRoot{},
NextValidatorsHash: suite.valsHash,
},
false},
Expand Down
Loading

0 comments on commit 3076a8b

Please sign in to comment.