Skip to content

Commit

Permalink
Merge pull request #325 from terra-money/fix/app
Browse files Browse the repository at this point in the history
fix: app encodings and remove unnecessary code
  • Loading branch information
emidev98 authored Mar 12, 2024
2 parents a8564db + 76363d4 commit 5221f96
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 350 deletions.
199 changes: 58 additions & 141 deletions app/app.go

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func TestAppExportAndBlockedAddrs(t *testing.T) {
map[int64]bool{},
DefaultNodeHome,
0,
MakeTestEncodingConfig(),
EmptyAppOptions{},
)
blockedAddrs := app.BlockedModuleAccountAddrs()
Expand Down
2 changes: 1 addition & 1 deletion app/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (app *App) ExportAppStateAndValidators(
app.prepForZeroHeightGenesis(ctx, jailAllowedAddrs)
}

genState, err := app.mm.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
genState, err := app.ModuleManager.ExportGenesisForModules(ctx, app.appCodec, modulesToExport)
if err != nil {
return servertypes.ExportedApp{}, err
}
Expand Down
23 changes: 0 additions & 23 deletions app/genesis.go

This file was deleted.

2 changes: 1 addition & 1 deletion app/params/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// This is provided for compatibility between protobuf and amino implementations.
type EncodingConfig struct {
InterfaceRegistry types.InterfaceRegistry
Marshaler codec.Codec
Codec codec.Codec
TxConfig client.TxConfig
Amino *codec.LegacyAmino
}
36 changes: 0 additions & 36 deletions app/params/proto.go

This file was deleted.

4 changes: 0 additions & 4 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestFullAppSimulation(t *testing.T) {
map[int64]bool{},
app.DefaultNodeHome,
simcli.FlagPeriodValue,
app.MakeTestEncodingConfig(),
simtestutil.EmptyAppOptions{},
baseapp.SetChainID(simulationAppChainID),
)
Expand Down Expand Up @@ -107,16 +106,13 @@ func BenchmarkSimulation(b *testing.B) {
require.NoError(b, err)
})

encoding := app.MakeTestEncodingConfig()

app := app.New(logger,
db,
nil,
true,
map[int64]bool{},
app.DefaultNodeHome,
0,
encoding,
simtestutil.EmptyAppOptions{},
)

Expand Down
22 changes: 7 additions & 15 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/testutil/mock"
"github.com/cosmos/cosmos-sdk/testutil/network"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
Expand Down Expand Up @@ -113,15 +112,14 @@ func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs
return app
}

func setup(withGenesis bool, invCheckPeriod uint) (*App, GenesisState) {
func setup(withGenesis bool, invCheckPeriod uint) (*App, map[string]json.RawMessage) {
db := dbm.NewMemDB()
encCdc := MakeTestEncodingConfig()

app := New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, encCdc, EmptyAppOptions{})
app := New(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, invCheckPeriod, EmptyAppOptions{})
if withGenesis {
return app, NewDefaultGenesisState(encCdc.Marshaler)
return app, app.DefaultGenesis()
}
return app, GenesisState{}
return app, app.DefaultGenesis()
}

func MakeTestEncodingConfig() params.EncodingConfig {
Expand All @@ -141,16 +139,11 @@ func MakeTestEncodingConfig() params.EncodingConfig {

encodingConfig := params.EncodingConfig{
InterfaceRegistry: interfaceRegistry,
Marshaler: pcdc,
Codec: pcdc,
TxConfig: tx.NewTxConfig(pcdc, tx.DefaultSignModes),
Amino: cdc,
}

std.RegisterLegacyAminoCodec(encodingConfig.Amino)
std.RegisterInterfaces(encodingConfig.InterfaceRegistry)
ModuleBasics.RegisterLegacyAminoCodec(encodingConfig.Amino)
ModuleBasics.RegisterInterfaces(encodingConfig.InterfaceRegistry)

return encodingConfig
}

Expand Down Expand Up @@ -311,13 +304,12 @@ func NewTestNetworkFixture() network.TestFixture {
map[int64]bool{},
dir,
0,
MakeTestEncodingConfig(),
EmptyAppOptions{},
)
appCtr := func(val network.ValidatorI) servertypes.Application {
return New(
val.GetCtx().Logger, dbm.NewMemDB(), nil, true, map[int64]bool{},
val.GetCtx().Config.RootDir, 0, MakeTestEncodingConfig(),
val.GetCtx().Config.RootDir, 0,
EmptyAppOptions{},
bam.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
bam.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
Expand All @@ -326,7 +318,7 @@ func NewTestNetworkFixture() network.TestFixture {

return network.TestFixture{
AppConstructor: appCtr,
GenesisState: NewDefaultGenesisState(app.AppCodec()),
GenesisState: app.DefaultGenesis(),
EncodingConfig: testutil.TestEncodingConfig{
InterfaceRegistry: app.InterfaceRegistry(),
Codec: app.AppCodec(),
Expand Down
101 changes: 59 additions & 42 deletions cmd/allianced/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import (
"io"
"os"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/genutil"

"github.com/spf13/cast"

Expand All @@ -25,15 +30,17 @@ import (
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"

"github.com/terra-money/alliance/app"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
"github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/crisis"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"

Expand All @@ -43,9 +50,27 @@ import (
// NewRootCmd creates a new root command for simd. It is called once in the
// main function.
func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
encodingConfig := app.MakeTestEncodingConfig()
// we "pre"-instantiate the application for getting the injected/configured encoding configuration
// note, this is not necessary when using app wiring, as depinject can be directly used (see root_v2.go)
tempApp := app.New(log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
app.DefaultNodeHome,
simcli.FlagPeriodValue,
simtestutil.EmptyAppOptions{},
baseapp.SetChainID("alliance-testnet-1"),
)

encodingConfig := params.EncodingConfig{
InterfaceRegistry: tempApp.InterfaceRegistry(),
Codec: tempApp.AppCodec(),
TxConfig: tempApp.TxConfig(),
Amino: tempApp.LegacyAmino(),
}
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Marshaler).
WithCodec(encodingConfig.Codec).
WithInterfaceRegistry(encodingConfig.InterfaceRegistry).
WithTxConfig(encodingConfig.TxConfig).
WithLegacyAmino(encodingConfig.Amino).
Expand All @@ -72,6 +97,22 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
return err
}

// This needs to go after ReadFromClientConfig, as that function
// sets the RPC client needed for SIGN_MODE_TEXTUAL.
enabledSignModes := append(tx.DefaultSignModes, signing.SignMode_SIGN_MODE_TEXTUAL) //nolint:gocritic // we know we aren't appending to the same slice
txConfigOpts := tx.ConfigOptions{
EnabledSignModes: enabledSignModes,
TextualCoinMetadataQueryFn: txmodule.NewGRPCCoinMetadataQueryFn(initClientCtx),
}
txConfigWithTextual, err := tx.NewTxConfigWithOptions(
codec.NewProtoCodec(encodingConfig.InterfaceRegistry),
txConfigOpts,
)
if err != nil {
return err
}
initClientCtx = initClientCtx.WithTxConfig(txConfigWithTextual)

if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil {
return err
}
Expand All @@ -83,7 +124,7 @@ func NewRootCmd() (*cobra.Command, params.EncodingConfig) {
},
}

initRootCmd(rootCmd, encodingConfig)
initRootCmd(rootCmd, encodingConfig, tempApp.BasicModuleManager)

return rootCmd, encodingConfig
}
Expand Down Expand Up @@ -157,44 +198,30 @@ lru_size = 0`
return customAppTemplate, customAppConfig
}

func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) {
func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig, basicManager module.BasicManager) {
cfg := sdk.GetConfig()
cfg.Seal()
gentxModule := app.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic)
a := appCreator{encodingConfig}

tempApp := app.New(
log.NewNopLogger(),
dbm.NewMemDB(),
nil,
true,
map[int64]bool{},
app.DefaultNodeHome,
0,
encodingConfig,
app.EmptyAppOptions{},
)

mb := tempApp.BasicModuleManager()
gentxModule := basicManager[genutiltypes.ModuleName].(genutil.AppModuleBasic)

rootCmd.AddCommand(
genutilcli.InitCmd(mb, app.DefaultNodeHome),
genutilcli.InitCmd(basicManager, app.DefaultNodeHome),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, gentxModule.GenTxValidator, authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr)),
genutilcli.MigrateGenesisCmd(genutiltypes.MigrationMap{}),
genutilcli.GenTxCmd(mb, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr)),
genutilcli.ValidateGenesisCmd(mb),
genesisCommand(encodingConfig, tempApp.BasicModuleManager()),
genutilcli.GenTxCmd(basicManager, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome, authcodec.NewBech32Codec(sdk.Bech32PrefixValAddr)),
genutilcli.ValidateGenesisCmd(basicManager),
genesisCommand(encodingConfig, basicManager),
tmcli.NewCompletionCmd(rootCmd, true),
NewTestnetCmd(mb, banktypes.GenesisBalancesIterator{}),
NewTestnetCmd(basicManager, banktypes.GenesisBalancesIterator{}),
debug.Cmd(),
)

server.AddCommands(rootCmd, app.DefaultNodeHome, a.newApp, a.appExport, addModuleInitFlags)
server.AddCommands(rootCmd, app.DefaultNodeHome, newApp, appExport, addModuleInitFlags)

// add keybase, auxiliary RPC, query, and tx child commands
rootCmd.AddCommand(
queryCommand(),
txCommand(mb),
txCommand(),
keys.Commands(),
)
}
Expand Down Expand Up @@ -228,14 +255,11 @@ func queryCommand() *cobra.Command {
authcmd.QueryTxsByEventsCmd(),
authcmd.QueryTxCmd(),
)

app.ModuleBasics.AddQueryCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

return cmd
}

func txCommand(manager module.BasicManager) *cobra.Command {
func txCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tx",
Short: "Transactions subcommands",
Expand All @@ -254,18 +278,12 @@ func txCommand(manager module.BasicManager) *cobra.Command {
authcmd.GetEncodeCommand(),
authcmd.GetDecodeCommand(),
)
manager.AddTxCommands(cmd)
cmd.PersistentFlags().String(flags.FlagChainID, "", "The network chain ID")

return cmd
}

type appCreator struct {
encCfg params.EncodingConfig
}

// newApp is an appCreator
func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
func newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appOpts servertypes.AppOptions) servertypes.Application {
baseappOptions := server.DefaultBaseappOptions(appOpts)

skipUpgradeHeights := make(map[int64]bool)
Expand All @@ -277,15 +295,14 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a
logger, db, traceStore, true, skipUpgradeHeights,
cast.ToString(appOpts.Get(flags.FlagHome)),
cast.ToUint(appOpts.Get(server.FlagInvCheckPeriod)),
a.encCfg,
appOpts,
baseappOptions...,
)
}

// appExport creates a new app (optionally at a given height)
// and exports state.
func (a appCreator) appExport(
func appExport(
logger log.Logger, db dbm.DB, traceStore io.Writer, height int64, forZeroHeight bool, jailAllowedAddrs []string, appOpts servertypes.AppOptions, modulesToExport []string,
) (servertypes.ExportedApp, error) {
var simApp *app.App
Expand All @@ -295,13 +312,13 @@ func (a appCreator) appExport(
}

if height != -1 {
simApp = app.New(logger, db, traceStore, false, map[int64]bool{}, homePath, uint(1), a.encCfg, appOpts)
simApp = app.New(logger, db, traceStore, false, map[int64]bool{}, homePath, uint(1), appOpts)

if err := simApp.LoadHeight(height); err != nil {
return servertypes.ExportedApp{}, err
}
} else {
simApp = app.New(logger, db, traceStore, true, map[int64]bool{}, homePath, uint(1), a.encCfg, appOpts)
simApp = app.New(logger, db, traceStore, true, map[int64]bool{}, homePath, uint(1), appOpts)
}

return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport)
Expand Down
Loading

0 comments on commit 5221f96

Please sign in to comment.