From 0e4d67601f780ad29b03df214209635096246b80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Thu, 6 Aug 2020 13:06:14 +0200 Subject: [PATCH] Migrate Consensus State to being proto encoded/decoded in 02-client (#6960) * flip switch to proto on consensus state in 02-client * change consensus state to pointer * fix bug * remove amino from connection * some test cleanup --- x/ibc/02-client/client/utils/utils.go | 12 +++--- x/ibc/02-client/exported/exported.go | 1 - x/ibc/02-client/keeper/client_test.go | 12 +++--- x/ibc/02-client/keeper/encoding.go | 41 +++++++++++++++++++ x/ibc/02-client/keeper/keeper.go | 15 +++---- x/ibc/02-client/keeper/keeper_test.go | 20 ++------- x/ibc/02-client/types/codec.go | 4 ++ x/ibc/03-connection/keeper/handshake_test.go | 4 +- x/ibc/03-connection/keeper/keeper.go | 4 +- x/ibc/03-connection/keeper/verify.go | 2 +- x/ibc/03-connection/keeper/verify_test.go | 2 +- x/ibc/03-connection/types/codec.go | 2 +- x/ibc/07-tendermint/misbehaviour.go | 8 ++-- x/ibc/07-tendermint/misbehaviour_test.go | 28 ++++++------- x/ibc/07-tendermint/types/client_state.go | 7 ++-- .../07-tendermint/types/client_state_test.go | 2 +- x/ibc/07-tendermint/types/consensus_state.go | 4 +- .../types/consensus_state_test.go | 16 ++++---- x/ibc/07-tendermint/types/header.go | 4 +- x/ibc/07-tendermint/types/msgs.go | 2 +- x/ibc/07-tendermint/update.go | 8 ++-- x/ibc/07-tendermint/update_test.go | 4 +- x/ibc/09-localhost/types/client_state.go | 2 +- x/ibc/09-localhost/types/client_state_test.go | 2 +- x/ibc/keeper/keeper.go | 4 +- 25 files changed, 117 insertions(+), 93 deletions(-) diff --git a/x/ibc/02-client/client/utils/utils.go b/x/ibc/02-client/client/utils/utils.go index e47a4890e..dbd11ac22 100644 --- a/x/ibc/02-client/client/utils/utils.go +++ b/x/ibc/02-client/client/utils/utils.go @@ -124,31 +124,31 @@ func QueryTendermintHeader(clientCtx client.Context) (ibctmtypes.Header, int64, // QueryNodeConsensusState takes a client context and returns the appropriate // tendermint consensus state -func QueryNodeConsensusState(clientCtx client.Context) (ibctmtypes.ConsensusState, int64, error) { +func QueryNodeConsensusState(clientCtx client.Context) (*ibctmtypes.ConsensusState, int64, error) { node, err := clientCtx.GetNode() if err != nil { - return ibctmtypes.ConsensusState{}, 0, err + return &ibctmtypes.ConsensusState{}, 0, err } info, err := node.ABCIInfo() if err != nil { - return ibctmtypes.ConsensusState{}, 0, err + return &ibctmtypes.ConsensusState{}, 0, err } height := info.Response.LastBlockHeight commit, err := node.Commit(&height) if err != nil { - return ibctmtypes.ConsensusState{}, 0, err + return &ibctmtypes.ConsensusState{}, 0, err } nextHeight := height + 1 nextVals, err := node.Validators(&nextHeight, 0, 10000) if err != nil { - return ibctmtypes.ConsensusState{}, 0, err + return &ibctmtypes.ConsensusState{}, 0, err } - state := ibctmtypes.ConsensusState{ + state := &ibctmtypes.ConsensusState{ Timestamp: commit.Time, Root: commitmenttypes.NewMerkleRoot(commit.AppHash), NextValidatorsHash: tmtypes.NewValidatorSet(nextVals.Validators).Hash(), diff --git a/x/ibc/02-client/exported/exported.go b/x/ibc/02-client/exported/exported.go index dab670839..405433f18 100644 --- a/x/ibc/02-client/exported/exported.go +++ b/x/ibc/02-client/exported/exported.go @@ -30,7 +30,6 @@ type ClientState interface { VerifyClientConsensusState( store sdk.KVStore, cdc codec.BinaryMarshaler, - aminoCdc *codec.Codec, root commitmentexported.Root, height uint64, counterpartyClientIdentifier string, diff --git a/x/ibc/02-client/keeper/client_test.go b/x/ibc/02-client/keeper/client_test.go index 05ffd1e73..b3a5754a6 100644 --- a/x/ibc/02-client/keeper/client_test.go +++ b/x/ibc/02-client/keeper/client_test.go @@ -82,7 +82,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { _, err := suite.keeper.CreateClient(suite.ctx, testClientID, clientState, suite.consensusState) // store intermediate consensus state to check that trustedHeight does not need to be highest consensus state before header height - intermediateConsState := ibctmtypes.ConsensusState{ + intermediateConsState := &ibctmtypes.ConsensusState{ Height: testClientHeight + 1, Timestamp: suite.now.Add(time.Minute), NextValidatorsHash: suite.valSetHash, @@ -101,7 +101,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { suite.Require().NoError(err) // store previous consensus state - prevConsState := ibctmtypes.ConsensusState{ + prevConsState := &ibctmtypes.ConsensusState{ Height: 1, Timestamp: suite.past, NextValidatorsHash: suite.valSetHash, @@ -109,7 +109,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { suite.keeper.SetClientConsensusState(suite.ctx, testClientID, 1, prevConsState) // store intermediate consensus state to check that trustedHeight does not need to be hightest consensus state before header height - intermediateConsState := ibctmtypes.ConsensusState{ + intermediateConsState := &ibctmtypes.ConsensusState{ Height: 2, Timestamp: suite.past.Add(time.Minute), NextValidatorsHash: suite.valSetHash, @@ -161,7 +161,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { suite.Require().NoError(err) // store previous consensus state - prevConsState := ibctmtypes.ConsensusState{ + prevConsState := &ibctmtypes.ConsensusState{ Height: 1, Timestamp: suite.past, NextValidatorsHash: suite.valSetHash, @@ -199,7 +199,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { if tc.expPass { suite.Require().NoError(err, err) - expConsensusState := ibctmtypes.ConsensusState{ + expConsensusState := &ibctmtypes.ConsensusState{ Height: updateHeader.GetHeight(), Timestamp: updateHeader.Time, Root: commitmenttypes.NewMerkleRoot(updateHeader.AppHash), @@ -303,7 +303,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() { _, err := suite.keeper.CreateClient(suite.ctx, testClientID, clientState, suite.consensusState) // store intermediate consensus state to check that trustedHeight does not need to be highest consensus state before header height - intermediateConsState := ibctmtypes.ConsensusState{ + intermediateConsState := &ibctmtypes.ConsensusState{ Height: testClientHeight + 3, Timestamp: suite.now.Add(time.Minute), NextValidatorsHash: suite.valSetHash, diff --git a/x/ibc/02-client/keeper/encoding.go b/x/ibc/02-client/keeper/encoding.go index 266a256b1..2b6bfd535 100644 --- a/x/ibc/02-client/keeper/encoding.go +++ b/x/ibc/02-client/keeper/encoding.go @@ -47,3 +47,44 @@ func (k Keeper) UnmarshalClientState(bz []byte) (exported.ClientState, error) { return clientState, nil } + +// MustUnmarshalConsensusState attempts to decode and return an ConsensusState object from +// raw encoded bytes. It panics on error. +func (k Keeper) MustUnmarshalConsensusState(bz []byte) exported.ConsensusState { + consensusState, err := k.UnmarshalConsensusState(bz) + if err != nil { + panic(fmt.Errorf("failed to decode consensus state: %w", err)) + } + + return consensusState +} + +// MustMarshalConsensusState attempts to encode an ConsensusState object and returns the +// raw encoded bytes. It panics on error. +func (k Keeper) MustMarshalConsensusState(consensusState exported.ConsensusState) []byte { + bz, err := k.MarshalConsensusState(consensusState) + if err != nil { + panic(fmt.Errorf("failed to encode consensus state: %w", err)) + } + + return bz +} + +// MarshalConsensusState marshals an ConsensusState interface. If the given type implements +// the Marshaler interface, it is treated as a Proto-defined message and +// serialized that way. +func (k Keeper) MarshalConsensusState(consensusStateI exported.ConsensusState) ([]byte, error) { + return codec.MarshalAny(k.cdc, consensusStateI) +} + +// UnmarshalConsensusState returns an ConsensusState interface from raw encoded clientState +// bytes of a Proto-based ConsensusState type. An error is returned upon decoding +// failure. +func (k Keeper) UnmarshalConsensusState(bz []byte) (exported.ConsensusState, error) { + var consensusState exported.ConsensusState + if err := codec.UnmarshalAny(k.cdc, &consensusState, bz); err != nil { + return nil, err + } + + return consensusState, nil +} diff --git a/x/ibc/02-client/keeper/keeper.go b/x/ibc/02-client/keeper/keeper.go index 549a3531d..9f3017d9a 100644 --- a/x/ibc/02-client/keeper/keeper.go +++ b/x/ibc/02-client/keeper/keeper.go @@ -21,16 +21,14 @@ import ( type Keeper struct { storeKey sdk.StoreKey cdc codec.BinaryMarshaler - aminoCdc *codec.Codec stakingKeeper types.StakingKeeper } // NewKeeper creates a new NewKeeper instance -func NewKeeper(cdc codec.BinaryMarshaler, aminoCdc *codec.Codec, key sdk.StoreKey, sk types.StakingKeeper) Keeper { +func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey, sk types.StakingKeeper) Keeper { return Keeper{ storeKey: key, cdc: cdc, - aminoCdc: aminoCdc, stakingKeeper: sk, } } @@ -83,8 +81,7 @@ func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height return nil, false } - var consensusState exported.ConsensusState - k.aminoCdc.MustUnmarshalBinaryBare(bz, &consensusState) + consensusState := k.MustUnmarshalConsensusState(bz) return consensusState, true } @@ -92,8 +89,7 @@ func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height // height func (k Keeper) SetClientConsensusState(ctx sdk.Context, clientID string, height uint64, consensusState exported.ConsensusState) { store := k.ClientStore(ctx, clientID) - bz := k.aminoCdc.MustMarshalBinaryBare(consensusState) - store.Set(host.KeyConsensusState(height), bz) + store.Set(host.KeyConsensusState(height), k.MustMarshalConsensusState(consensusState)) } // IterateConsensusStates provides an iterator over all stored consensus states. @@ -111,8 +107,7 @@ func (k Keeper) IterateConsensusStates(ctx sdk.Context, cb func(clientID string, continue } clientID := keySplit[1] - var consensusState exported.ConsensusState - k.aminoCdc.MustUnmarshalBinaryBare(iterator.Value(), &consensusState) + consensusState := k.MustUnmarshalConsensusState(iterator.Value()) if cb(clientID, consensusState) { break @@ -206,7 +201,7 @@ func (k Keeper) GetSelfConsensusState(ctx sdk.Context, height uint64) (exported. return nil, false } - consensusState := ibctmtypes.ConsensusState{ + consensusState := &ibctmtypes.ConsensusState{ Height: height, Timestamp: histInfo.Header.Time, Root: commitmenttypes.NewMerkleRoot(histInfo.Header.AppHash), diff --git a/x/ibc/02-client/keeper/keeper_test.go b/x/ibc/02-client/keeper/keeper_test.go index 9e3d26f78..4f56a12a5 100644 --- a/x/ibc/02-client/keeper/keeper_test.go +++ b/x/ibc/02-client/keeper/keeper_test.go @@ -40,10 +40,9 @@ type KeeperTestSuite struct { suite.Suite cdc codec.Marshaler - aminoCdc *codec.Codec ctx sdk.Context keeper *keeper.Keeper - consensusState ibctmtypes.ConsensusState + consensusState *ibctmtypes.ConsensusState header ibctmtypes.Header valSet *tmtypes.ValidatorSet valSetHash tmbytes.HexBytes @@ -60,7 +59,6 @@ func (suite *KeeperTestSuite) SetupTest() { app := simapp.Setup(isCheckTx) suite.cdc = app.AppCodec() - suite.aminoCdc = app.Codec() suite.ctx = app.BaseApp.NewContext(isCheckTx, abci.Header{Height: testClientHeight, ChainID: testClientID, Time: now2}) suite.keeper = &app.IBCKeeper.ClientKeeper suite.privVal = tmtypes.NewMockPV() @@ -72,12 +70,7 @@ func (suite *KeeperTestSuite) SetupTest() { suite.valSet = tmtypes.NewValidatorSet([]*tmtypes.Validator{validator}) suite.valSetHash = suite.valSet.Hash() suite.header = ibctmtypes.CreateTestHeader(testChainID, testClientHeight, testClientHeight-1, now2, suite.valSet, suite.valSet, []tmtypes.PrivValidator{suite.privVal}) - suite.consensusState = ibctmtypes.ConsensusState{ - Height: testClientHeight, - Timestamp: suite.now, - Root: commitmenttypes.NewMerkleRoot([]byte("hash")), - NextValidatorsHash: suite.valSetHash, - } + suite.consensusState = ibctmtypes.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot([]byte("hash")), testClientHeight, suite.valSetHash) var validators stakingtypes.Validators for i := 1; i < 11; i++ { @@ -120,7 +113,7 @@ func (suite *KeeperTestSuite) TestSetClientConsensusState() { retrievedConsState, found := suite.keeper.GetClientConsensusState(suite.ctx, testClientID, testClientHeight) suite.Require().True(found, "GetConsensusState failed") - tmConsState, ok := retrievedConsState.(ibctmtypes.ConsensusState) + tmConsState, ok := retrievedConsState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) suite.Require().Equal(suite.consensusState, tmConsState, "ConsensusState not stored correctly") } @@ -209,12 +202,7 @@ func (suite KeeperTestSuite) TestConsensusStateHelpers() { suite.keeper.SetClientState(suite.ctx, testClientID, clientState) suite.keeper.SetClientConsensusState(suite.ctx, testClientID, testClientHeight, suite.consensusState) - nextState := ibctmtypes.ConsensusState{ - Height: testClientHeight + 5, - Timestamp: suite.now, - Root: commitmenttypes.NewMerkleRoot([]byte("next")), - NextValidatorsHash: suite.valSetHash, - } + nextState := ibctmtypes.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot([]byte("next")), testClientHeight+5, suite.valSetHash) header := ibctmtypes.CreateTestHeader(testClientID, testClientHeight+5, testClientHeight, suite.header.Time.Add(time.Minute), suite.valSet, suite.valSet, []tmtypes.PrivValidator{suite.privVal}) diff --git a/x/ibc/02-client/types/codec.go b/x/ibc/02-client/types/codec.go index b83b00831..da341afa7 100644 --- a/x/ibc/02-client/types/codec.go +++ b/x/ibc/02-client/types/codec.go @@ -22,6 +22,10 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { "cosmos_sdk.ibc.v1.client.ClientState", (*exported.ClientState)(nil), ) + registry.RegisterInterface( + "cosmos_sdk.ibc.v1.client.ConsensusState", + (*exported.ConsensusState)(nil), + ) } var ( diff --git a/x/ibc/03-connection/keeper/handshake_test.go b/x/ibc/03-connection/keeper/handshake_test.go index 09b9a5e6d..ff250e4bb 100644 --- a/x/ibc/03-connection/keeper/handshake_test.go +++ b/x/ibc/03-connection/keeper/handshake_test.go @@ -116,7 +116,7 @@ func (suite *KeeperTestSuite) TestConnOpenTry() { consState, found := suite.chainA.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainA.GetContext(), clientA) suite.Require().True(found) - tmConsState, ok := consState.(ibctmtypes.ConsensusState) + tmConsState, ok := consState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) tmConsState.Timestamp = time.Now() @@ -304,7 +304,7 @@ func (suite *KeeperTestSuite) TestConnOpenAck() { consState, found := suite.chainB.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), clientB) suite.Require().True(found) - tmConsState, ok := consState.(ibctmtypes.ConsensusState) + tmConsState, ok := consState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) tmConsState.Timestamp = time.Now() diff --git a/x/ibc/03-connection/keeper/keeper.go b/x/ibc/03-connection/keeper/keeper.go index 85abbf745..060790146 100644 --- a/x/ibc/03-connection/keeper/keeper.go +++ b/x/ibc/03-connection/keeper/keeper.go @@ -22,16 +22,14 @@ type Keeper struct { types.QueryServer storeKey sdk.StoreKey - aminoCdc *codec.Codec // amino codec. TODO: remove after clients have been migrated to proto cdc codec.BinaryMarshaler clientKeeper types.ClientKeeper } // NewKeeper creates a new IBC connection Keeper instance -func NewKeeper(aminoCdc *codec.Codec, cdc codec.BinaryMarshaler, key sdk.StoreKey, ck types.ClientKeeper) Keeper { +func NewKeeper(cdc codec.BinaryMarshaler, key sdk.StoreKey, ck types.ClientKeeper) Keeper { return Keeper{ storeKey: key, - aminoCdc: aminoCdc, cdc: cdc, clientKeeper: ck, } diff --git a/x/ibc/03-connection/keeper/verify.go b/x/ibc/03-connection/keeper/verify.go index 2676fec54..29e9ed6d1 100644 --- a/x/ibc/03-connection/keeper/verify.go +++ b/x/ibc/03-connection/keeper/verify.go @@ -31,7 +31,7 @@ func (k Keeper) VerifyClientConsensusState( } if err := clientState.VerifyClientConsensusState( - k.clientKeeper.ClientStore(ctx, clientID), k.cdc, k.aminoCdc, targetConsState.GetRoot(), height, + k.clientKeeper.ClientStore(ctx, clientID), k.cdc, targetConsState.GetRoot(), height, connection.GetCounterparty().GetClientID(), consensusHeight, connection.GetCounterparty().GetPrefix(), proof, consensusState, ); err != nil { return sdkerrors.Wrap(err, "failed consensus state verification") diff --git a/x/ibc/03-connection/keeper/verify_test.go b/x/ibc/03-connection/keeper/verify_test.go index 31efee937..0a312cd97 100644 --- a/x/ibc/03-connection/keeper/verify_test.go +++ b/x/ibc/03-connection/keeper/verify_test.go @@ -48,7 +48,7 @@ func (suite *KeeperTestSuite) TestVerifyClientConsensusState() { consState, found := suite.chainB.App.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(suite.chainB.GetContext(), clientB) suite.Require().True(found) - tmConsState, ok := consState.(ibctmtypes.ConsensusState) + tmConsState, ok := consState.(*ibctmtypes.ConsensusState) suite.Require().True(ok) tmConsState.Timestamp = time.Now() diff --git a/x/ibc/03-connection/types/codec.go b/x/ibc/03-connection/types/codec.go index 74f37c047..23befac92 100644 --- a/x/ibc/03-connection/types/codec.go +++ b/x/ibc/03-connection/types/codec.go @@ -19,7 +19,7 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { } var ( - // SubModuleCdc references the global x/ibc/03-connectionl module codec. Note, the codec should + // SubModuleCdc references the global x/ibc/03-connection module codec. Note, the codec should // ONLY be used in certain instances of tests and for JSON encoding. // // The actual codec used for serialization should be provided to x/ibc/03-connectionl and diff --git a/x/ibc/07-tendermint/misbehaviour.go b/x/ibc/07-tendermint/misbehaviour.go index 62373cfa3..6e4f45a7d 100644 --- a/x/ibc/07-tendermint/misbehaviour.go +++ b/x/ibc/07-tendermint/misbehaviour.go @@ -29,7 +29,7 @@ func CheckMisbehaviourAndUpdateState( // cast the interface to specific types before checking for misbehaviour tmClientState, ok := clientState.(*types.ClientState) if !ok { - return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "expected type %T, got %T", types.ClientState{}, clientState) + return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "expected type %T, got %T", &types.ClientState{}, clientState) } // If client is already frozen at earlier height than evidence, return with error @@ -38,9 +38,9 @@ func CheckMisbehaviourAndUpdateState( "client is already frozen at earlier height %d than misbehaviour height %d", tmClientState.FrozenHeight, misbehaviour.GetHeight()) } - tmConsensusState, ok := consensusState.(types.ConsensusState) + tmConsensusState, ok := consensusState.(*types.ConsensusState) if !ok { - return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "expected type %T, got %T", consensusState, types.ConsensusState{}) + return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "expected type %T, got %T", &types.ConsensusState{}, consensusState) } tmEvidence, ok := misbehaviour.(types.Evidence) @@ -60,7 +60,7 @@ func CheckMisbehaviourAndUpdateState( // checkMisbehaviour checks if the evidence provided is a valid light client misbehaviour func checkMisbehaviour( - clientState *types.ClientState, consensusState types.ConsensusState, evidence types.Evidence, + clientState *types.ClientState, consensusState *types.ConsensusState, evidence types.Evidence, height uint64, currentTimestamp time.Time, consensusParams *abci.ConsensusParams, ) error { // calculate the age of the misbehaviour evidence diff --git a/x/ibc/07-tendermint/misbehaviour_test.go b/x/ibc/07-tendermint/misbehaviour_test.go index fc586998c..1953cc4ec 100644 --- a/x/ibc/07-tendermint/misbehaviour_test.go +++ b/x/ibc/07-tendermint/misbehaviour_test.go @@ -55,7 +55,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "valid misbehavior evidence", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, bothValSet, bothSigners), @@ -70,7 +70,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "valid misbehavior at height greater than last consensusState", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Height: height - 1, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height-1, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height-1, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height-1, suite.now.Add(time.Minute), bothValSet, bothValSet, bothSigners), @@ -85,7 +85,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "consensus state's valset hash different from evidence should still pass", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Height: height, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: suite.valsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, suite.valsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, suite.valSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, suite.valSet, bothSigners), @@ -100,7 +100,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "invalid tendermint client state", nil, - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, altValSet, bothSigners), @@ -114,8 +114,8 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { }, { "already frozen client state", - types.ClientState{FrozenHeight: 1}, - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + &types.ClientState{FrozenHeight: 1}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, bothValSet, bothSigners), @@ -145,7 +145,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "invalid tendermint misbehaviour evidence", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), nil, simapp.DefaultConsensusParams, height, @@ -155,7 +155,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "rejected misbehaviour due to expired age", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, int64(2*height+uint64(simapp.DefaultConsensusParams.Evidence.MaxAgeNumBlocks)), height, suite.now, bothValSet, bothValSet, bothSigners), @@ -172,7 +172,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "provided height > header height", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height-1, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height-1, suite.now.Add(time.Minute), bothValSet, bothValSet, bothSigners), @@ -187,7 +187,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "unbonding period expired", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: time.Time{}, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(time.Time{}, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, bothValSet, bothSigners), @@ -202,7 +202,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "trusted validators is incorrect for given consensus state", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, suite.valSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, suite.valSet, bothSigners), @@ -217,7 +217,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "first valset has too much change", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, altValSet, bothValSet, altSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), bothValSet, bothValSet, bothSigners), @@ -232,7 +232,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "second valset has too much change", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, bothValSet, bothValSet, bothSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), altValSet, bothValSet, altSigners), @@ -247,7 +247,7 @@ func (suite *TendermintTestSuite) TestCheckMisbehaviour() { { "both valsets have too much change", types.NewClientState(chainID, types.DefaultTrustLevel, trustingPeriod, ubdPeriod, maxClockDrift, height, commitmenttypes.GetSDKSpecs()), - types.ConsensusState{Timestamp: suite.now, Root: commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), NextValidatorsHash: bothValsHash}, + types.NewConsensusState(suite.now, commitmenttypes.NewMerkleRoot(tmhash.Sum([]byte("app_hash"))), height, bothValsHash), types.Evidence{ Header1: types.CreateTestHeader(chainID, height, height, suite.now, altValSet, bothValSet, altSigners), Header2: types.CreateTestHeader(chainID, height, height, suite.now.Add(time.Minute), altValSet, bothValSet, altSigners), diff --git a/x/ibc/07-tendermint/types/client_state.go b/x/ibc/07-tendermint/types/client_state.go index 4be704d7e..330ba3313 100644 --- a/x/ibc/07-tendermint/types/client_state.go +++ b/x/ibc/07-tendermint/types/client_state.go @@ -125,7 +125,6 @@ func (cs ClientState) GetProofSpecs() []*ics23.ProofSpec { func (cs ClientState) VerifyClientConsensusState( _ sdk.KVStore, cdc codec.BinaryMarshaler, - aminoCdc *codec.Codec, provingRoot commitmentexported.Root, height uint64, counterpartyClientIdentifier string, @@ -145,7 +144,7 @@ func (cs ClientState) VerifyClientConsensusState( return err } - bz, err := aminoCdc.MarshalBinaryBare(consensusState) + bz, err := codec.MarshalAny(cdc, consensusState) if err != nil { return err } @@ -404,9 +403,9 @@ func sanitizeVerificationArgs( return commitmenttypes.MerkleProof{}, sdkerrors.Wrap(clienttypes.ErrInvalidConsensus, "consensus state cannot be empty") } - _, ok = consensusState.(ConsensusState) + _, ok = consensusState.(*ConsensusState) if !ok { - return commitmenttypes.MerkleProof{}, sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type %T, expected %T", consensusState, ConsensusState{}) + return commitmenttypes.MerkleProof{}, sdkerrors.Wrapf(clienttypes.ErrInvalidConsensus, "invalid consensus type %T, expected %T", consensusState, &ConsensusState{}) } return merkleProof, nil diff --git a/x/ibc/07-tendermint/types/client_state_test.go b/x/ibc/07-tendermint/types/client_state_test.go index 4558708f9..77d6ae5b1 100644 --- a/x/ibc/07-tendermint/types/client_state_test.go +++ b/x/ibc/07-tendermint/types/client_state_test.go @@ -149,7 +149,7 @@ func (suite *TendermintTestSuite) TestVerifyClientConsensusState() { tc := tc err := tc.clientState.VerifyClientConsensusState( - nil, suite.cdc, suite.aminoCdc, tc.consensusState.Root, height, "chainA", tc.consensusState.GetHeight(), tc.prefix, tc.proof, tc.consensusState, + nil, suite.cdc, tc.consensusState.Root, height, "chainA", tc.consensusState.GetHeight(), tc.prefix, tc.proof, tc.consensusState, ) if tc.expPass { diff --git a/x/ibc/07-tendermint/types/consensus_state.go b/x/ibc/07-tendermint/types/consensus_state.go index d70abb6da..6f5b6a390 100644 --- a/x/ibc/07-tendermint/types/consensus_state.go +++ b/x/ibc/07-tendermint/types/consensus_state.go @@ -17,8 +17,8 @@ import ( func NewConsensusState( timestamp time.Time, root commitmenttypes.MerkleRoot, height uint64, nextValsHash tmbytes.HexBytes, -) ConsensusState { - return ConsensusState{ +) *ConsensusState { + return &ConsensusState{ Timestamp: timestamp, Root: root, Height: height, diff --git a/x/ibc/07-tendermint/types/consensus_state_test.go b/x/ibc/07-tendermint/types/consensus_state_test.go index d2ad8b0ae..3bb0a2d5b 100644 --- a/x/ibc/07-tendermint/types/consensus_state_test.go +++ b/x/ibc/07-tendermint/types/consensus_state_test.go @@ -4,18 +4,18 @@ import ( "time" clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" - ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" + "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types" commitmenttypes "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/types" ) func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() { testCases := []struct { msg string - consensusState ibctmtypes.ConsensusState + consensusState *types.ConsensusState expectPass bool }{ {"success", - ibctmtypes.ConsensusState{ + &types.ConsensusState{ Timestamp: suite.now, Height: height, Root: commitmenttypes.NewMerkleRoot([]byte("app_hash")), @@ -23,7 +23,7 @@ func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() { }, true}, {"root is nil", - ibctmtypes.ConsensusState{ + &types.ConsensusState{ Timestamp: suite.now, Height: height, Root: commitmenttypes.MerkleRoot{}, @@ -31,7 +31,7 @@ func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() { }, false}, {"root is empty", - ibctmtypes.ConsensusState{ + &types.ConsensusState{ Timestamp: suite.now, Height: height, Root: commitmenttypes.MerkleRoot{}, @@ -39,7 +39,7 @@ func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() { }, false}, {"nextvalshash is invalid", - ibctmtypes.ConsensusState{ + &types.ConsensusState{ Timestamp: suite.now, Height: height, Root: commitmenttypes.NewMerkleRoot([]byte("app_hash")), @@ -48,7 +48,7 @@ func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() { false}, {"height is 0", - ibctmtypes.ConsensusState{ + &types.ConsensusState{ Timestamp: suite.now, Height: 0, Root: commitmenttypes.NewMerkleRoot([]byte("app_hash")), @@ -56,7 +56,7 @@ func (suite *TendermintTestSuite) TestConsensusStateValidateBasic() { }, false}, {"timestamp is zero", - ibctmtypes.ConsensusState{ + &types.ConsensusState{ Timestamp: time.Time{}, Height: height, Root: commitmenttypes.NewMerkleRoot([]byte("app_hash")), diff --git a/x/ibc/07-tendermint/types/header.go b/x/ibc/07-tendermint/types/header.go index 89003415e..999f6cfb8 100644 --- a/x/ibc/07-tendermint/types/header.go +++ b/x/ibc/07-tendermint/types/header.go @@ -36,8 +36,8 @@ func (h Header) ClientType() clientexported.ClientType { } // ConsensusState returns the updated consensus state associated with the header -func (h Header) ConsensusState() ConsensusState { - return ConsensusState{ +func (h Header) ConsensusState() *ConsensusState { + return &ConsensusState{ Height: uint64(h.Height), Timestamp: h.Time, Root: commitmenttypes.NewMerkleRoot(h.AppHash), diff --git a/x/ibc/07-tendermint/types/msgs.go b/x/ibc/07-tendermint/types/msgs.go index 372d4cc78..d617f330a 100644 --- a/x/ibc/07-tendermint/types/msgs.go +++ b/x/ibc/07-tendermint/types/msgs.go @@ -140,7 +140,7 @@ func (msg MsgCreateClient) GetClientType() string { func (msg MsgCreateClient) GetConsensusState() clientexported.ConsensusState { // Construct initial consensus state from provided Header root := commitmenttypes.NewMerkleRoot(msg.Header.AppHash) - return ConsensusState{ + return &ConsensusState{ Timestamp: msg.Header.Time, Root: root, Height: uint64(msg.Header.Height), diff --git a/x/ibc/07-tendermint/update.go b/x/ibc/07-tendermint/update.go index e36cf0d77..415edc97a 100644 --- a/x/ibc/07-tendermint/update.go +++ b/x/ibc/07-tendermint/update.go @@ -44,7 +44,7 @@ func CheckValidityAndUpdateState( ) } - tmConsState, ok := consState.(types.ConsensusState) + tmConsState, ok := consState.(*types.ConsensusState) if !ok { return nil, nil, sdkerrors.Wrapf( clienttypes.ErrInvalidConsensus, "expected type %T, got %T", types.ConsensusState{}, consState, @@ -69,7 +69,7 @@ func CheckValidityAndUpdateState( // checkValidity checks if the Tendermint header is valid. // CONTRACT: consState.Height == header.TrustedHeight func checkValidity( - clientState *types.ClientState, consState types.ConsensusState, + clientState *types.ClientState, consState *types.ConsensusState, header types.Header, currentTimestamp time.Time, ) error { // assert that trustedVals is NextValidators of last trusted header @@ -140,11 +140,11 @@ func checkValidity( } // update the consensus state from a new header -func update(clientState *types.ClientState, header types.Header) (*types.ClientState, types.ConsensusState) { +func update(clientState *types.ClientState, header types.Header) (*types.ClientState, *types.ConsensusState) { if uint64(header.Height) > clientState.LatestHeight { clientState.LatestHeight = uint64(header.Height) } - consensusState := types.ConsensusState{ + consensusState := &types.ConsensusState{ Height: uint64(header.Height), Timestamp: header.Time, Root: commitmenttypes.NewMerkleRoot(header.AppHash), diff --git a/x/ibc/07-tendermint/update_test.go b/x/ibc/07-tendermint/update_test.go index c782519de..57b699181 100644 --- a/x/ibc/07-tendermint/update_test.go +++ b/x/ibc/07-tendermint/update_test.go @@ -14,7 +14,7 @@ import ( func (suite *TendermintTestSuite) TestCheckValidity() { var ( clientState *types.ClientState - consensusState types.ConsensusState + consensusState *types.ConsensusState newHeader types.Header currentTime time.Time ) @@ -192,7 +192,7 @@ func (suite *TendermintTestSuite) TestCheckValidity() { // setup test tc.setup() - expectedConsensus := types.ConsensusState{ + expectedConsensus := &types.ConsensusState{ Height: uint64(newHeader.Height), Timestamp: newHeader.Time, Root: commitmenttypes.NewMerkleRoot(newHeader.AppHash), diff --git a/x/ibc/09-localhost/types/client_state.go b/x/ibc/09-localhost/types/client_state.go index 9c875f213..a8de30cee 100644 --- a/x/ibc/09-localhost/types/client_state.go +++ b/x/ibc/09-localhost/types/client_state.go @@ -75,7 +75,7 @@ func (cs ClientState) GetProofSpecs() []*ics23.ProofSpec { // VerifyClientConsensusState returns an error since a local host client does not store consensus // states. func (cs ClientState) VerifyClientConsensusState( - sdk.KVStore, codec.BinaryMarshaler, *codec.Codec, commitmentexported.Root, + sdk.KVStore, codec.BinaryMarshaler, commitmentexported.Root, uint64, string, uint64, commitmentexported.Prefix, []byte, clientexported.ConsensusState, ) error { return ErrConsensusStatesNotStored diff --git a/x/ibc/09-localhost/types/client_state_test.go b/x/ibc/09-localhost/types/client_state_test.go index 234f5368b..7666ae8af 100644 --- a/x/ibc/09-localhost/types/client_state_test.go +++ b/x/ibc/09-localhost/types/client_state_test.go @@ -50,7 +50,7 @@ func (suite *LocalhostTestSuite) TestValidate() { func (suite *LocalhostTestSuite) TestVerifyClientConsensusState() { clientState := types.NewClientState("chainID", 10) err := clientState.VerifyClientConsensusState( - nil, nil, nil, nil, 0, "", 0, nil, nil, nil, + nil, nil, nil, 0, "", 0, nil, nil, nil, ) suite.Require().Error(err) } diff --git a/x/ibc/keeper/keeper.go b/x/ibc/keeper/keeper.go index f556f03cd..3e9f1ee1f 100644 --- a/x/ibc/keeper/keeper.go +++ b/x/ibc/keeper/keeper.go @@ -34,8 +34,8 @@ type Keeper struct { func NewKeeper( aminoCdc *codec.Codec, cdc codec.BinaryMarshaler, key sdk.StoreKey, stakingKeeper clienttypes.StakingKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, ) *Keeper { - clientKeeper := clientkeeper.NewKeeper(cdc, aminoCdc, key, stakingKeeper) - connectionKeeper := connectionkeeper.NewKeeper(aminoCdc, cdc, key, clientKeeper) + clientKeeper := clientkeeper.NewKeeper(cdc, key, stakingKeeper) + connectionKeeper := connectionkeeper.NewKeeper(cdc, key, clientKeeper) portKeeper := portkeeper.NewKeeper(scopedKeeper) channelKeeper := channelkeeper.NewKeeper(cdc, key, clientKeeper, connectionKeeper, portKeeper, scopedKeeper)