Skip to content

Commit

Permalink
fix: add chain id into baseApp
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Jan 3, 2023
1 parent 908651b commit 1be3b0a
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 13 deletions.
10 changes: 10 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type BaseApp struct { // nolint: maligned
moduleRouter

appConfig serverconfig.Config
chainID string

// volatile states:
//
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion client/pruning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion server/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
23 changes: 14 additions & 9 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion server/types/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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),
)
}

Expand Down

0 comments on commit 1be3b0a

Please sign in to comment.