Skip to content

Commit

Permalink
fix(baseapp): avoid header height overwrite block height (cosmos#20107)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe authored Apr 22, 2024
1 parent 06a2ae3 commit b97caa3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i
* (x/bank) [#20028](https://github.com/cosmos/cosmos-sdk/pull/20028) Align query with multi denoms for send-enabled.
* (cli) [#20020](https://github.com/cosmos/cosmos-sdk/pull/20020) Make bootstrap-state command support both new and legacy genesis format.
* (baseapp) [#19616](https://github.com/cosmos/cosmos-sdk/pull/19616) Don't share gas meter in tx execution.
* (baseapp) [#20107](https://github.com/cosmos/cosmos-sdk/pull/20107) Allow height overwrite BlockHeight in header.

### API Breaking Changes

Expand Down
4 changes: 2 additions & 2 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -1250,13 +1250,13 @@ func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, e
// branch the commit multi-store for safety
ctx := sdk.NewContext(cacheMS, true, app.logger).
WithMinGasPrices(app.minGasPrices).
WithBlockHeight(height).
WithGasMeter(storetypes.NewGasMeter(app.queryGasLimit)).
WithHeaderInfo(coreheader.Info{
ChainID: app.chainID,
Height: height,
}).
WithBlockHeader(app.checkState.Context().BlockHeader())
WithBlockHeader(app.checkState.Context().BlockHeader()).
WithBlockHeight(height)

if height != lastBlockHeight {
rms, ok := app.cms.(*rootmulti.Store)
Expand Down
27 changes: 18 additions & 9 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,24 +709,33 @@ func TestABCI_CreateQueryContext(t *testing.T) {
_, err = app.Commit()
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())
}
})
}
Expand Down

0 comments on commit b97caa3

Please sign in to comment.