Skip to content

Commit

Permalink
refactor: improve how the encoding config is build
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoM committed Oct 8, 2024
1 parent 822ee7c commit 4d82135
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 68 deletions.
44 changes: 44 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,50 @@ var (
// DefaultNodeHome default home directories for the application daemon
DefaultNodeHome string

ModuleBasics = module.NewBasicManager(
auth.AppModule{},
bank.AppModule{},
crisis.AppModule{},
opchild.AppModule{},
capability.AppModule{},
feegrantmodule.AppModule{},
upgrade.AppModule{},
authzmodule.AppModule{},
groupmodule.AppModule{},
consensus.AppModule{},
wasm.AppModule{},
auction.AppModule{},
tokenfactory.AppModule{},
// ibc modules
ibc.AppModule{},
ibctransfer.AppModule{},
ica.AppModule{},
icaauth.AppModule{},
ibcfee.AppModule{},
ibctm.AppModule{},
solomachine.AppModule{},
packetforward.AppModule{},
ibchooks.AppModule{},
forwarding.AppModule{},
ratelimit.AppModule{},
// connect modules
oracle.AppModule{},
marketmap.AppModule{},
// liquid staking modules
stakeibc.AppModule{},
epochs.AppModule{},
interchainquery.AppModule{},
records.AppModule{},
icacallbacks.AppModule{},
// custom modules
services.AppModule{},
operators.AppModule{},
pools.AppModule{},
restaking.AppModule{},
assets.AppModule{},
rewards.AppModule{},
)

// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
Expand Down
88 changes: 33 additions & 55 deletions app/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,62 @@ package app

import (
"os"
"testing"

"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/log"
"cosmossdk.io/x/tx/signing"
wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/cosmos-sdk/codec"
codecaddress "github.com/cosmos/cosmos-sdk/codec/address"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/std"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/gogoproto/proto"

"github.com/initia-labs/initia/app/params"
)

type EncodingConfigCreator func() params.EncodingConfig

// makeCodecs creates the necessary codecs for Amino and Protobuf using the provided EncodingConfigCreator
func makeCodecs(createConfig EncodingConfigCreator) (codec.Codec, *codec.LegacyAmino) {
encodingConfig := createConfig()
return encodingConfig.Codec, encodingConfig.Amino
}

// MakeCodecs creates the necessary codecs for Amino and Protobuf
func MakeCodecs() (codec.Codec, *codec.LegacyAmino) {
return makeCodecs(MakeEncodingConfig)
}
// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
// Build the interface registry specifying the validator and address codec
interfaceRegistry, _ := codectypes.NewInterfaceRegistryWithOptions(codectypes.InterfaceRegistryOptions{
ProtoFiles: proto.HybridResolver,
SigningOptions: signing.Options{
AddressCodec: codecaddress.NewBech32Codec(sdk.GetConfig().GetBech32AccountAddrPrefix()),
ValidatorAddressCodec: codecaddress.NewBech32Codec(sdk.GetConfig().GetBech32ValidatorAddrPrefix()),
},
})
cdc := codec.NewProtoCodec(interfaceRegistry)
amino := codec.NewLegacyAmino()

// makeEncodingConfig creates an EncodingConfig instance by creating a temporary app with the given options
func makeEncodingConfig(appOptions types.AppOptions) params.EncodingConfig {
tempApp := NewMilkyWayApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, appOptions)
// Build the encoding config
encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
TxConfig: tempApp.TxConfig(),
Amino: tempApp.LegacyAmino(),
InterfaceRegistry: interfaceRegistry,
Codec: cdc,
TxConfig: tx.NewTxConfig(cdc, tx.DefaultSignModes),
Amino: amino,
}
return encodingConfig
}

// MakeEncodingConfig creates an EncodingConfig for testing
func MakeEncodingConfig() params.EncodingConfig {
return makeEncodingConfig(EmptyAppOptions{})
}
// Register the stdlib and module basics
std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)

// MakeTestEncodingConfig creates an EncodingConfig for testing
func MakeTestEncodingConfig(t *testing.T) params.EncodingConfig {
t.Helper()
return makeEncodingConfig(simtestutil.NewAppOptionsWithFlagHome(t.TempDir()))
return encodingConfig
}

// MakeTestCodecs creates the necessary testing codecs for Amino and Protobuf
func MakeTestCodecs(t *testing.T) (codec.Codec, *codec.LegacyAmino) {
t.Helper()
return makeCodecs(func() params.EncodingConfig {
return MakeTestEncodingConfig(t)
})
// MakeCodecs creates the necessary testing codecs for Amino and Protobuf
func MakeCodecs() (codec.Codec, *codec.LegacyAmino) {
encodingConfig := MakeEncodingConfig()
return encodingConfig.Codec, encodingConfig.Amino
}

// AutoCLIOpts returns the options for the auto-generated CLI
Expand Down Expand Up @@ -91,21 +87,3 @@ func AutoCLIOpts() (autocli.AppOptions, error) {
ConsensusAddressCodec: authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
}, nil
}

func BasicManager() module.BasicManager {
tempApp := NewMilkyWayApp(log.NewNopLogger(), dbm.NewMemDB(), dbm.NewMemDB(), nil, true, []wasmkeeper.Option{}, NewEmptyAppOptions())
return tempApp.BasicModuleManager
}

// EmptyAppOptions is a stub implementing AppOptions
type EmptyAppOptions struct {
}

func NewEmptyAppOptions() EmptyAppOptions {
return EmptyAppOptions{}
}

// Get implements AppOptions
func (ao EmptyAppOptions) Get(_ string) interface{} {
return nil
}
12 changes: 6 additions & 6 deletions cmd/milkywayd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/prometheus/client_golang/prometheus"
"github.com/skip-mev/block-sdk/v2/tests/app"

"github.com/spf13/cast"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -70,7 +71,6 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
// sdkConfig.Seal()

encodingConfig := milkywayapp.MakeEncodingConfig()
basicManager := milkywayapp.BasicManager()

// Get the executable name and configure the viper instance so that environmental
// variables are checked based off that name. The underscore character is used
Expand Down Expand Up @@ -138,7 +138,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
},
}

initRootCmd(rootCmd, encodingConfig, basicManager)
initRootCmd(rootCmd, encodingConfig)

initClientCtx, _ = config.ReadFromClientConfig(initClientCtx)

Expand All @@ -157,11 +157,11 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
return rootCmd, encodingConfig
}

func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, basicManager module.BasicManager) {
func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
a := &appCreator{}

rootCmd.AddCommand(
InitCmd(basicManager, milkywayapp.DefaultNodeHome),
InitCmd(app.ModuleBasics, milkywayapp.DefaultNodeHome),
debug.Cmd(),
confixcmd.ConfigCommand(),
pruning.Cmd(a.AppCreator(), milkywayapp.DefaultNodeHome),
Expand All @@ -174,14 +174,14 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, b
// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
server.StatusCommand(),
genesisCommand(encodingConfig, basicManager),
genesisCommand(encodingConfig, app.ModuleBasics),
queryCommand(),
txCommand(),
keys.Commands(),
)

// add launch commands
rootCmd.AddCommand(LaunchCommand(a, encodingConfig, basicManager))
rootCmd.AddCommand(LaunchCommand(a, encodingConfig, app.ModuleBasics))
rootCmd.AddCommand(NewMultipleRollbackCmd(a.AppCreator()))
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ require (
cloud.google.com/go/iam v1.2.0 // indirect
cloud.google.com/go/storage v1.43.0 // indirect
cosmossdk.io/depinject v1.0.0 // indirect
cosmossdk.io/x/circuit v0.1.1 // indirect
cosmossdk.io/x/evidence v0.1.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/4meepo/tagalign v1.3.4 // indirect
Expand Down
2 changes: 1 addition & 1 deletion x/operators/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestBeginBlocker(t *testing.T) {
require.NoError(t, err)

ctx := sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger())
cdc, _ := app.MakeTestCodecs(t)
cdc, _ := app.MakeCodecs()

operatorsKeeper := keeper.NewKeeper(cdc, keys[types.StoreKey], nil, nil, "")

Expand Down
2 changes: 1 addition & 1 deletion x/operators/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (suite *KeeperTestSuite) SetupTest() {
}

suite.ctx = sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger())
suite.cdc, suite.legacyAminoCdc = app.MakeTestCodecs(suite.T())
suite.cdc, suite.legacyAminoCdc = app.MakeCodecs()

// Mock initialization
suite.ctrl = gomock.NewController(suite.T())
Expand Down
2 changes: 1 addition & 1 deletion x/pools/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (suite *KeeperTestSuite) SetupTest() {
}

suite.ctx = sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger())
suite.cdc, suite.legacyAminoCdc = app.MakeTestCodecs(suite.T())
suite.cdc, suite.legacyAminoCdc = app.MakeCodecs()

// Authority address
authorityAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
Expand Down
2 changes: 1 addition & 1 deletion x/restaking/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (suite *KeeperTestSuite) SetupTest() {
}

suite.ctx = sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger())
suite.cdc, suite.legacyAminoCdc = app.MakeTestCodecs(suite.T())
suite.cdc, suite.legacyAminoCdc = app.MakeCodecs()

// Authority address
authorityAddr := authtypes.NewModuleAddress(govtypes.ModuleName).String()
Expand Down
2 changes: 1 addition & 1 deletion x/rewards/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestGenesisState_Validate(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cdc, _ := app.MakeTestCodecs(t)
cdc, _ := app.MakeCodecs()
err := tc.genesis.Validate(cdc)
if tc.shouldErr {
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion x/rewards/types/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TestRewardsPlan_Validate(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cdc, _ := app.MakeTestCodecs(t)
cdc, _ := app.MakeCodecs()
err := tc.plan.Validate(cdc)
if tc.shouldErr {
require.Error(t, err)
Expand Down
2 changes: 1 addition & 1 deletion x/services/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (suite *KeeperTestSuite) SetupTest() {
}

suite.ctx = sdk.NewContext(ms, tmproto.Header{ChainID: "test-chain"}, false, log.NewNopLogger())
suite.cdc, suite.legacyAminoCdc = app.MakeTestCodecs(suite.T())
suite.cdc, suite.legacyAminoCdc = app.MakeCodecs()

// Mocks initializations
suite.ctrl = gomock.NewController(suite.T())
Expand Down

0 comments on commit 4d82135

Please sign in to comment.