Skip to content

Commit

Permalink
chore(DefaultServiceOptions): minor chainID loading refactoring (#2022)
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 committed Sep 27, 2024
1 parent af61ea8 commit 9cbd0b0
Showing 1 changed file with 28 additions and 17 deletions.
45 changes: 28 additions & 17 deletions mod/node-core/pkg/builder/baseapp_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package builder

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -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]){
Expand All @@ -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()
}

0 comments on commit 9cbd0b0

Please sign in to comment.