Skip to content

Commit eca1273

Browse files
author
MarcoFalke
committed
Merge #15383: [rpc] mining: Omit uninitialized currentblockweight, currentblocktx
fa178a6 [rpc] mining: Omit uninitialized currentblockweight, currentblocktx (MarcoFalke) Pull request description: Previously we'd report "0", which could be mistaken for a valid number. E.g. the number of transactions is 0 or the block weight is 0, whatever that means. Tree-SHA512: ee94ab203a329e272211b726f4c23edec4b09c650ec363b77fd59ad9264165d73064f78ebb9e11b5c2c543b73c157752410a307655560531c7d5444d203aa0ea
2 parents bf3677a + fa178a6 commit eca1273

File tree

7 files changed

+62
-39
lines changed

7 files changed

+62
-39
lines changed

doc/release-notes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ in the Low-level Changes section below.
263263

264264
- See the [Mining](#mining) section for changes to `getblocktemplate`.
265265

266+
- The `getmininginfo` RPC now omits `currentblockweight` and `currentblocktx`
267+
when a block was never assembled via RPC on this node.
268+
266269
- The `getrawtransaction` RPC & REST endpoints no longer check the
267270
unspent UTXO set for a transaction. The remaining behaviors are as
268271
follows: 1. If a blockhash is provided, check the corresponding block.

src/miner.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -10,8 +10,8 @@
1010
#include <chainparams.h>
1111
#include <coins.h>
1212
#include <consensus/consensus.h>
13-
#include <consensus/tx_verify.h>
1413
#include <consensus/merkle.h>
14+
#include <consensus/tx_verify.h>
1515
#include <consensus/validation.h>
1616
#include <hash.h>
1717
#include <net.h>
@@ -21,22 +21,14 @@
2121
#include <primitives/transaction.h>
2222
#include <script/standard.h>
2323
#include <timedata.h>
24-
#include <util/system.h>
2524
#include <util/moneystr.h>
25+
#include <util/system.h>
2626
#include <validationinterface.h>
2727

2828
#include <algorithm>
2929
#include <queue>
3030
#include <utility>
3131

32-
// Unconfirmed transactions in the memory pool often depend on other
33-
// transactions in the memory pool. When we select transactions from the
34-
// pool, we select by highest fee rate of a transaction combined with all
35-
// its ancestors.
36-
37-
uint64_t nLastBlockTx = 0;
38-
uint64_t nLastBlockWeight = 0;
39-
4032
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
4133
{
4234
int64_t nOldTime = pblock->nTime;
@@ -95,6 +87,9 @@ void BlockAssembler::resetBlock()
9587
nFees = 0;
9688
}
9789

90+
Optional<int64_t> BlockAssembler::m_last_block_num_txs{nullopt};
91+
Optional<int64_t> BlockAssembler::m_last_block_weight{nullopt};
92+
9893
std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& scriptPubKeyIn)
9994
{
10095
int64_t nTimeStart = GetTimeMicros();
@@ -147,8 +142,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
147142

148143
int64_t nTime1 = GetTimeMicros();
149144

150-
nLastBlockTx = nBlockTx;
151-
nLastBlockWeight = nBlockWeight;
145+
m_last_block_num_txs = nBlockTx;
146+
m_last_block_weight = nBlockWeight;
152147

153148
// Create coinbase transaction.
154149
CMutableTransaction coinbaseTx;

src/miner.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#ifndef BITCOIN_MINER_H
77
#define BITCOIN_MINER_H
88

9+
#include <optional.h>
910
#include <primitives/block.h>
1011
#include <txmempool.h>
1112
#include <validation.h>
1213

13-
#include <stdint.h>
1414
#include <memory>
15+
#include <stdint.h>
16+
1517
#include <boost/multi_index_container.hpp>
1618
#include <boost/multi_index/ordered_index.hpp>
1719

@@ -159,6 +161,9 @@ class BlockAssembler
159161
/** Construct a new block template with coinbase to scriptPubKeyIn */
160162
std::unique_ptr<CBlockTemplate> CreateNewBlock(const CScript& scriptPubKeyIn);
161163

164+
static Optional<int64_t> m_last_block_num_txs;
165+
static Optional<int64_t> m_last_block_weight;
166+
162167
private:
163168
// utility functions
164169
/** Clear the block's state and prepare for assembling a new block */

src/rpc/mining.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,36 +187,36 @@ static UniValue generatetoaddress(const JSONRPCRequest& request)
187187

188188
static UniValue getmininginfo(const JSONRPCRequest& request)
189189
{
190-
if (request.fHelp || request.params.size() != 0)
190+
if (request.fHelp || request.params.size() != 0) {
191191
throw std::runtime_error(
192192
RPCHelpMan{"getmininginfo",
193193
"\nReturns a json object containing mining-related information.",
194194
{},
195195
RPCResult{
196-
"{\n"
197-
" \"blocks\": nnn, (numeric) The current block\n"
198-
" \"currentblockweight\": nnn, (numeric) The last block weight\n"
199-
" \"currentblocktx\": nnn, (numeric) The last block transaction\n"
200-
" \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
201-
" \"networkhashps\": nnn, (numeric) The network hashes per second\n"
202-
" \"pooledtx\": n (numeric) The size of the mempool\n"
203-
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
204-
" \"warnings\": \"...\" (string) any network and blockchain warnings\n"
205-
"}\n"
196+
"{\n"
197+
" \"blocks\": nnn, (numeric) The current block\n"
198+
" \"currentblockweight\": nnn, (numeric, optional) The block weight of the last assembled block (only present if a block was ever assembled)\n"
199+
" \"currentblocktx\": nnn, (numeric, optional) The number of block transactions of the last assembled block (only present if a block was ever assembled)\n"
200+
" \"difficulty\": xxx.xxxxx (numeric) The current difficulty\n"
201+
" \"networkhashps\": nnn, (numeric) The network hashes per second\n"
202+
" \"pooledtx\": n (numeric) The size of the mempool\n"
203+
" \"chain\": \"xxxx\", (string) current network name as defined in BIP70 (main, test, regtest)\n"
204+
" \"warnings\": \"...\" (string) any network and blockchain warnings\n"
205+
"}\n"
206206
},
207207
RPCExamples{
208208
HelpExampleCli("getmininginfo", "")
209209
+ HelpExampleRpc("getmininginfo", "")
210210
},
211211
}.ToString());
212-
212+
}
213213

214214
LOCK(cs_main);
215215

216216
UniValue obj(UniValue::VOBJ);
217217
obj.pushKV("blocks", (int)chainActive.Height());
218-
obj.pushKV("currentblockweight", (uint64_t)nLastBlockWeight);
219-
obj.pushKV("currentblocktx", (uint64_t)nLastBlockTx);
218+
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
219+
if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs);
220220
obj.pushKV("difficulty", (double)GetDifficulty(chainActive.Tip()));
221221
obj.pushKV("networkhashps", getnetworkhashps(request));
222222
obj.pushKV("pooledtx", (uint64_t)mempool.size());

src/validation.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -14,8 +14,8 @@
1414
#include <coins.h>
1515
#include <crypto/common.h> // for ReadLE64
1616
#include <fs.h>
17-
#include <protocol.h> // For CMessageHeader::MessageStartChars
1817
#include <policy/feerate.h>
18+
#include <protocol.h> // For CMessageHeader::MessageStartChars
1919
#include <script/script_error.h>
2020
#include <sync.h>
2121
#include <versionbits.h>
@@ -152,8 +152,6 @@ extern CTxMemPool mempool;
152152
extern std::atomic_bool g_is_mempool_loaded;
153153
typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
154154
extern BlockMap& mapBlockIndex GUARDED_BY(cs_main);
155-
extern uint64_t nLastBlockTx;
156-
extern uint64_t nLastBlockWeight;
157155
extern const std::string strMessageMagic;
158156
extern Mutex g_best_block_mutex;
159157
extern std::condition_variable g_best_block_cv;

test/functional/mining_basic.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2014-2018 The Bitcoin Core developers
2+
# Copyright (c) 2014-2019 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test mining RPCs
@@ -11,7 +11,10 @@
1111
import copy
1212
from decimal import Decimal
1313

14-
from test_framework.blocktools import create_coinbase
14+
from test_framework.blocktools import (
15+
create_coinbase,
16+
TIME_GENESIS_BLOCK,
17+
)
1518
from test_framework.messages import (
1619
CBlock,
1720
CBlockHeader,
@@ -25,9 +28,11 @@
2528
assert_equal,
2629
assert_raises_rpc_error,
2730
bytes_to_hex_str as b2x,
31+
connect_nodes_bi,
2832
)
2933
from test_framework.script import CScriptNum
3034

35+
3136
def assert_template(node, block, expect, rehash=True):
3237
if rehash:
3338
block.hashMerkleRoot = block.calc_merkle_root()
@@ -38,9 +43,22 @@ def assert_template(node, block, expect, rehash=True):
3843
class MiningTest(BitcoinTestFramework):
3944
def set_test_params(self):
4045
self.num_nodes = 2
41-
self.setup_clean_chain = False
46+
self.setup_clean_chain = True
47+
48+
def mine_chain(self):
49+
self.log.info('Create some old blocks')
50+
for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
51+
self.nodes[0].setmocktime(t)
52+
self.nodes[0].generate(1)
53+
mining_info = self.nodes[0].getmininginfo()
54+
assert_equal(mining_info['blocks'], 200)
55+
assert_equal(mining_info['currentblocktx'], 0)
56+
assert_equal(mining_info['currentblockweight'], 4000)
57+
self.restart_node(0)
58+
connect_nodes_bi(self.nodes, 0, 1)
4259

4360
def run_test(self):
61+
self.mine_chain()
4462
node = self.nodes[0]
4563

4664
def assert_submitblock(block, result_str_1, result_str_2=None):
@@ -53,8 +71,8 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
5371
mining_info = node.getmininginfo()
5472
assert_equal(mining_info['blocks'], 200)
5573
assert_equal(mining_info['chain'], 'regtest')
56-
assert_equal(mining_info['currentblocktx'], 0)
57-
assert_equal(mining_info['currentblockweight'], 0)
74+
assert 'currentblocktx' not in mining_info
75+
assert 'currentblockweight' not in mining_info
5876
assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
5977
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
6078
assert_equal(mining_info['pooledtx'], 0)

test/functional/test_framework/blocktools.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
# Copyright (c) 2015-2018 The Bitcoin Core developers
2+
# Copyright (c) 2015-2019 The Bitcoin Core developers
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Utilities for manipulating blocks and transactions."""
@@ -43,9 +43,13 @@
4343

4444
MAX_BLOCK_SIGOPS = 20000
4545

46+
# Genesis block time (regtest)
47+
TIME_GENESIS_BLOCK = 1296688602
48+
4649
# From BIP141
4750
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
4851

52+
4953
def create_block(hashprev, coinbase, ntime=None, *, version=1):
5054
"""Create a block (with regtest difficulty)."""
5155
block = CBlock()

0 commit comments

Comments
 (0)