From 59dcceea58529c10e3d9eecec92fd3c854350afa Mon Sep 17 00:00:00 2001 From: Daniel Edgecumbe Date: Thu, 21 Sep 2017 00:52:20 +0100 Subject: [PATCH] [backport] [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. Rebase-from: bitcoin#b7dfc6c4 --- qa/rpc-tests/blockchain.py | 53 +++++++++++++++++++++++++++++++++++++- src/rpc/blockchain.cpp | 32 ++++++++++++++++------- src/validation.cpp | 6 +++++ src/validation.h | 3 +++ 4 files changed, 83 insertions(+), 11 deletions(-) diff --git a/qa/rpc-tests/blockchain.py b/qa/rpc-tests/blockchain.py index a46e047c4..827c46d42 100755 --- a/qa/rpc-tests/blockchain.py +++ b/qa/rpc-tests/blockchain.py @@ -14,10 +14,13 @@ from test_framework.test_framework import BitcoinTestFramework from test_framework.authproxy import JSONRPCException from test_framework.util import ( assert_equal, + assert_greater_than, + assert_greater_than_or_equal, assert_raises, assert_is_hex_string, assert_is_hash_string, start_nodes, + start_node, connect_nodes_bi, ) @@ -37,7 +40,9 @@ class BlockchainTest(BitcoinTestFramework): self.num_nodes = 2 def setup_network(self, split=False): - self.nodes = start_nodes(self.num_nodes, self.options.tmpdir) + self.nodes = [] + self.nodes.append(start_node(0, self.options.tmpdir, ["-prune=1"])) + self.nodes.append(start_node(1, self.options.tmpdir, ["-prune=2200"])) connect_nodes_bi(self.nodes, 0, 1) self.is_network_split = False self.sync_all() @@ -45,8 +50,54 @@ class BlockchainTest(BitcoinTestFramework): def run_test(self): self._test_gettxoutsetinfo() self._test_getblockheader() + self._test_getblockchaininfo() self.nodes[0].verifychain(4, 0) + # PL backported this entire test from upstream 0.16 to 1.14.3 + def _test_getblockchaininfo(self): + + keys = [ + 'bestblockhash', + 'bip9_softforks', + 'blocks', + 'chain', + 'chainwork', + 'difficulty', + 'headers', + 'initialblockdownload', + 'mediantime', + 'pruned', + 'size_on_disk', + 'softforks', + 'verificationprogress', + 'warnings', + ] + res = self.nodes[0].getblockchaininfo() + + # result should have these additional pruning keys if manual pruning is enabled + assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning'] + keys)) + + # size_on_disk should be > 0 + assert_greater_than(res['size_on_disk'], 0) + + # pruneheight should be greater or equal to 0 + assert_greater_than_or_equal(res['pruneheight'], 0) + + # check other pruning fields given that prune=1 + assert res['pruned'] + assert not res['automatic_pruning'] + + res = self.nodes[1].getblockchaininfo() + # result should have these additional pruning keys if prune=2200 + assert_equal(sorted(res.keys()), sorted(['pruneheight', 'automatic_pruning', 'prune_target_size'] + keys)) + + # check related fields + assert res['pruned'] + assert_equal(res['pruneheight'], 0) + assert res['automatic_pruning'] + assert_equal(res['prune_target_size'], 2306867200) + assert_greater_than(res['size_on_disk'], 0) + def _test_gettxoutsetinfo(self): node = self.nodes[0] res = node.gettxoutsetinfo() diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 6d3264130..c80dd85fa 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1147,8 +1147,11 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) " \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n" " \"initialblockdownload\": xxxx, (bool) (debug information) estimate of whether this node is in Initial Block Download mode.\n" " \"chainwork\": \"xxxx\" (string) total amount of work in active chain, in hexadecimal\n" + " \"size_on_disk\": xxxxxx, (numeric) the estimated size of the block and undo files on disk\n" " \"pruned\": xx, (boolean) if the blocks are subject to pruning\n" - " \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored\n" + " \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored (only present if pruning is enabled)\n" + " \"automatic_pruning\": xx, (boolean) whether automatic pruning is enabled (only present if pruning is enabled)\n" + " \"prune_target_size\": xxxxxx, (numeric) the target size used by pruning (only present if automatic pruning is enabled)\n" " \"softforks\": [ (array) status of softforks in progress\n" " {\n" " \"id\": \"xxxx\", (string) name of softfork\n" @@ -1185,7 +1188,24 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) obj.push_back(Pair("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip()))); obj.push_back(Pair("initialblockdownload", IsInitialBlockDownload())); obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex())); + obj.push_back(Pair("size_on_disk", CalculateCurrentUsage())); obj.push_back(Pair("pruned", fPruneMode)); + if (fPruneMode) { + CBlockIndex* block = chainActive.Tip(); + assert(block); + while (block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) { + block = block->pprev; + } + + obj.push_back(Pair("pruneheight", block->nHeight)); + + // if 0, execution bypasses the whole if block. + bool automatic_pruning = (GetArg("-prune", 0) != 1); + obj.push_back(Pair("automatic_pruning", automatic_pruning)); + if (automatic_pruning) { + obj.push_back(Pair("prune_target_size", nPruneTarget)); + } + } const Consensus::Params& consensusParams = Params().GetConsensus(0); CBlockIndex* tip = chainActive.Tip(); @@ -1198,15 +1218,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request) BIP9SoftForkDescPushBack(bip9_softforks, "segwit", consensusParams, Consensus::DEPLOYMENT_SEGWIT); obj.push_back(Pair("softforks", softforks)); obj.push_back(Pair("bip9_softforks", bip9_softforks)); - - if (fPruneMode) - { - CBlockIndex *block = chainActive.Tip(); - while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA)) - block = block->pprev; - - obj.push_back(Pair("pruneheight", block->nHeight)); - } + obj.push_back(Pair("warnings", GetWarnings("statusbar"))); return obj; } diff --git a/src/validation.cpp b/src/validation.cpp index 5b27b9ec5..3ddf67717 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3356,6 +3356,8 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, /* Calculate the amount of disk space the block & undo files currently use */ uint64_t CalculateCurrentUsage() { + LOCK(cs_LastBlockFile); + uint64_t retval = 0; BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) { retval += file.nSize + file.nUndoSize; @@ -3366,6 +3368,8 @@ uint64_t CalculateCurrentUsage() /* Prune a block file (modify associated database entries)*/ void PruneOneBlockFile(const int fileNumber) { + LOCK(cs_LastBlockFile); + for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) { CBlockIndex* pindex = it->second; if (pindex->nFile == fileNumber) { @@ -4231,6 +4235,8 @@ std::string CBlockFileInfo::ToString() const CBlockFileInfo* GetBlockFileInfo(size_t n) { + LOCK(cs_LastBlockFile); + return &vinfoBlockFile.at(n); } diff --git a/src/validation.h b/src/validation.h index de986e972..fb4f3982e 100644 --- a/src/validation.h +++ b/src/validation.h @@ -285,6 +285,9 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams); /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */ double GuessVerificationProgress(const ChainTxData& data, CBlockIndex* pindex); +/** Calculate the amount of disk space the block & undo files currently use */ +uint64_t CalculateCurrentUsage(); + /** * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target. * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new