From dd534663d2a69211aae81370ee7de1ad3526bd01 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Tue, 22 Feb 2022 12:03:01 +0100 Subject: [PATCH 1/7] fix: implement Amino serialization for x/authz and x/feegrant (cosmos-sdk #11224) * fix: Add RegisterLegacyAminoCodec for authz/feegrant * add module name * Fix GetSignByes, add tests * removed module names from registered messages to match other modules * added interfaces and concrete types registration * unseal amino instances to allow external grant and authorization registration * fixed messages tests * allow to register external types into authz modulecdc * use legacy.Cdc instead of ModuleCdc inside x/authz * move the legacy.Cdc initialization outside init function * added serialization docs * Update docs/core/encoding.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * added the Ledger specification * fixed tests Co-authored-by: Amaury M <1293565+amaurym@users.noreply.github.com> --- codec/legacy/codec.go | 3 +- docs/core/encoding.md | 279 +++++++++++++++++++++++ x/auth/types/codec.go | 13 ++ x/authz/authorization_grant.go | 9 +- x/authz/authorization_grant_test.go | 6 +- x/authz/codec.go | 20 +- x/authz/keeper/keeper.go | 6 +- x/authz/keeper/keeper_test.go | 16 +- x/authz/module/module.go | 4 +- x/authz/msgs_test.go | 64 ++++++ x/authz/simulation/decoder_test.go | 3 +- x/authz/simulation/operations_test.go | 6 +- x/bank/types/codec.go | 6 + x/crisis/types/codec.go | 5 + x/distribution/types/codec.go | 5 + x/evidence/types/codec.go | 5 + x/feegrant/codec.go | 34 +++ x/feegrant/module/module.go | 1 + x/feegrant/msgs.go | 5 +- x/feegrant/msgs_test.go | 29 +++ x/feegrant/simulation/operations_test.go | 4 +- x/gov/types/codec.go | 5 + x/slashing/types/codec.go | 5 + x/staking/types/codec.go | 10 + 24 files changed, 512 insertions(+), 31 deletions(-) create mode 100644 docs/core/encoding.md diff --git a/codec/legacy/codec.go b/codec/legacy/codec.go index 4b2ec9595c..0a73b6cc66 100644 --- a/codec/legacy/codec.go +++ b/codec/legacy/codec.go @@ -10,10 +10,9 @@ import ( // has all Tendermint crypto and evidence types registered. // // TODO: Deprecated - remove this global. -var Cdc *codec.LegacyAmino +var Cdc = codec.NewLegacyAmino() func init() { - Cdc = codec.NewLegacyAmino() cryptocodec.RegisterCrypto(Cdc) codec.RegisterEvidences(Cdc) } diff --git a/docs/core/encoding.md b/docs/core/encoding.md new file mode 100644 index 0000000000..2d08f05f8c --- /dev/null +++ b/docs/core/encoding.md @@ -0,0 +1,279 @@ + + +# Encoding + +While encoding in the Cosmos SDK used to be mainly handled by `go-amino` codec, the Cosmos SDK is moving towards using `gogoprotobuf` for both state and client-side encoding. {synopsis} + +## Pre-requisite Readings + +* [Anatomy of a Cosmos SDK application](../basics/app-anatomy.md) {prereq} + +## Encoding + +The Cosmos SDK utilizes two binary wire encoding protocols, [Amino](https://github.com/tendermint/go-amino/) which is an object encoding specification and [Protocol Buffers](https://developers.google.com/protocol-buffers), a subset of Proto3 with an extension for +interface support. See the [Proto3 spec](https://developers.google.com/protocol-buffers/docs/proto3) +for more information on Proto3, which Amino is largely compatible with (but not with Proto2). + +Due to Amino having significant performance drawbacks, being reflection-based, and +not having any meaningful cross-language/client support, Protocol Buffers, specifically +[gogoprotobuf](https://github.com/gogo/protobuf/), is being used in place of Amino. +Note, this process of using Protocol Buffers over Amino is still an ongoing process. + +Binary wire encoding of types in the Cosmos SDK can be broken down into two main +categories, client encoding and store encoding. Client encoding mainly revolves +around transaction processing and signing, whereas store encoding revolves around +types used in state-machine transitions and what is ultimately stored in the Merkle +tree. + +For store encoding, protobuf definitions can exist for any type and will typically +have an Amino-based "intermediary" type. Specifically, the protobuf-based type +definition is used for serialization and persistence, whereas the Amino-based type +is used for business logic in the state-machine where they may converted back-n-forth. +Note, the Amino-based types may slowly be phased-out in the future so developers +should take note to use the protobuf message definitions where possible. + +In the `codec` package, there exists two core interfaces, `Marshaler` and `ProtoMarshaler`, +where the former encapsulates the current Amino interface except it operates on +types implementing the latter instead of generic `interface{}` types. + +In addition, there exists two implementations of `Marshaler`. The first being +`AminoCodec`, where both binary and JSON serialization is handled via Amino. The +second being `ProtoCodec`, where both binary and JSON serialization is handled +via Protobuf. + +This means that modules may use Amino or Protobuf encoding but the types must +implement `ProtoMarshaler`. If modules wish to avoid implementing this interface +for their types, they may use an Amino codec directly. + +### Amino + +Every module uses an Amino codec to serialize types and interfaces. This codec typically +has types and interfaces registered in that module's domain only (e.g. messages), +but there are exceptions like `x/gov`. Each module exposes a `RegisterLegacyAminoCodec` function +that allows a user to provide a codec and have all the types registered. An application +will call this method for each necessary module. + +Where there is no protobuf-based type definition for a module (see below), Amino +is used to encode and decode raw wire bytes to the concrete type or interface: + +```go +bz := keeper.cdc.MustMarshal(typeOrInterface) +keeper.cdc.MustUnmarshal(bz, &typeOrInterface) +``` + +Note, there are length-prefixed variants of the above functionality and this is +typically used for when the data needs to be streamed or grouped together +(e.g. `ResponseDeliverTx.Data`) + +#### Authz authorizations + +Since the `MsgExec` message type can contain different messages instances, it is important that developers +add the following code inside the `init` method of their module's `codec.go` file: + +```go +import "github.com/cosmos/cosmos-sdk/codec/legacy" + +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) +} +``` + +This will allow the `x/authz` module to properly serialize and de-serializes `MsgExec` instances using Amino, +which is required when signing this kind of messages using a Ledger. + +### Gogoproto + +Modules are encouraged to utilize Protobuf encoding for their respective types. In the Cosmos SDK, we use the [Gogoproto](https://github.com/gogo/protobuf) specific implementation of the Protobuf spec that offers speed and DX improvements compared to the official [Google protobuf implementation](https://github.com/protocolbuffers/protobuf). + +### Guidelines for protobuf message definitions + +In addition to [following official Protocol Buffer guidelines](https://developers.google.com/protocol-buffers/docs/proto3#simple), we recommend using these annotations in .proto files when dealing with interfaces: + +* use `cosmos_proto.accepts_interface` to annote fields that accept interfaces +* pass the same fully qualified name as `protoName` to `InterfaceRegistry.RegisterInterface` +* annotate interface implementations with `cosmos_proto.implements_interface` +* pass the same fully qualified name as `protoName` to `InterfaceRegistry.RegisterInterface` + +### Transaction Encoding + +Another important use of Protobuf is the encoding and decoding of +[transactions](./transactions.md). Transactions are defined by the application or +the Cosmos SDK but are then passed to the underlying consensus engine to be relayed to +other peers. Since the underlying consensus engine is agnostic to the application, +the consensus engine accepts only transactions in the form of raw bytes. + +* The `TxEncoder` object performs the encoding. +* The `TxDecoder` object performs the decoding. + ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0-rc4/types/tx_msg.go#L83-L87 + +A standard implementation of both these objects can be found in the [`auth` module](../../x/auth/spec/README.md): + ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0-rc4/x/auth/tx/decoder.go + ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0-rc4/x/auth/tx/encoder.go + +See [ADR-020](../architecture/adr-020-protobuf-transaction-encoding.md) for details of how a transaction is encoded. + +### Interface Encoding and Usage of `Any` + +The Protobuf DSL is strongly typed, which can make inserting variable-typed fields difficult. Imagine we want to create a `Profile` protobuf message that serves as a wrapper over [an account](../basics/accounts.md): + +```proto +message Profile { + // account is the account associated to a profile. + cosmos.auth.v1beta1.BaseAccount account = 1; + // bio is a short description of the account. + string bio = 4; +} +``` + +In this `Profile` example, we hardcoded `account` as a `BaseAccount`. However, there are several other types of [user accounts related to vesting](../../x/auth/spec/05_vesting.md), such as `BaseVestingAccount` or `ContinuousVestingAccount`. All of these accounts are different, but they all implement the `AccountI` interface. How would you create a `Profile` that allows all these types of accounts with an `account` field that accepts an `AccountI` interface? + ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.42.1/x/auth/types/account.go#L307-L330 + +In [ADR-019](../architecture/adr-019-protobuf-state-encoding.md), it has been decided to use [`Any`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto)s to encode interfaces in protobuf. An `Any` contains an arbitrary serialized message as bytes, along with a URL that acts as a globally unique identifier for and resolves to that message's type. This strategy allows us to pack arbitrary Go types inside protobuf messages. Our new `Profile` then looks like: + +```protobuf +message Profile { + // account is the account associated to a profile. + google.protobuf.Any account = 1 [ + (cosmos_proto.accepts_interface) = "AccountI"; // Asserts that this field only accepts Go types implementing `AccountI`. It is purely informational for now. + ]; + // bio is a short description of the account. + string bio = 4; +} +``` + +To add an account inside a profile, we need to "pack" it inside an `Any` first, using `codectypes.NewAnyWithValue`: + +```go +var myAccount AccountI +myAccount = ... // Can be a BaseAccount, a ContinuousVestingAccount or any struct implementing `AccountI` + +// Pack the account into an Any +accAny, err := codectypes.NewAnyWithValue(myAccount) +if err != nil { + return nil, err +} + +// Create a new Profile with the any. +profile := Profile { + Account: accAny, + Bio: "some bio", +} + +// We can then marshal the profile as usual. +bz, err := cdc.Marshal(profile) +jsonBz, err := cdc.MarshalJSON(profile) +``` + +To summarize, to encode an interface, you must 1/ pack the interface into an `Any` and 2/ marshal the `Any`. For convenience, the Cosmos SDK provides a `MarshalInterface` method to bundle these two steps. Have a look at [a real-life example in the x/auth module](https://github.com/cosmos/cosmos-sdk/blob/v0.42.1/x/auth/keeper/keeper.go#L218-L221). + +The reverse operation of retrieving the concrete Go type from inside an `Any`, called "unpacking", is done with the `GetCachedValue()` on `Any`. + +```go +profileBz := ... // The proto-encoded bytes of a Profile, e.g. retrieved through gRPC. +var myProfile Profile +// Unmarshal the bytes into the myProfile struct. +err := cdc.Unmarshal(profilebz, &myProfile) + +// Let's see the types of the Account field. +fmt.Printf("%T\n", myProfile.Account) // Prints "Any" +fmt.Printf("%T\n", myProfile.Account.GetCachedValue()) // Prints "BaseAccount", "ContinuousVestingAccount" or whatever was initially packed in the Any. + +// Get the address of the accountt. +accAddr := myProfile.Account.GetCachedValue().(AccountI).GetAddress() +``` + +It is important to note that for `GetCachedValue()` to work, `Profile` (and any other structs embedding `Profile`) must implement the `UnpackInterfaces` method: + +```go +func (p *Profile) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { + if p.Account != nil { + var account AccountI + return unpacker.UnpackAny(p.Account, &account) + } + + return nil +} +``` + +The `UnpackInterfaces` gets called recursively on all structs implementing this method, to allow all `Any`s to have their `GetCachedValue()` correctly populated. + +For more information about interface encoding, and especially on `UnpackInterfaces` and how the `Any`'s `type_url` gets resolved using the `InterfaceRegistry`, please refer to [ADR-019](../architecture/adr-019-protobuf-state-encoding.md). + +#### `Any` Encoding in the Cosmos SDK + +The above `Profile` example is a fictive example used for educational purposes. In the Cosmos SDK, we use `Any` encoding in several places (non-exhaustive list): + +* the `cryptotypes.PubKey` interface for encoding different types of public keys, +* the `sdk.Msg` interface for encoding different `Msg`s in a transaction, +* the `AccountI` interface for encodinig different types of accounts (similar to the above example) in the x/auth query responses, +* the `Evidencei` interface for encoding different types of evidences in the x/evidence module, +* the `AuthorizationI` interface for encoding different types of x/authz authorizations, +* the [`Validator`](https://github.com/cosmos/cosmos-sdk/blob/v0.42.5/x/staking/types/staking.pb.go#L306-L337) struct that contains information about a validator. + +A real-life example of encoding the pubkey as `Any` inside the Validator struct in x/staking is shown in the following example: + ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.42.1/x/staking/types/validator.go#L40-L61 + +## FAQ + +### How to create modules using protobuf encoding + +#### Defining module types + +Protobuf types can be defined to encode: + +* state +* [`Msg`s](../building-modules/messages-and-queries.md#messages) +* [Query services](../building-modules/query-services.md) +* [genesis](../building-modules/genesis.md) + +#### Naming and conventions + +We encourage developers to follow industry guidelines: [Protocol Buffers style guide](https://developers.google.com/protocol-buffers/docs/style) +and [Buf](https://buf.build/docs/style-guide), see more details in [ADR 023](../architecture/adr-023-protobuf-naming.md) + +### How to update modules to protobuf encoding + +If modules do not contain any interfaces (e.g. `Account` or `Content`), then they +may simply migrate any existing types that +are encoded and persisted via their concrete Amino codec to Protobuf (see 1. for further guidelines) and accept a `Marshaler` as the codec which is implemented via the `ProtoCodec` +without any further customization. + +However, if a module type composes an interface, it must wrap it in the `skd.Any` (from `/types` package) type. To do that, a module-level .proto file must use [`google.protobuf.Any`](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/any.proto) for respective message type interface types. + +For example, in the `x/evidence` module defines an `Evidence` interface, which is used by the `MsgSubmitEvidence`. The structure definition must use `sdk.Any` to wrap the evidence file. In the proto file we define it as follows: + +```protobuf +// proto/cosmos/evidence/v1beta1/tx.proto + +message MsgSubmitEvidence { + string submitter = 1; + google.protobuf.Any evidence = 2 [(cosmos_proto.accepts_interface) = "Evidence"]; +} +``` + +The Cosmos SDK `codec.Codec` interface provides support methods `MarshalInterface` and `UnmarshalInterface` to easy encoding of state to `Any`. + +Module should register interfaces using `InterfaceRegistry` which provides a mechanism for registering interfaces: `RegisterInterface(protoName string, iface interface{})` and implementations: `RegisterImplementations(iface interface{}, impls ...proto.Message)` that can be safely unpacked from Any, similarly to type registration with Amino: + ++++ https://github.com/cosmos/cosmos-sdk/blob/v0.40.0-rc4/codec/types/interface_registry.go#L25-L66 + +In addition, an `UnpackInterfaces` phase should be introduced to deserialization to unpack interfaces before they're needed. Protobuf types that contain a protobuf `Any` either directly or via one of their members should implement the `UnpackInterfacesMessage` interface: + +```go +type UnpackInterfacesMessage interface { + UnpackInterfaces(InterfaceUnpacker) error +} +``` + +## Next {hide} + +Learn about [gRPC, REST and other endpoints](./grpc_rest.md) {hide} diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index dbd30ff2ba..470e6732da 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -1,12 +1,21 @@ package types import ( + //<<<<<<< HEAD "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/auth/legacy/legacytx" + //======= + // "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" + // "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" + //>>>>>>> 81cfc6cc85 (fix: implement Amino serialization for x/authz and x/feegrant (#11224)) ) // RegisterLegacyAminoCodec registers the account interfaces and concrete types on the @@ -53,4 +62,8 @@ var ( func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) + + // 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) } diff --git a/x/authz/authorization_grant.go b/x/authz/authorization_grant.go index c69be5d0b9..185696eb23 100644 --- a/x/authz/authorization_grant.go +++ b/x/authz/authorization_grant.go @@ -10,11 +10,10 @@ import ( ) // NewGrant returns new Grant -func NewGrant( /*blockTime time.Time, */ a Authorization, expiration time.Time) (Grant, error) { - // TODO: add this for 0.45 - // if !expiration.After(blockTime) { - // return Grant{}, sdkerrors.ErrInvalidRequest.Wrapf("expiration must be after the current block time (%v), got %v", blockTime.Format(time.RFC3339), expiration.Format(time.RFC3339)) - // } +func NewGrant(blockTime time.Time, a Authorization, expiration time.Time) (Grant, error) { + if !expiration.After(blockTime) { + return Grant{}, sdkerrors.ErrInvalidRequest.Wrapf("expiration must be after the current block time (%v), got %v", blockTime.Format(time.RFC3339), expiration.Format(time.RFC3339)) + } g := Grant{ Expiration: expiration, } diff --git a/x/authz/authorization_grant_test.go b/x/authz/authorization_grant_test.go index 4f36890a9a..dc9cc03ba4 100644 --- a/x/authz/authorization_grant_test.go +++ b/x/authz/authorization_grant_test.go @@ -27,8 +27,8 @@ func TestNewGrant(t *testing.T) { expire time.Time err string }{ - // {"wrong expire time (1)", a, time.Unix(10, 0), time.Unix(8, 0), "expiration must be after"}, - // {"wrong expire time (2)", a, time.Unix(10, 0), time.Unix(10, 0), "expiration must be after"}, + {"wrong expire time (1)", a, time.Unix(10, 0), time.Unix(8, 0), "expiration must be after"}, + {"wrong expire time (2)", a, time.Unix(10, 0), time.Unix(10, 0), "expiration must be after"}, {"good expire time (1)", a, time.Unix(10, 0), time.Unix(10, 1), ""}, {"good expire time (2)", a, time.Unix(10, 0), time.Unix(11, 0), ""}, } @@ -36,7 +36,7 @@ func TestNewGrant(t *testing.T) { for _, tc := range tcs { t.Run(tc.title, func(t *testing.T) { // _, err := NewGrant(tc.blockTime, tc.a, tc.expire) - _, err := NewGrant(tc.a, tc.expire) + _, err := NewGrant(tc.blockTime, tc.a, tc.expire) expecError(require.New(t), tc.err, err) }) } diff --git a/x/authz/codec.go b/x/authz/codec.go index 7ae5b715c1..e72ade1c6d 100644 --- a/x/authz/codec.go +++ b/x/authz/codec.go @@ -1,11 +1,24 @@ package authz import ( - types "github.com/line/lbm-sdk/codec/types" + "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" + "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" ) +// 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) + + cdc.RegisterInterface((*Authorization)(nil), nil) + cdc.RegisterConcrete(&GenericAuthorization{}, "cosmos-sdk/GenericAuthorization", nil) +} + // RegisterInterfaces registers the interfaces types with the interface registry func RegisterInterfaces(registry types.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), @@ -22,3 +35,8 @@ 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 + // used to properly serialize MsgGrant and MsgExec instances + RegisterLegacyAminoCodec(legacy.Cdc) +} diff --git a/x/authz/keeper/keeper.go b/x/authz/keeper/keeper.go index 0558547df0..50dffaaaf8 100644 --- a/x/authz/keeper/keeper.go +++ b/x/authz/keeper/keeper.go @@ -132,7 +132,7 @@ func (k Keeper) DispatchActions(ctx sdk.Context, grantee sdk.AccAddress, msgs [] func (k Keeper) SaveGrant(ctx sdk.Context, grantee, granter sdk.AccAddress, authorization authz.Authorization, expiration time.Time) error { store := ctx.KVStore(k.storeKey) - grant, err := authz.NewGrant(authorization, expiration) + grant, err := authz.NewGrant(ctx.BlockTime(), authorization, expiration) if err != nil { return err } @@ -233,6 +233,10 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *authz.GenesisState { // InitGenesis new authz genesis func (k Keeper) InitGenesis(ctx sdk.Context, data *authz.GenesisState) { for _, entry := range data.Authorization { + if entry.Expiration.Before(ctx.BlockTime()) { + continue + } + grantee := sdk.MustAccAddressFromBech32(entry.Grantee) granter := sdk.MustAccAddressFromBech32(entry.Granter) a, ok := entry.Authorization.GetCachedValue().(authz.Authorization) diff --git a/x/authz/keeper/keeper_test.go b/x/authz/keeper/keeper_test.go index 29ff25e656..38124e96a8 100644 --- a/x/authz/keeper/keeper_test.go +++ b/x/authz/keeper/keeper_test.go @@ -61,7 +61,7 @@ func (s *TestSuite) TestKeeper() { s.T().Log("verify if expired authorization is rejected") x := &banktypes.SendAuthorization{SpendLimit: newCoins} err := app.AuthzKeeper.SaveGrant(ctx, granterAddr, granteeAddr, x, now.Add(-1*time.Hour)) - s.Require().NoError(err) + s.Require().Error(err) authorization, _ = app.AuthzKeeper.GetCleanAuthorization(ctx, granteeAddr, granterAddr, bankSendAuthMsgType) s.Require().Nil(authorization) @@ -105,14 +105,14 @@ func (s *TestSuite) TestKeeperIter() { authorization, expiration := app.AuthzKeeper.GetCleanAuthorization(ctx, granteeAddr, granterAddr, "Abcd") s.Require().Nil(authorization) s.Require().Equal(time.Time{}, expiration) - now := s.ctx.BlockHeader().Time + now := s.ctx.BlockHeader().Time.Add(time.Second) s.Require().NotNil(now) newCoins := sdk.NewCoins(sdk.NewInt64Coin("steak", 100)) s.T().Log("verify if expired authorization is rejected") x := &banktypes.SendAuthorization{SpendLimit: newCoins} err := app.AuthzKeeper.SaveGrant(ctx, granteeAddr, granterAddr, x, now.Add(-1*time.Hour)) - s.Require().NoError(err) + s.Require().Error(err) authorization, _ = app.AuthzKeeper.GetCleanAuthorization(ctx, granteeAddr, granterAddr, "abcd") s.Require().Nil(authorization) @@ -131,8 +131,7 @@ func (s *TestSuite) TestKeeperFees() { granteeAddr := addrs[1] recipientAddr := addrs[2] s.Require().NoError(simapp.FundAccount(app, s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - now := s.ctx.BlockHeader().Time - s.Require().NotNil(now) + expiration := s.ctx.BlockHeader().Time.Add(1 * time.Second) smallCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 20)) someCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 123)) @@ -157,7 +156,7 @@ func (s *TestSuite) TestKeeperFees() { s.T().Log("verify dispatch executes with correct information") // grant authorization - err = app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, &banktypes.SendAuthorization{SpendLimit: smallCoin}, now) + err = app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, &banktypes.SendAuthorization{SpendLimit: smallCoin}, expiration) s.Require().NoError(err) authorization, _ := app.AuthzKeeper.GetCleanAuthorization(s.ctx, granteeAddr, granterAddr, bankSendAuthMsgType) s.Require().NotNil(authorization) @@ -206,8 +205,7 @@ func (s *TestSuite) TestDispatchedEvents() { granteeAddr := addrs[1] recipientAddr := addrs[2] require.NoError(simapp.FundAccount(app, s.ctx, granterAddr, sdk.NewCoins(sdk.NewInt64Coin("steak", 10000)))) - now := s.ctx.BlockHeader().Time - require.NotNil(now) + expiration := s.ctx.BlockHeader().Time.Add(1 * time.Second) // must be in the future smallCoin := sdk.NewCoins(sdk.NewInt64Coin("steak", 20)) msgs := authz.NewMsgExec(granteeAddr, []sdk.Msg{ @@ -219,7 +217,7 @@ func (s *TestSuite) TestDispatchedEvents() { }) // grant authorization - err := app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, &banktypes.SendAuthorization{SpendLimit: smallCoin}, now) + err := app.AuthzKeeper.SaveGrant(s.ctx, granteeAddr, granterAddr, &banktypes.SendAuthorization{SpendLimit: smallCoin}, expiration) require.NoError(err) authorization, _ := app.AuthzKeeper.GetCleanAuthorization(s.ctx, granteeAddr, granterAddr, bankSendAuthMsgType) require.NotNil(authorization) diff --git a/x/authz/module/module.go b/x/authz/module/module.go index 2f21a2dae7..2f728da7c8 100644 --- a/x/authz/module/module.go +++ b/x/authz/module/module.go @@ -48,7 +48,9 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { } // RegisterLegacyAminoCodec registers the authz module's types for the given codec. -func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {} +func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + authz.RegisterLegacyAminoCodec(cdc) +} // RegisterInterfaces registers the authz module's interface types func (AppModuleBasic) RegisterInterfaces(registry cdctypes.InterfaceRegistry) { diff --git a/x/authz/msgs_test.go b/x/authz/msgs_test.go index a7006d11b5..5b748858f5 100644 --- a/x/authz/msgs_test.go +++ b/x/authz/msgs_test.go @@ -8,8 +8,10 @@ import ( cdctypes "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" + "github.com/line/lbm-sdk/x/auth/legacy/legacytx" "github.com/line/lbm-sdk/x/authz" banktypes "github.com/line/lbm-sdk/x/bank/types" + stakingtypes "github.com/line/lbm-sdk/x/staking/types" ) var ( @@ -115,3 +117,65 @@ func TestMsgGrantGetAuthorization(t *testing.T) { m.SetAuthorization(&g) require.Equal(m.GetAuthorization(), &g) } + +func TestAminoJSON(t *testing.T) { + tx := legacytx.StdTx{} + var msg legacytx.LegacyMsg + someDate := time.Date(1, 1, 1, 1, 1, 1, 1, time.UTC) + msgSend := banktypes.MsgSend{FromAddress: "cosmos1ghi", ToAddress: "cosmos1jkl"} + typeURL := sdk.MsgTypeURL(&msgSend) + msgSendAny, err := cdctypes.NewAnyWithValue(&msgSend) + require.NoError(t, err) + grant, err := authz.NewGrant(someDate, authz.NewGenericAuthorization(typeURL), someDate.Add(time.Hour)) + require.NoError(t, err) + sendGrant, err := authz.NewGrant(someDate, banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(1000)))), someDate.Add(time.Hour)) + require.NoError(t, err) + valAddr, err := sdk.ValAddressFromBech32("linkvaloper1hcnhauxt4crgz04zwvdl60a9pk9wzzqe8uelc0") + require.NoError(t, err) + stakingAuth, err := stakingtypes.NewStakeAuthorization([]sdk.ValAddress{valAddr}, nil, stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE, &sdk.Coin{Denom: "stake", Amount: sdk.NewInt(1000)}) + require.NoError(t, err) + delegateGrant, err := authz.NewGrant(someDate, stakingAuth, someDate.Add(time.Hour)) + require.NoError(t, err) + + // Amino JSON encoding has changed in authz since v0.46. + // Before, it was outputting something like: + // `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"grant":{"authorization":{"msg":"/cosmos.bank.v1beta1.MsgSend"},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}],"sequence":"1","timeout_height":"1"}` + // + // This was a bug. Now, it's as below, See how there's `type` & `value` fields. + // ref: https://github.com/cosmos/cosmos-sdk/issues/11190 + // ref: https://github.com/cosmos/cosmjs/issues/1026 + msg = &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: grant} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/GenericAuthorization","value":{"msg":"/cosmos.bank.v1beta1.MsgSend"}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) + + msg = &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: sendGrant} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/SendAuthorization","value":{"spend_limit":[{"amount":"1000","denom":"stake"}]}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) + + msg = &authz.MsgGrant{Granter: "cosmos1abc", Grantee: "cosmos1def", Grant: delegateGrant} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrant","value":{"grant":{"authorization":{"type":"cosmos-sdk/StakeAuthorization","value":{"Validators":{"type":"cosmos-sdk/StakeAuthorization/AllowList","value":{"allow_list":{"address":["linkvaloper1hcnhauxt4crgz04zwvdl60a9pk9wzzqe8uelc0"]}}},"authorization_type":1,"max_tokens":{"amount":"1000","denom":"stake"}}},"expiration":"0001-01-01T02:01:01.000000001Z"},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) + + msg = &authz.MsgRevoke{Granter: "cosmos1abc", Grantee: "cosmos1def", MsgTypeUrl: typeURL} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevoke","value":{"grantee":"cosmos1def","granter":"cosmos1abc","msg_type_url":"/cosmos.bank.v1beta1.MsgSend"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) + + msg = &authz.MsgExec{Grantee: "cosmos1def", Msgs: []*cdctypes.Any{msgSendAny}} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgExec","value":{"grantee":"cosmos1def","msgs":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[],"from_address":"cosmos1ghi","to_address":"cosmos1jkl"}}]}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) +} diff --git a/x/authz/simulation/decoder_test.go b/x/authz/simulation/decoder_test.go index 8236aa7578..0a2322c55c 100644 --- a/x/authz/simulation/decoder_test.go +++ b/x/authz/simulation/decoder_test.go @@ -20,7 +20,8 @@ func TestDecodeStore(t *testing.T) { cdc := simapp.MakeTestEncodingConfig().Marshaler dec := simulation.NewDecodeStore(cdc) - grant, _ := authz.NewGrant(banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewInt64Coin("foo", 123))), time.Now().UTC()) + now := time.Now().UTC() + grant, _ := authz.NewGrant(now, banktypes.NewSendAuthorization(sdk.NewCoins(sdk.NewInt64Coin("foo", 123))), now.Add(1)) grantBz, err := cdc.Marshal(&grant) require.NoError(t, err) kvPairs := kv.Pairs{ diff --git a/x/authz/simulation/operations_test.go b/x/authz/simulation/operations_test.go index d2c8d7d602..0bdc841e9f 100644 --- a/x/authz/simulation/operations_test.go +++ b/x/authz/simulation/operations_test.go @@ -106,7 +106,7 @@ func (suite *SimTestSuite) TestSimulateGrant() { suite.Require().NoError(err) var msg authz.MsgGrant - suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg) + suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal(granter.Address.String(), msg.Granter) suite.Require().Equal(grantee.Address.String(), msg.Grantee) @@ -143,7 +143,7 @@ func (suite *SimTestSuite) TestSimulateRevoke() { suite.Require().NoError(err) var msg authz.MsgRevoke - suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg) + suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal(granter.Address.String(), msg.Granter) @@ -179,7 +179,7 @@ func (suite *SimTestSuite) TestSimulateExec() { var msg authz.MsgExec - suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg) + suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal(grantee.Address.String(), msg.Grantee) diff --git a/x/bank/types/codec.go b/x/bank/types/codec.go index 590ab38225..914d404ba0 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -14,6 +15,7 @@ import ( func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&MsgSend{}, "cosmos-sdk/MsgSend", nil) cdc.RegisterConcrete(&MsgMultiSend{}, "cosmos-sdk/MsgMultiSend", nil) + cdc.RegisterConcrete(&SendAuthorization{}, "cosmos-sdk/SendAuthorization", nil) } func RegisterInterfaces(registry types.InterfaceRegistry) { @@ -45,4 +47,8 @@ func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) amino.Seal() + + // 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) } diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index 57e1646c87..43babee50c 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" codectypes "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -38,4 +39,8 @@ func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) amino.Seal() + + // 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) } diff --git a/x/distribution/types/codec.go b/x/distribution/types/codec.go index 00567d3089..cdadb9ea7c 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -51,4 +52,8 @@ func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) amino.Seal() + + // 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) } diff --git a/x/evidence/types/codec.go b/x/evidence/types/codec.go index dfb4b47b9c..ed73a736aa 100644 --- a/x/evidence/types/codec.go +++ b/x/evidence/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -44,4 +45,8 @@ func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) amino.Seal() + + // 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) } diff --git a/x/feegrant/codec.go b/x/feegrant/codec.go index 3f77ca02bb..1e61455a84 100644 --- a/x/feegrant/codec.go +++ b/x/feegrant/codec.go @@ -1,11 +1,25 @@ package feegrant import ( + "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" ) +// 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) + + cdc.RegisterInterface((*FeeAllowanceI)(nil), nil) + cdc.RegisterConcrete(&BasicAllowance{}, "cosmos-sdk/BasicAllowance", nil) + cdc.RegisterConcrete(&PeriodicAllowance{}, "cosmos-sdk/PeriodicAllowance", nil) + cdc.RegisterConcrete(&AllowedMsgAllowance{}, "cosmos-sdk/AllowedMsgAllowance", nil) +} + // RegisterInterfaces registers the interfaces types with the interface registry func RegisterInterfaces(registry types.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), @@ -23,3 +37,23 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } + +var ( + amino = codec.NewLegacyAmino() + + // ModuleCdc references the global x/feegrant 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/feegrant and + // defined at the application level. + ModuleCdc = codec.NewAminoCodec(amino) +) + +func init() { + RegisterLegacyAminoCodec(amino) + + // 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) +} diff --git a/x/feegrant/module/module.go b/x/feegrant/module/module.go index 67560e6589..59e62e4d3f 100644 --- a/x/feegrant/module/module.go +++ b/x/feegrant/module/module.go @@ -52,6 +52,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) { // RegisterLegacyAminoCodec registers the feegrant module's types for the given codec. func (AppModuleBasic) RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { + feegrant.RegisterLegacyAminoCodec(cdc) } // RegisterInterfaces registers the feegrant module's interface types diff --git a/x/feegrant/msgs.go b/x/feegrant/msgs.go index bbbcdcf678..ab31571852 100644 --- a/x/feegrant/msgs.go +++ b/x/feegrant/msgs.go @@ -3,7 +3,6 @@ package feegrant import ( "github.com/gogo/protobuf/proto" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" @@ -77,7 +76,7 @@ func (msg MsgGrantAllowance) Route() string { // GetSignBytes implements the LegacyMsg.GetSignBytes method. func (msg MsgGrantAllowance) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } // GetFeeAllowanceI returns unpacked FeeAllowance @@ -140,5 +139,5 @@ func (msg MsgRevokeAllowance) Route() string { // GetSignBytes implements the LegacyMsg.GetSignBytes method. func (msg MsgRevokeAllowance) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } diff --git a/x/feegrant/msgs_test.go b/x/feegrant/msgs_test.go index 1a2e3f2c46..7a566ecc7b 100644 --- a/x/feegrant/msgs_test.go +++ b/x/feegrant/msgs_test.go @@ -9,6 +9,7 @@ import ( "github.com/line/lbm-sdk/codec" codectypes "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" + "github.com/line/lbm-sdk/x/auth/legacy/legacytx" "github.com/line/lbm-sdk/x/feegrant" ) @@ -132,3 +133,31 @@ func TestMsgRevokeAllowance(t *testing.T) { } } } + +func TestAminoJSON(t *testing.T) { + tx := legacytx.StdTx{} + var msg legacytx.LegacyMsg + allowanceAny, err := codectypes.NewAnyWithValue(&feegrant.BasicAllowance{SpendLimit: sdk.NewCoins(sdk.NewCoin("foo", sdk.NewInt(100)))}) + require.NoError(t, err) + + // Amino JSON encoding has changed in feegrant since v0.46. + // Before, it was outputting something like: + // `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"allowance":{"spend_limit":[{"amount":"100","denom":"foo"}]},"grantee":"cosmos1def","granter":"cosmos1abc"}],"sequence":"1","timeout_height":"1"}` + // + // This was a bug. Now, it's as below, See how there's `type` & `value` fields. + // ref: https://github.com/cosmos/cosmos-sdk/issues/11190 + // ref: https://github.com/cosmos/cosmjs/issues/1026 + msg = &feegrant.MsgGrantAllowance{Granter: "cosmos1abc", Grantee: "cosmos1def", Allowance: allowanceAny} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgGrantAllowance","value":{"allowance":{"type":"cosmos-sdk/BasicAllowance","value":{"spend_limit":[{"amount":"100","denom":"foo"}]}},"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) + + msg = &feegrant.MsgRevokeAllowance{Granter: "cosmos1abc", Grantee: "cosmos1def"} + tx.Msgs = []sdk.Msg{msg} + require.Equal(t, + `{"account_number":"1","chain_id":"foo","fee":{"amount":[],"gas":"0"},"memo":"memo","msgs":[{"type":"cosmos-sdk/MsgRevokeAllowance","value":{"grantee":"cosmos1def","granter":"cosmos1abc"}}],"sequence":"1","timeout_height":"1"}`, + string(legacytx.StdSignBytes("foo", 1, 1, 1, legacytx.StdFee{}, []sdk.Msg{msg}, "memo")), + ) +} diff --git a/x/feegrant/simulation/operations_test.go b/x/feegrant/simulation/operations_test.go index 79014dcda0..0ab46ca59d 100644 --- a/x/feegrant/simulation/operations_test.go +++ b/x/feegrant/simulation/operations_test.go @@ -113,7 +113,7 @@ func (suite *SimTestSuite) TestSimulateMsgGrantAllowance() { require.NoError(err) var msg feegrant.MsgGrantAllowance - suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg) + suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) require.True(operationMsg.OK) require.Equal(accounts[2].Address.String(), msg.Granter) @@ -155,7 +155,7 @@ func (suite *SimTestSuite) TestSimulateMsgRevokeAllowance() { require.NoError(err) var msg feegrant.MsgRevokeAllowance - suite.app.AppCodec().UnmarshalJSON(operationMsg.Msg, &msg) + suite.app.LegacyAmino().UnmarshalJSON(operationMsg.Msg, &msg) require.True(operationMsg.OK) require.Equal(granter.Address.String(), msg.Granter) diff --git a/x/gov/types/codec.go b/x/gov/types/codec.go index 160129457f..c56ade486d 100644 --- a/x/gov/types/codec.go +++ b/x/gov/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -60,4 +61,8 @@ var ( func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) + + // 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) } diff --git a/x/slashing/types/codec.go b/x/slashing/types/codec.go index 6e333b741b..e9ab724a7a 100644 --- a/x/slashing/types/codec.go +++ b/x/slashing/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -37,4 +38,8 @@ func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) amino.Seal() + + // 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) } diff --git a/x/staking/types/codec.go b/x/staking/types/codec.go index 2c6e335635..6b880cbfb3 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" @@ -17,6 +18,11 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&MsgDelegate{}, "cosmos-sdk/MsgDelegate", nil) cdc.RegisterConcrete(&MsgUndelegate{}, "cosmos-sdk/MsgUndelegate", nil) cdc.RegisterConcrete(&MsgBeginRedelegate{}, "cosmos-sdk/MsgBeginRedelegate", nil) + + cdc.RegisterInterface((*isStakeAuthorization_Validators)(nil), nil) + cdc.RegisterConcrete(&StakeAuthorization_AllowList{}, "cosmos-sdk/StakeAuthorization/AllowList", nil) + cdc.RegisterConcrete(&StakeAuthorization_DenyList{}, "cosmos-sdk/StakeAuthorization/DenyList", nil) + cdc.RegisterConcrete(&StakeAuthorization{}, "cosmos-sdk/StakeAuthorization", nil) } // RegisterInterfaces registers the x/staking interfaces types with the interface registry @@ -52,4 +58,8 @@ func init() { RegisterLegacyAminoCodec(amino) cryptocodec.RegisterCrypto(amino) amino.Seal() + + // 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) } From d76c512c4a86a5afd460c23caeb5470d9e7ee634 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Tue, 18 Oct 2022 12:09:57 +0900 Subject: [PATCH 2/7] chore: remove unused comment. Signed-off-by: zemyblue --- x/auth/types/codec.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index 470e6732da..c9cda17712 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -1,7 +1,6 @@ package types import ( - //<<<<<<< HEAD "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" @@ -9,13 +8,6 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/auth/legacy/legacytx" - //======= - // "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" - // "github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx" - //>>>>>>> 81cfc6cc85 (fix: implement Amino serialization for x/authz and x/feegrant (#11224)) ) // RegisterLegacyAminoCodec registers the account interfaces and concrete types on the From 6d5ae71a46cbadd0f8fdfc54909ae5941d72f246 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 23 Feb 2022 15:13:36 +0100 Subject: [PATCH 3/7] feat: replace all ModuleCdc instances with legacy.Cdc (cosmos-sdk #11240) Signed-off-by: zemyblue --- codec/legacy/codec.go | 2 ++ x/auth/types/codec.go | 11 -------- x/auth/types/msgs.go | 3 +- x/auth/vesting/types/codec.go | 6 ++-- x/auth/vesting/types/msgs.go | 3 +- x/bank/simulation/operations_test.go | 9 +++--- x/bank/types/codec.go | 19 ------------- x/bank/types/msgs.go | 5 ++-- x/crisis/types/codec.go | 19 ------------- x/crisis/types/msgs.go | 3 +- x/distribution/client/common/common_test.go | 4 +-- x/distribution/simulation/operations_test.go | 9 +++--- x/distribution/types/codec.go | 19 ------------- x/distribution/types/msg.go | 9 +++--- x/distribution/types/proposal.go | 1 - x/evidence/types/codec.go | 19 ------------- x/evidence/types/msgs.go | 3 +- x/gov/simulation/operations_test.go | 13 ++++++--- x/gov/types/codec.go | 28 ------------------- x/gov/types/msgs.go | 29 ++++++++++---------- x/params/types/proposal/proposal.go | 1 - x/slashing/simulation/operations_test.go | 3 +- x/slashing/types/codec.go | 19 ------------- x/slashing/types/msg.go | 3 +- x/staking/simulation/operations_test.go | 11 ++++---- x/staking/types/codec.go | 19 ------------- x/staking/types/historical_info_test.go | 6 ++-- x/staking/types/msg.go | 11 ++++---- x/upgrade/types/proposal.go | 2 -- x/wasm/alias.go | 1 - x/wasm/lbmtypes/codec.go | 15 ++-------- x/wasm/lbmtypes/proposal.go | 2 -- x/wasm/lbmtypes/tx.go | 3 +- x/wasm/types/codec.go | 14 ++-------- x/wasm/types/proposal.go | 10 ------- x/wasm/types/tx.go | 17 ++++++------ 36 files changed, 90 insertions(+), 261 deletions(-) diff --git a/codec/legacy/codec.go b/codec/legacy/codec.go index 0a73b6cc66..dffc0bea94 100644 --- a/codec/legacy/codec.go +++ b/codec/legacy/codec.go @@ -4,6 +4,7 @@ import ( "github.com/line/lbm-sdk/codec" cryptocodec "github.com/line/lbm-sdk/crypto/codec" cryptotypes "github.com/line/lbm-sdk/crypto/types" + sdk "github.com/line/lbm-sdk/types" ) // Cdc defines a global generic sealed Amino codec to be used throughout sdk. It @@ -15,6 +16,7 @@ var Cdc = codec.NewLegacyAmino() func init() { cryptocodec.RegisterCrypto(Cdc) codec.RegisterEvidences(Cdc) + sdk.RegisterLegacyAminoCodec(Cdc) } // PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index c9cda17712..34ae4b0fd6 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/auth/legacy/legacytx" @@ -46,16 +45,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { ) } -var ( - amino = codec.NewLegacyAmino() - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - - // 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) } diff --git a/x/auth/types/msgs.go b/x/auth/types/msgs.go index 771650e40c..171f220dcb 100644 --- a/x/auth/types/msgs.go +++ b/x/auth/types/msgs.go @@ -1,6 +1,7 @@ package types import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -32,7 +33,7 @@ func (msg MsgEmpty) ValidateBasic() error { // GetSignBytes Implements Msg. func (msg MsgEmpty) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } // GetSigners Implements Msg. diff --git a/x/auth/vesting/types/codec.go b/x/auth/vesting/types/codec.go index f0d0659bdc..9f2653252b 100644 --- a/x/auth/vesting/types/codec.go +++ b/x/auth/vesting/types/codec.go @@ -2,6 +2,7 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" @@ -58,9 +59,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var amino = codec.NewLegacyAmino() - func init() { - RegisterLegacyAminoCodec(amino) - amino.Seal() + RegisterLegacyAminoCodec(legacy.Cdc) } diff --git a/x/auth/vesting/types/msgs.go b/x/auth/vesting/types/msgs.go index 0d04e99a65..e54378136c 100644 --- a/x/auth/vesting/types/msgs.go +++ b/x/auth/vesting/types/msgs.go @@ -1,6 +1,7 @@ package types import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -64,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(amino.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } // GetSigners returns the expected signers for a MsgCreateVestingAccount. diff --git a/x/bank/simulation/operations_test.go b/x/bank/simulation/operations_test.go index bba4a6d862..31b4fb5c8c 100644 --- a/x/bank/simulation/operations_test.go +++ b/x/bank/simulation/operations_test.go @@ -8,6 +8,7 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/suite" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -79,7 +80,7 @@ func (suite *SimTestSuite) TestSimulateMsgSend() { suite.Require().NoError(err) var msg types.MsgSend - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("65337742stake", msg.Amount.String()) @@ -108,7 +109,7 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSend() { require.NoError(err) var msg types.MsgMultiSend - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(operationMsg.OK) require.Len(msg.Inputs, 3) @@ -146,7 +147,7 @@ func (suite *SimTestSuite) TestSimulateModuleAccountMsgSend() { suite.Require().NoError(err) var msg types.MsgSend - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Empty(operationMsg.Comment) @@ -176,7 +177,7 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSendToModuleAccount() { suite.Require().NoError(err) var msg types.MsgMultiSend - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) // sending tokens to a module account should fail suite.Require().Empty(operationMsg.Comment) diff --git a/x/bank/types/codec.go b/x/bank/types/codec.go index 914d404ba0..34a55f3e46 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/authz" @@ -31,24 +30,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/bank 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/staking and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() - - // 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) } diff --git a/x/bank/types/msgs.go b/x/bank/types/msgs.go index c6765e37ba..52978eb242 100644 --- a/x/bank/types/msgs.go +++ b/x/bank/types/msgs.go @@ -1,6 +1,7 @@ package types import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -50,7 +51,7 @@ func (msg MsgSend) ValidateBasic() error { // GetSignBytes Implements Msg. func (msg MsgSend) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } // GetSigners Implements Msg. @@ -92,7 +93,7 @@ func (msg MsgMultiSend) ValidateBasic() error { // GetSignBytes Implements Msg. func (msg MsgMultiSend) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } // GetSigners Implements Msg. diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index 43babee50c..05a90582b3 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" codectypes "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" ) @@ -23,24 +22,6 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/crisis 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/crisis and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() - - // 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) } diff --git a/x/crisis/types/msgs.go b/x/crisis/types/msgs.go index c5780d3c9b..91b24f7f86 100644 --- a/x/crisis/types/msgs.go +++ b/x/crisis/types/msgs.go @@ -1,6 +1,7 @@ package types import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" ) @@ -28,7 +29,7 @@ func (msg MsgVerifyInvariant) GetSigners() []sdk.AccAddress { // GetSignBytes gets the sign bytes for the msg MsgVerifyInvariant func (msg MsgVerifyInvariant) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/distribution/client/common/common_test.go b/x/distribution/client/common/common_test.go index 1410c0e02c..f0c5341339 100644 --- a/x/distribution/client/common/common_test.go +++ b/x/distribution/client/common/common_test.go @@ -6,11 +6,11 @@ import ( "github.com/stretchr/testify/require" "github.com/line/lbm-sdk/client" - "github.com/line/lbm-sdk/x/distribution/types" + "github.com/line/lbm-sdk/codec/legacy" ) func TestQueryDelegationRewardsAddrValidation(t *testing.T) { - clientCtx := client.Context{}.WithLegacyAmino(types.ModuleCdc.LegacyAmino) + clientCtx := client.Context{}.WithLegacyAmino(legacy.Cdc) type args struct { delAddr string diff --git a/x/distribution/simulation/operations_test.go b/x/distribution/simulation/operations_test.go index 1f3571fa61..960724dfd0 100644 --- a/x/distribution/simulation/operations_test.go +++ b/x/distribution/simulation/operations_test.go @@ -8,6 +8,7 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/suite" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -72,7 +73,7 @@ func (suite *SimTestSuite) TestSimulateMsgSetWithdrawAddress() { suite.Require().NoError(err) var msg types.MsgSetWithdrawAddress - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("link1ghekyjucln7y67ntx7cf27m9dpuxxemnqk82wt", msg.DelegatorAddress) @@ -113,7 +114,7 @@ func (suite *SimTestSuite) TestSimulateMsgWithdrawDelegatorReward() { suite.Require().NoError(err) var msg types.MsgWithdrawDelegatorReward - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("linkvaloper1l4s054098kk9hmr5753c6k3m2kw65h68cr83wt", msg.ValidatorAddress) @@ -169,7 +170,7 @@ func (suite *SimTestSuite) testSimulateMsgWithdrawValidatorCommission(tokenName suite.Require().NoError(err) var msg types.MsgWithdrawValidatorCommission - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("linkvaloper1tnh2q55v8wyygtt9srz5safamzdengsn8rx882", msg.ValidatorAddress) @@ -195,7 +196,7 @@ func (suite *SimTestSuite) TestSimulateMsgFundCommunityPool() { suite.Require().NoError(err) var msg types.MsgFundCommunityPool - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("4896096stake", msg.Amount.String()) diff --git a/x/distribution/types/codec.go b/x/distribution/types/codec.go index cdadb9ea7c..30fcd5685b 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" govtypes "github.com/line/lbm-sdk/x/gov/types" @@ -36,24 +35,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/distribution 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/distribution and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() - - // 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) } diff --git a/x/distribution/types/msg.go b/x/distribution/types/msg.go index 3b80735905..8c36eae9c7 100644 --- a/x/distribution/types/msg.go +++ b/x/distribution/types/msg.go @@ -1,6 +1,7 @@ package types import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -37,7 +38,7 @@ func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress { // get the bytes for the message signer to sign on func (msg MsgSetWithdrawAddress) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -74,7 +75,7 @@ func (msg MsgWithdrawDelegatorReward) GetSigners() []sdk.AccAddress { // get the bytes for the message signer to sign on func (msg MsgWithdrawDelegatorReward) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -109,7 +110,7 @@ func (msg MsgWithdrawValidatorCommission) GetSigners() []sdk.AccAddress { // get the bytes for the message signer to sign on func (msg MsgWithdrawValidatorCommission) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -149,7 +150,7 @@ func (msg MsgFundCommunityPool) GetSigners() []sdk.AccAddress { // GetSignBytes returns the raw bytes for a MsgFundCommunityPool message that // the expected signer needs to sign. func (msg MsgFundCommunityPool) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/distribution/types/proposal.go b/x/distribution/types/proposal.go index 93e7afd078..dcb09d8c8f 100644 --- a/x/distribution/types/proposal.go +++ b/x/distribution/types/proposal.go @@ -18,7 +18,6 @@ var _ govtypes.Content = &CommunityPoolSpendProposal{} func init() { govtypes.RegisterProposalType(ProposalTypeCommunityPoolSpend) - govtypes.RegisterProposalTypeCodec(&CommunityPoolSpendProposal{}, "cosmos-sdk/CommunityPoolSpendProposal") } // NewCommunityPoolSpendProposal creates a new community pool spned proposal. diff --git a/x/evidence/types/codec.go b/x/evidence/types/codec.go index ed73a736aa..89203a0e52 100644 --- a/x/evidence/types/codec.go +++ b/x/evidence/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/evidence/exported" @@ -29,24 +28,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/evidence 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/evidence and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() - - // 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) } diff --git a/x/evidence/types/msgs.go b/x/evidence/types/msgs.go index fee9a43197..fa54949bec 100644 --- a/x/evidence/types/msgs.go +++ b/x/evidence/types/msgs.go @@ -5,6 +5,7 @@ import ( "github.com/gogo/protobuf/proto" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" @@ -62,7 +63,7 @@ func (m MsgSubmitEvidence) ValidateBasic() error { // GetSignBytes returns the raw bytes a signer is expected to sign when submitting // a MsgSubmitEvidence message. func (m MsgSubmitEvidence) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&m)) } // GetSigners returns the single expected signer for a MsgSubmitEvidence. diff --git a/x/gov/simulation/operations_test.go b/x/gov/simulation/operations_test.go index 455a7b19b3..27032e608c 100644 --- a/x/gov/simulation/operations_test.go +++ b/x/gov/simulation/operations_test.go @@ -10,6 +10,7 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/require" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -113,7 +114,8 @@ func TestSimulateMsgSubmitProposal(t *testing.T) { require.NoError(t, err) var msg types.MsgSubmitProposal - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + require.NoError(t, err) require.True(t, operationMsg.OK) require.Equal(t, "link1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7fmx8x8", msg.Proposer) @@ -156,7 +158,8 @@ func TestSimulateMsgDeposit(t *testing.T) { require.NoError(t, err) var msg types.MsgDeposit - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + require.NoError(t, err) require.True(t, operationMsg.OK) require.Equal(t, uint64(1), msg.ProposalId) @@ -198,7 +201,8 @@ func TestSimulateMsgVote(t *testing.T) { require.NoError(t, err) var msg types.MsgVote - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + require.NoError(t, err) require.True(t, operationMsg.OK) require.Equal(t, uint64(1), msg.ProposalId) @@ -240,7 +244,8 @@ func TestSimulateMsgVoteWeighted(t *testing.T) { require.NoError(t, err) var msg types.MsgVoteWeighted - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + require.NoError(t, err) require.True(t, operationMsg.OK) require.Equal(t, uint64(1), msg.ProposalId) diff --git a/x/gov/types/codec.go b/x/gov/types/codec.go index c56ade486d..4e7f811d5a 100644 --- a/x/gov/types/codec.go +++ b/x/gov/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" ) @@ -36,33 +35,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -// RegisterProposalTypeCodec registers an external proposal content type defined -// in another module for the internal ModuleCdc. This allows the MsgSubmitProposal -// to be correctly Amino encoded and decoded. -// -// NOTE: This should only be used for applications that are still using a concrete -// Amino codec for serialization. -func RegisterProposalTypeCodec(o interface{}, name string) { - amino.RegisterConcrete(o, name, nil) -} - -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/gov 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/gov and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - - // 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) } diff --git a/x/gov/types/msgs.go b/x/gov/types/msgs.go index b0c9a9f1be..b55be739e2 100644 --- a/x/gov/types/msgs.go +++ b/x/gov/types/msgs.go @@ -3,10 +3,10 @@ package types import ( "fmt" - yaml "gopkg.in/yaml.v2" - "github.com/gogo/protobuf/proto" + yaml "gopkg.in/yaml.v2" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" @@ -83,8 +83,8 @@ func (m MsgSubmitProposal) Type() string { return TypeMsgSubmitProposal } // ValidateBasic implements Msg func (m MsgSubmitProposal) ValidateBasic() error { - if m.Proposer == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, m.Proposer) + if _, err := sdk.AccAddressFromBech32(m.Proposer); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid proposer address: %s", err) } if !m.InitialDeposit.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, m.InitialDeposit.String()) @@ -109,7 +109,7 @@ func (m MsgSubmitProposal) ValidateBasic() error { // GetSignBytes implements Msg func (m MsgSubmitProposal) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&m) + bz := legacy.Cdc.MustMarshalJSON(&m) return sdk.MustSortJSON(bz) } @@ -145,8 +145,8 @@ func (msg MsgDeposit) Type() string { return TypeMsgDeposit } // ValidateBasic implements Msg func (msg MsgDeposit) ValidateBasic() error { - if msg.Depositor == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Depositor) + if _, err := sdk.AccAddressFromBech32(msg.Depositor); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid depositor address: %s", err) } if !msg.Amount.IsValid() { return sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String()) @@ -166,7 +166,7 @@ func (msg MsgDeposit) String() string { // GetSignBytes implements Msg func (msg MsgDeposit) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -190,10 +190,9 @@ func (msg MsgVote) Type() string { return TypeMsgVote } // ValidateBasic implements Msg func (msg MsgVote) ValidateBasic() error { - if msg.Voter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter) + if _, err := sdk.AccAddressFromBech32(msg.Voter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", err) } - if !ValidVoteOption(msg.Option) { return sdkerrors.Wrap(ErrInvalidVote, msg.Option.String()) } @@ -209,7 +208,7 @@ func (msg MsgVote) String() string { // GetSignBytes implements Msg func (msg MsgVote) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -233,8 +232,8 @@ func (msg MsgVoteWeighted) Type() string { return TypeMsgVoteWeighted } // ValidateBasic implements Msg func (msg MsgVoteWeighted) ValidateBasic() error { - if msg.Voter == "" { - return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, msg.Voter) + if _, err := sdk.AccAddressFromBech32(msg.Voter); err != nil { + return sdkerrors.ErrInvalidAddress.Wrapf("invalid voter address: %s", err) } if len(msg.Options) == 0 { @@ -273,7 +272,7 @@ func (msg MsgVoteWeighted) String() string { // GetSignBytes implements Msg func (msg MsgVoteWeighted) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/params/types/proposal/proposal.go b/x/params/types/proposal/proposal.go index 6c2319e156..673b1f7611 100644 --- a/x/params/types/proposal/proposal.go +++ b/x/params/types/proposal/proposal.go @@ -19,7 +19,6 @@ var _ govtypes.Content = &ParameterChangeProposal{} func init() { govtypes.RegisterProposalType(ProposalTypeChange) - govtypes.RegisterProposalTypeCodec(&ParameterChangeProposal{}, "cosmos-sdk/ParameterChangeProposal") } func NewParameterChangeProposal(title, description string, changes []ParamChange) *ParameterChangeProposal { diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 3c85eb76a4..8a54d658f1 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -9,6 +9,7 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/require" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -94,7 +95,7 @@ func TestSimulateMsgUnjail(t *testing.T) { require.NoError(t, err) var msg types.MsgUnjail - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, types.TypeMsgUnjail, msg.Type()) diff --git a/x/slashing/types/codec.go b/x/slashing/types/codec.go index e9ab724a7a..966895b7a2 100644 --- a/x/slashing/types/codec.go +++ b/x/slashing/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" ) @@ -22,24 +21,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/slashing 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/slashing and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() - - // 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) } diff --git a/x/slashing/types/msg.go b/x/slashing/types/msg.go index e715e1a952..c24d334bbc 100644 --- a/x/slashing/types/msg.go +++ b/x/slashing/types/msg.go @@ -1,6 +1,7 @@ package types import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" ) @@ -32,7 +33,7 @@ func (msg MsgUnjail) GetSigners() []sdk.AccAddress { // GetSignBytes gets the bytes for the message signer to sign on func (msg MsgUnjail) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/staking/simulation/operations_test.go b/x/staking/simulation/operations_test.go index e5e21a073d..ba1ec6a4d3 100644 --- a/x/staking/simulation/operations_test.go +++ b/x/staking/simulation/operations_test.go @@ -9,6 +9,7 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/require" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -79,7 +80,7 @@ func TestSimulateMsgCreateValidator(t *testing.T) { require.NoError(t, err) var msg types.MsgCreateValidator - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "0.080000000000000000", msg.Commission.MaxChangeRate.String()) @@ -116,7 +117,7 @@ func TestSimulateMsgEditValidator(t *testing.T) { require.NoError(t, err) var msg types.MsgEditValidator - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "0.280623462081924936", msg.CommissionRate.String()) @@ -154,7 +155,7 @@ func TestSimulateMsgDelegate(t *testing.T) { require.NoError(t, err) var msg types.MsgDelegate - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "link1ghekyjucln7y67ntx7cf27m9dpuxxemnqk82wt", msg.DelegatorAddress) @@ -199,7 +200,7 @@ func TestSimulateMsgUndelegate(t *testing.T) { require.NoError(t, err) var msg types.MsgUndelegate - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "link1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7fmx8x8", msg.DelegatorAddress) @@ -250,7 +251,7 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) { require.NoError(t, err) var msg types.MsgBeginRedelegate - types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "link12gwd9jchc69wck8dhstxgwz3z8qs8yv6t0s9q5", msg.DelegatorAddress) diff --git a/x/staking/types/codec.go b/x/staking/types/codec.go index 6b880cbfb3..d7b15e8dc8 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -4,7 +4,6 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/authz" @@ -42,24 +41,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/staking 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/staking and - // defined at the application level. - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() - - // 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) } diff --git a/x/staking/types/historical_info_test.go b/x/staking/types/historical_info_test.go index f559594115..4114386ed2 100644 --- a/x/staking/types/historical_info_test.go +++ b/x/staking/types/historical_info_test.go @@ -9,6 +9,8 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" + "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/x/staking/types" ) @@ -33,11 +35,11 @@ func TestHistoricalInfo(t *testing.T) { var value []byte require.NotPanics(t, func() { - value = types.ModuleCdc.MustMarshal(&hi) + value = legacy.Cdc.MustMarshal(&hi) }) require.NotNil(t, value, "Marshalled HistoricalInfo is nil") - recv, err := types.UnmarshalHistoricalInfo(types.ModuleCdc, value) + recv, err := types.UnmarshalHistoricalInfo(codec.NewAminoCodec(legacy.Cdc), value) require.Nil(t, err, "Unmarshalling HistoricalInfo failed") require.Equal(t, hi.Header, recv.Header) for i := range hi.Valset { diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index e0c50463e0..87aff717f5 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -3,6 +3,7 @@ package types import ( "bytes" + "github.com/line/lbm-sdk/codec/legacy" codectypes "github.com/line/lbm-sdk/codec/types" cryptotypes "github.com/line/lbm-sdk/crypto/types" sdk "github.com/line/lbm-sdk/types" @@ -82,7 +83,7 @@ func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress { // GetSignBytes returns the message bytes to sign over. func (msg MsgCreateValidator) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -177,7 +178,7 @@ func (msg MsgEditValidator) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgEditValidator) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -234,7 +235,7 @@ func (msg MsgDelegate) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgDelegate) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -288,7 +289,7 @@ func (msg MsgBeginRedelegate) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgBeginRedelegate) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -343,7 +344,7 @@ func (msg MsgUndelegate) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgUndelegate) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := legacy.Cdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/upgrade/types/proposal.go b/x/upgrade/types/proposal.go index 2bf58fbef2..3eea7813a2 100644 --- a/x/upgrade/types/proposal.go +++ b/x/upgrade/types/proposal.go @@ -20,9 +20,7 @@ var _ gov.Content = &SoftwareUpgradeProposal{} func init() { gov.RegisterProposalType(ProposalTypeSoftwareUpgrade) - gov.RegisterProposalTypeCodec(&SoftwareUpgradeProposal{}, "cosmos-sdk/SoftwareUpgradeProposal") gov.RegisterProposalType(ProposalTypeCancelSoftwareUpgrade) - gov.RegisterProposalTypeCodec(&CancelSoftwareUpgradeProposal{}, "cosmos-sdk/CancelSoftwareUpgradeProposal") } func (sup *SoftwareUpgradeProposal) GetTitle() string { return sup.Title } diff --git a/x/wasm/alias.go b/x/wasm/alias.go index 8d77f9c59f..82f5d07973 100644 --- a/x/wasm/alias.go +++ b/x/wasm/alias.go @@ -69,7 +69,6 @@ var ( NewCountTXDecorator = keeper.NewCountTXDecorator // variable aliases - ModuleCdc = types.ModuleCdc DefaultCodespace = types.DefaultCodespace ErrCreateFailed = types.ErrCreateFailed ErrAccountExists = types.ErrAccountExists diff --git a/x/wasm/lbmtypes/codec.go b/x/wasm/lbmtypes/codec.go index d14671a394..0af8b2c484 100644 --- a/x/wasm/lbmtypes/codec.go +++ b/x/wasm/lbmtypes/codec.go @@ -2,8 +2,8 @@ package lbmtypes import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" govtypes "github.com/line/lbm-sdk/x/gov/types" @@ -12,7 +12,6 @@ import ( // RegisterLegacyAminoCodec registers the account types and interface func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { //nolint:staticcheck - wasmTypes.RegisterLegacyAminoCodec(cdc) cdc.RegisterConcrete(&MsgStoreCodeAndInstantiateContract{}, "wasm/StoreCodeAndInstantiateContract", nil) cdc.RegisterConcrete(&DeactivateContractProposal{}, "wasm/DeactivateContractProposal", nil) @@ -34,16 +33,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/wasm module codec. - - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() + RegisterLegacyAminoCodec(legacy.Cdc) } diff --git a/x/wasm/lbmtypes/proposal.go b/x/wasm/lbmtypes/proposal.go index a7276e11d3..a8a56ee0f2 100644 --- a/x/wasm/lbmtypes/proposal.go +++ b/x/wasm/lbmtypes/proposal.go @@ -22,8 +22,6 @@ var EnableAllProposals = append([]wasmtypes.ProposalType{ func init() { govtypes.RegisterProposalType(string(ProposalTypeDeactivateContract)) govtypes.RegisterProposalType(string(ProposalTypeActivateContract)) - govtypes.RegisterProposalTypeCodec(&DeactivateContractProposal{}, "wasm/DeactivateContractProposal") - govtypes.RegisterProposalTypeCodec(&ActivateContractProposal{}, "wasm/ActivateContractProposal") } func (p DeactivateContractProposal) GetTitle() string { return p.Title } diff --git a/x/wasm/lbmtypes/tx.go b/x/wasm/lbmtypes/tx.go index cb74722472..60be6ebeb5 100644 --- a/x/wasm/lbmtypes/tx.go +++ b/x/wasm/lbmtypes/tx.go @@ -1,6 +1,7 @@ package lbmtypes import ( + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" wasmtypes "github.com/line/lbm-sdk/x/wasm/types" @@ -50,7 +51,7 @@ func (msg MsgStoreCodeAndInstantiateContract) ValidateBasic() error { } func (msg MsgStoreCodeAndInstantiateContract) GetSignBytes() []byte { - return sdk.MustSortJSON(wasmtypes.ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgStoreCodeAndInstantiateContract) GetSigners() []sdk.AccAddress { diff --git a/x/wasm/types/codec.go b/x/wasm/types/codec.go index 509c508b0e..be4201a0c4 100644 --- a/x/wasm/types/codec.go +++ b/x/wasm/types/codec.go @@ -2,8 +2,8 @@ package types import ( "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" govtypes "github.com/line/lbm-sdk/x/gov/types" @@ -61,16 +61,6 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - - // ModuleCdc references the global x/wasm module codec. - - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - RegisterLegacyAminoCodec(amino) - cryptocodec.RegisterCrypto(amino) - amino.Seal() + RegisterLegacyAminoCodec(legacy.Cdc) } diff --git a/x/wasm/types/proposal.go b/x/wasm/types/proposal.go index 6b50d6a67e..2c8bdfe7df 100644 --- a/x/wasm/types/proposal.go +++ b/x/wasm/types/proposal.go @@ -71,16 +71,6 @@ func init() { // register new content types with the sdk govtypes.RegisterProposalType(string(ProposalTypePinCodes)) govtypes.RegisterProposalType(string(ProposalTypeUnpinCodes)) govtypes.RegisterProposalType(string(ProposalTypeUpdateInstantiateConfig)) - govtypes.RegisterProposalTypeCodec(&StoreCodeProposal{}, "wasm/StoreCodeProposal") - govtypes.RegisterProposalTypeCodec(&InstantiateContractProposal{}, "wasm/InstantiateContractProposal") - govtypes.RegisterProposalTypeCodec(&MigrateContractProposal{}, "wasm/MigrateContractProposal") - govtypes.RegisterProposalTypeCodec(&SudoContractProposal{}, "wasm/SudoContractProposal") - govtypes.RegisterProposalTypeCodec(&ExecuteContractProposal{}, "wasm/ExecuteContractProposal") - govtypes.RegisterProposalTypeCodec(&UpdateAdminProposal{}, "wasm/UpdateAdminProposal") - govtypes.RegisterProposalTypeCodec(&ClearAdminProposal{}, "wasm/ClearAdminProposal") - govtypes.RegisterProposalTypeCodec(&PinCodesProposal{}, "wasm/PinCodesProposal") - govtypes.RegisterProposalTypeCodec(&UnpinCodesProposal{}, "wasm/UnpinCodesProposal") - govtypes.RegisterProposalTypeCodec(&UpdateInstantiateConfigProposal{}, "wasm/UpdateInstantiateConfigProposal") } // ProposalRoute returns the routing key of a parameter change proposal. diff --git a/x/wasm/types/tx.go b/x/wasm/types/tx.go index 818b6e79b2..3b138ea25e 100644 --- a/x/wasm/types/tx.go +++ b/x/wasm/types/tx.go @@ -5,6 +5,7 @@ import ( "errors" "strings" + "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -67,7 +68,7 @@ func (msg MsgStoreCode) ValidateBasic() error { } func (msg MsgStoreCode) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgStoreCode) GetSigners() []sdk.AccAddress { @@ -115,7 +116,7 @@ func (msg MsgInstantiateContract) ValidateBasic() error { } func (msg MsgInstantiateContract) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgInstantiateContract) GetSigners() []sdk.AccAddress { @@ -152,7 +153,7 @@ func (msg MsgExecuteContract) ValidateBasic() error { } func (msg MsgExecuteContract) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgExecuteContract) GetSigners() []sdk.AccAddress { @@ -190,7 +191,7 @@ func (msg MsgMigrateContract) ValidateBasic() error { } func (msg MsgMigrateContract) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgMigrateContract) GetSigners() []sdk.AccAddress { @@ -226,7 +227,7 @@ func (msg MsgUpdateAdmin) ValidateBasic() error { } func (msg MsgUpdateAdmin) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgUpdateAdmin) GetSigners() []sdk.AccAddress { @@ -256,7 +257,7 @@ func (msg MsgClearAdmin) ValidateBasic() error { } func (msg MsgClearAdmin) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgClearAdmin) GetSigners() []sdk.AccAddress { @@ -280,7 +281,7 @@ func (msg MsgIBCSend) ValidateBasic() error { } func (msg MsgIBCSend) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgIBCSend) GetSigners() []sdk.AccAddress { @@ -300,7 +301,7 @@ func (msg MsgIBCCloseChannel) ValidateBasic() error { } func (msg MsgIBCCloseChannel) GetSignBytes() []byte { - return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) } func (msg MsgIBCCloseChannel) GetSigners() []sdk.AccAddress { From 781122c39d2d6a8f7cba936682d3f949ce242e4a Mon Sep 17 00:00:00 2001 From: Marie Gauthier Date: Wed, 16 Mar 2022 14:21:00 +0100 Subject: [PATCH 4/7] fix: RegisterLegacyAminoCodec should error if Msg name is >39 chars (cosmos-sdk #11349) --- baseapp/baseapp_test.go | 11 +++++----- codec/legacy/amino_msg.go | 18 ++++++++++++++++ codec/legacy/amino_msg_test.go | 39 ++++++++++++++++++++++++++++++++++ codec/legacy/doc.go | 2 ++ x/auth/vesting/types/codec.go | 1 + x/authz/codec.go | 6 +++--- x/bank/types/codec.go | 4 ++-- x/crisis/types/codec.go | 2 +- x/distribution/types/codec.go | 8 +++---- x/evidence/types/codec.go | 2 +- x/feegrant/codec.go | 4 ++-- x/gov/types/codec.go | 8 +++---- x/slashing/types/codec.go | 2 +- x/staking/types/codec.go | 10 ++++----- 14 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 codec/legacy/amino_msg.go create mode 100644 codec/legacy/amino_msg_test.go diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 0547834547..cec1cc826e 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -21,6 +21,7 @@ import ( dbm "github.com/tendermint/tm-db" "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/snapshots" snapshottypes "github.com/line/lbm-sdk/snapshots/types" "github.com/line/lbm-sdk/store/rootmulti" @@ -91,10 +92,10 @@ func registerTestCodec(cdc *codec.LegacyAmino) { // register test types cdc.RegisterConcrete(&txTest{}, "cosmos-sdk/baseapp/txTest", nil) - cdc.RegisterConcrete(&msgCounter{}, "cosmos-sdk/baseapp/msgCounter", nil) - cdc.RegisterConcrete(&msgCounter2{}, "cosmos-sdk/baseapp/msgCounter2", nil) - cdc.RegisterConcrete(&msgKeyValue{}, "cosmos-sdk/baseapp/msgKeyValue", nil) - cdc.RegisterConcrete(&msgNoRoute{}, "cosmos-sdk/baseapp/msgNoRoute", nil) + legacy.RegisterAminoMsg(cdc, &msgCounter{}, "cosmos-sdk/baseapp/msgCounter") + legacy.RegisterAminoMsg(cdc, &msgCounter2{}, "cosmos-sdk/baseapp/msgCounter2") + legacy.RegisterAminoMsg(cdc, &msgKeyValue{}, "cosmos-sdk/baseapp/msgKeyValue") + legacy.RegisterAminoMsg(cdc, &msgNoRoute{}, "cosmos-sdk/baseapp/msgNoRoute") } // aminoTxEncoder creates a amino TxEncoder for testing purposes. @@ -1257,7 +1258,7 @@ func TestRunInvalidTransaction(t *testing.T) { // new codec so we can encode the tx, but we shouldn't be able to decode newCdc := codec.NewLegacyAmino() registerTestCodec(newCdc) - newCdc.RegisterConcrete(&msgNoDecode{}, "cosmos-sdk/baseapp/msgNoDecode", nil) + legacy.RegisterAminoMsg(newCdc, &msgNoDecode{}, "cosmos-sdk/baseapp/msgNoDecode") txBytes, err := newCdc.Marshal(tx) require.NoError(t, err) diff --git a/codec/legacy/amino_msg.go b/codec/legacy/amino_msg.go new file mode 100644 index 0000000000..b896f74fce --- /dev/null +++ b/codec/legacy/amino_msg.go @@ -0,0 +1,18 @@ +package legacy + +import ( + "fmt" + + "github.com/line/lbm-sdk/codec" + sdk "github.com/line/lbm-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 0000000000..7e020b3eea --- /dev/null +++ b/codec/legacy/amino_msg_test.go @@ -0,0 +1,39 @@ +package legacy_test + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/line/lbm-sdk/codec" + "github.com/line/lbm-sdk/codec/legacy" + "github.com/line/lbm-sdk/testutil/testdata" +) + +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 d89944f3df..f3976d871f 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 9f2653252b..315aa341f6 100644 --- a/x/auth/vesting/types/codec.go +++ b/x/auth/vesting/types/codec.go @@ -19,6 +19,7 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&DelayedVestingAccount{}, "cosmos-sdk/DelayedVestingAccount", nil) cdc.RegisterConcrete(&PeriodicVestingAccount{}, "cosmos-sdk/PeriodicVestingAccount", nil) cdc.RegisterConcrete(&PermanentLockedAccount{}, "cosmos-sdk/PermanentLockedAccount", 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 e72ade1c6d..9cbb59927f 100644 --- a/x/authz/codec.go +++ b/x/authz/codec.go @@ -11,9 +11,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 34a55f3e46..466b5369a7 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -12,8 +12,8 @@ import ( // 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) } diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index 05a90582b3..e577f0ab7a 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -11,7 +11,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 30fcd5685b..772059ea62 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -12,10 +12,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 89203a0e52..c88be2dc43 100644 --- a/x/evidence/types/codec.go +++ b/x/evidence/types/codec.go @@ -13,7 +13,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 1e61455a84..2bd5b92288 100644 --- a/x/feegrant/codec.go +++ b/x/feegrant/codec.go @@ -11,8 +11,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 4e7f811d5a..3ac394c76f 100644 --- a/x/gov/types/codec.go +++ b/x/gov/types/codec.go @@ -12,10 +12,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/MsgSubmitProposal") + legacy.RegisterAminoMsg(cdc, &MsgDeposit{}, "cosmos-sdk/MsgDeposit") + legacy.RegisterAminoMsg(cdc, &MsgVote{}, "cosmos-sdk/MsgVote") + legacy.RegisterAminoMsg(cdc, &MsgVoteWeighted{}, "cosmos-sdk/MsgVoteWeighted") cdc.RegisterConcrete(&TextProposal{}, "cosmos-sdk/TextProposal", nil) } diff --git a/x/slashing/types/codec.go b/x/slashing/types/codec.go index 966895b7a2..2c3969a6b3 100644 --- a/x/slashing/types/codec.go +++ b/x/slashing/types/codec.go @@ -10,7 +10,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 d7b15e8dc8..3d6491ae81 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -12,11 +12,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) From 3c1c23538d06f97240aa0f3408d50d12a59e6120 Mon Sep 17 00:00:00 2001 From: Riccardo Montagnin Date: Wed, 20 Apr 2022 11:27:40 +0200 Subject: [PATCH 5/7] revert: replace all ModuleCdc instances with legacy.Cdc (cosmos-sdk #11680) Signed-off-by: zemyblue --- docs/core/encoding.md | 8 ++++---- x/auth/types/codec.go | 16 ++++++++++++++-- x/auth/types/msgs.go | 3 +-- x/auth/vesting/types/codec.go | 15 ++++++++++++++- x/auth/vesting/types/msgs.go | 3 +-- x/authz/codec.go | 5 +++-- x/authz/codec/cdc.go | 18 ++++++++++++++++++ x/authz/codec/doc.go | 18 ++++++++++++++++++ x/authz/msgs.go | 8 ++++---- x/bank/simulation/operations_test.go | 9 ++++----- x/bank/types/codec.go | 15 ++++++++++++++- x/bank/types/msgs.go | 5 ++--- x/crisis/types/codec.go | 15 ++++++++++++++- x/crisis/types/msgs.go | 3 +-- x/distribution/simulation/operations_test.go | 9 ++++----- x/distribution/types/codec.go | 15 ++++++++++++++- x/distribution/types/msg.go | 9 ++++----- x/evidence/types/codec.go | 15 ++++++++++++++- x/evidence/types/msgs.go | 3 +-- x/feegrant/codec.go | 10 +++++++--- x/gov/simulation/operations_test.go | 9 ++++----- x/gov/types/codec.go | 15 ++++++++++++++- x/gov/types/msgs.go | 9 ++++----- x/slashing/simulation/operations_test.go | 3 +-- x/slashing/types/codec.go | 15 ++++++++++++++- x/slashing/types/msg.go | 3 +-- x/staking/simulation/operations_test.go | 11 +++++------ x/staking/types/codec.go | 15 ++++++++++++++- x/staking/types/msg.go | 11 +++++------ x/upgrade/types/codec.go | 18 ++++++++++++++++++ x/wasm/lbmtypes/tx.go | 3 +-- x/wasm/types/codec.go | 16 ++++++++++++++-- x/wasm/types/tx.go | 17 ++++++++--------- 33 files changed, 259 insertions(+), 88 deletions(-) create mode 100644 x/authz/codec/cdc.go create mode 100644 x/authz/codec/doc.go diff --git a/docs/core/encoding.md b/docs/core/encoding.md index 2d08f05f8c..d4098dca62 100644 --- a/docs/core/encoding.md +++ b/docs/core/encoding.md @@ -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) } ``` diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index 34ae4b0fd6..078d5fb2a7 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -2,11 +2,12 @@ package types import ( "github.com/line/lbm-sdk/codec" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/auth/legacy/legacytx" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the account interfaces and concrete types on the @@ -45,6 +46,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) } diff --git a/x/auth/types/msgs.go b/x/auth/types/msgs.go index 171f220dcb..771650e40c 100644 --- a/x/auth/types/msgs.go +++ b/x/auth/types/msgs.go @@ -1,7 +1,6 @@ package types import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -33,7 +32,7 @@ func (msg MsgEmpty) ValidateBasic() error { // GetSignBytes Implements Msg. func (msg MsgEmpty) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } // GetSigners Implements Msg. diff --git a/x/auth/vesting/types/codec.go b/x/auth/vesting/types/codec.go index 315aa341f6..0da08ba747 100644 --- a/x/auth/vesting/types/codec.go +++ b/x/auth/vesting/types/codec.go @@ -4,10 +4,12 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authtypes "github.com/line/lbm-sdk/x/auth/types" "github.com/line/lbm-sdk/x/auth/vesting/exported" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the vesting interfaces and concrete types on the @@ -60,6 +62,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) } diff --git a/x/auth/vesting/types/msgs.go b/x/auth/vesting/types/msgs.go index e54378136c..e835834404 100644 --- a/x/auth/vesting/types/msgs.go +++ b/x/auth/vesting/types/msgs.go @@ -1,7 +1,6 @@ package types import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -65,7 +64,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. diff --git a/x/authz/codec.go b/x/authz/codec.go index 9cbb59927f..d333f82274 100644 --- a/x/authz/codec.go +++ b/x/authz/codec.go @@ -6,6 +6,7 @@ import ( "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/authz interfaces and concrete types @@ -36,7 +37,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) } diff --git a/x/authz/codec/cdc.go b/x/authz/codec/cdc.go new file mode 100644 index 0000000000..ba6fa1ed87 --- /dev/null +++ b/x/authz/codec/cdc.go @@ -0,0 +1,18 @@ +package codec + +import ( + "github.com/line/lbm-sdk/codec" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" + sdk "github.com/line/lbm-sdk/types" +) + +var ( + Amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewAminoCodec(Amino) +) + +func init() { + cryptocodec.RegisterCrypto(Amino) + codec.RegisterEvidences(Amino) + sdk.RegisterLegacyAminoCodec(Amino) +} diff --git a/x/authz/codec/doc.go b/x/authz/codec/doc.go new file mode 100644 index 0000000000..ecc365a221 --- /dev/null +++ b/x/authz/codec/doc.go @@ -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 diff --git a/x/authz/msgs.go b/x/authz/msgs.go index cc0a0046d4..a75039e365 100644 --- a/x/authz/msgs.go +++ b/x/authz/msgs.go @@ -5,11 +5,11 @@ import ( "github.com/gogo/protobuf/proto" - "github.com/line/lbm-sdk/codec/legacy" cdctypes "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" "github.com/line/lbm-sdk/x/auth/legacy/legacytx" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) var ( @@ -79,7 +79,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. @@ -172,7 +172,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 @@ -243,5 +243,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)) } diff --git a/x/bank/simulation/operations_test.go b/x/bank/simulation/operations_test.go index 31b4fb5c8c..bba4a6d862 100644 --- a/x/bank/simulation/operations_test.go +++ b/x/bank/simulation/operations_test.go @@ -8,7 +8,6 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/suite" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -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()) @@ -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) @@ -147,7 +146,7 @@ func (suite *SimTestSuite) TestSimulateModuleAccountMsgSend() { 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().Empty(operationMsg.Comment) @@ -177,7 +176,7 @@ func (suite *SimTestSuite) TestSimulateMsgMultiSendToModuleAccount() { suite.Require().NoError(err) var msg types.MsgMultiSend - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) // sending tokens to a module account should fail suite.Require().Empty(operationMsg.Comment) diff --git a/x/bank/types/codec.go b/x/bank/types/codec.go index 466b5369a7..b54fe3d98a 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -4,9 +4,11 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/authz" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/bank interfaces and concrete types @@ -30,6 +32,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) } diff --git a/x/bank/types/msgs.go b/x/bank/types/msgs.go index 52978eb242..c6765e37ba 100644 --- a/x/bank/types/msgs.go +++ b/x/bank/types/msgs.go @@ -1,7 +1,6 @@ package types import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -51,7 +50,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. @@ -93,7 +92,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. diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index e577f0ab7a..e4c190a084 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -4,8 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" codectypes "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/crisis interfaces and concrete types @@ -22,6 +24,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) } diff --git a/x/crisis/types/msgs.go b/x/crisis/types/msgs.go index 91b24f7f86..c5780d3c9b 100644 --- a/x/crisis/types/msgs.go +++ b/x/crisis/types/msgs.go @@ -1,7 +1,6 @@ package types import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" ) @@ -29,7 +28,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) } diff --git a/x/distribution/simulation/operations_test.go b/x/distribution/simulation/operations_test.go index 960724dfd0..1f3571fa61 100644 --- a/x/distribution/simulation/operations_test.go +++ b/x/distribution/simulation/operations_test.go @@ -8,7 +8,6 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/suite" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -73,7 +72,7 @@ func (suite *SimTestSuite) TestSimulateMsgSetWithdrawAddress() { suite.Require().NoError(err) var msg types.MsgSetWithdrawAddress - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("link1ghekyjucln7y67ntx7cf27m9dpuxxemnqk82wt", msg.DelegatorAddress) @@ -114,7 +113,7 @@ func (suite *SimTestSuite) TestSimulateMsgWithdrawDelegatorReward() { suite.Require().NoError(err) var msg types.MsgWithdrawDelegatorReward - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("linkvaloper1l4s054098kk9hmr5753c6k3m2kw65h68cr83wt", msg.ValidatorAddress) @@ -170,7 +169,7 @@ func (suite *SimTestSuite) testSimulateMsgWithdrawValidatorCommission(tokenName suite.Require().NoError(err) var msg types.MsgWithdrawValidatorCommission - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("linkvaloper1tnh2q55v8wyygtt9srz5safamzdengsn8rx882", msg.ValidatorAddress) @@ -196,7 +195,7 @@ func (suite *SimTestSuite) TestSimulateMsgFundCommunityPool() { suite.Require().NoError(err) var msg types.MsgFundCommunityPool - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) suite.Require().True(operationMsg.OK) suite.Require().Equal("4896096stake", msg.Amount.String()) diff --git a/x/distribution/types/codec.go b/x/distribution/types/codec.go index 772059ea62..84a523621d 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -4,8 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" ) @@ -35,6 +37,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) } diff --git a/x/distribution/types/msg.go b/x/distribution/types/msg.go index 8c36eae9c7..3b80735905 100644 --- a/x/distribution/types/msg.go +++ b/x/distribution/types/msg.go @@ -1,7 +1,6 @@ package types import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -38,7 +37,7 @@ func (msg MsgSetWithdrawAddress) GetSigners() []sdk.AccAddress { // get the bytes for the message signer to sign on func (msg MsgSetWithdrawAddress) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -75,7 +74,7 @@ func (msg MsgWithdrawDelegatorReward) GetSigners() []sdk.AccAddress { // get the bytes for the message signer to sign on func (msg MsgWithdrawDelegatorReward) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -110,7 +109,7 @@ func (msg MsgWithdrawValidatorCommission) GetSigners() []sdk.AccAddress { // get the bytes for the message signer to sign on func (msg MsgWithdrawValidatorCommission) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -150,7 +149,7 @@ func (msg MsgFundCommunityPool) GetSigners() []sdk.AccAddress { // GetSignBytes returns the raw bytes for a MsgFundCommunityPool message that // the expected signer needs to sign. func (msg MsgFundCommunityPool) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/evidence/types/codec.go b/x/evidence/types/codec.go index c88be2dc43..f7de91ac18 100644 --- a/x/evidence/types/codec.go +++ b/x/evidence/types/codec.go @@ -4,8 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" "github.com/line/lbm-sdk/x/evidence/exported" ) @@ -28,6 +30,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) } diff --git a/x/evidence/types/msgs.go b/x/evidence/types/msgs.go index fa54949bec..fee9a43197 100644 --- a/x/evidence/types/msgs.go +++ b/x/evidence/types/msgs.go @@ -5,7 +5,6 @@ import ( "github.com/gogo/protobuf/proto" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" @@ -63,7 +62,7 @@ func (m MsgSubmitEvidence) ValidateBasic() error { // GetSignBytes returns the raw bytes a signer is expected to sign when submitting // a MsgSubmitEvidence message. func (m MsgSubmitEvidence) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&m)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m)) } // GetSigners returns the single expected signer for a MsgSubmitEvidence. diff --git a/x/feegrant/codec.go b/x/feegrant/codec.go index 2bd5b92288..b74a71e20a 100644 --- a/x/feegrant/codec.go +++ b/x/feegrant/codec.go @@ -4,8 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/feegrant interfaces and concrete types @@ -52,8 +54,10 @@ var ( func init() { RegisterLegacyAminoCodec(amino) + cryptocodec.RegisterCrypto(amino) + sdk.RegisterLegacyAminoCodec(amino) - // 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) } diff --git a/x/gov/simulation/operations_test.go b/x/gov/simulation/operations_test.go index 27032e608c..35411cdbab 100644 --- a/x/gov/simulation/operations_test.go +++ b/x/gov/simulation/operations_test.go @@ -10,7 +10,6 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/require" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -114,7 +113,7 @@ func TestSimulateMsgSubmitProposal(t *testing.T) { require.NoError(t, err) var msg types.MsgSubmitProposal - err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) @@ -158,7 +157,7 @@ func TestSimulateMsgDeposit(t *testing.T) { require.NoError(t, err) var msg types.MsgDeposit - err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) @@ -201,7 +200,7 @@ func TestSimulateMsgVote(t *testing.T) { require.NoError(t, err) var msg types.MsgVote - err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) @@ -244,7 +243,7 @@ func TestSimulateMsgVoteWeighted(t *testing.T) { require.NoError(t, err) var msg types.MsgVoteWeighted - err = legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) diff --git a/x/gov/types/codec.go b/x/gov/types/codec.go index 3ac394c76f..8a7aea4d00 100644 --- a/x/gov/types/codec.go +++ b/x/gov/types/codec.go @@ -4,8 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers all the necessary types and interfaces for the @@ -35,6 +37,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) } diff --git a/x/gov/types/msgs.go b/x/gov/types/msgs.go index b55be739e2..65b942aa15 100644 --- a/x/gov/types/msgs.go +++ b/x/gov/types/msgs.go @@ -6,7 +6,6 @@ import ( "github.com/gogo/protobuf/proto" yaml "gopkg.in/yaml.v2" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" @@ -109,7 +108,7 @@ func (m MsgSubmitProposal) ValidateBasic() error { // GetSignBytes implements Msg func (m MsgSubmitProposal) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&m) + bz := ModuleCdc.MustMarshalJSON(&m) return sdk.MustSortJSON(bz) } @@ -166,7 +165,7 @@ func (msg MsgDeposit) String() string { // GetSignBytes implements Msg func (msg MsgDeposit) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -208,7 +207,7 @@ func (msg MsgVote) String() string { // GetSignBytes implements Msg func (msg MsgVote) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -272,7 +271,7 @@ func (msg MsgVoteWeighted) String() string { // GetSignBytes implements Msg func (msg MsgVoteWeighted) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/slashing/simulation/operations_test.go b/x/slashing/simulation/operations_test.go index 8a54d658f1..3c85eb76a4 100644 --- a/x/slashing/simulation/operations_test.go +++ b/x/slashing/simulation/operations_test.go @@ -9,7 +9,6 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/require" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -95,7 +94,7 @@ func TestSimulateMsgUnjail(t *testing.T) { require.NoError(t, err) var msg types.MsgUnjail - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, types.TypeMsgUnjail, msg.Type()) diff --git a/x/slashing/types/codec.go b/x/slashing/types/codec.go index 2c3969a6b3..0a8bf8aa8c 100644 --- a/x/slashing/types/codec.go +++ b/x/slashing/types/codec.go @@ -4,8 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers concrete types on LegacyAmino codec @@ -21,6 +23,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) } diff --git a/x/slashing/types/msg.go b/x/slashing/types/msg.go index c24d334bbc..e715e1a952 100644 --- a/x/slashing/types/msg.go +++ b/x/slashing/types/msg.go @@ -1,7 +1,6 @@ package types import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" ) @@ -33,7 +32,7 @@ func (msg MsgUnjail) GetSigners() []sdk.AccAddress { // GetSignBytes gets the bytes for the message signer to sign on func (msg MsgUnjail) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/staking/simulation/operations_test.go b/x/staking/simulation/operations_test.go index ba1ec6a4d3..e5e21a073d 100644 --- a/x/staking/simulation/operations_test.go +++ b/x/staking/simulation/operations_test.go @@ -9,7 +9,6 @@ import ( ocproto "github.com/line/ostracon/proto/ostracon/types" "github.com/stretchr/testify/require" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/simapp" simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" @@ -80,7 +79,7 @@ func TestSimulateMsgCreateValidator(t *testing.T) { require.NoError(t, err) var msg types.MsgCreateValidator - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "0.080000000000000000", msg.Commission.MaxChangeRate.String()) @@ -117,7 +116,7 @@ func TestSimulateMsgEditValidator(t *testing.T) { require.NoError(t, err) var msg types.MsgEditValidator - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "0.280623462081924936", msg.CommissionRate.String()) @@ -155,7 +154,7 @@ func TestSimulateMsgDelegate(t *testing.T) { require.NoError(t, err) var msg types.MsgDelegate - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "link1ghekyjucln7y67ntx7cf27m9dpuxxemnqk82wt", msg.DelegatorAddress) @@ -200,7 +199,7 @@ func TestSimulateMsgUndelegate(t *testing.T) { require.NoError(t, err) var msg types.MsgUndelegate - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "link1p8wcgrjr4pjju90xg6u9cgq55dxwq8j7fmx8x8", msg.DelegatorAddress) @@ -251,7 +250,7 @@ func TestSimulateMsgBeginRedelegate(t *testing.T) { require.NoError(t, err) var msg types.MsgBeginRedelegate - legacy.Cdc.UnmarshalJSON(operationMsg.Msg, &msg) + types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.True(t, operationMsg.OK) require.Equal(t, "link12gwd9jchc69wck8dhstxgwz3z8qs8yv6t0s9q5", msg.DelegatorAddress) diff --git a/x/staking/types/codec.go b/x/staking/types/codec.go index 3d6491ae81..ee68780589 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -4,9 +4,11 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/authz" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" ) // RegisterLegacyAminoCodec registers the necessary x/staking interfaces and concrete types @@ -41,6 +43,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) } diff --git a/x/staking/types/msg.go b/x/staking/types/msg.go index 87aff717f5..e0c50463e0 100644 --- a/x/staking/types/msg.go +++ b/x/staking/types/msg.go @@ -3,7 +3,6 @@ package types import ( "bytes" - "github.com/line/lbm-sdk/codec/legacy" codectypes "github.com/line/lbm-sdk/codec/types" cryptotypes "github.com/line/lbm-sdk/crypto/types" sdk "github.com/line/lbm-sdk/types" @@ -83,7 +82,7 @@ func (msg MsgCreateValidator) GetSigners() []sdk.AccAddress { // GetSignBytes returns the message bytes to sign over. func (msg MsgCreateValidator) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -178,7 +177,7 @@ func (msg MsgEditValidator) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgEditValidator) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -235,7 +234,7 @@ func (msg MsgDelegate) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgDelegate) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -289,7 +288,7 @@ func (msg MsgBeginRedelegate) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgBeginRedelegate) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -344,7 +343,7 @@ func (msg MsgUndelegate) GetSigners() []sdk.AccAddress { // GetSignBytes implements the sdk.Msg interface. func (msg MsgUndelegate) GetSignBytes() []byte { - bz := legacy.Cdc.MustMarshalJSON(&msg) + bz := ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/upgrade/types/codec.go b/x/upgrade/types/codec.go index 746c8f082f..5d806345d9 100644 --- a/x/upgrade/types/codec.go +++ b/x/upgrade/types/codec.go @@ -3,6 +3,9 @@ package types import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" + sdk "github.com/line/lbm-sdk/types" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" ) @@ -20,3 +23,18 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { &CancelSoftwareUpgradeProposal{}, ) } + +var ( + amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewAminoCodec(amino) +) + +func init() { + 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) +} diff --git a/x/wasm/lbmtypes/tx.go b/x/wasm/lbmtypes/tx.go index 60be6ebeb5..cb74722472 100644 --- a/x/wasm/lbmtypes/tx.go +++ b/x/wasm/lbmtypes/tx.go @@ -1,7 +1,6 @@ package lbmtypes import ( - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" wasmtypes "github.com/line/lbm-sdk/x/wasm/types" @@ -51,7 +50,7 @@ func (msg MsgStoreCodeAndInstantiateContract) ValidateBasic() error { } func (msg MsgStoreCodeAndInstantiateContract) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(wasmtypes.ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgStoreCodeAndInstantiateContract) GetSigners() []sdk.AccAddress { diff --git a/x/wasm/types/codec.go b/x/wasm/types/codec.go index be4201a0c4..240d28c259 100644 --- a/x/wasm/types/codec.go +++ b/x/wasm/types/codec.go @@ -2,10 +2,11 @@ package types import ( "github.com/line/lbm-sdk/codec" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" ) @@ -61,6 +62,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) } diff --git a/x/wasm/types/tx.go b/x/wasm/types/tx.go index 3b138ea25e..818b6e79b2 100644 --- a/x/wasm/types/tx.go +++ b/x/wasm/types/tx.go @@ -5,7 +5,6 @@ import ( "errors" "strings" - "github.com/line/lbm-sdk/codec/legacy" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" ) @@ -68,7 +67,7 @@ func (msg MsgStoreCode) ValidateBasic() error { } func (msg MsgStoreCode) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgStoreCode) GetSigners() []sdk.AccAddress { @@ -116,7 +115,7 @@ func (msg MsgInstantiateContract) ValidateBasic() error { } func (msg MsgInstantiateContract) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgInstantiateContract) GetSigners() []sdk.AccAddress { @@ -153,7 +152,7 @@ func (msg MsgExecuteContract) ValidateBasic() error { } func (msg MsgExecuteContract) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgExecuteContract) GetSigners() []sdk.AccAddress { @@ -191,7 +190,7 @@ func (msg MsgMigrateContract) ValidateBasic() error { } func (msg MsgMigrateContract) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgMigrateContract) GetSigners() []sdk.AccAddress { @@ -227,7 +226,7 @@ func (msg MsgUpdateAdmin) ValidateBasic() error { } func (msg MsgUpdateAdmin) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgUpdateAdmin) GetSigners() []sdk.AccAddress { @@ -257,7 +256,7 @@ func (msg MsgClearAdmin) ValidateBasic() error { } func (msg MsgClearAdmin) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgClearAdmin) GetSigners() []sdk.AccAddress { @@ -281,7 +280,7 @@ func (msg MsgIBCSend) ValidateBasic() error { } func (msg MsgIBCSend) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgIBCSend) GetSigners() []sdk.AccAddress { @@ -301,7 +300,7 @@ func (msg MsgIBCCloseChannel) ValidateBasic() error { } func (msg MsgIBCCloseChannel) GetSignBytes() []byte { - return sdk.MustSortJSON(legacy.Cdc.MustMarshalJSON(&msg)) + return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&msg)) } func (msg MsgIBCCloseChannel) GetSigners() []sdk.AccAddress { From a1e9db9bdb39d0239f872eae6532edf582a968cf Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Thu, 8 Sep 2022 17:35:57 +0200 Subject: [PATCH 6/7] fix!: Fix gov amino codec (cosmos-sdk #13196) Signed-off-by: zemyblue --- docs/core/encoding.md | 14 +++++++++----- x/auth/types/codec.go | 6 ++++-- x/auth/vesting/types/codec.go | 6 ++++-- x/authz/codec.go | 6 ++++-- x/bank/types/codec.go | 6 ++++-- x/crisis/types/codec.go | 6 ++++-- x/distribution/types/codec.go | 7 +++++-- x/evidence/types/codec.go | 6 ++++-- x/feegrant/codec.go | 6 ++++-- x/gov/codec/cdc.go | 18 ++++++++++++++++++ x/gov/codec/doc.go | 18 ++++++++++++++++++ x/gov/simulation/operations_test.go | 9 +++++---- x/gov/types/codec.go | 16 ++++------------ x/gov/types/msgs.go | 9 +++++---- x/mint/types/codec.go | 3 ++- x/slashing/types/codec.go | 6 ++++-- x/staking/types/codec.go | 6 ++++-- x/upgrade/types/codec.go | 7 +++++-- x/wasm/lbmtypes/codec.go | 18 ++++++++++++++++-- x/wasm/types/codec.go | 4 +++- 20 files changed, 126 insertions(+), 51 deletions(-) create mode 100644 x/gov/codec/cdc.go create mode 100644 x/gov/codec/doc.go diff --git a/docs/core/encoding.md b/docs/core/encoding.md index d4098dca62..e6b8a5baca 100644 --- a/docs/core/encoding.md +++ b/docs/core/encoding.md @@ -67,18 +67,22 @@ Note, there are length-prefixed variants of the above functionality and this is typically used for when the data needs to be streamed or grouped together (e.g. `ResponseDeliverTx.Data`) -#### Authz authorizations +#### Authz authorizations and Gov proposals -Since the `MsgExec` message type can contain different messages instances, it is important that developers +Since authz's `MsgExec` and `MsgGrant` message types, as well as gov's `MsgSubmitProposal`, can contain different messages instances, it is important that developers add the following code inside the `init` method of their module's `codec.go` file: ```go -import authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" +import ( + authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec" + govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec" +) 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } ``` diff --git a/x/auth/types/codec.go b/x/auth/types/codec.go index 078d5fb2a7..9d0ac1baef 100644 --- a/x/auth/types/codec.go +++ b/x/auth/types/codec.go @@ -8,6 +8,7 @@ import ( "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/auth/legacy/legacytx" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the account interfaces and concrete types on the @@ -56,7 +57,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/auth/vesting/types/codec.go b/x/auth/vesting/types/codec.go index 0da08ba747..0e0e05c04f 100644 --- a/x/auth/vesting/types/codec.go +++ b/x/auth/vesting/types/codec.go @@ -10,6 +10,7 @@ import ( authtypes "github.com/line/lbm-sdk/x/auth/types" "github.com/line/lbm-sdk/x/auth/vesting/exported" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the vesting interfaces and concrete types on the @@ -72,7 +73,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/authz/codec.go b/x/authz/codec.go index d333f82274..e8df8d6aea 100644 --- a/x/authz/codec.go +++ b/x/authz/codec.go @@ -7,6 +7,7 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the necessary x/authz interfaces and concrete types @@ -37,7 +38,8 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, MsgServiceDesc()) } 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/bank/types/codec.go b/x/bank/types/codec.go index b54fe3d98a..afd79d9af0 100644 --- a/x/bank/types/codec.go +++ b/x/bank/types/codec.go @@ -9,6 +9,7 @@ import ( "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/authz" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the necessary x/bank interfaces and concrete types @@ -42,7 +43,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/crisis/types/codec.go b/x/crisis/types/codec.go index e4c190a084..500b22fa45 100644 --- a/x/crisis/types/codec.go +++ b/x/crisis/types/codec.go @@ -8,6 +8,7 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the necessary x/crisis interfaces and concrete types @@ -34,7 +35,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/distribution/types/codec.go b/x/distribution/types/codec.go index 84a523621d..4c326872d8 100644 --- a/x/distribution/types/codec.go +++ b/x/distribution/types/codec.go @@ -8,6 +8,7 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" ) @@ -47,7 +48,9 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec + // so that this can later be used to properly serialize MsgGrant and MsgExec + // instances. RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/evidence/types/codec.go b/x/evidence/types/codec.go index f7de91ac18..f7abd556df 100644 --- a/x/evidence/types/codec.go +++ b/x/evidence/types/codec.go @@ -9,6 +9,7 @@ import ( "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" "github.com/line/lbm-sdk/x/evidence/exported" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers all the necessary types and interfaces for the @@ -40,7 +41,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/feegrant/codec.go b/x/feegrant/codec.go index b74a71e20a..fc1868f31e 100644 --- a/x/feegrant/codec.go +++ b/x/feegrant/codec.go @@ -8,6 +8,7 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the necessary x/feegrant interfaces and concrete types @@ -57,7 +58,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/gov/codec/cdc.go b/x/gov/codec/cdc.go new file mode 100644 index 0000000000..ba6fa1ed87 --- /dev/null +++ b/x/gov/codec/cdc.go @@ -0,0 +1,18 @@ +package codec + +import ( + "github.com/line/lbm-sdk/codec" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" + sdk "github.com/line/lbm-sdk/types" +) + +var ( + Amino = codec.NewLegacyAmino() + ModuleCdc = codec.NewAminoCodec(Amino) +) + +func init() { + cryptocodec.RegisterCrypto(Amino) + codec.RegisterEvidences(Amino) + sdk.RegisterLegacyAminoCodec(Amino) +} diff --git a/x/gov/codec/doc.go b/x/gov/codec/doc.go new file mode 100644 index 0000000000..574a18d57c --- /dev/null +++ b/x/gov/codec/doc.go @@ -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 MsgSubmitProposal 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(govcodec.Amino) + + } + +The codec instance is put inside this package and not the x/gov/types package in order to avoid any dependency cycle. +*/ +package codec diff --git a/x/gov/simulation/operations_test.go b/x/gov/simulation/operations_test.go index 35411cdbab..05d5c28e2d 100644 --- a/x/gov/simulation/operations_test.go +++ b/x/gov/simulation/operations_test.go @@ -14,6 +14,7 @@ import ( simappparams "github.com/line/lbm-sdk/simapp/params" sdk "github.com/line/lbm-sdk/types" simtypes "github.com/line/lbm-sdk/types/simulation" + govcodec "github.com/line/lbm-sdk/x/gov/codec" "github.com/line/lbm-sdk/x/gov/simulation" "github.com/line/lbm-sdk/x/gov/types" minttypes "github.com/line/lbm-sdk/x/mint/types" @@ -113,7 +114,7 @@ func TestSimulateMsgSubmitProposal(t *testing.T) { require.NoError(t, err) var msg types.MsgSubmitProposal - err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) @@ -157,7 +158,7 @@ func TestSimulateMsgDeposit(t *testing.T) { require.NoError(t, err) var msg types.MsgDeposit - err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) @@ -200,7 +201,7 @@ func TestSimulateMsgVote(t *testing.T) { require.NoError(t, err) var msg types.MsgVote - err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) @@ -243,7 +244,7 @@ func TestSimulateMsgVoteWeighted(t *testing.T) { require.NoError(t, err) var msg types.MsgVoteWeighted - err = types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) + err = govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg) require.NoError(t, err) require.True(t, operationMsg.OK) diff --git a/x/gov/types/codec.go b/x/gov/types/codec.go index 8a7aea4d00..5a38d0946f 100644 --- a/x/gov/types/codec.go +++ b/x/gov/types/codec.go @@ -4,10 +4,10 @@ import ( "github.com/line/lbm-sdk/codec" "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" - cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers all the necessary types and interfaces for the @@ -37,17 +37,9 @@ func RegisterInterfaces(registry types.InterfaceRegistry) { msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc) } -var ( - amino = codec.NewLegacyAmino() - ModuleCdc = codec.NewAminoCodec(amino) -) - func init() { - 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/gov/types/msgs.go b/x/gov/types/msgs.go index 65b942aa15..0139e80e53 100644 --- a/x/gov/types/msgs.go +++ b/x/gov/types/msgs.go @@ -9,6 +9,7 @@ import ( "github.com/line/lbm-sdk/codec/types" sdk "github.com/line/lbm-sdk/types" sdkerrors "github.com/line/lbm-sdk/types/errors" + "github.com/line/lbm-sdk/x/gov/codec" ) // Governance message types and routes @@ -108,7 +109,7 @@ func (m MsgSubmitProposal) ValidateBasic() error { // GetSignBytes implements Msg func (m MsgSubmitProposal) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&m) + bz := codec.ModuleCdc.MustMarshalJSON(&m) return sdk.MustSortJSON(bz) } @@ -165,7 +166,7 @@ func (msg MsgDeposit) String() string { // GetSignBytes implements Msg func (msg MsgDeposit) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := codec.ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -207,7 +208,7 @@ func (msg MsgVote) String() string { // GetSignBytes implements Msg func (msg MsgVote) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := codec.ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } @@ -271,7 +272,7 @@ func (msg MsgVoteWeighted) String() string { // GetSignBytes implements Msg func (msg MsgVoteWeighted) GetSignBytes() []byte { - bz := ModuleCdc.MustMarshalJSON(&msg) + bz := codec.ModuleCdc.MustMarshalJSON(&msg) return sdk.MustSortJSON(bz) } diff --git a/x/mint/types/codec.go b/x/mint/types/codec.go index 77698e667c..e4ef0fc2b8 100644 --- a/x/mint/types/codec.go +++ b/x/mint/types/codec.go @@ -3,6 +3,7 @@ package types import ( "github.com/line/lbm-sdk/codec" cryptocodec "github.com/line/lbm-sdk/crypto/codec" + sdk "github.com/line/lbm-sdk/types" ) var ( @@ -11,5 +12,5 @@ var ( func init() { cryptocodec.RegisterCrypto(amino) - amino.Seal() + sdk.RegisterLegacyAminoCodec(amino) } diff --git a/x/slashing/types/codec.go b/x/slashing/types/codec.go index 0a8bf8aa8c..17ea68c315 100644 --- a/x/slashing/types/codec.go +++ b/x/slashing/types/codec.go @@ -8,6 +8,7 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers concrete types on LegacyAmino codec @@ -33,7 +34,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/staking/types/codec.go b/x/staking/types/codec.go index ee68780589..deeea4740c 100644 --- a/x/staking/types/codec.go +++ b/x/staking/types/codec.go @@ -9,6 +9,7 @@ import ( "github.com/line/lbm-sdk/types/msgservice" "github.com/line/lbm-sdk/x/authz" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" ) // RegisterLegacyAminoCodec registers the necessary x/staking interfaces and concrete types @@ -53,7 +54,8 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/upgrade/types/codec.go b/x/upgrade/types/codec.go index 5d806345d9..a888019cb9 100644 --- a/x/upgrade/types/codec.go +++ b/x/upgrade/types/codec.go @@ -6,6 +6,7 @@ import ( cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" ) @@ -34,7 +35,9 @@ func init() { 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 + // Register all Amino interfaces and concrete types on the authz and gov Amino codec + // so that this can later be used to properly serialize MsgGrant and MsgExec + // instances. RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/wasm/lbmtypes/codec.go b/x/wasm/lbmtypes/codec.go index 0af8b2c484..7a643f4510 100644 --- a/x/wasm/lbmtypes/codec.go +++ b/x/wasm/lbmtypes/codec.go @@ -2,10 +2,12 @@ package lbmtypes import ( "github.com/line/lbm-sdk/codec" - "github.com/line/lbm-sdk/codec/legacy" "github.com/line/lbm-sdk/codec/types" + cryptocodec "github.com/line/lbm-sdk/crypto/codec" sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" + authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" wasmTypes "github.com/line/lbm-sdk/x/wasm/types" ) @@ -33,6 +35,18 @@ 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 and gov Amino codec so that this can later be + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances + RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } diff --git a/x/wasm/types/codec.go b/x/wasm/types/codec.go index 240d28c259..97e8425dde 100644 --- a/x/wasm/types/codec.go +++ b/x/wasm/types/codec.go @@ -7,6 +7,7 @@ import ( sdk "github.com/line/lbm-sdk/types" "github.com/line/lbm-sdk/types/msgservice" authzcodec "github.com/line/lbm-sdk/x/authz/codec" + govcodec "github.com/line/lbm-sdk/x/gov/codec" govtypes "github.com/line/lbm-sdk/x/gov/types" ) @@ -73,6 +74,7 @@ func init() { 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 + // used to properly serialize MsgGrant, MsgExec and MsgSubmitProposal instances RegisterLegacyAminoCodec(authzcodec.Amino) + RegisterLegacyAminoCodec(govcodec.Amino) } From 2e10f5298a654dd901542d3ea579acc4e9e4e5a3 Mon Sep 17 00:00:00 2001 From: zemyblue Date: Wed, 19 Oct 2022 10:43:39 +0900 Subject: [PATCH 7/7] chore: update changelog. Signed-off-by: zemyblue --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65cebf1935..c902260d7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -84,6 +84,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (x/bank) [\#716](https://github.com/line/lbm-sdk/pull/716) remove useless DenomMetadata key function * (x/foundation) [\#704](https://github.com/line/lbm-sdk/pull/704) update x/foundation params * (x/wasm) [\#695](https://github.com/line/lbm-sdk/pull/695) fix to prevent external filesystem dependency of simulation +* (amino) [\#736](https://github.com/line/lbm-sdk/pull/736) apply the missing amino codec registratoin of cosmos-sdk ### Bug Fixes * (x/wasm) [\#453](https://github.com/line/lbm-sdk/pull/453) modify wasm grpc query api path