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
This commit is contained in:
colin axnér 2020-08-05 17:14:24 +02:00 committed by GitHub
parent 392121ecc4
commit 1a531cb645
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 157 additions and 94 deletions

View File

@ -14,7 +14,7 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec" "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" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/module"
@ -79,7 +79,7 @@ func (AppModuleBasic) GetQueryCmd() *cobra.Command {
} }
// RegisterInterfaces registers module concrete types into protobuf Any. // RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry) types.RegisterInterfaces(registry)
} }

View File

@ -2,13 +2,13 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" sdk "github.com/cosmos/cosmos-sdk/types"
) )
// RegisterInterfaces register the ibc transfer module interfaces to protobuf // RegisterInterfaces register the ibc transfer module interfaces to protobuf
// Any. // Any.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil), &MsgTransfer{}) 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 // The actual codec used for serialization should be provided to x/ibc-transfer and
// defined at the application level. // defined at the application level.
ModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) ModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
) )

View File

@ -69,7 +69,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
} }
var ( var (
updateHeader ibctmtypes.Header updateHeader ibctmtypes.Header
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
) )
cases := []struct { cases := []struct {
@ -128,7 +128,7 @@ func (suite *KeeperTestSuite) TestUpdateClientTendermint() {
return nil return nil
}, false}, }, false},
{"frozen client before update", func() error { {"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.SetClientState(suite.ctx, testClientID, clientState)
suite.keeper.SetClientType(suite.ctx, testClientID, exported.Tendermint) suite.keeper.SetClientType(suite.ctx, testClientID, exported.Tendermint)
updateHeader = createFutureUpdateFn(suite) updateHeader = createFutureUpdateFn(suite)
@ -309,7 +309,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
ClientID: testClientID, ClientID: testClientID,
}, },
func() error { 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.SetClientState(suite.ctx, testClientID, clientState)
return nil return nil
}, },
@ -324,7 +324,7 @@ func (suite *KeeperTestSuite) TestCheckMisbehaviourAndUpdateState() {
ClientID: testClientID, ClientID: testClientID,
}, },
func() error { 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.SetClientState(suite.ctx, testClientID, clientState)
return nil return nil
}, },

View File

@ -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
}

View File

@ -22,15 +22,17 @@ import (
// state information // state information
type Keeper struct { type Keeper struct {
storeKey sdk.StoreKey storeKey sdk.StoreKey
cdc *codec.Codec cdc codec.BinaryMarshaler
aminoCdc *codec.Codec
stakingKeeper types.StakingKeeper stakingKeeper types.StakingKeeper
} }
// NewKeeper creates a new NewKeeper instance // 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{ return Keeper{
storeKey: key, storeKey: key,
cdc: cdc, cdc: cdc,
aminoCdc: aminoCdc,
stakingKeeper: sk, stakingKeeper: sk,
} }
} }
@ -48,16 +50,14 @@ func (k Keeper) GetClientState(ctx sdk.Context, clientID string) (exported.Clien
return nil, false return nil, false
} }
var clientState exported.ClientState clientState := k.MustUnmarshalClientState(bz)
k.cdc.MustUnmarshalBinaryBare(bz, &clientState)
return clientState, true return clientState, true
} }
// SetClientState sets a particular Client to the store // SetClientState sets a particular Client to the store
func (k Keeper) SetClientState(ctx sdk.Context, clientID string, clientState exported.ClientState) { func (k Keeper) SetClientState(ctx sdk.Context, clientID string, clientState exported.ClientState) {
store := k.ClientStore(ctx, clientID) store := k.ClientStore(ctx, clientID)
bz := k.cdc.MustMarshalBinaryBare(clientState) store.Set(host.KeyClientState(), k.MustMarshalClientState(clientState))
store.Set(host.KeyClientState(), bz)
} }
// GetClientType gets the consensus type for a specific client // 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 var consensusState exported.ConsensusState
k.cdc.MustUnmarshalBinaryBare(bz, &consensusState) k.aminoCdc.MustUnmarshalBinaryBare(bz, &consensusState)
return consensusState, true return consensusState, true
} }
@ -94,7 +94,7 @@ func (k Keeper) GetClientConsensusState(ctx sdk.Context, clientID string, height
// height // height
func (k Keeper) SetClientConsensusState(ctx sdk.Context, clientID string, height uint64, consensusState exported.ConsensusState) { func (k Keeper) SetClientConsensusState(ctx sdk.Context, clientID string, height uint64, consensusState exported.ConsensusState) {
store := k.ClientStore(ctx, clientID) store := k.ClientStore(ctx, clientID)
bz := k.cdc.MustMarshalBinaryBare(consensusState) bz := k.aminoCdc.MustMarshalBinaryBare(consensusState)
store.Set(host.KeyConsensusState(height), bz) store.Set(host.KeyConsensusState(height), bz)
} }
@ -114,7 +114,7 @@ func (k Keeper) IterateConsensusStates(ctx sdk.Context, cb func(clientID string,
} }
clientID := keySplit[1] clientID := keySplit[1]
var consensusState exported.ConsensusState var consensusState exported.ConsensusState
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &consensusState) k.aminoCdc.MustUnmarshalBinaryBare(iterator.Value(), &consensusState)
if cb(clientID, consensusState) { if cb(clientID, consensusState) {
break break
@ -233,8 +233,7 @@ func (k Keeper) IterateClients(ctx sdk.Context, cb func(clientID string, cs expo
if keySplit[len(keySplit)-1] != "clientState" { if keySplit[len(keySplit)-1] != "clientState" {
continue continue
} }
var clientState exported.ClientState clientState := k.MustUnmarshalClientState(iterator.Value())
k.cdc.MustUnmarshalBinaryBare(iterator.Value(), &clientState)
// key is ibc/{clientid}/clientState // key is ibc/{clientid}/clientState
// Thus, keySplit[1] is clientID // Thus, keySplit[1] is clientID

View File

@ -38,7 +38,8 @@ const (
type KeeperTestSuite struct { type KeeperTestSuite struct {
suite.Suite suite.Suite
cdc *codec.Codec cdc codec.Marshaler
aminoCdc *codec.Codec
ctx sdk.Context ctx sdk.Context
keeper *keeper.Keeper keeper *keeper.Keeper
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
@ -56,7 +57,8 @@ func (suite *KeeperTestSuite) SetupTest() {
now2 := suite.now.Add(time.Hour) now2 := suite.now.Add(time.Hour)
app := simapp.Setup(isCheckTx) 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.ctx = app.BaseApp.NewContext(isCheckTx, abci.Header{Height: testClientHeight, ChainID: testClientID, Time: now2})
suite.keeper = &app.IBCKeeper.ClientKeeper suite.keeper = &app.IBCKeeper.ClientKeeper
suite.privVal = tmtypes.NewMockPV() suite.privVal = tmtypes.NewMockPV()

View File

@ -2,25 +2,41 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" "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 // RegisterCodec registers the IBC client interfaces and types
func RegisterCodec(cdc *codec.Codec) { 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.MsgCreateClient)(nil), nil)
cdc.RegisterInterface((*exported.MsgUpdateClient)(nil), nil) cdc.RegisterInterface((*exported.MsgUpdateClient)(nil), nil)
cdc.RegisterInterface((*exported.ConsensusState)(nil), nil) cdc.RegisterInterface((*exported.ConsensusState)(nil), nil)
cdc.RegisterInterface((*exported.Header)(nil), nil) cdc.RegisterInterface((*exported.Header)(nil), nil)
cdc.RegisterInterface((*exported.Misbehaviour)(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()
}

View File

@ -2,13 +2,13 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" sdk "github.com/cosmos/cosmos-sdk/types"
) )
// RegisterInterfaces register the ibc interfaces submodule implementations to protobuf // RegisterInterfaces register the ibc interfaces submodule implementations to protobuf
// Any. // Any.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations( registry.RegisterImplementations(
(*sdk.Msg)(nil), (*sdk.Msg)(nil),
&MsgConnectionOpenInit{}, &MsgConnectionOpenInit{},
@ -24,5 +24,5 @@ var (
// //
// The actual codec used for serialization should be provided to x/ibc/03-connectionl and // The actual codec used for serialization should be provided to x/ibc/03-connectionl and
// defined at the application level. // defined at the application level.
SubModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
) )

View File

@ -2,13 +2,13 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" sdk "github.com/cosmos/cosmos-sdk/types"
) )
// RegisterInterfaces register the ibc channel submodule interfaces to protobuf // RegisterInterfaces register the ibc channel submodule interfaces to protobuf
// Any. // Any.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations( registry.RegisterImplementations(
(*sdk.Msg)(nil), (*sdk.Msg)(nil),
&MsgChannelOpenInit{}, &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 // The actual codec used for serialization should be provided to x/ibc/04-channel and
// defined at the application level. // defined at the application level.
var SubModuleCdc = codec.NewProtoCodec(cdctypes.NewInterfaceRegistry()) var SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())

View File

@ -26,7 +26,7 @@ func CheckMisbehaviourAndUpdateState(
) (clientexported.ClientState, error) { ) (clientexported.ClientState, error) {
// cast the interface to specific types before checking for misbehaviour // cast the interface to specific types before checking for misbehaviour
tmClientState, ok := clientState.(types.ClientState) tmClientState, ok := clientState.(*types.ClientState)
if !ok { 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)
} }
@ -59,7 +59,7 @@ func CheckMisbehaviourAndUpdateState(
// checkMisbehaviour checks if the evidence provided is a valid light client misbehaviour // checkMisbehaviour checks if the evidence provided is a valid light client misbehaviour
func checkMisbehaviour( 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, height uint64, currentTimestamp time.Time, consensusParams *abci.ConsensusParams,
) error { ) error {
// calculate the age of the misbehaviour evidence // calculate the age of the misbehaviour evidence

View File

@ -21,10 +21,10 @@ import (
host "github.com/cosmos/cosmos-sdk/x/ibc/24-host" 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 // 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, return NewClientState(msg.Header.ChainID, msg.TrustLevel,
msg.TrustingPeriod, msg.UnbondingPeriod, msg.MaxClockDrift, msg.TrustingPeriod, msg.UnbondingPeriod, msg.MaxClockDrift,
uint64(msg.Header.Height), msg.ProofSpecs, uint64(msg.Header.Height), msg.ProofSpecs,
@ -36,8 +36,8 @@ func NewClientState(
chainID string, trustLevel Fraction, chainID string, trustLevel Fraction,
trustingPeriod, ubdPeriod, maxClockDrift time.Duration, trustingPeriod, ubdPeriod, maxClockDrift time.Duration,
latestHeight uint64, specs []*ics23.ProofSpec, latestHeight uint64, specs []*ics23.ProofSpec,
) ClientState { ) *ClientState {
return ClientState{ return &ClientState{
ChainID: chainID, ChainID: chainID,
TrustLevel: trustLevel, TrustLevel: trustLevel,
TrustingPeriod: trustingPeriod, TrustingPeriod: trustingPeriod,

View File

@ -21,7 +21,7 @@ const (
func (suite *TendermintTestSuite) TestValidate() { func (suite *TendermintTestSuite) TestValidate() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
expPass bool expPass bool
}{ }{
{ {
@ -89,7 +89,7 @@ func (suite *TendermintTestSuite) TestValidate() {
func (suite *TendermintTestSuite) TestVerifyClientConsensusState() { func (suite *TendermintTestSuite) TestVerifyClientConsensusState() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -125,7 +125,7 @@ func (suite *TendermintTestSuite) TestVerifyClientConsensusState() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
}, },
@ -166,7 +166,7 @@ func (suite *TendermintTestSuite) TestVerifyConnectionState() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
connection connectiontypes.ConnectionEnd connection connectiontypes.ConnectionEnd
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
@ -206,7 +206,7 @@ func (suite *TendermintTestSuite) TestVerifyConnectionState() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
connection: conn, connection: conn,
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
@ -249,7 +249,7 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
channel channeltypes.Channel channel channeltypes.Channel
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
@ -289,7 +289,7 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
channel: ch, channel: ch,
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
@ -329,7 +329,7 @@ func (suite *TendermintTestSuite) TestVerifyChannelState() {
func (suite *TendermintTestSuite) TestVerifyPacketCommitment() { func (suite *TendermintTestSuite) TestVerifyPacketCommitment() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
commitment []byte commitment []byte
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
@ -369,7 +369,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketCommitment() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
commitment: []byte{}, commitment: []byte{},
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
@ -409,7 +409,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketCommitment() {
func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() { func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
ack []byte ack []byte
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
@ -449,7 +449,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
ack: []byte{}, ack: []byte{},
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
@ -489,7 +489,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgement() {
func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() { func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -526,7 +526,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
}, },
@ -564,7 +564,7 @@ func (suite *TendermintTestSuite) TestVerifyPacketAcknowledgementAbsence() {
func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() { func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() {
testCases := []struct { testCases := []struct {
name string name string
clientState ibctmtypes.ClientState clientState *ibctmtypes.ClientState
consensusState ibctmtypes.ConsensusState consensusState ibctmtypes.ConsensusState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -601,7 +601,7 @@ func (suite *TendermintTestSuite) TestVerifyNextSeqRecv() {
}, },
{ {
name: "client is frozen", name: "client is frozen",
clientState: ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1}, clientState: &ibctmtypes.ClientState{LatestHeight: height, FrozenHeight: height - 1},
consensusState: ibctmtypes.ConsensusState{ consensusState: ibctmtypes.ConsensusState{
Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash), Root: commitmenttypes.NewMerkleRoot(suite.header.AppHash),
}, },

View File

@ -2,7 +2,7 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" 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 // RegisterInterfaces registers the tendermint concrete evidence and client-related
// implementations and interfaces. // implementations and interfaces.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations( registry.RegisterImplementations(
(*clientexported.ClientState)(nil), (*clientexported.ClientState)(nil),
&ClientState{}, &ClientState{},
@ -37,7 +37,7 @@ var (
// //
// The actual codec used for serialization should be provided to x/ibc/07-tendermint and // The actual codec used for serialization should be provided to x/ibc/07-tendermint and
// defined at the application level. // defined at the application level.
SubModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry()) SubModuleCdc = codec.NewHybridCodec(amino, codectypes.NewInterfaceRegistry())
) )
func init() { func init() {

View File

@ -36,7 +36,7 @@ func CheckValidityAndUpdateState(
clientState clientexported.ClientState, consState clientexported.ConsensusState, clientState clientexported.ClientState, consState clientexported.ConsensusState,
header clientexported.Header, currentTimestamp time.Time, header clientexported.Header, currentTimestamp time.Time,
) (clientexported.ClientState, clientexported.ConsensusState, error) { ) (clientexported.ClientState, clientexported.ConsensusState, error) {
tmClientState, ok := clientState.(types.ClientState) tmClientState, ok := clientState.(*types.ClientState)
if !ok { if !ok {
return nil, nil, sdkerrors.Wrapf( return nil, nil, sdkerrors.Wrapf(
clienttypes.ErrInvalidClientType, "expected type %T, got %T", types.ClientState{}, clientState, clienttypes.ErrInvalidClientType, "expected type %T, got %T", types.ClientState{}, clientState,
@ -67,7 +67,7 @@ func CheckValidityAndUpdateState(
// checkValidity checks if the Tendermint header is valid. // checkValidity checks if the Tendermint header is valid.
func checkValidity( 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 { ) error {
// assert trusting period has not yet passed // assert trusting period has not yet passed
if currentTimestamp.Sub(consState.Timestamp) >= clientState.TrustingPeriod { if currentTimestamp.Sub(consState.Timestamp) >= clientState.TrustingPeriod {
@ -127,7 +127,7 @@ func checkValidity(
} }
// update the consensus state from a new header // 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 { if uint64(header.Height) > clientState.LatestHeight {
clientState.LatestHeight = uint64(header.Height) clientState.LatestHeight = uint64(header.Height)
} }

View File

@ -13,7 +13,7 @@ import (
func (suite *TendermintTestSuite) TestCheckValidity() { func (suite *TendermintTestSuite) TestCheckValidity() {
var ( var (
clientState types.ClientState clientState *types.ClientState
consensusState types.ConsensusState consensusState types.ConsensusState
newHeader types.Header newHeader types.Header
currentTime time.Time currentTime time.Time

View File

@ -24,8 +24,8 @@ import (
var _ clientexported.ClientState = (*ClientState)(nil) var _ clientexported.ClientState = (*ClientState)(nil)
// NewClientState creates a new ClientState instance // NewClientState creates a new ClientState instance
func NewClientState(chainID string, height int64) ClientState { func NewClientState(chainID string, height int64) *ClientState {
return ClientState{ return &ClientState{
ChainID: chainID, ChainID: chainID,
Height: uint64(height), Height: uint64(height),
} }

View File

@ -17,7 +17,7 @@ const (
func (suite *LocalhostTestSuite) TestValidate() { func (suite *LocalhostTestSuite) TestValidate() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
expPass bool expPass bool
}{ }{
{ {
@ -61,7 +61,7 @@ func (suite *LocalhostTestSuite) TestVerifyConnectionState() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
connection connectiontypes.ConnectionEnd connection connectiontypes.ConnectionEnd
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -105,7 +105,7 @@ func (suite *LocalhostTestSuite) TestVerifyChannelState() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
channel channeltypes.Channel channel channeltypes.Channel
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -153,7 +153,7 @@ func (suite *LocalhostTestSuite) TestVerifyChannelState() {
func (suite *LocalhostTestSuite) TestVerifyPacketCommitment() { func (suite *LocalhostTestSuite) TestVerifyPacketCommitment() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
commitment []byte commitment []byte
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -208,7 +208,7 @@ func (suite *LocalhostTestSuite) TestVerifyPacketCommitment() {
func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgement() { func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgement() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
ack []byte ack []byte
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
@ -263,7 +263,7 @@ func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgement() {
func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgementAbsence() { func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgementAbsence() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
expPass bool expPass bool
@ -294,7 +294,7 @@ func (suite *LocalhostTestSuite) TestVerifyPacketAcknowledgementAbsence() {
func (suite *LocalhostTestSuite) TestVerifyNextSeqRecv() { func (suite *LocalhostTestSuite) TestVerifyNextSeqRecv() {
testCases := []struct { testCases := []struct {
name string name string
clientState types.ClientState clientState *types.ClientState
prefix commitmenttypes.MerklePrefix prefix commitmenttypes.MerklePrefix
proof []byte proof []byte
expPass bool expPass bool

View File

@ -2,20 +2,21 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" sdk "github.com/cosmos/cosmos-sdk/types"
clientexported "github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported" 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 // RegisterCodec registers client state on the provided Amino codec. This type is used for
// Amino JSON serialization. // Amino JSON serialization.
// TODO: remove after genesis and exporting use proto
func RegisterCodec(cdc *codec.Codec) { func RegisterCodec(cdc *codec.Codec) {
cdc.RegisterConcrete(ClientState{}, "ibc/client/localhost/ClientState", nil) cdc.RegisterConcrete(ClientState{}, "ibc/client/localhost/ClientState", nil)
} }
// RegisterInterfaces register the ibc interfaces submodule implementations to protobuf // RegisterInterfaces register the ibc interfaces submodule implementations to protobuf
// Any. // Any.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterImplementations( registry.RegisterImplementations(
(*sdk.Msg)(nil), (*sdk.Msg)(nil),
&MsgCreateClient{}, &MsgCreateClient{},
@ -27,17 +28,10 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
} }
var ( var (
amino = codec.New()
// SubModuleCdc references the global x/ibc/09-localhost module codec. Note, the codec should // 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. // 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 // The actual codec used for serialization should be provided to x/ibc/09-localhost and
// defined at the application level. // defined at the application level.
SubModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry()) SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
) )
func init() {
RegisterCodec(amino)
amino.Seal()
}

View File

@ -2,12 +2,12 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" "github.com/cosmos/cosmos-sdk/x/ibc/23-commitment/exported"
) )
// RegisterInterfaces registers the commitment interfaces to protobuf Any. // RegisterInterfaces registers the commitment interfaces to protobuf Any.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
registry.RegisterInterface( registry.RegisterInterface(
"cosmos.ibc.commitment.Root", "cosmos.ibc.commitment.Root",
(*exported.Root)(nil), (*exported.Root)(nil),
@ -66,7 +66,7 @@ var (
// //
// The actual codec used for serialization should be provided to x/ibc/23-commitmentl and // The actual codec used for serialization should be provided to x/ibc/23-commitmentl and
// defined at the application level. // defined at the application level.
SubModuleCdc = codec.NewHybridCodec(amino, cdctypes.NewInterfaceRegistry()) SubModuleCdc = codec.NewHybridCodec(amino, codectypes.NewInterfaceRegistry())
) )
func init() { func init() {

View File

@ -34,7 +34,7 @@ type Keeper struct {
func NewKeeper( func NewKeeper(
aminoCdc *codec.Codec, cdc codec.BinaryMarshaler, key sdk.StoreKey, stakingKeeper clienttypes.StakingKeeper, scopedKeeper capabilitykeeper.ScopedKeeper, aminoCdc *codec.Codec, cdc codec.BinaryMarshaler, key sdk.StoreKey, stakingKeeper clienttypes.StakingKeeper, scopedKeeper capabilitykeeper.ScopedKeeper,
) *Keeper { ) *Keeper {
clientKeeper := clientkeeper.NewKeeper(aminoCdc, key, stakingKeeper) clientKeeper := clientkeeper.NewKeeper(cdc, aminoCdc, key, stakingKeeper)
connectionKeeper := connectionkeeper.NewKeeper(aminoCdc, cdc, key, clientKeeper) connectionKeeper := connectionkeeper.NewKeeper(aminoCdc, cdc, key, clientKeeper)
portKeeper := portkeeper.NewKeeper(scopedKeeper) portKeeper := portkeeper.NewKeeper(scopedKeeper)
channelKeeper := channelkeeper.NewKeeper(cdc, key, clientKeeper, connectionKeeper, portKeeper, scopedKeeper) channelKeeper := channelkeeper.NewKeeper(cdc, key, clientKeeper, connectionKeeper, portKeeper, scopedKeeper)

View File

@ -13,7 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec" "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" sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module" "github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation" 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. // RegisterInterfaces registers module concrete types into protobuf Any.
func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func (AppModuleBasic) RegisterInterfaces(registry codectypes.InterfaceRegistry) {
types.RegisterInterfaces(registry) types.RegisterInterfaces(registry)
} }

View File

@ -2,7 +2,7 @@ package types
import ( import (
"github.com/cosmos/cosmos-sdk/codec" "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" clienttypes "github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
connectiontypes "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types" connectiontypes "github.com/cosmos/cosmos-sdk/x/ibc/03-connection/types"
channeltypes "github.com/cosmos/cosmos-sdk/x/ibc/04-channel/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. // RegisterInterfaces registers x/ibc interfaces into protobuf Any.
func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
clienttypes.RegisterInterfaces(registry)
connectiontypes.RegisterInterfaces(registry) connectiontypes.RegisterInterfaces(registry)
channeltypes.RegisterInterfaces(registry) channeltypes.RegisterInterfaces(registry)
ibctmtypes.RegisterInterfaces(registry)
localhosttypes.RegisterInterfaces(registry)
commitmenttypes.RegisterInterfaces(registry) commitmenttypes.RegisterInterfaces(registry)
} }