Skip to content

Commit 67aee75

Browse files
authored
fix(server): consensus failure while restart node with wrong chainId in genesis (#18920)
1 parent 5d406c1 commit 67aee75

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## [Unreleased]
3939

40+
### Bug Fixes
41+
42+
* (server) [#18920](https://github.com/cosmos/cosmos-sdk/pull/18920) fixes consensus failure while restart node with wrong `chainId` in genesis
43+
4044
### Improvements
4145

4246
* (client/tx) [#18852](https://github.com/cosmos/cosmos-sdk/pull/18852) Add `WithFromName` to tx factory.

server/util.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
tmcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
2626
tmcfg "github.com/cometbft/cometbft/config"
2727
tmlog "github.com/cometbft/cometbft/libs/log"
28+
"github.com/cometbft/cometbft/node"
29+
tmstore "github.com/cometbft/cometbft/store"
2830
tmtypes "github.com/cometbft/cometbft/types"
2931

3032
"github.com/cosmos/cosmos-sdk/baseapp"
@@ -459,13 +461,13 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
459461
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
460462
chainID := cast.ToString(appOpts.Get(flags.FlagChainID))
461463
if chainID == "" {
462-
// fallback to genesis chain-id
463-
appGenesis, err := tmtypes.GenesisDocFromFile(filepath.Join(homeDir, "config", "genesis.json"))
464+
// read the chainID from home directory (either from comet or genesis).
465+
chainId, err := readChainIdFromHome(homeDir)
464466
if err != nil {
465467
panic(err)
466468
}
467469

468-
chainID = appGenesis.ChainID
470+
chainID = chainId
469471
}
470472

471473
snapshotStore, err := GetSnapshotStore(appOpts)
@@ -500,6 +502,38 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) {
500502
}
501503
}
502504

505+
// readChainIdFromHome reads chain id from home directory.
506+
func readChainIdFromHome(homeDir string) (string, error) {
507+
cfg := tmcfg.DefaultConfig()
508+
cfg.SetRoot(homeDir)
509+
510+
// if the node's current height is not zero then try to read the chainID from comet db.
511+
db, err := node.DefaultDBProvider(&node.DBContext{ID: "blockstore", Config: cfg})
512+
if err != nil {
513+
return "", err
514+
}
515+
516+
blockStore := tmstore.NewBlockStore(db)
517+
defer func() {
518+
if err := blockStore.Close(); err != nil {
519+
panic(err)
520+
}
521+
}()
522+
523+
// if the blockStore.LoadBaseMeta() is nil (no blocks are created/synced so far), fallback to genesis chain-id.
524+
baseMeta := blockStore.LoadBaseMeta()
525+
if baseMeta != nil {
526+
return baseMeta.Header.ChainID, nil
527+
}
528+
529+
appGenesis, err := tmtypes.GenesisDocFromFile(filepath.Join(homeDir, "config", "genesis.json"))
530+
if err != nil {
531+
return "", err
532+
}
533+
534+
return appGenesis.ChainID, nil
535+
}
536+
503537
func GetSnapshotStore(appOpts types.AppOptions) (*snapshots.Store, error) {
504538
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
505539
snapshotDir := filepath.Join(homeDir, "data", "snapshots")

0 commit comments

Comments
 (0)