Skip to content

Commit 9497ecf

Browse files
furszyluke-jr
authored andcommitted
test: rpc_getblockfrompeer.py, remove magic numbers usage
Instead of hardcoding the `pruneblockchain(<height>)` heights, use 'getblockfileinfo' to obtain the highest block number of each of the block files. Making the test more robust and readable by stating which file is being pruned at every point of time (the goal is to mimic how the automatic pruning process work). Github-Pull: bitcoin#27770 Rebased-From: 5110139
1 parent 4a1e08b commit 9497ecf

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

test/functional/rpc_getblockfrompeer.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,21 @@ def run_test(self):
122122
pruned_node = self.nodes[2]
123123
self.generate(self.nodes[0], 400, sync_fun=self.no_op)
124124
self.sync_blocks([self.nodes[0], pruned_node])
125-
pruneheight = pruned_node.pruneblockchain(300)
126-
assert_equal(pruneheight, 248)
125+
126+
# The goal now will be to mimic the automatic pruning process and verify what happens when we fetch an historic
127+
# block at any point of time.
128+
#
129+
# Starting with three blocks files. The pruning process will prune them one by one. And, at the second pruning
130+
# event, the test will fetch the past block. Which will be stored at the latest block file. Which can only be
131+
# pruned when the latest block file is full (in this case, the third one), and a new one is created.
132+
133+
# First prune event, prune first block file
134+
highest_pruned_block_num = pruned_node.getblockfileinfo(0)["highest_block"]
135+
pruneheight = pruned_node.pruneblockchain(highest_pruned_block_num + 1)
136+
assert_equal(pruneheight, highest_pruned_block_num)
127137
# Ensure the block is actually pruned
128-
pruned_block = self.nodes[0].getblockhash(2)
138+
fetch_block_num = 2
139+
pruned_block = self.nodes[0].getblockhash(fetch_block_num)
129140
assert_raises_rpc_error(-1, "Block not available (pruned data)", pruned_node.getblock, pruned_block)
130141

131142
self.log.info("Fetch pruned block")
@@ -136,18 +147,29 @@ def run_test(self):
136147
self.wait_until(lambda: self.check_for_block(node=2, hash=pruned_block), timeout=1)
137148
assert_equal(result, {})
138149

150+
# Validate that the re-fetched block was stored at the last, current, block file
151+
assert_equal(fetch_block_num, pruned_node.getblockfileinfo(2)["lowest_block"])
152+
139153
self.log.info("Fetched block persists after next pruning event")
140154
self.generate(self.nodes[0], 250, sync_fun=self.no_op)
141155
self.sync_blocks([self.nodes[0], pruned_node])
142-
pruneheight += 251
143-
assert_equal(pruned_node.pruneblockchain(700), pruneheight)
156+
157+
# Second prune event, prune second block file
158+
highest_pruned_block_num = pruned_node.getblockfileinfo(1)["highest_block"]
159+
pruneheight = pruned_node.pruneblockchain(highest_pruned_block_num + 1)
160+
assert_equal(pruneheight, highest_pruned_block_num)
161+
# As the re-fetched block is in the third file, and we just pruned the second one, 'getblock' must work.
144162
assert_equal(pruned_node.getblock(pruned_block)["hash"], "36c56c5b5ebbaf90d76b0d1a074dcb32d42abab75b7ec6fa0ffd9b4fbce8f0f7")
145163

146-
self.log.info("Fetched block can be pruned again when prune height exceeds the height of the tip at the time when the block was fetched")
164+
self.log.info("Re-fetched block can be pruned again when a new block file is created")
147165
self.generate(self.nodes[0], 250, sync_fun=self.no_op)
148166
self.sync_blocks([self.nodes[0], pruned_node])
149-
pruneheight += 250
150-
assert_equal(pruned_node.pruneblockchain(1000), pruneheight)
167+
168+
# Third prune event, prune third block file
169+
highest_pruned_block_num = pruned_node.getblockfileinfo(2)["highest_block"]
170+
pruneheight = pruned_node.pruneblockchain(highest_pruned_block_num + 1)
171+
assert_equal(pruneheight, highest_pruned_block_num)
172+
# and check that the re-fetched block file is now pruned
151173
assert_raises_rpc_error(-1, "Block not available (pruned data)", pruned_node.getblock, pruned_block)
152174

153175

0 commit comments

Comments
 (0)