Skip to content

Commit

Permalink
fix: use genesis file app version if it is set (#1227)
Browse files Browse the repository at this point in the history
## Description

Closes: #1226
  • Loading branch information
cmwaters committed Feb 16, 2024
1 parent edd9b9d commit 48abbee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
18 changes: 8 additions & 10 deletions consensus/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,10 @@ func (h *Handshaker) Handshake(proxyApp proxy.AppConns) (string, error) {
}
appHash := res.LastBlockAppHash

h.logger.Info("ABCI Handshake App Info",
"height", blockHeight,
"hash", appHash,
"software-version", res.Version,
"protocol-version", res.AppVersion,
)

// Only set the version if there is no existing state.
if h.initialState.LastBlockHeight == 0 {
appVersion := h.initialState.Version.Consensus.App
// set app version if it's not set via genesis
if h.initialState.LastBlockHeight == 0 && appVersion == 0 && res.AppVersion != 0 {
appVersion = res.AppVersion
h.initialState.Version.Consensus.App = res.AppVersion
}

Expand All @@ -271,7 +266,10 @@ func (h *Handshaker) Handshake(proxyApp proxy.AppConns) (string, error) {
}

h.logger.Info("Completed ABCI Handshake - CometBFT and App are synced",
"appHeight", blockHeight, "appHash", appHash)
"appHeight", blockHeight,
"appHash", appHash,
"appVersion", appVersion,
)

// TODO: (on restart) replay mempool

Expand Down
26 changes: 15 additions & 11 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@ var (

//-----------------------------------------------------------------------------

// InitStateVersion sets the Consensus.Block and Software versions,
// but leaves the Consensus.App version blank.
// The Consensus.App version will be set during the Handshake, once
// we hear from the app what protocol version it is running.
var InitStateVersion = cmtstate.Version{
Consensus: cmtversion.Consensus{
Block: version.BlockProtocol,
App: 0,
},
Software: version.TMCoreSemVer,
// InitStateVersion sets the Consensus.Block, Consensus.App and Software versions
func InitStateVersion(appVersion uint64) cmtstate.Version {
return cmtstate.Version{
Consensus: cmtversion.Consensus{
Block: version.BlockProtocol,
App: appVersion,
},
Software: version.TMCoreSemVer,
}
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -331,8 +330,13 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) {
nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1)
}

appVersion := uint64(0)
if genDoc.ConsensusParams != nil {
appVersion = genDoc.ConsensusParams.Version.App
}

return State{
Version: InitStateVersion,
Version: InitStateVersion(appVersion),
ChainID: genDoc.ChainID,
InitialHeight: genDoc.InitialHeight,

Expand Down
16 changes: 16 additions & 0 deletions state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
cmtstate "github.com/cometbft/cometbft/proto/tendermint/state"
sm "github.com/cometbft/cometbft/state"
"github.com/cometbft/cometbft/types"
"github.com/cometbft/cometbft/version"
)

// setupTestCase does setup common to all test cases.
Expand Down Expand Up @@ -73,6 +74,21 @@ func TestMakeGenesisStateNilValidators(t *testing.T) {
require.Equal(t, 0, len(state.NextValidators.Validators))
}

func TestMakeGenesisStateSetsAppVersion(t *testing.T) {
cp := types.DefaultConsensusParams()
appVersion := uint64(5)
cp.Version.App = appVersion
doc := types.GenesisDoc{
ChainID: "dummy",
ConsensusParams: cp,
}
require.Nil(t, doc.ValidateAndComplete())
state, err := sm.MakeGenesisState(&doc)
require.Nil(t, err)
require.Equal(t, appVersion, state.Version.Consensus.App)
require.Equal(t, version.BlockProtocol, state.Version.Consensus.Block)
}

// TestStateSaveLoad tests saving and loading State from a db.
func TestStateSaveLoad(t *testing.T) {
tearDown, stateDB, state := setupTestCase(t)
Expand Down

0 comments on commit 48abbee

Please sign in to comment.