Skip to content

Commit cf0a628

Browse files
committed
rpc: add next to getmininginfo
Obtain difficulty and target for the next block without having to call getblocktemplate.
1 parent 2d18a07 commit cf0a628

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/rpc/mining.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,13 @@ static RPCHelpMan getmininginfo()
428428
{RPCResult::Type::NUM, "pooledtx", "The size of the mempool"},
429429
{RPCResult::Type::STR, "chain", "current network name (" LIST_CHAIN_NAMES ")"},
430430
{RPCResult::Type::STR_HEX, "signet_challenge", /*optional=*/true, "The block challenge (aka. block script), in hexadecimal (only present if the current network is a signet)"},
431+
{RPCResult::Type::OBJ, "next", "The next block",
432+
{
433+
{RPCResult::Type::NUM, "height", "The next height"},
434+
{RPCResult::Type::STR_HEX, "bits", "The next target nBits"},
435+
{RPCResult::Type::NUM, "difficulty", "The next difficulty"},
436+
{RPCResult::Type::STR_HEX, "target", "The next target"}
437+
}},
431438
(IsDeprecatedRPCEnabled("warnings") ?
432439
RPCResult{RPCResult::Type::STR, "warnings", "any network and blockchain warnings (DEPRECATED)"} :
433440
RPCResult{RPCResult::Type::ARR, "warnings", "any network and blockchain warnings (run with `-deprecatedrpc=warnings` to return the latest warning as a single string)",
@@ -460,9 +467,20 @@ static RPCHelpMan getmininginfo()
460467
obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request));
461468
obj.pushKV("pooledtx", (uint64_t)mempool.size());
462469
obj.pushKV("chain", chainman.GetParams().GetChainTypeString());
470+
471+
UniValue next(UniValue::VOBJ);
472+
CBlockIndex next_index;
473+
NextEmptyBlockIndex(tip, chainman.GetConsensus(), next_index);
474+
475+
next.pushKV("height", next_index.nHeight);
476+
next.pushKV("bits", strprintf("%08x", next_index.nBits));
477+
next.pushKV("difficulty", GetDifficulty(next_index));
478+
next.pushKV("target", GetTarget(next_index, chainman.GetConsensus().powLimit).GetHex());
479+
obj.pushKV("next", next);
480+
463481
if (chainman.GetParams().GetChainType() == ChainType::SIGNET) {
464482
const std::vector<uint8_t>& signet_challenge =
465-
chainman.GetParams().GetConsensus().signet_challenge;
483+
chainman.GetConsensus().signet_challenge;
466484
obj.pushKV("signet_challenge", HexStr(signet_challenge));
467485
}
468486
obj.pushKV("warnings", node::GetWarningsForRpc(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings")));

src/rpc/server_util.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
#include <rpc/server_util.h>
66

7+
#include <chain.h>
78
#include <common/args.h>
89
#include <net_processing.h>
910
#include <node/context.h>
11+
#include <node/miner.h>
1012
#include <policy/fees.h>
13+
#include <pow.h>
1114
#include <rpc/protocol.h>
1215
#include <rpc/request.h>
1316
#include <txmempool.h>
@@ -17,6 +20,7 @@
1720
#include <any>
1821

1922
using node::NodeContext;
23+
using node::UpdateTime;
2024

2125
NodeContext& EnsureAnyNodeContext(const std::any& context)
2226
{
@@ -129,3 +133,18 @@ AddrMan& EnsureAnyAddrman(const std::any& context)
129133
{
130134
return EnsureAddrman(EnsureAnyNodeContext(context));
131135
}
136+
137+
void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index)
138+
{
139+
CBlockHeader next_header{};
140+
next_header.hashPrevBlock = tip.GetBlockHash();
141+
UpdateTime(&next_header, consensusParams, &tip);
142+
next_header.nBits = GetNextWorkRequired(&tip, &next_header, consensusParams);
143+
next_header.nNonce = 0;
144+
145+
next_index.pprev = &tip;
146+
next_index.nTime = next_header.nTime;
147+
next_index.nBits = next_header.nBits;
148+
next_index.nNonce = next_header.nNonce;
149+
next_index.nHeight = tip.nHeight + 1;
150+
}

src/rpc/server_util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
#include <any>
99

10+
#include <consensus/params.h>
11+
1012
class AddrMan;
1113
class ArgsManager;
14+
class CBlockIndex;
1215
class CBlockPolicyEstimator;
1316
class CConnman;
1417
class CTxMemPool;
@@ -39,4 +42,7 @@ PeerManager& EnsurePeerman(const node::NodeContext& node);
3942
AddrMan& EnsureAddrman(const node::NodeContext& node);
4043
AddrMan& EnsureAnyAddrman(const std::any& context);
4144

45+
/** Return an empty block index on top of the tip, with height, time and nBits set */
46+
void NextEmptyBlockIndex(CBlockIndex& tip, const Consensus::Params& consensusParams, CBlockIndex& next_index);
47+
4248
#endif // BITCOIN_RPC_SERVER_UTIL_H

test/functional/mining_basic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,12 @@ def assert_submitblock(block, result_str_1, result_str_2=None):
213213
assert_equal(mining_info['bits'], nbits_str(REGTEST_N_BITS))
214214
assert_equal(mining_info['target'], target_str(REGTEST_TARGET))
215215
assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10'))
216+
assert_equal(mining_info['next'], {
217+
'height': 201,
218+
'target': target_str(REGTEST_TARGET),
219+
'bits': nbits_str(REGTEST_N_BITS),
220+
'difficulty': Decimal('4.656542373906925E-10')
221+
})
216222
assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334'))
217223
assert_equal(mining_info['pooledtx'], 0)
218224

0 commit comments

Comments
 (0)