Skip to content

Commit ba7e17e

Browse files
committed
rpc, test: document {previous,next}blockhash as optional
Affects the following RPCs: - getblockheader - getblock Also adds trivial tests on genesis block (should not contain "previousblockhash") and best block (should not contain "nextblockhash").
1 parent 32b191f commit ba7e17e

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

src/rpc/blockchain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,8 @@ static RPCHelpMan getblockheader()
821821
{RPCResult::Type::NUM, "difficulty", "The difficulty"},
822822
{RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the current chain"},
823823
{RPCResult::Type::NUM, "nTx", "The number of transactions in the block"},
824-
{RPCResult::Type::STR_HEX, "previousblockhash", "The hash of the previous block"},
825-
{RPCResult::Type::STR_HEX, "nextblockhash", "The hash of the next block"},
824+
{RPCResult::Type::STR_HEX, "previousblockhash", /* optional */ true, "The hash of the previous block (if available)"},
825+
{RPCResult::Type::STR_HEX, "nextblockhash", /* optional */ true, "The hash of the next block (if available)"},
826826
}},
827827
RPCResult{"for verbose=false",
828828
RPCResult::Type::STR_HEX, "", "A string that is serialized, hex-encoded data for block 'hash'"},
@@ -929,8 +929,8 @@ static RPCHelpMan getblock()
929929
{RPCResult::Type::NUM, "difficulty", "The difficulty"},
930930
{RPCResult::Type::STR_HEX, "chainwork", "Expected number of hashes required to produce the chain up to this block (in hex)"},
931931
{RPCResult::Type::NUM, "nTx", "The number of transactions in the block"},
932-
{RPCResult::Type::STR_HEX, "previousblockhash", "The hash of the previous block"},
933-
{RPCResult::Type::STR_HEX, "nextblockhash", "The hash of the next block"},
932+
{RPCResult::Type::STR_HEX, "previousblockhash", /* optional */ true, "The hash of the previous block (if available)"},
933+
{RPCResult::Type::STR_HEX, "nextblockhash", /* optional */ true, "The hash of the next block (if available)"},
934934
}},
935935
RPCResult{"for verbosity = 2",
936936
RPCResult::Type::OBJ, "", "",

test/functional/rpc_blockchain.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ def _test_getblockheader(self):
304304
header.calc_sha256()
305305
assert_equal(header.hash, besthash)
306306

307+
assert 'previousblockhash' not in node.getblockheader(node.getblockhash(0))
308+
assert 'nextblockhash' not in node.getblockheader(node.getbestblockhash())
309+
307310
def _test_getdifficulty(self):
308311
difficulty = self.nodes[0].getdifficulty()
309312
# 1 hash in 2 should be valid, so difficulty should be 1/2**31
@@ -408,6 +411,9 @@ def move_block_file(old, new):
408411
# Restore chain state
409412
move_block_file('rev_wrong', 'rev00000.dat')
410413

414+
assert 'previousblockhash' not in node.getblock(node.getblockhash(0))
415+
assert 'nextblockhash' not in node.getblock(node.getbestblockhash())
416+
411417

412418
if __name__ == '__main__':
413419
BlockchainTest().main()

0 commit comments

Comments
 (0)