rpc: add target and bits to getblockchaininfo

This commit is contained in:
Sjors Provoost 2025-01-04 14:28:31 +01:00
parent baa504fdfa
commit f153f57acc
No known key found for this signature in database
GPG key ID: 57FF9BDBCC301009
2 changed files with 9 additions and 0 deletions

View file

@ -1299,6 +1299,8 @@ RPCHelpMan getblockchaininfo()
{RPCResult::Type::NUM, "blocks", "the height of the most-work fully-validated chain. The genesis block has height 0"}, {RPCResult::Type::NUM, "blocks", "the height of the most-work fully-validated chain. The genesis block has height 0"},
{RPCResult::Type::NUM, "headers", "the current number of headers we have validated"}, {RPCResult::Type::NUM, "headers", "the current number of headers we have validated"},
{RPCResult::Type::STR, "bestblockhash", "the hash of the currently best block"}, {RPCResult::Type::STR, "bestblockhash", "the hash of the currently best block"},
{RPCResult::Type::STR_HEX, "bits", "nBits: compact representation of the block difficulty target"},
{RPCResult::Type::STR_HEX, "target", "The difficulty target"},
{RPCResult::Type::NUM, "difficulty", "the current difficulty"}, {RPCResult::Type::NUM, "difficulty", "the current difficulty"},
{RPCResult::Type::NUM_TIME, "time", "The block time expressed in " + UNIX_EPOCH_TIME}, {RPCResult::Type::NUM_TIME, "time", "The block time expressed in " + UNIX_EPOCH_TIME},
{RPCResult::Type::NUM_TIME, "mediantime", "The median block time expressed in " + UNIX_EPOCH_TIME}, {RPCResult::Type::NUM_TIME, "mediantime", "The median block time expressed in " + UNIX_EPOCH_TIME},
@ -1337,6 +1339,8 @@ RPCHelpMan getblockchaininfo()
obj.pushKV("blocks", height); obj.pushKV("blocks", height);
obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1); obj.pushKV("headers", chainman.m_best_header ? chainman.m_best_header->nHeight : -1);
obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex()); obj.pushKV("bestblockhash", tip.GetBlockHash().GetHex());
obj.pushKV("bits", strprintf("%08x", tip.nBits));
obj.pushKV("target", GetTarget(tip, chainman.GetConsensus().powLimit).GetHex());
obj.pushKV("difficulty", GetDifficulty(tip)); obj.pushKV("difficulty", GetDifficulty(tip));
obj.pushKV("time", tip.GetBlockTime()); obj.pushKV("time", tip.GetBlockTime());
obj.pushKV("mediantime", tip.GetMedianTimePast()); obj.pushKV("mediantime", tip.GetMedianTimePast());

View file

@ -133,6 +133,7 @@ class BlockchainTest(BitcoinTestFramework):
keys = [ keys = [
'bestblockhash', 'bestblockhash',
'bits',
'blocks', 'blocks',
'chain', 'chain',
'chainwork', 'chainwork',
@ -142,6 +143,7 @@ class BlockchainTest(BitcoinTestFramework):
'mediantime', 'mediantime',
'pruned', 'pruned',
'size_on_disk', 'size_on_disk',
'target',
'time', 'time',
'verificationprogress', 'verificationprogress',
'warnings', 'warnings',
@ -198,6 +200,9 @@ class BlockchainTest(BitcoinTestFramework):
assert_equal(res['prune_target_size'], 576716800) assert_equal(res['prune_target_size'], 576716800)
assert_greater_than(res['size_on_disk'], 0) assert_greater_than(res['size_on_disk'], 0)
assert_equal(res['bits'], nbits_str(REGTEST_N_BITS))
assert_equal(res['target'], target_str(REGTEST_TARGET))
def check_signalling_deploymentinfo_result(self, gdi_result, height, blockhash, status_next): def check_signalling_deploymentinfo_result(self, gdi_result, height, blockhash, status_next):
assert height >= 144 and height <= 287 assert height >= 144 and height <= 287