Skip to content

Commit b7dfc6c

Browse files
[rpc] getblockchaininfo: add size_on_disk, prune_target_size, automatic_pruning
Fix pruneheight help text. Move fPruneMode block to match output ordering with help text. Add functional tests for new fields in getblockchaininfo.
1 parent ff4cd60 commit b7dfc6c

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

src/rpc/blockchain.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,11 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
11361136
" \"mediantime\": xxxxxx, (numeric) median time for the current best block\n"
11371137
" \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
11381138
" \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n"
1139+
" \"size_on_disk\": xxxxxx, (numeric) the estimated size of the block and undo files on disk\n"
11391140
" \"pruned\": xx, (boolean) if the blocks are subject to pruning\n"
1140-
" \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored\n"
1141+
" \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored (only present if pruning is enabled)\n"
1142+
" \"automatic_pruning\": xx, (boolean) whether automatic pruning is enabled (only present if pruning is enabled)\n"
1143+
" \"prune_target_size\": xxxxxx, (numeric) the target size used by pruning (only present if automatic pruning is enabled)\n"
11411144
" \"softforks\": [ (array) status of softforks in progress\n"
11421145
" {\n"
11431146
" \"id\": \"xxxx\", (string) name of softfork\n"
@@ -1181,7 +1184,24 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
11811184
obj.push_back(Pair("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()));
11821185
obj.push_back(Pair("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip())));
11831186
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
1187+
obj.push_back(Pair("size_on_disk", CalculateCurrentUsage()));
11841188
obj.push_back(Pair("pruned", fPruneMode));
1189+
if (fPruneMode) {
1190+
CBlockIndex* block = chainActive.Tip();
1191+
assert(block);
1192+
while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) {
1193+
block = block->pprev;
1194+
}
1195+
1196+
obj.push_back(Pair("pruneheight", block->nHeight));
1197+
1198+
// if 0, execution bypasses the whole if block.
1199+
bool automatic_pruning = (gArgs.GetArg("-prune", 0) != 1);
1200+
obj.push_back(Pair("automatic_pruning", automatic_pruning));
1201+
if (automatic_pruning) {
1202+
obj.push_back(Pair("prune_target_size", nPruneTarget));
1203+
}
1204+
}
11851205

11861206
const Consensus::Params& consensusParams = Params().GetConsensus();
11871207
CBlockIndex* tip = chainActive.Tip();
@@ -1195,14 +1215,6 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
11951215
obj.push_back(Pair("softforks", softforks));
11961216
obj.push_back(Pair("bip9_softforks", bip9_softforks));
11971217

1198-
if (fPruneMode)
1199-
{
1200-
CBlockIndex *block = chainActive.Tip();
1201-
while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA))
1202-
block = block->pprev;
1203-
1204-
obj.push_back(Pair("pruneheight", block->nHeight));
1205-
}
12061218
obj.push_back(Pair("warnings", GetWarnings("statusbar")));
12071219
return obj;
12081220
}

src/validation.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3233,8 +3233,10 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
32333233
*/
32343234

32353235
/* Calculate the amount of disk space the block & undo files currently use */
3236-
static uint64_t CalculateCurrentUsage()
3236+
uint64_t CalculateCurrentUsage()
32373237
{
3238+
LOCK(cs_LastBlockFile);
3239+
32383240
uint64_t retval = 0;
32393241
for (const CBlockFileInfo &file : vinfoBlockFile) {
32403242
retval += file.nSize + file.nUndoSize;
@@ -3245,6 +3247,8 @@ static uint64_t CalculateCurrentUsage()
32453247
/* Prune a block file (modify associated database entries)*/
32463248
void PruneOneBlockFile(const int fileNumber)
32473249
{
3250+
LOCK(cs_LastBlockFile);
3251+
32483252
for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
32493253
CBlockIndex* pindex = it->second;
32503254
if (pindex->nFile == fileNumber) {
@@ -4247,6 +4251,8 @@ std::string CBlockFileInfo::ToString() const
42474251

42484252
CBlockFileInfo* GetBlockFileInfo(size_t n)
42494253
{
4254+
LOCK(cs_LastBlockFile);
4255+
42504256
return &vinfoBlockFile.at(n);
42514257
}
42524258

src/validation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
280280
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
281281
double GuessVerificationProgress(const ChainTxData& data, CBlockIndex* pindex);
282282

283+
/** Calculate the amount of disk space the block & undo files currently use */
284+
uint64_t CalculateCurrentUsage();
285+
283286
/**
284287
* Mark one block file as pruned.
285288
*/

test/functional/blockchain.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
from test_framework.test_framework import BitcoinTestFramework
2525
from test_framework.util import (
2626
assert_equal,
27+
assert_greater_than,
28+
assert_greater_than_or_equal,
2729
assert_raises,
2830
assert_raises_jsonrpc,
2931
assert_is_hex_string,
@@ -58,21 +60,43 @@ def _test_getblockchaininfo(self):
5860
'headers',
5961
'mediantime',
6062
'pruned',
63+
'size_on_disk',
6164
'softforks',
6265
'verificationprogress',
6366
'warnings',
6467
]
6568
res = self.nodes[0].getblockchaininfo()
66-
# result should have pruneheight and default keys if pruning is enabled
67-
assert_equal(sorted(res.keys()), sorted(['pruneheight'] + keys))
69+
70+
# result should have these additional pruning keys if manual pruning is enabled
71+
assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning'] + keys))
72+
73+
# size_on_disk should be > 0
74+
assert_greater_than(res['size_on_disk'], 0)
75+
6876
# pruneheight should be greater or equal to 0
69-
assert res['pruneheight'] >= 0
77+
assert_greater_than_or_equal(res['pruneheight'], 0)
78+
79+
# check other pruning fields given that prune=1
80+
assert res['pruned']
81+
assert not res['automatic_pruning']
7082

7183
self.restart_node(0, ['-stopatheight=207'])
7284
res = self.nodes[0].getblockchaininfo()
7385
# should have exact keys
7486
assert_equal(sorted(res.keys()), keys)
7587

88+
self.restart_node(0, ['-stopatheight=207', '-prune=550'])
89+
res = self.nodes[0].getblockchaininfo()
90+
# result should have these additional pruning keys if prune=550
91+
assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning', 'prune_target_size'] + keys))
92+
93+
# check related fields
94+
assert res['pruned']
95+
assert_equal(res['pruneheight'], 0)
96+
assert res['automatic_pruning']
97+
assert_equal(res['prune_target_size'], 576716800)
98+
assert_greater_than(res['size_on_disk'], 0)
99+
76100
def _test_getchaintxstats(self):
77101
chaintxstats = self.nodes[0].getchaintxstats(1)
78102
# 200 txs plus genesis tx

0 commit comments

Comments
 (0)