From 87a5eab16c4373cd29e27c977da0ad537109c22c Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Tue, 31 Dec 2024 10:44:53 +0100 Subject: [PATCH] rpc: add next to getmininginfo --- src/rpc/mining.cpp | 26 +++++++++++++++++++++++--- test/functional/mining_basic.py | 6 ++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 1d3f1fa76e2bd6..d2ed4f169e6227 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -52,6 +52,7 @@ using node::BlockAssembler; using node::NodeContext; using node::RegenerateCommitments; using node::UpdateTime; +using node::NextEmptyBlockIndex; using util::ToString; /** @@ -428,6 +429,13 @@ static RPCHelpMan getmininginfo() {RPCResult::Type::NUM, "pooledtx", "The size of the mempool"}, {RPCResult::Type::STR, "chain", "current network name (" LIST_CHAIN_NAMES ")"}, {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)"}, + {RPCResult::Type::OBJ, "next", "The next block, if found now", + { + {RPCResult::Type::NUM, "height", "The next height"}, + {RPCResult::Type::STR_HEX, "bits", "The next target nBits"}, + {RPCResult::Type::NUM, "difficulty", "The next difficulty"}, + {RPCResult::Type::STR_HEX, "target", "The next target"} + }}, (IsDeprecatedRPCEnabled("warnings") ? RPCResult{RPCResult::Type::STR, "warnings", "any network and blockchain warnings (DEPRECATED)"} : RPCResult{RPCResult::Type::ARR, "warnings", "any network and blockchain warnings (run with `-deprecatedrpc=warnings` to return the latest warning as a single string)", @@ -447,8 +455,9 @@ static RPCHelpMan getmininginfo() const CTxMemPool& mempool = EnsureMemPool(node); ChainstateManager& chainman = EnsureChainman(node); LOCK(cs_main); + auto consensusParams{chainman.GetParams().GetConsensus()}; const CChain& active_chain = chainman.ActiveChain(); - const CBlockIndex* tip{CHECK_NONFATAL(active_chain.Tip())}; + CBlockIndex* tip{CHECK_NONFATAL(active_chain.Tip())}; UniValue obj(UniValue::VOBJ); obj.pushKV("blocks", active_chain.Height()); @@ -456,13 +465,24 @@ static RPCHelpMan getmininginfo() if (BlockAssembler::m_last_block_num_txs) obj.pushKV("currentblocktx", *BlockAssembler::m_last_block_num_txs); obj.pushKV("bits", strprintf("%08x", tip->nBits)); obj.pushKV("difficulty", GetDifficulty(*tip)); - obj.pushKV("target", GetTarget(*tip, chainman.GetParams().GetConsensus().powLimit).GetHex()); + obj.pushKV("target", GetTarget(*tip, consensusParams.powLimit).GetHex()); obj.pushKV("networkhashps", getnetworkhashps().HandleRequest(request)); obj.pushKV("pooledtx", (uint64_t)mempool.size()); obj.pushKV("chain", chainman.GetParams().GetChainTypeString()); + + UniValue next(UniValue::VOBJ); + CBlockIndex next_index; + NextEmptyBlockIndex(tip, consensusParams, next_index); + + next.pushKV("height", next_index.nHeight); + next.pushKV("bits", strprintf("%08x", next_index.nBits)); + next.pushKV("difficulty", GetDifficulty(next_index)); + next.pushKV("target", GetTarget(next_index,consensusParams.powLimit).GetHex()); + obj.pushKV("next", next); + if (chainman.GetParams().GetChainType() == ChainType::SIGNET) { const std::vector& signet_challenge = - chainman.GetParams().GetConsensus().signet_challenge; + consensusParams.signet_challenge; obj.pushKV("signet_challenge", HexStr(signet_challenge)); } obj.pushKV("warnings", node::GetWarningsForRpc(*CHECK_NONFATAL(node.warnings), IsDeprecatedRPCEnabled("warnings"))); diff --git a/test/functional/mining_basic.py b/test/functional/mining_basic.py index 912dbf284fadc9..2e9431cf7a06f1 100755 --- a/test/functional/mining_basic.py +++ b/test/functional/mining_basic.py @@ -211,6 +211,12 @@ def assert_submitblock(block, result_str_1, result_str_2=None): assert_equal(mining_info['bits'], REGTEST_N_BITS_STR) assert_equal(mining_info['target'], REGTEST_TARGET_STR) assert_equal(mining_info['difficulty'], Decimal('4.656542373906925E-10')) + assert_equal(mining_info['next'], { + 'height': 201, + 'bits': REGTEST_N_BITS_STR, + 'target': REGTEST_TARGET_STR, + 'difficulty': Decimal('4.656542373906925E-10') + }) assert_equal(mining_info['networkhashps'], Decimal('0.003333333333333334')) assert_equal(mining_info['pooledtx'], 0)