Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow legacy and upstream message types #530

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions custom/auth/ante/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"time"

custombanktypes "github.com/classic-terra/core/v3/custom/bank/types"
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -1057,3 +1059,60 @@ func (s *AnteTestSuite) TestOracleZeroFee() {
balances = s.app.BankKeeper.GetAllBalances(s.ctx, s.app.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName))
s.Require().Equal(sdk.Coins{}, balances)
}

func (s *AnteTestSuite) TestLegacyAndUpstreamMsgSendCompatibility() {
// Setup the test environment
s.SetupTest(true)

// Initialize the codec
amino := codec.NewLegacyAmino()
custombanktypes.RegisterLegacyAminoCodec(amino) // Your custom registration
amino.Seal()

require := s.Require()

// Test message
addr1 := "cosmos1addressfrom"
addr2 := "cosmos1addressto"
amount := sdk.NewCoins(sdk.NewInt64Coin("atom", 1000))

// Create messages
customMsg := custombanktypes.LegacyMsgSend{MsgSend: banktypes.MsgSend{FromAddress: addr1, ToAddress: addr2, Amount: amount}}
upstreamMsg := banktypes.MsgSend{FromAddress: addr1, ToAddress: addr2, Amount: amount}

// Encode and decode using "bank/MsgSend"
bankJSON, err := amino.MarshalJSON(&customMsg)
require.NoError(err, "Failed to encode LegacyMsgSend with bank/MsgSend")

var decodedCustomMsg custombanktypes.LegacyMsgSend
err = amino.UnmarshalJSON(bankJSON, &decodedCustomMsg)
require.NoError(err, "Failed to decode LegacyMsgSend with bank/MsgSend")
require.Equal(customMsg, decodedCustomMsg, "LegacyMsgSend (bank/MsgSend) mismatch after decoding")

// construct a custom byte message
msg := json.RawMessage([]byte(`{"type":"bank/MsgSend","value":{"from_address":"cosmos1addressfrom","to_address":"cosmos1addressto","amount":[{"denom":"atom","amount":"1000"}]}}`))
err = amino.UnmarshalJSON(msg, &decodedCustomMsg)
require.NoError(err, "Failed to decode LegacyMsgSend with bank/MsgSend")
require.Equal(customMsg, decodedCustomMsg, "LegacyMsgSend (bank/MsgSend) mismatch after decoding")

// Encode and decode using "cosmos-sdk/MsgSend"
cosmosJSON, err := amino.MarshalJSON(&upstreamMsg)
require.NoError(err, "Failed to encode MsgSend with cosmos-sdk/MsgSend")

var decodedUpstreamMsg banktypes.MsgSend
err = amino.UnmarshalJSON(cosmosJSON, &decodedUpstreamMsg)
require.NoError(err, "Failed to decode MsgSend with cosmos-sdk/MsgSend")
require.Equal(upstreamMsg, decodedUpstreamMsg, "MsgSend (cosmos-sdk/MsgSend) mismatch after decoding")

// construct a custom upstream byte message
msg = json.RawMessage([]byte(`{"type":"cosmos-sdk/MsgSend","value":{"from_address":"cosmos1addressfrom","to_address":"cosmos1addressto","amount":[{"denom":"atom","amount":"1000"}]}}`))
err = amino.UnmarshalJSON(msg, &decodedUpstreamMsg)
require.NoError(err, "Failed to decode MsgSend with cosmos-sdk/MsgSend")
require.Equal(upstreamMsg, decodedUpstreamMsg, "MsgSend (cosmos-sdk/MsgSend) mismatch after decoding")

// Ensure TypeURLs match
customTypeURL := sdk.MsgTypeURL(&customMsg.MsgSend)
upstreamTypeURL := sdk.MsgTypeURL(&upstreamMsg)
require.Equal("/cosmos.bank.v1beta1.MsgSend", customTypeURL, "LegacyMsgSend TypeURL mismatch")
require.Equal("/cosmos.bank.v1beta1.MsgSend", upstreamTypeURL, "MsgSend TypeURL mismatch")
}
52 changes: 46 additions & 6 deletions custom/auth/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,61 @@
package types

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/x/auth/migrations/legacytx"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

// Wrapper type for backward compatibility
type LegacyStdTx struct {
legacytx.StdTx
}

// Wrapper type for backward compatibility
type LegacyBaseAccount struct {
types.BaseAccount
}

// Wrapper type for backward compatibility
type LegacyModuleAccount struct {
types.ModuleAccount
}

func (l LegacyStdTx) MarshalJSON() ([]byte, error) {
return json.Marshal(l.StdTx)
}

func (l *LegacyStdTx) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.StdTx)
}

func (l LegacyBaseAccount) MarshalJSON() ([]byte, error) {
return json.Marshal(l.BaseAccount)
}

func (l *LegacyBaseAccount) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.BaseAccount)
}

func (l LegacyModuleAccount) MarshalJSON() ([]byte, error) {
return json.Marshal(l.ModuleAccount)
}

func (l *LegacyModuleAccount) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.ModuleAccount)
}

// RegisterLegacyAminoCodec registers the account interfaces and concrete types on the
// provided LegacyAmino codec. These types are used for Amino JSON serialization
func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) {
cdc.RegisterInterface((*types.ModuleAccountI)(nil), nil)
cdc.RegisterInterface((*types.GenesisAccount)(nil), nil)
cdc.RegisterInterface((*types.AccountI)(nil), nil)
cdc.RegisterConcrete(&types.BaseAccount{}, "core/Account", nil)
cdc.RegisterConcrete(&types.ModuleAccount{}, "core/ModuleAccount", nil)
cdc.RegisterConcrete(legacytx.StdTx{}, "core/StdTx", nil)
types.RegisterLegacyAminoCodec(cdc)

cdc.RegisterConcrete(&LegacyBaseAccount{}, "core/Account", nil)
cdc.RegisterConcrete(&LegacyModuleAccount{}, "core/ModuleAccount", nil)
cdc.RegisterConcrete(LegacyStdTx{}, "core/StdTx", nil)
}

var (
Expand Down
65 changes: 60 additions & 5 deletions custom/authz/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,73 @@
package types

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
"github.com/cosmos/cosmos-sdk/x/authz"
)

// Wrapper type for backward compatibility
type LegacyMsgGrant struct {
authz.MsgGrant
}

// Wrapper type for backward compatibility
type LegacyMsgRevoke struct {
authz.MsgRevoke
}

// Wrapper type for backward compatibility
type LegacyMsgExec struct {
authz.MsgExec
}

// Wrapper type for backward compatibility
type LegacyGenericAuthorization struct {
authz.GenericAuthorization
}

func (l LegacyMsgGrant) MarshalJSON() ([]byte, error) {
return json.Marshal(l.MsgGrant)
}

func (l *LegacyMsgGrant) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.MsgGrant)
}

func (l LegacyMsgRevoke) MarshalJSON() ([]byte, error) {
return json.Marshal(l.MsgRevoke)
}

func (l *LegacyMsgRevoke) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.MsgRevoke)
}

func (l LegacyMsgExec) MarshalJSON() ([]byte, error) {
return json.Marshal(l.MsgExec)
}

func (l *LegacyMsgExec) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.MsgExec)
}

func (l LegacyGenericAuthorization) MarshalJSON() ([]byte, error) {
return json.Marshal(l.GenericAuthorization)
}

func (l *LegacyGenericAuthorization) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.GenericAuthorization)
}

// 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) {
legacy.RegisterAminoMsg(cdc, &authz.MsgGrant{}, "msgauth/MsgGrantAuthorization")
legacy.RegisterAminoMsg(cdc, &authz.MsgRevoke{}, "msgauth/MsgRevokeAuthorization")
legacy.RegisterAminoMsg(cdc, &authz.MsgExec{}, "msgauth/MsgExecAuthorized")
authz.RegisterLegacyAminoCodec(cdc)

legacy.RegisterAminoMsg(cdc, &LegacyMsgGrant{}, "msgauth/MsgGrantAuthorization")
legacy.RegisterAminoMsg(cdc, &LegacyMsgRevoke{}, "msgauth/MsgRevokeAuthorization")
legacy.RegisterAminoMsg(cdc, &LegacyMsgExec{}, "msgauth/MsgExecAuthorized")

cdc.RegisterInterface((*authz.Authorization)(nil), nil)
cdc.RegisterConcrete(&authz.GenericAuthorization{}, "msgauth/GenericAuthorization", nil)
cdc.RegisterConcrete(&LegacyGenericAuthorization{}, "msgauth/GenericAuthorization", nil)
}
49 changes: 46 additions & 3 deletions custom/bank/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,61 @@
package types

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

// Wrapper type for backward compatibility
type LegacyMsgSend struct {
types.MsgSend
}

// Wrapper type for backward compatibility
type LegacyMsgMultiSend struct {
types.MsgMultiSend
}

type LegacySendAuthorization struct {
types.SendAuthorization
}

func (m LegacyMsgSend) MarshalJSON() ([]byte, error) {
return json.Marshal(m.MsgSend)
}

func (m *LegacyMsgSend) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.MsgSend)
}

func (m LegacyMsgMultiSend) MarshalJSON() ([]byte, error) {
return json.Marshal(m.MsgMultiSend)
}

func (m *LegacyMsgMultiSend) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.MsgMultiSend)
}

func (m LegacySendAuthorization) MarshalJSON() ([]byte, error) {
return json.Marshal(m.SendAuthorization)
}

func (m *LegacySendAuthorization) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &m.SendAuthorization)
}

// 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) {
legacy.RegisterAminoMsg(cdc, &types.MsgSend{}, "bank/MsgSend")
legacy.RegisterAminoMsg(cdc, &types.MsgMultiSend{}, "bank/MsgMultiSend")
cdc.RegisterConcrete(&types.SendAuthorization{}, "msgauth/SendAuthorization", nil)
// register the normal bank message types
types.RegisterLegacyAminoCodec(cdc)

legacy.RegisterAminoMsg(cdc, &LegacyMsgSend{}, "bank/MsgSend")
legacy.RegisterAminoMsg(cdc, &LegacyMsgMultiSend{}, "bank/MsgMultiSend")
cdc.RegisterConcrete(&LegacySendAuthorization{}, "msgauth/SendAuthorization", nil)
}

var (
Expand Down
18 changes: 17 additions & 1 deletion custom/crisis/types/codec.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package types

import (
"encoding/json"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/x/crisis/types"
)

// Wrapper type for backward compatibility
type LegacyMsgVerifyInvariant struct {
types.MsgVerifyInvariant
}

func (l LegacyMsgVerifyInvariant) MarshalJSON() ([]byte, error) {
return json.Marshal(l.MsgVerifyInvariant)
}

func (l *LegacyMsgVerifyInvariant) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &l.MsgVerifyInvariant)
}

// 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) {
legacy.RegisterAminoMsg(cdc, &types.MsgVerifyInvariant{}, "crisis/MsgVerifyInvariant")
types.RegisterLegacyAminoCodec(cdc)
legacy.RegisterAminoMsg(cdc, &LegacyMsgVerifyInvariant{}, "crisis/MsgVerifyInvariant")
}

var (
Expand Down
Loading
Loading