From feb128941a2c43259f8b59be8f305925abfcba2c Mon Sep 17 00:00:00 2001 From: mattverse Date: Thu, 9 Feb 2023 21:05:53 +0900 Subject: [PATCH] Fix Register legacy amino codec --- codec/legacy/amino_msg.go | 18 ++++++++++++++++ codec/legacy/amino_msg_test.go | 38 ++++++++++++++++++++++++++++++++++ codec/legacy/doc.go | 2 ++ x/auth/vesting/types/codec.go | 2 ++ x/authz/codec.go | 7 ++++--- x/bank/types/codec.go | 8 +++---- x/crisis/types/codec.go | 3 ++- x/distribution/types/codec.go | 9 ++++---- x/evidence/types/codec.go | 3 ++- x/feegrant/codec.go | 5 +++-- x/gov/types/codec.go | 9 ++++---- x/slashing/types/codec.go | 3 ++- x/staking/types/codec.go | 11 +++++----- 13 files changed, 93 insertions(+), 25 deletions(-) create mode 100644 codec/legacy/amino_msg.go create mode 100644 codec/legacy/amino_msg_test.go diff --git a/codec/legacy/amino_msg.go b/codec/legacy/amino_msg.go new file mode 100644 index 000000000000..58a02bed7b21 --- /dev/null +++ b/codec/legacy/amino_msg.go @@ -0,0 +1,18 @@ +package legacy + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// RegisterAminoMsg first checks that the msgName is <40 chars +// (else this would break ledger nano signing: https://github.com/cosmos/cosmos-sdk/issues/10870), +// then registers the concrete msg type with amino. +func RegisterAminoMsg(cdc *codec.LegacyAmino, msg sdk.Msg, msgName string) { + if len(msgName) > 39 { + panic(fmt.Errorf("msg name %s is too long to be registered with amino", msgName)) + } + cdc.RegisterConcrete(msg, msgName, nil) +} diff --git a/codec/legacy/amino_msg_test.go b/codec/legacy/amino_msg_test.go new file mode 100644 index 000000000000..c38317020e76 --- /dev/null +++ b/codec/legacy/amino_msg_test.go @@ -0,0 +1,38 @@ +package legacy_test + +import ( + "strings" + "testing" + + "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" + "github.com/cosmos/cosmos-sdk/testutil/testdata" + "github.com/stretchr/testify/require" +) + +func TestRegisterAminoMsg(t *testing.T) { + cdc := codec.NewLegacyAmino() + + testCases := map[string]struct { + msgName string + expPanic bool + }{ + "all good": { + msgName: "cosmos-sdk/Test", + }, + "msgName too long": { + msgName: strings.Repeat("a", 40), + expPanic: true, + }, + } + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { + fn := func() { legacy.RegisterAminoMsg(cdc, &testdata.TestMsg{}, tc.msgName) } + if tc.expPanic { + require.Panics(t, fn) + } else { + require.NotPanics(t, fn) + } + }) + } +} diff --git a/codec/legacy/doc.go b/codec/legacy/doc.go index d89944f3df42..f3976d871ffb 100644 --- a/codec/legacy/doc.go +++ b/codec/legacy/doc.go @@ -1,4 +1,6 @@ // Package legacy contains a global amino Cdc which is deprecated but // still used in several places within the SDK. This package is intended // to be removed at some point in the future when the global Cdc is removed. +// It also contains a util function RegisterAminoMsg that checks a msg name length +// before registering the concrete msg type with amino. package legacy diff --git a/x/auth/vesting/types/codec.go b/x/auth/vesting/types/codec.go index d3cad4ed3227..17764e6ef5c8 100644 --- a/x/auth/vesting/types/codec.go +++ b/x/auth/vesting/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -21,6 +22,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&PeriodicVestingAccount{}, "cosmos-sdk/PeriodicVestingAccount", nil) cdc.RegisterConcrete(&PermanentLockedAccount{}, "cosmos-sdk/PermanentLockedAccount", nil) cdc.RegisterConcrete(&ClawbackVestingAccount{}, "cosmos-sdk/ClawbackVestingAccount", nil) + legacy.RegisterAminoMsg(cdc, &MsgCreateVestingAccount{}, "cosmos-sdk/MsgCreateVestingAccount") } // RegisterInterface associates protoName with AccountI and VestingAccount diff --git a/x/authz/codec.go b/x/authz/codec.go index 65e67437049c..da8222c8ec45 100644 --- a/x/authz/codec.go +++ b/x/authz/codec.go @@ -2,6 +2,7 @@ package authz import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" types "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" @@ -11,9 +12,9 @@ import ( // RegisterLegacyAminoCodec registers the necessary x/authz interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgGrant{}, "cosmos-sdk/MsgGrant", nil) - cdc.RegisterConcrete(&MsgRevoke{}, "cosmos-sdk/MsgRevoke", nil) - cdc.RegisterConcrete(&MsgExec{}, "cosmos-sdk/MsgExec", nil) + legacy.RegisterAminoMsg(cdc, &MsgGrant{}, "cosmos-sdk/MsgGrant") + legacy.RegisterAminoMsg(cdc, &MsgRevoke{}, "cosmos-sdk/MsgRevoke") + legacy.RegisterAminoMsg(cdc, &MsgExec{}, "cosmos-sdk/MsgExec") cdc.RegisterInterface((*Authorization)(nil), nil) cdc.RegisterConcrete(&GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) diff --git a/x/bank/types/codec.go b/x/bank/types/codec.go index a29b48643515..ff595a3edbd4 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -2,19 +2,19 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/msgservice" "github.com/cosmos/cosmos-sdk/x/authz" - authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/bank interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgSend{}, "cosmos-sdk/MsgSend", nil) - cdc.RegisterConcrete(&MsgMultiSend{}, "cosmos-sdk/MsgMultiSend", nil) + legacy.RegisterAminoMsg(cdc, &MsgSend{}, "cosmos-sdk/MsgSend") + legacy.RegisterAminoMsg(cdc, &MsgMultiSend{}, "cosmos-sdk/MsgMultiSend") cdc.RegisterConcrete(&SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) } @@ -43,5 +43,5 @@ func init() { // Register all Amino interfaces and concrete types on the authz Amino codec so that this can later be // used to properly serialize MsgGrant and MsgExec instances - RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(legacy.Cdc) } diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index abf1c2c1a6d6..324ef56891ea 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -12,7 +13,7 @@ import ( // RegisterLegacyAminoCodec registers the necessary x/crisis interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgVerifyInvariant{}, "cosmos-sdk/MsgVerifyInvariant", nil) + legacy.RegisterAminoMsg(cdc, &MsgVerifyInvariant{}, "cosmos-sdk/MsgVerifyInvariant") } func RegisterInterfaces(registry codectypes.InterfaceRegistry) { diff --git a/x/distribution/types/codec.go b/x/distribution/types/codec.go index 22bf22ce1c58..3a17e473b6a5 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,10 +14,10 @@ import ( // RegisterLegacyAminoCodec registers the necessary x/distribution interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward", nil) - cdc.RegisterConcrete(&MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValidatorCommission", nil) - cdc.RegisterConcrete(&MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress", nil) - cdc.RegisterConcrete(&MsgFundCommunityPool{}, "cosmos-sdk/MsgFundCommunityPool", nil) + legacy.RegisterAminoMsg(cdc, &MsgWithdrawDelegatorReward{}, "cosmos-sdk/MsgWithdrawDelegationReward") + legacy.RegisterAminoMsg(cdc, &MsgWithdrawValidatorCommission{}, "cosmos-sdk/MsgWithdrawValCommission") + legacy.RegisterAminoMsg(cdc, &MsgSetWithdrawAddress{}, "cosmos-sdk/MsgModifyWithdrawAddress") + legacy.RegisterAminoMsg(cdc, &MsgFundCommunityPool{}, "cosmos-sdk/MsgFundCommunityPool") cdc.RegisterConcrete(&CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal", nil) } diff --git a/x/evidence/types/codec.go b/x/evidence/types/codec.go index 2dea1c40206c..19dcd12cbee9 100644 --- a/x/evidence/types/codec.go +++ b/x/evidence/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -14,7 +15,7 @@ import ( // evidence module. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterInterface((*exported.Evidence)(nil), nil) - cdc.RegisterConcrete(&MsgSubmitEvidence{}, "cosmos-sdk/MsgSubmitEvidence", nil) + legacy.RegisterAminoMsg(cdc, &MsgSubmitEvidence{}, "cosmos-sdk/MsgSubmitEvidence") cdc.RegisterConcrete(&Equivocation{}, "cosmos-sdk/Equivocation", nil) } diff --git a/x/feegrant/codec.go b/x/feegrant/codec.go index 1b6669673d07..0fd6eaeb9e7f 100644 --- a/x/feegrant/codec.go +++ b/x/feegrant/codec.go @@ -2,6 +2,7 @@ package feegrant import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -12,8 +13,8 @@ import ( // RegisterLegacyAminoCodec registers the necessary x/feegrant interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgGrantAllowance{}, "cosmos-sdk/MsgGrantAllowance", nil) - cdc.RegisterConcrete(&MsgRevokeAllowance{}, "cosmos-sdk/MsgRevokeAllowance", nil) + legacy.RegisterAminoMsg(cdc, &MsgGrantAllowance{}, "cosmos-sdk/MsgGrantAllowance") + legacy.RegisterAminoMsg(cdc, &MsgRevokeAllowance{}, "cosmos-sdk/MsgRevokeAllowance") cdc.RegisterInterface((*FeeAllowanceI)(nil), nil) cdc.RegisterConcrete(&BasicAllowance{}, "cosmos-sdk/BasicAllowance", nil) diff --git a/x/gov/types/codec.go b/x/gov/types/codec.go index e59e0ef49c66..33580813e349 100644 --- a/x/gov/types/codec.go +++ b/x/gov/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -12,10 +13,10 @@ import ( // governance module. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterInterface((*Content)(nil), nil) - cdc.RegisterConcrete(&MsgSubmitProposal{}, "cosmos-sdk/MsgSubmitProposal", nil) - cdc.RegisterConcrete(&MsgDeposit{}, "cosmos-sdk/MsgDeposit", nil) - cdc.RegisterConcrete(&MsgVote{}, "cosmos-sdk/MsgVote", nil) - cdc.RegisterConcrete(&MsgVoteWeighted{}, "cosmos-sdk/MsgVoteWeighted", nil) + legacy.RegisterAminoMsg(cdc, &MsgSubmitProposal{}, "cosmos-sdk/v1/MsgSubmitProposal") + legacy.RegisterAminoMsg(cdc, &MsgDeposit{}, "cosmos-sdk/v1/MsgDeposit") + legacy.RegisterAminoMsg(cdc, &MsgVote{}, "cosmos-sdk/v1/MsgVote") + legacy.RegisterAminoMsg(cdc, &MsgVoteWeighted{}, "cosmos-sdk/v1/MsgVoteWeighted") cdc.RegisterConcrete(&TextProposal{}, "cosmos-sdk/TextProposal", nil) } diff --git a/x/slashing/types/codec.go b/x/slashing/types/codec.go index dd6cfb345267..d1b54a969ee1 100644 --- a/x/slashing/types/codec.go +++ b/x/slashing/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -11,7 +12,7 @@ import ( // RegisterLegacyAminoCodec registers concrete types on LegacyAmino codec func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgUnjail{}, "cosmos-sdk/MsgUnjail", nil) + legacy.RegisterAminoMsg(cdc, &MsgUnjail{}, "cosmos-sdk/MsgUnjail") } func RegisterInterfaces(registry types.InterfaceRegistry) { diff --git a/x/staking/types/codec.go b/x/staking/types/codec.go index 4970eea73558..23ac6ddb29fd 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/cosmos/cosmos-sdk/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,11 +14,11 @@ import ( // RegisterLegacyAminoCodec registers the necessary x/staking interfaces and concrete types // on the provided LegacyAmino codec. These types are used for Amino JSON serialization. func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { - cdc.RegisterConcrete(&MsgCreateValidator{}, "cosmos-sdk/MsgCreateValidator", nil) - cdc.RegisterConcrete(&MsgEditValidator{}, "cosmos-sdk/MsgEditValidator", nil) - cdc.RegisterConcrete(&MsgDelegate{}, "cosmos-sdk/MsgDelegate", nil) - cdc.RegisterConcrete(&MsgUndelegate{}, "cosmos-sdk/MsgUndelegate", nil) - cdc.RegisterConcrete(&MsgBeginRedelegate{}, "cosmos-sdk/MsgBeginRedelegate", nil) + legacy.RegisterAminoMsg(cdc, &MsgCreateValidator{}, "cosmos-sdk/MsgCreateValidator") + legacy.RegisterAminoMsg(cdc, &MsgEditValidator{}, "cosmos-sdk/MsgEditValidator") + legacy.RegisterAminoMsg(cdc, &MsgDelegate{}, "cosmos-sdk/MsgDelegate") + legacy.RegisterAminoMsg(cdc, &MsgUndelegate{}, "cosmos-sdk/MsgUndelegate") + legacy.RegisterAminoMsg(cdc, &MsgBeginRedelegate{}, "cosmos-sdk/MsgBeginRedelegate") cdc.RegisterInterface((*isStakeAuthorization_Validators)(nil), nil) cdc.RegisterConcrete(&StakeAuthorization_AllowList{}, "cosmos-sdk/StakeAuthorization/AllowList", nil)