Skip to content

Commit

Permalink
chore(x/): use cosmossdk.io/core/codec instead of `github.com/cosmo…
Browse files Browse the repository at this point in the history
…s/cosmos-sdk/codec` (#23313)

(cherry picked from commit bc9ce39)

# Conflicts:
#	x/bank/v2/module.go
#	x/mint/module.go
  • Loading branch information
caseylove authored and mergify[bot] committed Jan 13, 2025
1 parent d828a91 commit 89b8891
Show file tree
Hide file tree
Showing 34 changed files with 406 additions and 106 deletions.
2 changes: 1 addition & 1 deletion x/accounts/defaults/base/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
signingv1beta1 "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
"cosmossdk.io/collections"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/event"
"cosmossdk.io/core/header"
"cosmossdk.io/core/store"
Expand All @@ -17,7 +18,6 @@ import (
accountsv1 "cosmossdk.io/x/accounts/v1"
"cosmossdk.io/x/tx/signing"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
Expand Down
3 changes: 1 addition & 2 deletions x/accounts/internal/implementation/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/transaction"

"github.com/cosmos/cosmos-sdk/codec"
)

// Dependencies are passed to the constructor of a smart account.
Expand Down
8 changes: 6 additions & 2 deletions x/accounts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/registry"
"cosmossdk.io/schema"
"cosmossdk.io/x/accounts/cli"
v1 "cosmossdk.io/x/accounts/v1"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/address"
"github.com/cosmos/cosmos-sdk/types/msgservice"
)
Expand Down Expand Up @@ -66,7 +66,11 @@ func (am AppModule) RegisterServices(registrar grpc.ServiceRegistrar) error {
// App module genesis

func (am AppModule) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(&v1.GenesisState{})
data, err := am.cdc.MarshalJSON(&v1.GenesisState{})
if err != nil {
panic(err)
}
return data
}

func (am AppModule) ValidateGenesis(message json.RawMessage) error {
Expand Down
7 changes: 5 additions & 2 deletions x/bank/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"cosmossdk.io/core/codec"

sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -101,7 +102,9 @@ func GetGenesisStateFromAppState(cdc codec.JSONCodec, appState map[string]json.R
var genesisState GenesisState

if appState[ModuleName] != nil {
cdc.MustUnmarshalJSON(appState[ModuleName], &genesisState)
if err := cdc.UnmarshalJSON(appState[ModuleName], &genesisState); err != nil {
panic(err)
}
}

return &genesisState
Expand Down
124 changes: 124 additions & 0 deletions x/bank/v2/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package bankv2

import (
"context"
"encoding/json"
"fmt"

"github.com/spf13/cobra"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/registry"
"cosmossdk.io/x/bank/v2/client/cli"
"cosmossdk.io/x/bank/v2/keeper"
"cosmossdk.io/x/bank/v2/types"
)

// ConsensusVersion defines the current x/bank/v2 module consensus version.
const ConsensusVersion = 1

var (
_ appmodulev2.AppModule = AppModule{}
_ appmodulev2.HasGenesis = AppModule{}
_ appmodulev2.HasRegisterInterfaces = AppModule{}
_ appmodulev2.HasQueryHandlers = AppModule{}
_ appmodulev2.HasMsgHandlers = AppModule{}
)

// AppModule implements an application module for the bank module.
type AppModule struct {
cdc codec.Codec
keeper *keeper.Keeper
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper *keeper.Keeper) AppModule {
return AppModule{
cdc: cdc,
keeper: keeper,
}
}

// IsAppModule implements the appmodule.AppModule interface.
func (am AppModule) IsAppModule() {}

// Name returns the bank module's name.
// Deprecated: kept for legacy reasons.
func (AppModule) Name() string { return types.ModuleName }

// ConsensusVersion implements HasConsensusVersion
func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// RegisterInterfaces registers interfaces and implementations of the bank module.
func (AppModule) RegisterInterfaces(registrar registry.InterfaceRegistrar) {
types.RegisterInterfaces(registrar)
}

// DefaultGenesis returns default genesis state as raw bytes for the bank module.
func (am AppModule) DefaultGenesis() json.RawMessage {
data, err := am.cdc.MarshalJSON(types.DefaultGenesisState())
if err != nil {
panic(err)
}
return data
}

// ValidateGenesis performs genesis state validation for the bank module.
func (am AppModule) ValidateGenesis(bz json.RawMessage) error {
var data types.GenesisState
if err := am.cdc.UnmarshalJSON(bz, &data); err != nil {
return fmt.Errorf("failed to unmarshal %s genesis state: %w", types.ModuleName, err)
}

return data.Validate()
}

// InitGenesis performs genesis initialization for the bank module.
func (am AppModule) InitGenesis(ctx context.Context, data json.RawMessage) error {
var genesisState types.GenesisState
if err := am.cdc.UnmarshalJSON(data, &genesisState); err != nil {
return err
}

return am.keeper.InitGenesis(ctx, &genesisState)
}

// ExportGenesis returns the exported genesis state as raw bytes for the bank/v2 module.
func (am AppModule) ExportGenesis(ctx context.Context) (json.RawMessage, error) {
gs, err := am.keeper.ExportGenesis(ctx)
if err != nil {
return nil, err
}

return am.cdc.MarshalJSON(gs)
}

// RegisterMsgHandlers registers the message handlers for the bank module.
func (am AppModule) RegisterMsgHandlers(router appmodulev2.MsgRouter) {
handlers := keeper.NewHandlers(am.keeper)

appmodulev2.RegisterMsgHandler(router, handlers.MsgUpdateParams)
appmodulev2.RegisterMsgHandler(router, handlers.MsgSend)
appmodulev2.RegisterMsgHandler(router, handlers.MsgMint)
}

// RegisterQueryHandlers registers the query handlers for the bank module.
func (am AppModule) RegisterQueryHandlers(router appmodulev2.QueryRouter) {
handlers := keeper.NewHandlers(am.keeper)

appmodulev2.RegisterMsgHandler(router, handlers.QueryParams)
appmodulev2.RegisterMsgHandler(router, handlers.QueryBalance)
}

// GetTxCmd returns the root tx command for the bank/v2 module.
// TODO: Remove & use autocli
func (AppModule) GetTxCmd() *cobra.Command {
return cli.NewTxCmd()
}

// GetQueryCmd returns the root query command for the bank/v2 module.
// TODO: Remove & use autocli
func (AppModule) GetQueryCmd() *cobra.Command {
return cli.GetQueryCmd()
}
8 changes: 6 additions & 2 deletions x/circuit/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/transaction"
"cosmossdk.io/schema"
Expand All @@ -19,7 +20,6 @@ import (
"cosmossdk.io/x/circuit/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
)

Expand Down Expand Up @@ -81,7 +81,11 @@ func (AppModule) ConsensusVersion() uint64 { return ConsensusVersion }

// DefaultGenesis returns default genesis state as raw bytes for the circuit module.
func (am AppModule) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(types.DefaultGenesisState())
data, err := am.cdc.MarshalJSON(types.DefaultGenesisState())
if err != nil {
panic(err)
}
return data
}

// ValidateGenesis performs genesis state validation for the circuit module.
Expand Down
2 changes: 1 addition & 1 deletion x/consensus/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/registry"
"cosmossdk.io/schema"
"cosmossdk.io/x/consensus/keeper"
"cosmossdk.io/x/consensus/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
)

Expand Down
7 changes: 5 additions & 2 deletions x/distribution/migrations/v4/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
gogotypes "github.com/cosmos/gogoproto/types"

"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/store"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -46,6 +46,9 @@ func GetPreviousProposerConsAddr(ctx context.Context, storeService store.KVStore
// SetPreviousProposerConsAddr set the proposer public key for this block.
func SetPreviousProposerConsAddr(ctx context.Context, storeService store.KVStoreService, cdc codec.BinaryCodec, consAddr sdk.ConsAddress) error {
kvStore := storeService.OpenKVStore(ctx)
bz := cdc.MustMarshal(&gogotypes.BytesValue{Value: consAddr})
bz, err := cdc.Marshal(&gogotypes.BytesValue{Value: consAddr})
if err != nil {
panic(err)
}
return kvStore.Set(OldProposerKey, bz)
}
8 changes: 6 additions & 2 deletions x/distribution/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"cosmossdk.io/collections"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/codec"
"cosmossdk.io/core/registry"
"cosmossdk.io/schema"
"cosmossdk.io/x/distribution/client/cli"
Expand All @@ -19,7 +20,6 @@ import (
"cosmossdk.io/x/distribution/types"

sdkclient "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simsx"
"github.com/cosmos/cosmos-sdk/types/module"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -115,7 +115,11 @@ func (am AppModule) RegisterMigrations(mr appmodule.MigrationRegistrar) error {

// DefaultGenesis returns default genesis state as raw bytes for the distribution module.
func (am AppModule) DefaultGenesis() json.RawMessage {
return am.cdc.MustMarshalJSON(types.DefaultGenesisState())
data, err := am.cdc.MarshalJSON(types.DefaultGenesisState())
if err != nil {
panic(err)
}
return data
}

// ValidateGenesis performs genesis state validation for the distribution module.
Expand Down
58 changes: 43 additions & 15 deletions x/distribution/simulation/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"fmt"

"cosmossdk.io/core/codec"
"cosmossdk.io/x/distribution/types"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/kv"
)
Expand All @@ -18,47 +18,75 @@ func NewDecodeStore(cdc codec.Codec) func(kvA, kvB kv.Pair) string {
switch {
case bytes.Equal(kvA.Key[:1], types.FeePoolKey):
var feePoolA, feePoolB types.FeePool
cdc.MustUnmarshal(kvA.Value, &feePoolA)
cdc.MustUnmarshal(kvB.Value, &feePoolB)
if err := cdc.Unmarshal(kvA.Value, &feePoolA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &feePoolB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", feePoolA, feePoolB)

case bytes.Equal(kvA.Key[:1], types.ValidatorOutstandingRewardsPrefix):
var rewardsA, rewardsB types.ValidatorOutstandingRewards
cdc.MustUnmarshal(kvA.Value, &rewardsA)
cdc.MustUnmarshal(kvB.Value, &rewardsB)
if err := cdc.Unmarshal(kvA.Value, &rewardsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &rewardsB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)

case bytes.Equal(kvA.Key[:1], types.DelegatorWithdrawAddrPrefix):
return fmt.Sprintf("%v\n%v", sdk.AccAddress(kvA.Value), sdk.AccAddress(kvB.Value))

case bytes.Equal(kvA.Key[:1], types.DelegatorStartingInfoPrefix):
var infoA, infoB types.DelegatorStartingInfo
cdc.MustUnmarshal(kvA.Value, &infoA)
cdc.MustUnmarshal(kvB.Value, &infoB)
if err := cdc.Unmarshal(kvA.Value, &infoA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &infoB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", infoA, infoB)

case bytes.Equal(kvA.Key[:1], types.ValidatorHistoricalRewardsPrefix):
var rewardsA, rewardsB types.ValidatorHistoricalRewards
cdc.MustUnmarshal(kvA.Value, &rewardsA)
cdc.MustUnmarshal(kvB.Value, &rewardsB)
if err := cdc.Unmarshal(kvA.Value, &rewardsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &rewardsB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)

case bytes.Equal(kvA.Key[:1], types.ValidatorCurrentRewardsPrefix):
var rewardsA, rewardsB types.ValidatorCurrentRewards
cdc.MustUnmarshal(kvA.Value, &rewardsA)
cdc.MustUnmarshal(kvB.Value, &rewardsB)
if err := cdc.Unmarshal(kvA.Value, &rewardsA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &rewardsB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", rewardsA, rewardsB)

case bytes.Equal(kvA.Key[:1], types.ValidatorAccumulatedCommissionPrefix):
var commissionA, commissionB types.ValidatorAccumulatedCommission
cdc.MustUnmarshal(kvA.Value, &commissionA)
cdc.MustUnmarshal(kvB.Value, &commissionB)
if err := cdc.Unmarshal(kvA.Value, &commissionA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &commissionB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", commissionA, commissionB)

case bytes.Equal(kvA.Key[:1], types.ValidatorSlashEventPrefix):
var eventA, eventB types.ValidatorSlashEvent
cdc.MustUnmarshal(kvA.Value, &eventA)
cdc.MustUnmarshal(kvB.Value, &eventB)
if err := cdc.Unmarshal(kvA.Value, &eventA); err != nil {
panic(err)
}
if err := cdc.Unmarshal(kvB.Value, &eventB); err != nil {
panic(err)
}
return fmt.Sprintf("%v\n%v", eventA, eventB)

default:
Expand Down
Loading

0 comments on commit 89b8891

Please sign in to comment.