Skip to content

Commit

Permalink
rpc: add optional blockhash to waitfornewblock
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjors committed Sep 4, 2024
1 parent 6c41928 commit 53c9118
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/release-30635.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Updated RPCs
------------

- The waitfornewblock RPC takes an optional `blockhash` argument. (#30635)
16 changes: 16 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ static RPCHelpMan waitfornewblock()
"\nMake sure to use no RPC timeout (bitcoin-cli -rpcclienttimeout=0)",
{
{"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "Last known block hash. Returns immediately if the tip is different."},
},
RPCResult{
RPCResult::Type::OBJ, "", "",
Expand All @@ -280,7 +281,22 @@ static RPCHelpMan waitfornewblock()
NodeContext& node = EnsureAnyNodeContext(request.context);
Mining& miner = EnsureMining(node);

// Use tip as reference block, unless a block hash was provided.
auto block{CHECK_NONFATAL(miner.getTip()).value()};
if (!request.params[1].isNull()) {
uint256 hash(ParseHashV(request.params[1], "blockhash"));
{
ChainstateManager& chainman = EnsureAnyChainman(request.context);
LOCK(cs_main);
const CBlockIndex* block_index{chainman.m_blockman.LookupBlockIndex(hash)};
if (!block_index) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
block.hash = block_index->GetBlockHash();
block.height = block_index->nHeight;
}
}

if (IsRPCRunning()) {
block = timeout ? miner.waitTipChanged(block.hash, std::chrono::milliseconds(timeout)) : miner.waitTipChanged(block.hash);
}
Expand Down
2 changes: 1 addition & 1 deletion test/functional/rpc_blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def _test_waitforblock(self):
node.reconsiderblock(rollback_hash)
# The chain has probably already been restored by the time reconsiderblock returns,
# but poll anyway.
self.wait_until(lambda: node.waitfornewblock(timeout=100)['hash'] == current_hash)
self.wait_until(lambda: node.waitfornewblock(blockhash=rollback_header['previousblockhash'])['hash'] == current_hash)

def _test_waitforblockheight(self):
self.log.info("Test waitforblockheight")
Expand Down

0 comments on commit 53c9118

Please sign in to comment.