Skip to content

Commit

Permalink
fix!: Fix gov amino codec (cosmos#13196)
Browse files Browse the repository at this point in the history
* fix!: Fix gov amino codec

* changelog
  • Loading branch information
amaury1093 authored and clevinson committed Dec 15, 2022
1 parent f71df80 commit 2873715
Show file tree
Hide file tree
Showing 23 changed files with 166 additions and 78 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ Ref: https://keepachangelog.com/en/1.0.0/

# Changelog

## [Unreleased]
## [v0.46.7-regen-1](https://github.com/regen/cosmos-sdk/releases/tag/v0.46.7-regen-1) - 2022-12-XX

### State Machine Breaking

* (codec) [#13196](https://github.com/cosmos/cosmos-sdk/pull/13196) Register all modules' `Msg`s with gov's ModuleCdc so that Amino sign bytes are correctly generated.

## [v0.46.7](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.46.7) - 2022-12-13

Expand Down
14 changes: 9 additions & 5 deletions docs/core/encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```

Expand Down
6 changes: 4 additions & 2 deletions x/auth/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
Expand Down Expand Up @@ -49,7 +50,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)
}
6 changes: 4 additions & 2 deletions x/auth/vesting/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting/exported"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers the vesting interfaces and concrete types on the
Expand Down Expand Up @@ -74,7 +75,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)
}
6 changes: 4 additions & 2 deletions x/authz/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/authz interfaces and concrete types
Expand Down Expand Up @@ -38,7 +39,8 @@ func RegisterInterfaces(registry types.InterfaceRegistry) {
}

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)
}
6 changes: 4 additions & 2 deletions x/bank/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/msgservice"
"github.com/cosmos/cosmos-sdk/x/authz"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/bank interfaces and concrete types
Expand Down Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions x/crisis/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/crisis interfaces and concrete types
Expand All @@ -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)
}
7 changes: 5 additions & 2 deletions x/distribution/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)

Expand Down Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions x/evidence/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers all the necessary types and interfaces for the
Expand Down Expand Up @@ -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)
}
6 changes: 4 additions & 2 deletions x/feegrant/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers the necessary x/feegrant interfaces and concrete types
Expand Down Expand Up @@ -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)
}
18 changes: 18 additions & 0 deletions x/gov/codec/cdc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package codec

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

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

func init() {
cryptocodec.RegisterCrypto(Amino)
codec.RegisterEvidences(Amino)
sdk.RegisterLegacyAminoCodec(Amino)
}
18 changes: 18 additions & 0 deletions x/gov/codec/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Package codec provides a singleton instance of Amino codec that should be used to register
any concrete type that can later be referenced inside a 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
9 changes: 5 additions & 4 deletions x/gov/simulation/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
"github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
Expand Down Expand Up @@ -114,7 +115,7 @@ func TestSimulateMsgSubmitProposal(t *testing.T) {
require.NoError(t, err)

var msg v1.MsgSubmitProposal
err = v1.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
err = govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
require.NoError(t, err)

require.True(t, operationMsg.OK)
Expand Down Expand Up @@ -161,7 +162,7 @@ func TestSimulateMsgDeposit(t *testing.T) {
require.NoError(t, err)

var msg v1.MsgDeposit
err = v1.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
err = govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
require.NoError(t, err)

require.True(t, operationMsg.OK)
Expand Down Expand Up @@ -207,7 +208,7 @@ func TestSimulateMsgVote(t *testing.T) {
require.NoError(t, err)

var msg v1.MsgVote
v1.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

require.True(t, operationMsg.OK)
require.Equal(t, uint64(1), msg.ProposalId)
Expand Down Expand Up @@ -250,7 +251,7 @@ func TestSimulateMsgVoteWeighted(t *testing.T) {
require.NoError(t, err)

var msg v1.MsgVoteWeighted
v1.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
govcodec.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)

require.True(t, operationMsg.OK)
require.Equal(t, uint64(1), msg.ProposalId)
Expand Down
18 changes: 4 additions & 14 deletions x/gov/types/v1/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/msgservice"
authzcodec "github.com/cosmos/cosmos-sdk/x/authz/codec"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
govcodec "github.com/cosmos/cosmos-sdk/x/gov/codec"
)

// RegisterLegacyAminoCodec registers all the necessary types and interfaces for the
Expand All @@ -33,18 +32,9 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
}

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

func init() {
RegisterLegacyAminoCodec(amino)
v1beta1.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)
}
9 changes: 5 additions & 4 deletions x/gov/types/v1/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdktx "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/x/gov/codec"
"github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
)
Expand Down Expand Up @@ -83,7 +84,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)
}

Expand Down Expand Up @@ -129,7 +130,7 @@ func (msg MsgDeposit) ValidateBasic() error {

// GetSignBytes implements Msg
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
bz := codec.ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

Expand Down Expand Up @@ -166,7 +167,7 @@ func (msg MsgVote) ValidateBasic() error {

// GetSignBytes implements Msg
func (msg MsgVote) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
bz := codec.ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

Expand Down Expand Up @@ -228,7 +229,7 @@ func (msg MsgVoteWeighted) ValidateBasic() error {

// GetSignBytes implements Msg
func (msg MsgVoteWeighted) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
bz := codec.ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}

Expand Down
Loading

0 comments on commit 2873715

Please sign in to comment.