Skip to content

Commit

Permalink
Merge pull request cosmos#201 from mmsqe/release/v0.47.x_revert
Browse files Browse the repository at this point in the history
Revert "fix(server): consensus failure while restart node with wrong chainId
  • Loading branch information
yihuang authored Feb 28, 2024
2 parents 5910717 + fbb68a1 commit 0f7197f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 38 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes

* [#19106](https://github.com/cosmos/cosmos-sdk/pull/19106) Allow empty public keys when setting signatures. Public keys aren't needed for every transaction.
* (server) [#18920](https://github.com/cosmos/cosmos-sdk/pull/18920) Fixes consensus failure while restart node with wrong `chainId` in genesis.

## [v0.47.7](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.7) - 2023-12-20

Expand Down
40 changes: 3 additions & 37 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
tmcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
tmcfg "github.com/cometbft/cometbft/config"
tmlog "github.com/cometbft/cometbft/libs/log"
"github.com/cometbft/cometbft/node"
tmstore "github.com/cometbft/cometbft/store"
tmtypes "github.com/cometbft/cometbft/types"

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

chainID = chainId
chainID = appGenesis.ChainID
}

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

// readChainIdFromHome reads chain id from home directory.
func readChainIdFromHome(homeDir string) (string, error) {
cfg := tmcfg.DefaultConfig()
cfg.SetRoot(homeDir)

// if the node's current height is not zero then try to read the chainID from comet db.
db, err := node.DefaultDBProvider(&node.DBContext{ID: "blockstore", Config: cfg})
if err != nil {
return "", err
}

blockStore := tmstore.NewBlockStore(db)
defer func() {
if err := blockStore.Close(); err != nil {
panic(err)
}
}()

// if the blockStore.LoadBaseMeta() is nil (no blocks are created/synced so far), fallback to genesis chain-id.
baseMeta := blockStore.LoadBaseMeta()
if baseMeta != nil {
return baseMeta.Header.ChainID, nil
}

appGenesis, err := tmtypes.GenesisDocFromFile(filepath.Join(homeDir, "config", "genesis.json"))
if err != nil {
return "", err
}

return appGenesis.ChainID, nil
}

func GetSnapshotStore(appOpts types.AppOptions) (*snapshots.Store, error) {
homeDir := cast.ToString(appOpts.Get(flags.FlagHome))
snapshotDir := filepath.Join(homeDir, "data", "snapshots")
Expand Down

0 comments on commit 0f7197f

Please sign in to comment.