Skip to content

Commit

Permalink
rpc: add next to getmininginfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Dec 31, 2024
1 parent 61121c6 commit 87a5eab
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ using node::BlockAssembler;
using node::NodeContext;
using node::RegenerateCommitments;
using node::UpdateTime;
using node::NextEmptyBlockIndex;
using util::ToString;

/**
Expand Down Expand Up @@ -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)",
Expand All @@ -447,22 +455,34 @@ 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());
if (BlockAssembler::m_last_block_weight) obj.pushKV("currentblockweight", *BlockAssembler::m_last_block_weight);
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<uint8_t>& 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")));
Expand Down
6 changes: 6 additions & 0 deletions test/functional/mining_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 87a5eab

Please sign in to comment.