aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/rpc/blockchain.cpp22
-rwxr-xr-xtest/functional/feature_pruning.py18
2 files changed, 33 insertions, 7 deletions
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 822498c6d0..fd1d1e4e87 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -786,16 +786,24 @@ static RPCHelpMan getblock()
std::optional<int> GetPruneHeight(const BlockManager& blockman, const CChain& chain) {
AssertLockHeld(::cs_main);
+ // Search for the last block missing block data or undo data. Don't let the
+ // search consider the genesis block, because the genesis block does not
+ // have undo data, but should not be considered pruned.
+ const CBlockIndex* first_block{chain[1]};
const CBlockIndex* chain_tip{chain.Tip()};
- if (!(chain_tip->nStatus & BLOCK_HAVE_DATA)) return chain_tip->nHeight;
- // Get first block with data, after the last block without data.
- // This is the start of the unpruned range of blocks.
- const auto& first_unpruned{*Assert(blockman.GetFirstBlock(*chain_tip, /*status_mask=*/BLOCK_HAVE_DATA))};
- if (!first_unpruned.pprev) {
- // No block before the first unpruned block means nothing is pruned.
- return std::nullopt;
+ // If there are no blocks after the genesis block, or no blocks at all, nothing is pruned.
+ if (!first_block || !chain_tip) return std::nullopt;
+
+ // If the chain tip is pruned, everything is pruned.
+ if (!((chain_tip->nStatus & BLOCK_HAVE_MASK) == BLOCK_HAVE_MASK)) return chain_tip->nHeight;
+
+ const auto& first_unpruned{*Assert(blockman.GetFirstBlock(*chain_tip, /*status_mask=*/BLOCK_HAVE_MASK, first_block))};
+ if (&first_unpruned == first_block) {
+ // All blocks between first_block and chain_tip have data, so nothing is pruned.
+ return std::nullopt;
}
+
// Block before the first unpruned block is the last pruned block.
return Assert(first_unpruned.pprev)->nHeight;
}
diff --git a/test/functional/feature_pruning.py b/test/functional/feature_pruning.py
index 4b548ef0f3..5f99b8dee8 100755
--- a/test/functional/feature_pruning.py
+++ b/test/functional/feature_pruning.py
@@ -25,6 +25,7 @@ from test_framework.util import (
assert_equal,
assert_greater_than,
assert_raises_rpc_error,
+ try_rpc,
)
# Rescans start at the earliest block up to 2 hours before a key timestamp, so
@@ -479,8 +480,12 @@ class PruneTest(BitcoinTestFramework):
self.log.info("Test invalid pruning command line options")
self.test_invalid_command_line_options()
+ self.log.info("Test scanblocks can not return pruned data")
self.test_scanblocks_pruned()
+ self.log.info("Test pruneheight reflects the presence of block and undo data")
+ self.test_pruneheight_undo_presence()
+
self.log.info("Done")
def test_scanblocks_pruned(self):
@@ -494,5 +499,18 @@ class PruneTest(BitcoinTestFramework):
assert_raises_rpc_error(-1, "Block not available (pruned data)", node.scanblocks,
"start", [{"desc": f"raw({false_positive_spk.hex()})"}], 0, 0, "basic", {"filter_false_positives": True})
+ def test_pruneheight_undo_presence(self):
+ node = self.nodes[2]
+ pruneheight = node.getblockchaininfo()["pruneheight"]
+ fetch_block = node.getblockhash(pruneheight - 1)
+
+ self.connect_nodes(1, 2)
+ peers = node.getpeerinfo()
+ node.getblockfrompeer(fetch_block, peers[0]["id"])
+ self.wait_until(lambda: not try_rpc(-1, "Block not available (pruned data)", node.getblock, fetch_block), timeout=5)
+
+ new_pruneheight = node.getblockchaininfo()["pruneheight"]
+ assert_equal(pruneheight, new_pruneheight)
+
if __name__ == '__main__':
PruneTest().main()