diff --git a/doc/release-notes.md b/doc/release-notes.md --- a/doc/release-notes.md +++ b/doc/release-notes.md @@ -18,3 +18,6 @@ like Docker, ensure you only bind RPC to your localhost, e.g. docker run [...] -p 127.0.0.1:8332:8332 (this is an extra :8332 over the normal Docker port specification). + - The `getmininginfo` RPC now omits `currentblocksize` and `currentblocktx` + when a block was never assembled via RPC on this node. + diff --git a/src/miner.h b/src/miner.h --- a/src/miner.h +++ b/src/miner.h @@ -1,11 +1,12 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_MINER_H #define BITCOIN_MINER_H +#include #include #include @@ -175,6 +176,9 @@ uint64_t GetMaxGeneratedBlockSize() const { return nMaxGeneratedBlockSize; } + static Optional m_last_block_num_txs; + static Optional m_last_block_size; + private: // utility functions /** Clear the block's state and prepare for assembling a new block */ diff --git a/src/miner.cpp b/src/miner.cpp --- a/src/miner.cpp +++ b/src/miner.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -33,14 +33,6 @@ #include #include -// Unconfirmed transactions in the memory pool often depend on other -// transactions in the memory pool. When we select transactions from the -// pool, we select by highest fee rate of a transaction combined with all -// its ancestors. - -uint64_t nLastBlockTx = 0; -uint64_t nLastBlockSize = 0; - int64_t UpdateTime(CBlockHeader *pblock, const Consensus::Params ¶ms, const CBlockIndex *pindexPrev) { int64_t nOldTime = pblock->nTime; @@ -127,6 +119,9 @@ nFees = Amount::zero(); } +Optional BlockAssembler::m_last_block_num_txs{nullopt}; +Optional BlockAssembler::m_last_block_size{nullopt}; + std::unique_ptr BlockAssembler::CreateNewBlock(const CScript &scriptPubKeyIn) { int64_t nTimeStart = GetTimeMicros(); @@ -191,8 +186,8 @@ int64_t nTime1 = GetTimeMicros(); - nLastBlockTx = nBlockTx; - nLastBlockSize = nBlockSize; + m_last_block_num_txs = nBlockTx; + m_last_block_size = nBlockSize; // Create coinbase transaction. CMutableTransaction coinbaseTx; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -242,9 +242,12 @@ RPCResult{ "{\n" " \"blocks\": nnn, (numeric) The current block\n" - " \"currentblocksize\": nnn, (numeric) The last block size\n" - " \"currentblocktx\": nnn, (numeric) The last block " - "transaction\n" + " \"currentblocksize\": nnn, (numeric, optional) The block " + "size of the last assembled block (only present if a block was " + "ever assembled)\n" + " \"currentblocktx\": nnn, (numeric, optional) The number " + "of block transactions of the last assembled block (only " + "present if a block was ever assembled)\n" " \"difficulty\": xxx.xxxxx (numeric) The current " "difficulty\n" " \"networkhashps\": nnn, (numeric) The network hashes " @@ -266,8 +269,12 @@ UniValue obj(UniValue::VOBJ); obj.pushKV("blocks", int(::ChainActive().Height())); - obj.pushKV("currentblocksize", uint64_t(nLastBlockSize)); - obj.pushKV("currentblocktx", uint64_t(nLastBlockTx)); + if (BlockAssembler::m_last_block_size) { + obj.pushKV("currentblocksize", *BlockAssembler::m_last_block_size); + } + if (BlockAssembler::m_last_block_num_txs) { + obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs); + } obj.pushKV("difficulty", double(GetDifficulty(::ChainActive().Tip()))); obj.pushKV("networkhashps", getnetworkhashps(config, request)); obj.pushKV("pooledtx", uint64_t(g_mempool.size())); diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2016 The Bitcoin Core developers +// Copyright (c) 2009-2019 The Bitcoin Core developers // Copyright (c) 2017-2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -179,8 +179,6 @@ extern CScript COINBASE_FLAGS; extern RecursiveMutex cs_main; extern CTxMemPool g_mempool; -extern uint64_t nLastBlockTx; -extern uint64_t nLastBlockSize; extern const std::string strMessageMagic; extern Mutex g_best_block_mutex; extern std::condition_variable g_best_block_cv; diff --git a/test/functional/mining_basic.py b/test/functional/mining_basic.py --- a/test/functional/mining_basic.py +++ b/test/functional/mining_basic.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright (c) 2014-2016 The Bitcoin Core developers +# Copyright (c) 2014-2019 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Test mining RPCs @@ -11,7 +11,10 @@ import copy from decimal import Decimal -from test_framework.blocktools import create_coinbase +from test_framework.blocktools import ( + create_coinbase, + TIME_GENESIS_BLOCK, +) from test_framework.messages import ( CBlock, CBlockHeader, @@ -23,6 +26,7 @@ from test_framework.util import ( assert_equal, assert_raises_rpc_error, + connect_nodes_bi, ) @@ -39,9 +43,25 @@ class MiningTest(BitcoinTestFramework): def set_test_params(self): self.num_nodes = 2 - self.setup_clean_chain = False + self.setup_clean_chain = True + + def mine_chain(self): + self.log.info('Create some old blocks') + node = self.nodes[0] + address = node.get_deterministic_priv_key().address + for t in range(TIME_GENESIS_BLOCK, + TIME_GENESIS_BLOCK + 200 * 600, 600): + node.setmocktime(t) + node.generatetoaddress(1, address) + mining_info = node.getmininginfo() + assert_equal(mining_info['blocks'], 200) + assert_equal(mining_info['currentblocktx'], 0) + assert_equal(mining_info['currentblocksize'], 1000) + self.restart_node(0) + connect_nodes_bi(self.nodes[0], self.nodes[1]) def run_test(self): + self.mine_chain() node = self.nodes[0] def assert_submitblock(block, result_str_1, result_str_2=None): @@ -56,8 +76,8 @@ mining_info = node.getmininginfo() assert_equal(mining_info['blocks'], 200) assert_equal(mining_info['chain'], 'regtest') - assert_equal(mining_info['currentblocksize'], 0) - assert_equal(mining_info['currentblocktx'], 0) + assert 'currentblocktx' not in mining_info + assert 'currentblocksize' not in mining_info assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10')) assert_equal(mining_info['networkhashps'], diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -# Copyright (c) 2015-2016 The Bitcoin Core developers +# Copyright (c) 2015-2019 The Bitcoin Core developers # Distributed under the MIT software license, see the accompanying # file COPYING or http://www.opensource.org/licenses/mit-license.php. """Utilities for manipulating blocks and transactions.""" @@ -27,10 +27,12 @@ from .txtools import pad_tx from .util import assert_equal, satoshi_round -# Create a block (with regtest difficulty) +# Genesis block time (regtest) +TIME_GENESIS_BLOCK = 1296688602 def create_block(hashprev, coinbase, nTime=None): + """Create a block (with regtest difficulty).""" block = CBlock() if nTime is None: import time