Skip to content

Commit

Permalink
revert: replace all ModuleCdc instances with legacy.Cdc (cosmos#11680)
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Oct 25, 2022
1 parent b51ec6f commit 0c2c96a
Show file tree
Hide file tree
Showing 29 changed files with 240 additions and 87 deletions.
8 changes: 4 additions & 4 deletions docs/core/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ Since the `MsgExec` message type can contain different messages instances, it is
add the following code inside the `init` method of their module's `codec.go` file:

```go
import "github.com/cosmos/cosmos-sdk/codec/legacy"
import authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"

init() {
// Register all Amino interfaces and concrete types on the global Amino codec so that this can later be
// used to properly serialize x/authz MsgExec instances
RegisterLegacyAminoCodec(legacy.Cdc)
// 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)
}
```

Expand Down
17 changes: 15 additions & 2 deletions x/auth/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ 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/x/auth/legacy/legacytx"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
)

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
Expand Down Expand Up @@ -37,6 +39,17 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(legacy.Cdc)
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// 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)
}
18 changes: 15 additions & 3 deletions x/auth/vesting/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ 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"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
)

// RegisterLegacyAminoCodec registers the vesting interfaces and concrete types on the
Expand All @@ -21,7 +22,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&PermanentLockedAccount{}, "cosmos-sdk/PermanentLockedAccount", nil)
}

// RegisterInterface associates protoName with AccountI and VestingAccount
// RegisterInterfaces associates protoName with AccountI and VestingAccount
// Interfaces and creates a registry of it's concrete implementations
func RegisterInterfaces(registry types.InterfaceRegistry) {
registry.RegisterInterface(
Expand Down Expand Up @@ -59,6 +60,17 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(legacy.Cdc)
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// 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)
}
4 changes: 1 addition & 3 deletions x/auth/vesting/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec/legacy"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -67,7 +65,7 @@ func (msg MsgCreateVestingAccount) ValidateBasic() error {
// GetSignBytes returns the bytes all expected signers must sign over for a
// MsgCreateVestingAccount.
func (msg MsgCreateVestingAccount) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners returns the expected signers for a MsgCreateVestingAccount.
Expand Down
6 changes: 3 additions & 3 deletions x/authz/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ 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"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/authz interfaces and concrete types
Expand Down Expand Up @@ -36,7 +36,7 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
msgservice.RegisterMsgServiceDesc(registry, MsgServiceDesc())
}
func init() {
// Register all Amino interfaces and concrete types on the global Amino codec so that this can later be
// 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(legacy.Cdc)
RegisterLegacyAminoCodec(authzcodec.Amino)
}
18 changes: 18 additions & 0 deletions x/authz/codec/cdc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package codec

import (
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
Amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(Amino)
)

func init() {
cryptocodec.RegisterCrypto(Amino)
codec.RegisterEvidences(Amino)
sdk.RegisterLegacyAminoCodec(Amino)
}
18 changes: 18 additions & 0 deletions x/authz/codec/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Package codec provides a singleton instance of Amino codec that should be used to register
any concrete type that can later be referenced inside a MsgGrant or MsgExec instance so that they
can be (de)serialized properly.
Amino types should be ideally registered inside this codec within the init function of each module's
codec.go file as follows:
func init() {
// ...
RegisterLegacyAminoCodec(authzcodec.Amino)
}
The codec instance is put inside this package and not the x/authz package in order to avoid any dependency cycle.
*/
package codec
9 changes: 5 additions & 4 deletions x/authz/msgs.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package authz

import (
"github.com/cosmos/cosmos-sdk/codec/legacy"
"time"

authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"

"github.com/gogo/protobuf/proto"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -80,7 +81,7 @@ func (msg MsgGrant) Route() string {

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgGrant) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msg))
}

// GetAuthorization returns the cache value from the MsgGrant.Authorization if present.
Expand Down Expand Up @@ -174,7 +175,7 @@ func (msg MsgRevoke) Route() string {

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgRevoke) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msg))
}

// NewMsgExec creates a new MsgExecAuthorized
Expand Down Expand Up @@ -246,5 +247,5 @@ func (msg MsgExec) Route() string {

// GetSignBytes implements the LegacyMsg.GetSignBytes method.
func (msg MsgExec) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(authzcodec.ModuleCdc.MustMarshalJSON(&msg))
}
9 changes: 4 additions & 5 deletions x/bank/simulation/operations_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simulation_test

import (
"github.com/cosmos/cosmos-sdk/codec/legacy"
"math/rand"
"testing"

Expand Down Expand Up @@ -80,7 +79,7 @@ func (suite *SimTestSuite) TestSimulateMsgSend() {
suite.Require().NoError(err)

var msg types.MsgSend
legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg)
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

suite.Require().True(operationMsg.OK)
suite.Require().Equal("65337742stake", msg.Amount.String())
Expand Down Expand Up @@ -109,7 +108,7 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSend() {
require.NoError(err)

var msg types.MsgMultiSend
legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg)
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

require.True(operationMsg.OK)
require.Len(msg.Inputs, 3)
Expand Down Expand Up @@ -146,7 +145,7 @@ func (suite *SimTestSuite) TestSimulateModuleAccountMsgSend() {
suite.Require().Error(err)

var msg types.MsgSend
legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg)
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

suite.Require().False(operationMsg.OK)
suite.Require().Equal(operationMsg.Comment, "invalid transfers")
Expand Down Expand Up @@ -175,7 +174,7 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSendToModuleAccount() {
suite.Require().Error(err)

var msg types.MsgMultiSend
legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg)
types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

suite.Require().False(operationMsg.OK) // sending tokens to a module account should fail
suite.Require().Equal(operationMsg.Comment, "invalid transfers")
Expand Down
16 changes: 14 additions & 2 deletions x/bank/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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
Expand All @@ -30,6 +31,17 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(legacy.Cdc)
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// 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)
}
5 changes: 2 additions & 3 deletions x/bank/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec/legacy"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -52,7 +51,7 @@ func (msg MsgSend) ValidateBasic() error {

// GetSignBytes Implements Msg.
func (msg MsgSend) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners Implements Msg.
Expand Down Expand Up @@ -94,7 +93,7 @@ func (msg MsgMultiSend) ValidateBasic() error {

// GetSignBytes Implements Msg.
func (msg MsgMultiSend) GetSignBytes() []byte {
return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg))
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg))
}

// GetSigners Implements Msg.
Expand Down
16 changes: 14 additions & 2 deletions x/crisis/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ 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"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/crisis interfaces and concrete types
Expand All @@ -22,6 +23,17 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

var (
amino = codec.NewLegacyAmino()
ModuleCdc = codec.NewAminoCodec(amino)
)

func init() {
RegisterLegacyAminoCodec(legacy.Cdc)
RegisterLegacyAminoCodec(amino)
cryptocodec.RegisterCrypto(amino)
sdk.RegisterLegacyAminoCodec(amino)

// 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)
}
3 changes: 1 addition & 2 deletions x/crisis/types/msgs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package types

import (
"github.com/cosmos/cosmos-sdk/codec/legacy"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -30,7 +29,7 @@ func (msg MsgVerifyInvariant) GetSigners() []sdk.AccAddress {

// GetSignBytes gets the sign bytes for the msg MsgVerifyInvariant
func (msg MsgVerifyInvariant) GetSignBytes() []byte {
bz := legacy.Cdc.MustMarshalJSON(&msg)
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

Expand Down
Loading

0 comments on commit 0c2c96a

Please sign in to comment.