diff --git a/mod/node-core/pkg/builder/baseapp_options.go b/mod/node-core/pkg/builder/baseapp_options.go index c01ac3b40d..a7ddcd2bae 100644 --- a/mod/node-core/pkg/builder/baseapp_options.go +++ b/mod/node-core/pkg/builder/baseapp_options.go @@ -21,6 +21,7 @@ package builder import ( + "errors" "fmt" "os" "path/filepath" @@ -58,28 +59,13 @@ func DefaultServiceOptions[ panic(err) } - homeDir := cast.ToString(appOpts.Get(flags.FlagHome)) + // get chainID, possibly falling back to genesis if flag is not set chainID := cast.ToString(appOpts.Get(flags.FlagChainID)) - var reader *os.File if chainID == "" { - // fallback to genesis chain-id - //#nosec:G304 // bet. - reader, err = os.Open(filepath.Join(homeDir, "config", "genesis.json")) + chainID, err = loadChainIDFromGenesis(appOpts) if err != nil { panic(err) } - //#nosec:307 // bet. - 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 []func(*cometbft.Service[LoggerT]){ @@ -98,3 +84,28 @@ func DefaultServiceOptions[ cometbft.SetChainID[LoggerT](chainID), } } + +func loadChainIDFromGenesis(appOpts config.AppOptions) (string, error) { + var ( + homeDir = cast.ToString(appOpts.Get(flags.FlagHome)) + fp = filepath.Join(homeDir, "config", "genesis.json") + ) + + f, err := os.Open(filepath.Clean(fp)) + if err != nil { + return "", err + } + + chainID, err := genutiltypes.ParseChainIDFromGenesis(f) + if err != nil { + return "", + errors.Join( + f.Close(), + fmt.Errorf( + "failed to parse chain-id from genesis file: %w", + err, + ), + ) + } + return chainID, f.Close() +}