From 553f8955c3214c21c2755811aae5a8f3e021f0f8 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Fri, 18 Apr 2025 01:11:35 +0900 Subject: [PATCH] fix: incorrect retention height when `commitHeight` equals `minRetainBlocks` (#24526) --- CHANGELOG.md | 1 + baseapp/abci.go | 10 ++++++---- baseapp/abci_test.go | 12 ++++++++++++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 367da4ffeb..0dc4e99aa0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -103,6 +103,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (baseapp) [#24074](https://github.com/cosmos/cosmos-sdk/pull/24074) Use CometBFT's ComputeProtoSizeForTxs in defaultTxSelector.SelectTxForProposal for consistency. * (cli) [#24090](https://github.com/cosmos/cosmos-sdk/pull/24090) Prune cmd should disable async pruning. * (x/auth) [#19239](https://github.com/cosmos/cosmos-sdk/pull/19239) Sets from flag in multi-sign command to avoid no key name provided error. +* (baseapp) [#24526](https://github.com/cosmos/cosmos-sdk/pull/24526) Fix incorrect retention height when `commitHeight` equals `minRetainBlocks`. ## [v0.50.12](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.12) - 2025-02-20 diff --git a/baseapp/abci.go b/baseapp/abci.go index 30761b5081..8d4d83befa 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -1319,8 +1319,11 @@ func (app *BaseApp) CreateQueryContextWithCheckHeader(height int64, prove, check // be a need to vary retention for other nodes, e.g. sentry nodes which do not // need historical blocks. func (app *BaseApp) GetBlockRetentionHeight(commitHeight int64) int64 { - // pruning is disabled if minRetainBlocks is zero - if app.minRetainBlocks == 0 { + // If minRetainBlocks is zero, pruning is disabled and we return 0 + // If commitHeight is less than or equal to minRetainBlocks, return 0 since there are not enough + // blocks to trigger pruning yet. This ensures we keep all blocks until we have at least minRetainBlocks. + retentionBlockWindow := commitHeight - int64(app.minRetainBlocks) + if app.minRetainBlocks == 0 || retentionBlockWindow <= 0 { return 0 } @@ -1362,8 +1365,7 @@ func (app *BaseApp) GetBlockRetentionHeight(commitHeight int64) int64 { } } - v := commitHeight - int64(app.minRetainBlocks) - retentionHeight = minNonZero(retentionHeight, v) + retentionHeight = minNonZero(retentionHeight, retentionBlockWindow) if retentionHeight <= 0 { // prune nothing in the case of a non-positive height diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 9ff7009ad5..1e1cfff985 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -1322,6 +1322,18 @@ func TestABCI_GetBlockRetentionHeight(t *testing.T) { commitHeight: 10000, expected: 0, }, + "no pruning due to min retain blocks equal to commit height": { + bapp: baseapp.NewBaseApp(name, logger, db, nil, baseapp.SetMinRetainBlocks(499000)), + maxAgeBlocks: 362880, + commitHeight: 499000, + expected: 0, + }, + "no pruning due to min retain blocks greater than commit height": { + bapp: baseapp.NewBaseApp(name, logger, db, nil, baseapp.SetMinRetainBlocks(499001)), + maxAgeBlocks: 362880, + commitHeight: 499000, + expected: 0, + }, "disable pruning": { bapp: baseapp.NewBaseApp( name, logger, db, nil,