Skip to content

Commit a65ced1

Browse files
author
MarcoFalke
committed
Merge #9518: Return height of last block pruned by pruneblockchain RPC
918d1fb Return height of last block pruned by pruneblockchain RPC (Russell Yanofsky)
2 parents 2456a83 + 918d1fb commit a65ced1

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

qa/rpc-tests/pruning.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import time
1717
import os
1818

19+
MIN_BLOCKS_TO_KEEP = 288
20+
1921

2022
def calc_usage(blockdir):
2123
return sum(os.path.getsize(blockdir+f) for f in os.listdir(blockdir) if os.path.isfile(blockdir+f)) / (1024. * 1024.)
@@ -241,6 +243,21 @@ def height(index):
241243
else:
242244
return index
243245

246+
def prune(index, expected_ret=None):
247+
ret = node.pruneblockchain(height(index))
248+
# Check the return value. When use_timestamp is True, just check
249+
# that the return value is less than or equal to the expected
250+
# value, because when more than one block is generated per second,
251+
# a timestamp will not be granular enough to uniquely identify an
252+
# individual block.
253+
if expected_ret is None:
254+
expected_ret = index
255+
if use_timestamp:
256+
assert_greater_than(ret, 0)
257+
assert_greater_than(expected_ret + 1, ret)
258+
else:
259+
assert_equal(ret, expected_ret)
260+
244261
def has_block(index):
245262
return os.path.isfile(self.options.tmpdir + "/node{}/regtest/blocks/blk{:05}.dat".format(node_number, index))
246263

@@ -264,30 +281,30 @@ def has_block(index):
264281
pass
265282

266283
# height=100 too low to prune first block file so this is a no-op
267-
node.pruneblockchain(height(100))
284+
prune(100)
268285
if not has_block(0):
269286
raise AssertionError("blk00000.dat is missing when should still be there")
270287

271288
# height=500 should prune first file
272-
node.pruneblockchain(height(500))
289+
prune(500)
273290
if has_block(0):
274291
raise AssertionError("blk00000.dat is still there, should be pruned by now")
275292
if not has_block(1):
276293
raise AssertionError("blk00001.dat is missing when should still be there")
277294

278295
# height=650 should prune second file
279-
node.pruneblockchain(height(650))
296+
prune(650)
280297
if has_block(1):
281298
raise AssertionError("blk00001.dat is still there, should be pruned by now")
282299

283300
# height=1000 should not prune anything more, because tip-288 is in blk00002.dat.
284-
node.pruneblockchain(height(1000))
301+
prune(1000, 1001 - MIN_BLOCKS_TO_KEEP)
285302
if not has_block(2):
286303
raise AssertionError("blk00002.dat is still there, should be pruned by now")
287304

288305
# advance the tip so blk00002.dat and blk00003.dat can be pruned (the last 288 blocks should now be in blk00004.dat)
289306
node.generate(288)
290-
node.pruneblockchain(height(1000))
307+
prune(1000)
291308
if has_block(2):
292309
raise AssertionError("blk00002.dat is still there, should be pruned by now")
293310
if has_block(3):

src/rpc/blockchain.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,12 @@ UniValue pruneblockchain(const JSONRPCRequest& request)
820820
throw runtime_error(
821821
"pruneblockchain\n"
822822
"\nArguments:\n"
823-
"1. \"height\" (numeric, required) The block height to prune up to. May be set to a discrete height, or to a unix timestamp to prune based on block time.\n");
823+
"1. \"height\" (numeric, required) The block height to prune up to. May be set to a discrete height, or to a unix timestamp to prune based on block time.\n"
824+
"\nResult:\n"
825+
"n (numeric) Height of the last block pruned.\n"
826+
"\nExamples:\n"
827+
+ HelpExampleCli("pruneblockchain", "1000")
828+
+ HelpExampleRpc("pruneblockchain", "1000"));
824829

825830
if (!fPruneMode)
826831
throw JSONRPCError(RPC_METHOD_NOT_FOUND, "Cannot prune blocks because node is not in prune mode.");
@@ -847,11 +852,13 @@ UniValue pruneblockchain(const JSONRPCRequest& request)
847852
throw JSONRPCError(RPC_INTERNAL_ERROR, "Blockchain is too short for pruning.");
848853
else if (height > chainHeight)
849854
throw JSONRPCError(RPC_INVALID_PARAMETER, "Blockchain is shorter than the attempted prune height.");
850-
else if (height > chainHeight - MIN_BLOCKS_TO_KEEP)
855+
else if (height > chainHeight - MIN_BLOCKS_TO_KEEP) {
851856
LogPrint("rpc", "Attempt to prune blocks close to the tip. Retaining the minimum number of blocks.");
857+
height = chainHeight - MIN_BLOCKS_TO_KEEP;
858+
}
852859

853860
PruneBlockFilesManual(height);
854-
return NullUniValue;
861+
return uint64_t(height);
855862
}
856863

857864
UniValue gettxoutsetinfo(const JSONRPCRequest& request)

0 commit comments

Comments
 (0)