From 64213b25b14edcce631ef778e63d2a44141d0521 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 27 Oct 2023 10:13:01 +0800 Subject: [PATCH] add AssertChainID --- server/util.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/server/util.go b/server/util.go index 3359bdf19d0..94b202e6679 100644 --- a/server/util.go +++ b/server/util.go @@ -441,6 +441,22 @@ func openTraceWriter(traceWriterFile string) (w io.WriteCloser, err error) { ) } +func AssertChainID(chainID, homeDir string) string { + if chainID == "" { + // fallback to genesis chain-id + reader, err := os.Open(filepath.Join(homeDir, "config", "genesis.json")) + if err != nil { + panic(err) + } + defer reader.Close() + chainID, err = genutiltypes.ParseChainIDFromGenesis(reader) + if err != nil { + panic(fmt.Errorf("failed to parse chain-id from genesis file: %w", err)) + } + } + return chainID +} + // DefaultBaseappOptions returns the default baseapp options provided by the Cosmos SDK func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) { var cache sdk.MultiStorePersistentCache @@ -455,21 +471,7 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) { } homeDir := cast.ToString(appOpts.Get(flags.FlagHome)) - chainID := cast.ToString(appOpts.Get(flags.FlagChainID)) - if chainID == "" { - // fallback to genesis chain-id - reader, err := os.Open(filepath.Join(homeDir, "config", "genesis.json")) - if err != nil { - panic(err) - } - defer reader.Close() - - chainID, err = genutiltypes.ParseChainIDFromGenesis(reader) - if err != nil { - panic(fmt.Errorf("failed to parse chain-id from genesis file: %w", err)) - } - } - + chainID := AssertChainID(cast.ToString(appOpts.Get(flags.FlagChainID)), homeDir) snapshotStore, err := GetSnapshotStore(appOpts) if err != nil { panic(err)