From aef322775c75783488b439cdf7ca33f38da08426 Mon Sep 17 00:00:00 2001 From: Rootul P Date: Thu, 5 Sep 2024 11:57:11 -0400 Subject: [PATCH] fix: mocha block sync (#1469) Unblocks https://github.com/celestiaorg/celestia-app/pull/3843 --- state/state.go | 15 +++++++++++---- state/state_test.go | 12 ++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/state/state.go b/state/state.go index 516af24119..451377079f 100644 --- a/state/state.go +++ b/state/state.go @@ -331,10 +331,7 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { nextValidatorSet = types.NewValidatorSet(validators).CopyIncrementProposerPriority(1) } - appVersion := uint64(0) - if genDoc.ConsensusParams != nil { - appVersion = genDoc.ConsensusParams.Version.AppVersion - } + appVersion := getAppVersion(genDoc) return State{ Version: InitStateVersion(appVersion), @@ -356,3 +353,13 @@ func MakeGenesisState(genDoc *types.GenesisDoc) (State, error) { AppHash: genDoc.AppHash, }, nil } + +func getAppVersion(genDoc *types.GenesisDoc) uint64 { + if genDoc.ConsensusParams != nil && + genDoc.ConsensusParams.Version.AppVersion != 0 { + return genDoc.ConsensusParams.Version.AppVersion + } + // Default to app version 1 because some chains (e.g. mocha-4) did not set + // an explicit app version in genesis.json. + return uint64(1) +} diff --git a/state/state_test.go b/state/state_test.go index fb3eb4fbf6..b7abc418ff 100644 --- a/state/state_test.go +++ b/state/state_test.go @@ -88,6 +88,18 @@ func TestMakeGenesisStateSetsAppVersion(t *testing.T) { require.Nil(t, err) require.Equal(t, appVersion, state.Version.Consensus.App) require.Equal(t, version.BlockProtocol, state.Version.Consensus.Block) + t.Run("MakeGenesisState defaults to 1 if app version is not set", func(t *testing.T) { + cp := types.DefaultConsensusParams() + cp.Version = cmtproto.VersionParams{} // zero value + doc := types.GenesisDoc{ + ChainID: "chain-id", + ConsensusParams: cp, + } + require.NoError(t, doc.ValidateAndComplete()) + state, err := sm.MakeGenesisState(&doc) + require.NoError(t, err) + require.Equal(t, uint64(1), state.Version.Consensus.App) + }) } // TestStateSaveLoad tests saving and loading State from a db.