From 1a531cb6454a2e98147639655eb60bcd28b6d320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?colin=20axn=C3=A9r?= <25233464+colin-axner@users.noreply.github.com> Date: Wed, 5 Aug 2020 17:14:24 +0200 Subject: [PATCH] x/ibc: migrate 02-client to use proto encoded/decoded client states (#6948) * begin migration * make client state a pointer * fix build * fixes from self review and rename cdctypes -> codectypes * add godoc --- x/ibc-transfer/module.go | 4 +- x/ibc-transfer/types/codec.go | 6 +-- x/ibc/02-client/keeper/client_test.go | 8 +-- x/ibc/02-client/keeper/encoding.go | 49 +++++++++++++++++++ x/ibc/02-client/keeper/keeper.go | 21 ++++---- x/ibc/02-client/keeper/keeper_test.go | 6 ++- x/ibc/02-client/types/codec.go | 38 +++++++++----- x/ibc/03-connection/types/codec.go | 6 +-- x/ibc/04-channel/types/codec.go | 6 +-- x/ibc/07-tendermint/misbehaviour.go | 4 +- x/ibc/07-tendermint/types/client_state.go | 8 +-- .../07-tendermint/types/client_state_test.go | 30 ++++++------ x/ibc/07-tendermint/types/codec.go | 6 +-- x/ibc/07-tendermint/update.go | 6 +-- x/ibc/07-tendermint/update_test.go | 2 +- x/ibc/09-localhost/types/client_state.go | 4 +- x/ibc/09-localhost/types/client_state_test.go | 14 +++--- x/ibc/09-localhost/types/codec.go | 14 ++---- x/ibc/23-commitment/types/codec.go | 6 +-- x/ibc/keeper/keeper.go | 2 +- x/ibc/module.go | 4 +- x/ibc/types/codec.go | 7 ++- 22 files changed, 157 insertions(+), 94 deletions(-) create mode 100644 x/ibc/02-client/keeper/encoding.go diff --git a/x/ibc-transfer/module.go b/x/ibc-transfer/module.go index ddeee27c5..132ba89f8 100644 --- a/x/ibc-transfer/module.go +++ b/x/ibc-transfer/module.go @@ -14,7 +14,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/cosmos/cosmos-sdk/types/module" @@ -79,7 +79,7 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { } // RegisterInterfaces registers module concrete types into protobuf Any. -func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { types.RegisterInterfaces(registry) } diff --git a/x/ibc-transfer/types/codec.go b/x/ibc-transfer/types/codec.go index 84f3664de..e7ee37abb 100644 --- a/x/ibc-transfer/types/codec.go +++ b/x/ibc-transfer/types/codec.go @@ -2,13 +2,13 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" ) // RegisterInterfaces register the ibc transfer module interfaces to protobuf // Any. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), &MsgTransfer{}) } @@ -18,5 +18,5 @@ var ( // // The actual codec used for serialization should be provided to x/ibc-transfer and // defined at the application level. - ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) + ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) ) diff --git a/x/ibc/02-client/keeper/client_test.go b/x/ibc/02-client/keeper/client_test.go index 57dfef008..3d87540be 100644 --- a/x/ibc/02-client/keeper/client_test.go +++ b/x/ibc/02-client/keeper/client_test.go @@ -69,7 +69,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { } var ( updateHeader ibctmtypes.Header - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState ) cases := []struct { @@ -128,7 +128,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() { return nil }, false}, {"frozen client before update", func() error { - clientState = ibctmtypes.ClientState{FrozenHeight: 1, LatestHeight: testClientHeight} + clientState = &ibctmtypes.ClientState{FrozenHeight: 1, LatestHeight: testClientHeight} suite.keeper.SetClientState(suite.ctx, testClientID, clientState) suite.keeper.SetClientType(suite.ctx, testClientID, exported.Tendermint) updateHeader = createFutureUpdateFn(suite) @@ -309,7 +309,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() { ClientID: testClientID, }, func() error { - clientState := ibctmtypes.ClientState{FrozenHeight: 1, LatestHeight: testClientHeight} + clientState := &ibctmtypes.ClientState{FrozenHeight: 1, LatestHeight: testClientHeight} suite.keeper.SetClientState(suite.ctx, testClientID, clientState) return nil }, @@ -324,7 +324,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() { ClientID: testClientID, }, func() error { - clientState := ibctmtypes.ClientState{FrozenHeight: 1, LatestHeight: testClientHeight} + clientState := &ibctmtypes.ClientState{FrozenHeight: 1, LatestHeight: testClientHeight} suite.keeper.SetClientState(suite.ctx, testClientID, clientState) return nil }, diff --git a/x/ibc/02-client/keeper/encoding.go b/x/ibc/02-client/keeper/encoding.go new file mode 100644 index 000000000..266a256b1 --- /dev/null +++ b/x/ibc/02-client/keeper/encoding.go @@ -0,0 +1,49 @@ +package keeper + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" +) + +// MustUnmarshalClientState attempts to decode and return an ClientState object from +// raw encoded bytes. It panics on error. +func (k Keeper) MustUnmarshalClientState(bz []byte) exported.ClientState { + clientState, err := k.UnmarshalClientState(bz) + if err != nil { + panic(fmt.Errorf("failed to decode client state: %w", err)) + } + + return clientState +} + +// MustMarshalClientState attempts to encode an ClientState object and returns the +// raw encoded bytes. It panics on error. +func (k Keeper) MustMarshalClientState(clientState exported.ClientState) []byte { + bz, err := k.MarshalClientState(clientState) + if err != nil { + panic(fmt.Errorf("failed to encode client state: %w", err)) + } + + return bz +} + +// MarshalClientState marshals an ClientState interface. If the given type implements +// the Marshaler interface, it is treated as a Proto-defined message and +// serialized that way. +func (k Keeper) MarshalClientState(clientStateI exported.ClientState) ([]byte, error) { + return codec.MarshalAny(k.cdc, clientStateI) +} + +// UnmarshalClientState returns an ClientState interface from raw encoded clientState +// bytes of a Proto-based ClientState type. An error is returned upon decoding +// failure. +func (k Keeper) UnmarshalClientState(bz []byte) (exported.ClientState, error) { + var clientState exported.ClientState + if err := codec.UnmarshalAny(k.cdc, &clientState, bz); err != nil { + return nil, err + } + + return clientState, nil +} diff --git a/x/ibc/02-client/keeper/keeper.go b/x/ibc/02-client/keeper/keeper.go index afa9be88a..f60baef62 100644 --- a/x/ibc/02-client/keeper/keeper.go +++ b/x/ibc/02-client/keeper/keeper.go @@ -22,15 +22,17 @@ import ( // state information type Keeper struct { storeKey sdk.StoreKey - cdc *codec.Codec + cdc codec.BinaryMarshaler + aminoCdc *codec.Codec stakingKeeper types.StakingKeeper } // NewKeeper creates a new NewKeeper instance -func NewKeeper(cdc *codec.Codec, key sdk.StoreKey, sk types.StakingKeeper) Keeper { +func NewKeeper(cdc codec.BinaryMarshaler, aminoCdc *codec.Codec, key sdk.StoreKey, sk types.StakingKeeper) Keeper { return Keeper{ storeKey: key, cdc: cdc, + aminoCdc: aminoCdc, stakingKeeper: sk, } } @@ -48,16 +50,14 @@ func (k Keeper) GetClientState(ctx sdk.Context, clientID string) (exported.Clien return nil, false } - var clientState exported.ClientState - k.cdc.MustUnmarshalBinaryBare(bz, &clientState) + clientState := k.MustUnmarshalClientState(bz) return clientState, true } // SetClientState sets a particular Client to the store func (k Keeper) SetClientState(ctx sdk.Context, clientID string, clientState exported.ClientState) { store := k.ClientStore(ctx, clientID) - bz := k.cdc.MustMarshalBinaryBare(clientState) - store.Set(host.KeyClientState(), bz) + store.Set(host.KeyClientState(), k.MustMarshalClientState(clientState)) } // GetClientType gets the consensus type for a specific client @@ -86,7 +86,7 @@ func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height } var consensusState exported.ConsensusState - k.cdc.MustUnmarshalBinaryBare(bz, &consensusState) + k.aminoCdc.MustUnmarshalBinaryBare(bz, &consensusState) return consensusState, true } @@ -94,7 +94,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.cdc.MustMarshalBinaryBare(consensusState) + bz := k.aminoCdc.MustMarshalBinaryBare(consensusState) store.Set(host.KeyConsensusState(height), bz) } @@ -114,7 +114,7 @@ func (k Keeper) IterateConsensusStates(ctx sdk.Context, cb func(clientID string, } clientID := keySplit[1] var consensusState exported.ConsensusState - k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &consensusState) + k.aminoCdc.MustUnmarshalBinaryBare(iterator.Value(), &consensusState) if cb(clientID, consensusState) { break @@ -233,8 +233,7 @@ func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs expo if keySplit[len(keySplit)-1] != "clientState" { continue } - var clientState exported.ClientState - k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &clientState) + clientState := k.MustUnmarshalClientState(iterator.Value()) // key is ibc/{clientid}/clientState // Thus, keySplit[1] is clientID diff --git a/x/ibc/02-client/keeper/keeper_test.go b/x/ibc/02-client/keeper/keeper_test.go index 970c766a6..6853b0653 100644 --- a/x/ibc/02-client/keeper/keeper_test.go +++ b/x/ibc/02-client/keeper/keeper_test.go @@ -38,7 +38,8 @@ const ( type KeeperTestSuite struct { suite.Suite - cdc *codec.Codec + cdc codec.Marshaler + aminoCdc *codec.Codec ctx sdk.Context keeper *keeper.Keeper consensusState ibctmtypes.ConsensusState @@ -56,7 +57,8 @@ func (suite *KeeperTestSuite) SetupTest() { now2 := suite.now.Add(time.Hour) app := simapp.Setup(isCheckTx) - suite.cdc = app.Codec() + 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() diff --git a/x/ibc/02-client/types/codec.go b/x/ibc/02-client/types/codec.go index 792a05a8d..b83b00831 100644 --- a/x/ibc/02-client/types/codec.go +++ b/x/ibc/02-client/types/codec.go @@ -2,25 +2,41 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" ) -// SubModuleCdc defines the IBC client codec. -var SubModuleCdc *codec.Codec - -func init() { - SubModuleCdc = codec.New() - cryptocodec.RegisterCrypto(SubModuleCdc) - RegisterCodec(SubModuleCdc) -} - // RegisterCodec registers the IBC client interfaces and types func RegisterCodec(cdc *codec.Codec) { - cdc.RegisterInterface((*exported.ClientState)(nil), nil) + cdc.RegisterInterface((*exported.ClientState)(nil), nil) // remove after genesis migration cdc.RegisterInterface((*exported.MsgCreateClient)(nil), nil) cdc.RegisterInterface((*exported.MsgUpdateClient)(nil), nil) cdc.RegisterInterface((*exported.ConsensusState)(nil), nil) cdc.RegisterInterface((*exported.Header)(nil), nil) cdc.RegisterInterface((*exported.Misbehaviour)(nil), nil) } + +// RegisterInterfaces registers the client interfaces to protobuf Any. +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + registry.RegisterInterface( + "cosmos_sdk.ibc.v1.client.ClientState", + (*exported.ClientState)(nil), + ) +} + +var ( + amino = codec.New() + + // SubModuleCdc references the global x/ibc/02-client module codec. Note, the codec should + // ONLY be used in certain instances of tests and for JSON encoding as Amino is + // still used for that purpose. + // + // The actual codec used for serialization should be provided to x/ibc/02-client and + // defined at the application level. + SubModuleCdc = codec.NewHybridCodec(amino, codectypes.NewInterfaceRegistry()) +) + +func init() { + RegisterCodec(amino) + amino.Seal() +} diff --git a/x/ibc/03-connection/types/codec.go b/x/ibc/03-connection/types/codec.go index 466235fb1..74f37c047 100644 --- a/x/ibc/03-connection/types/codec.go +++ b/x/ibc/03-connection/types/codec.go @@ -2,13 +2,13 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" ) // RegisterInterfaces register the ibc interfaces submodule implementations to protobuf // Any. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations( (*sdk.Msg)(nil), &MsgConnectionOpenInit{}, @@ -24,5 +24,5 @@ var ( // // The actual codec used for serialization should be provided to x/ibc/03-connectionl and // defined at the application level. - SubModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) + SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) ) diff --git a/x/ibc/04-channel/types/codec.go b/x/ibc/04-channel/types/codec.go index 6ff4c4812..22348bd6a 100644 --- a/x/ibc/04-channel/types/codec.go +++ b/x/ibc/04-channel/types/codec.go @@ -2,13 +2,13 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" ) // RegisterInterfaces register the ibc channel submodule interfaces to protobuf // Any. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations( (*sdk.Msg)(nil), &MsgChannelOpenInit{}, @@ -28,4 +28,4 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { // // The actual codec used for serialization should be provided to x/ibc/04-channel and // defined at the application level. -var SubModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) +var SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) diff --git a/x/ibc/07-tendermint/misbehaviour.go b/x/ibc/07-tendermint/misbehaviour.go index 5f14e8b21..1e28afec3 100644 --- a/x/ibc/07-tendermint/misbehaviour.go +++ b/x/ibc/07-tendermint/misbehaviour.go @@ -26,7 +26,7 @@ func CheckMisbehaviourAndUpdateState( ) (clientexported.ClientState, error) { // cast the interface to specific types before checking for misbehaviour - tmClientState, ok := clientState.(types.ClientState) + tmClientState, ok := clientState.(*types.ClientState) if !ok { return nil, sdkerrors.Wrapf(clienttypes.ErrInvalidClientType, "expected type %T, got %T", types.ClientState{}, clientState) } @@ -59,7 +59,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/types/client_state.go b/x/ibc/07-tendermint/types/client_state.go index a3067e2b5..4be704d7e 100644 --- a/x/ibc/07-tendermint/types/client_state.go +++ b/x/ibc/07-tendermint/types/client_state.go @@ -21,10 +21,10 @@ import ( host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" ) -var _ clientexported.ClientState = ClientState{} +var _ clientexported.ClientState = (*ClientState)(nil) // InitializeFromMsg creates a tendermint client state from a CreateClientMsg -func InitializeFromMsg(msg *MsgCreateClient) ClientState { +func InitializeFromMsg(msg *MsgCreateClient) *ClientState { return NewClientState(msg.Header.ChainID, msg.TrustLevel, msg.TrustingPeriod, msg.UnbondingPeriod, msg.MaxClockDrift, uint64(msg.Header.Height), msg.ProofSpecs, @@ -36,8 +36,8 @@ func NewClientState( chainID string, trustLevel Fraction, trustingPeriod, ubdPeriod, maxClockDrift time.Duration, latestHeight uint64, specs []*ics23.ProofSpec, -) ClientState { - return ClientState{ +) *ClientState { + return &ClientState{ ChainID: chainID, TrustLevel: trustLevel, TrustingPeriod: trustingPeriod, diff --git a/x/ibc/07-tendermint/types/client_state_test.go b/x/ibc/07-tendermint/types/client_state_test.go index 7957f84fc..79e8b0009 100644 --- a/x/ibc/07-tendermint/types/client_state_test.go +++ b/x/ibc/07-tendermint/types/client_state_test.go @@ -21,7 +21,7 @@ const ( func (suite *TendermintTestSuite) TestValidate() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState expPass bool }{ { @@ -89,7 +89,7 @@ func (suite *TendermintTestSuite) TestValidate() { func (suite *TendermintTestSuite) TestVerifyClientConsensusState() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix proof []byte @@ -125,7 +125,7 @@ func (suite *TendermintTestSuite) TestVerifyClientConsensusState() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), }, @@ -166,7 +166,7 @@ func (suite *TendermintTestSuite) TestVerifyConnectionState() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState connection connectiontypes.ConnectionEnd consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix @@ -206,7 +206,7 @@ func (suite *TendermintTestSuite) TestVerifyConnectionState() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, connection: conn, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), @@ -249,7 +249,7 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState channel channeltypes.Channel consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix @@ -289,7 +289,7 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, channel: ch, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), @@ -329,7 +329,7 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() { func (suite *TendermintTestSuite) TestVerifyPacketCommitment() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState commitment []byte consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix @@ -369,7 +369,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketCommitment() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, commitment: []byte{}, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), @@ -409,7 +409,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketCommitment() { func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState ack []byte consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix @@ -449,7 +449,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, ack: []byte{}, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), @@ -489,7 +489,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() { func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix proof []byte @@ -526,7 +526,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), }, @@ -564,7 +564,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() { func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() { testCases := []struct { name string - clientState ibctmtypes.ClientState + clientState *ibctmtypes.ClientState consensusState ibctmtypes.ConsensusState prefix commitmenttypes.MerklePrefix proof []byte @@ -601,7 +601,7 @@ func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() { }, { name: "client is frozen", - clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, + clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, consensusState: ibctmtypes.ConsensusState{ Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), }, diff --git a/x/ibc/07-tendermint/types/codec.go b/x/ibc/07-tendermint/types/codec.go index 50825f8ba..ce7917392 100644 --- a/x/ibc/07-tendermint/types/codec.go +++ b/x/ibc/07-tendermint/types/codec.go @@ -2,7 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" ) @@ -21,7 +21,7 @@ func RegisterCodec(cdc *codec.Codec) { // RegisterInterfaces registers the tendermint concrete evidence and client-related // implementations and interfaces. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations( (*clientexported.ClientState)(nil), &ClientState{}, @@ -37,7 +37,7 @@ var ( // // The actual codec used for serialization should be provided to x/ibc/07-tendermint and // defined at the application level. - SubModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry()) + SubModuleCdc = codec.NewHybridCodec(amino, codectypes.NewInterfaceRegistry()) ) func init() { diff --git a/x/ibc/07-tendermint/update.go b/x/ibc/07-tendermint/update.go index 6088fed98..bb95cddbd 100644 --- a/x/ibc/07-tendermint/update.go +++ b/x/ibc/07-tendermint/update.go @@ -36,7 +36,7 @@ func CheckValidityAndUpdateState( clientState clientexported.ClientState, consState clientexported.ConsensusState, header clientexported.Header, currentTimestamp time.Time, ) (clientexported.ClientState, clientexported.ConsensusState, error) { - tmClientState, ok := clientState.(types.ClientState) + tmClientState, ok := clientState.(*types.ClientState) if !ok { return nil, nil, sdkerrors.Wrapf( clienttypes.ErrInvalidClientType, "expected type %T, got %T", types.ClientState{}, clientState, @@ -67,7 +67,7 @@ func CheckValidityAndUpdateState( // checkValidity checks if the Tendermint header is valid. func checkValidity( - clientState types.ClientState, consState types.ConsensusState, header types.Header, currentTimestamp time.Time, + clientState *types.ClientState, consState types.ConsensusState, header types.Header, currentTimestamp time.Time, ) error { // assert trusting period has not yet passed if currentTimestamp.Sub(consState.Timestamp) >= clientState.TrustingPeriod { @@ -127,7 +127,7 @@ 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) } diff --git a/x/ibc/07-tendermint/update_test.go b/x/ibc/07-tendermint/update_test.go index dd6ba0824..9f392ae95 100644 --- a/x/ibc/07-tendermint/update_test.go +++ b/x/ibc/07-tendermint/update_test.go @@ -13,7 +13,7 @@ import ( func (suite *TendermintTestSuite) TestCheckValidity() { var ( - clientState types.ClientState + clientState *types.ClientState consensusState types.ConsensusState newHeader types.Header currentTime time.Time diff --git a/x/ibc/09-localhost/types/client_state.go b/x/ibc/09-localhost/types/client_state.go index cda9ad10a..9c875f213 100644 --- a/x/ibc/09-localhost/types/client_state.go +++ b/x/ibc/09-localhost/types/client_state.go @@ -24,8 +24,8 @@ import ( var _ clientexported.ClientState = (*ClientState)(nil) // NewClientState creates a new ClientState instance -func NewClientState(chainID string, height int64) ClientState { - return ClientState{ +func NewClientState(chainID string, height int64) *ClientState { + return &ClientState{ ChainID: chainID, Height: uint64(height), } diff --git a/x/ibc/09-localhost/types/client_state_test.go b/x/ibc/09-localhost/types/client_state_test.go index 9138bd060..234f5368b 100644 --- a/x/ibc/09-localhost/types/client_state_test.go +++ b/x/ibc/09-localhost/types/client_state_test.go @@ -17,7 +17,7 @@ const ( func (suite *LocalhostTestSuite) TestValidate() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState expPass bool }{ { @@ -61,7 +61,7 @@ func (suite *LocalhostTestSuite) TestVerifyConnectionState() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState connection connectiontypes.ConnectionEnd prefix commitmenttypes.MerklePrefix proof []byte @@ -105,7 +105,7 @@ func (suite *LocalhostTestSuite) TestVerifyChannelState() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState channel channeltypes.Channel prefix commitmenttypes.MerklePrefix proof []byte @@ -153,7 +153,7 @@ func (suite *LocalhostTestSuite) TestVerifyChannelState() { func (suite *LocalhostTestSuite) TestVerifyPacketCommitment() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState commitment []byte prefix commitmenttypes.MerklePrefix proof []byte @@ -208,7 +208,7 @@ func (suite *LocalhostTestSuite) TestVerifyPacketCommitment() { func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgement() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState ack []byte prefix commitmenttypes.MerklePrefix proof []byte @@ -263,7 +263,7 @@ func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgement() { func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgementAbsence() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState prefix commitmenttypes.MerklePrefix proof []byte expPass bool @@ -294,7 +294,7 @@ func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgementAbsence() { func (suite *LocalhostTestSuite) TestVerifyNextSeqRecv() { testCases := []struct { name string - clientState types.ClientState + clientState *types.ClientState prefix commitmenttypes.MerklePrefix proof []byte expPass bool diff --git a/x/ibc/09-localhost/types/codec.go b/x/ibc/09-localhost/types/codec.go index 399996304..c8952a756 100644 --- a/x/ibc/09-localhost/types/codec.go +++ b/x/ibc/09-localhost/types/codec.go @@ -2,20 +2,21 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" ) // RegisterCodec registers client state on the provided Amino codec. This type is used for // Amino JSON serialization. +// TODO: remove after genesis and exporting use proto func RegisterCodec(cdc *codec.Codec) { cdc.RegisterConcrete(ClientState{}, "ibc/client/localhost/ClientState", nil) } // RegisterInterfaces register the ibc interfaces submodule implementations to protobuf // Any. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterImplementations( (*sdk.Msg)(nil), &MsgCreateClient{}, @@ -27,17 +28,10 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { } var ( - amino = codec.New() - // SubModuleCdc references the global x/ibc/09-localhost 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/09-localhost and // defined at the application level. - SubModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry()) + SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry()) ) - -func init() { - RegisterCodec(amino) - amino.Seal() -} diff --git a/x/ibc/23-commitment/types/codec.go b/x/ibc/23-commitment/types/codec.go index b53c0ce73..412daf58f 100644 --- a/x/ibc/23-commitment/types/codec.go +++ b/x/ibc/23-commitment/types/codec.go @@ -2,12 +2,12 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported" ) // RegisterInterfaces registers the commitment interfaces to protobuf Any. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { registry.RegisterInterface( "cosmos.ibc.commitment.Root", (*exported.Root)(nil), @@ -66,7 +66,7 @@ var ( // // The actual codec used for serialization should be provided to x/ibc/23-commitmentl and // defined at the application level. - SubModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry()) + SubModuleCdc = codec.NewHybridCodec(amino, codectypes.NewInterfaceRegistry()) ) func init() { diff --git a/x/ibc/keeper/keeper.go b/x/ibc/keeper/keeper.go index 759d402bd..f556f03cd 100644 --- a/x/ibc/keeper/keeper.go +++ b/x/ibc/keeper/keeper.go @@ -34,7 +34,7 @@ type Keeper struct { func NewKeeper( aminoCdc *codec.Codec, cdc codec.BinaryMarshaler, key sdk.StoreKey, stakingKeeper clienttypes.StakingKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, ) *Keeper { - clientKeeper := clientkeeper.NewKeeper(aminoCdc, key, stakingKeeper) + clientKeeper := clientkeeper.NewKeeper(cdc, aminoCdc, key, stakingKeeper) connectionKeeper := connectionkeeper.NewKeeper(aminoCdc, cdc, key, clientKeeper) portKeeper := portkeeper.NewKeeper(scopedKeeper) channelKeeper := channelkeeper.NewKeeper(cdc, key, clientKeeper, connectionKeeper, portKeeper, scopedKeeper) diff --git a/x/ibc/module.go b/x/ibc/module.go index 4c85ac062..52f50ebff 100644 --- a/x/ibc/module.go +++ b/x/ibc/module.go @@ -13,7 +13,7 @@ import ( "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" @@ -79,7 +79,7 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command { } // RegisterInterfaces registers module concrete types into protobuf Any. -func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) { types.RegisterInterfaces(registry) } diff --git a/x/ibc/types/codec.go b/x/ibc/types/codec.go index 3a337d02f..661e7b18b 100644 --- a/x/ibc/types/codec.go +++ b/x/ibc/types/codec.go @@ -2,7 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" - cdctypes "github.com/cosmos/cosmos-sdk/codec/types" + codectypes "github.com/cosmos/cosmos-sdk/codec/types" clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types" connectiontypes "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types" channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/types" @@ -21,8 +21,11 @@ func RegisterCodec(cdc *codec.Codec) { } // RegisterInterfaces registers x/ibc interfaces into protobuf Any. -func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { +func RegisterInterfaces(registry codectypes.InterfaceRegistry) { + clienttypes.RegisterInterfaces(registry) connectiontypes.RegisterInterfaces(registry) channeltypes.RegisterInterfaces(registry) + ibctmtypes.RegisterInterfaces(registry) + localhosttypes.RegisterInterfaces(registry) commitmenttypes.RegisterInterfaces(registry) }