From 1be3b0a49971fadf25099905488cb0a548a6f883 Mon Sep 17 00:00:00 2001 From: j75689 Date: Fri, 30 Dec 2022 06:22:04 +0800 Subject: [PATCH] fix: add chain id into baseApp --- baseapp/baseapp.go | 10 ++++++++++ baseapp/options.go | 6 ++++++ client/pruning/main.go | 2 +- server/rollback.go | 8 +++++++- server/start.go | 23 ++++++++++++++--------- server/types/app.go | 2 +- simapp/simd/cmd/root.go | 3 ++- 7 files changed, 41 insertions(+), 13 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index a956ea1b0b..e420574225 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -61,6 +61,7 @@ type BaseApp struct { // nolint: maligned moduleRouter appConfig serverconfig.Config + chainID string // volatile states: // @@ -397,6 +398,10 @@ func (app *BaseApp) setAppConfig(config serverconfig.Config) { app.appConfig = config } +func (app *BaseApp) setChainID(chainID string) { + app.chainID = chainID +} + // Router returns the legacy router of the BaseApp. func (app *BaseApp) Router() sdk.Router { if app.sealed { @@ -479,6 +484,11 @@ func (app *BaseApp) AppConfig() serverconfig.Config { return app.appConfig } +// ChainID returns the chain id. +func (app *BaseApp) ChainID() string { + return app.chainID +} + // AddRunTxRecoveryHandler adds custom app.runTx method panic handlers. func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) { for _, h := range handlers { diff --git a/baseapp/options.go b/baseapp/options.go index 06fec9c046..a4950f04ad 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -81,10 +81,16 @@ func SetSnapshot(snapshotStore *snapshots.Store, opts snapshottypes.SnapshotOpti return func(app *BaseApp) { app.SetSnapshot(snapshotStore, opts) } } +// SetAppConfig sets the server/config.Config. func SetAppConfig(config serverconfig.Config) func(*BaseApp) { return func(app *BaseApp) { app.setAppConfig(config) } } +// SetAppConfig sets the chain id. +func SetChainID(chainID string) func(*BaseApp) { + return func(app *BaseApp) { app.setChainID(chainID) } +} + func (app *BaseApp) SetName(name string) { if app.sealed { panic("SetName() on sealed BaseApp") diff --git a/client/pruning/main.go b/client/pruning/main.go index 1a1ff49df9..7bdf1f107a 100644 --- a/client/pruning/main.go +++ b/client/pruning/main.go @@ -69,7 +69,7 @@ func PruningCmd(appCreator servertypes.AppCreator) *cobra.Command { return err } - app := appCreator(logger, db, nil, config, vp) + app := appCreator(logger, db, nil, config, "", vp) cms := app.CommitMultiStore() rootMultiStore, ok := cms.(*rootmulti.Store) diff --git a/server/rollback.go b/server/rollback.go index 9ddd95fe66..c30e2f6f58 100644 --- a/server/rollback.go +++ b/server/rollback.go @@ -8,6 +8,7 @@ import ( "github.com/cosmos/cosmos-sdk/server/types" "github.com/spf13/cobra" tmcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + "github.com/tendermint/tendermint/node" ) // NewRollbackCmd creates a command to rollback tendermint and multistore state by one height. @@ -35,7 +36,12 @@ application. if err != nil { return err } - app := appCreator(ctx.Logger, db, nil, config, ctx.Viper) + genDocProvider := node.DefaultGenesisDocProviderFunc(ctx.Config) + genDoc, err := genDocProvider() + if err != nil { + return err + } + app := appCreator(ctx.Logger, db, nil, config, genDoc.ChainID, ctx.Viper) // rollback tendermint state height, hash, err := tmcmd.RollbackState(ctx.Config) if err != nil { diff --git a/server/start.go b/server/start.go index 8f6f89f5a6..09267253e9 100644 --- a/server/start.go +++ b/server/start.go @@ -216,7 +216,13 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error { return err } - app := appCreator(ctx.Logger, db, traceWriter, config, ctx.Viper) + genDocProvider := node.DefaultGenesisDocProviderFunc(ctx.Config) + genDoc, err := genDocProvider() + if err != nil { + return err + } + + app := appCreator(ctx.Logger, db, traceWriter, config, genDoc.ChainID, ctx.Viper) _, err = startTelemetry(config) if err != nil { @@ -290,15 +296,19 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App return err } - app := appCreator(ctx.Logger, db, traceWriter, config, ctx.Viper) + genDocProvider := node.DefaultGenesisDocProviderFunc(cfg) + genDoc, err := genDocProvider() + if err != nil { + return err + } + + app := appCreator(ctx.Logger, db, traceWriter, config, genDoc.ChainID, ctx.Viper) nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile()) if err != nil { return err } - genDocProvider := node.DefaultGenesisDocProviderFunc(cfg) - var ( tmNode *node.Node gRPCOnly = ctx.Viper.GetBool(flagGRPCOnly) @@ -349,11 +359,6 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App var apiSrv *api.Server if config.API.Enable { - genDoc, err := genDocProvider() - if err != nil { - return err - } - clientCtx := clientCtx.WithHomeDir(home).WithChainID(genDoc.ChainID) if config.GRPC.Enable { diff --git a/server/types/app.go b/server/types/app.go index a8c84888e1..07f3def5ed 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -70,7 +70,7 @@ type ( // AppCreator is a function that allows us to lazily initialize an // application using various configurations. - AppCreator func(log.Logger, dbm.DB, io.Writer, serverconfig.Config, AppOptions) Application + AppCreator func(log.Logger, dbm.DB, io.Writer, serverconfig.Config, string, AppOptions) Application // ModuleInitFlags takes a start command and adds modules specific init flags. ModuleInitFlags func(startCmd *cobra.Command) diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 9ebd556ca3..ba594b5687 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -247,7 +247,7 @@ type appCreator struct { } // newApp is an appCreator -func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appConfig serverconfig.Config, appOpts servertypes.AppOptions) servertypes.Application { +func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, appConfig serverconfig.Config, chainID string, appOpts servertypes.AppOptions) servertypes.Application { var cache sdk.MultiStorePersistentCache if cast.ToBool(appOpts.Get(server.FlagInterBlockCache)) { @@ -297,6 +297,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))), baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(server.FlagDisableIAVLFastNode))), baseapp.SetAppConfig(appConfig), + baseapp.SetChainID(chainID), ) }