diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ff2eddbfcba..6050aba36c0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Bug Fixes + +* (baseapp) [#20107](https://github.com/cosmos/cosmos-sdk/pull/20107) Avoid header height overwrite block height. + ## [v0.50.6](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.6) - 2024-04-22 ### Features diff --git a/baseapp/abci.go b/baseapp/abci.go index a08e68144019..2bef4642ba41 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -1230,8 +1230,9 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e header := app.checkState.Context().BlockHeader() ctx := sdk.NewContext(cacheMS, header, true, app.logger). WithMinGasPrices(app.minGasPrices). - WithBlockHeight(height). - WithGasMeter(storetypes.NewGasMeter(app.queryGasLimit)).WithBlockHeader(header) + WithGasMeter(storetypes.NewGasMeter(app.queryGasLimit)). + WithBlockHeader(header). + WithBlockHeight(height) if height != lastBlockHeight { rms, ok := app.cms.(*rootmulti.Store) diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 1ba3fb82d4d8..f847009e8e49 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -702,24 +702,33 @@ func TestABCI_CreateQueryContext(t *testing.T) { require.NoError(t, err) testCases := []struct { - name string - height int64 - prove bool - expErr bool + name string + height int64 + headerHeight int64 + prove bool + expErr bool }{ - {"valid height", 2, true, false}, - {"future height", 10, true, true}, - {"negative height, prove=true", -1, true, true}, - {"negative height, prove=false", -1, false, true}, + {"valid height", 2, 2, true, false}, + {"valid height with different initial height", 2, 1, true, false}, + {"future height", 10, 10, true, true}, + {"negative height, prove=true", -1, -1, true, true}, + {"negative height, prove=false", -1, -1, false, true}, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - _, err := app.CreateQueryContext(tc.height, tc.prove) + if tc.headerHeight != tc.height { + _, err := app.InitChain(&abci.RequestInitChain{ + InitialHeight: tc.headerHeight, + }) + require.NoError(t, err) + } + ctx, err := app.CreateQueryContext(tc.height, tc.prove) if tc.expErr { require.Error(t, err) } else { require.NoError(t, err) + require.Equal(t, tc.height, ctx.BlockHeight()) } }) }