Merge bitcoin/bitcoin#30635: rpc: add optional blockhash to waitfornewblock, unhide wait methods in help

c6e2c31c55 rpc: unhide waitfor{block,newblock,blockheight} (Sjors Provoost)
0786b7509a rpc: add optional blockhash to waitfornewblock (Sjors Provoost)

Pull request description:

  The `waitfornewblock` is inherently racy as the tip may have changed since the last RPC call, and can even change during initial processing of this call.

  Add an optional `blockhash` argument so the caller can specify their current tip. Return immediately if our tip is different.

  I've made it fail if `LookupBlockIndex` fails. This should never happen if the user got the block hash from our RPC in the first place.

  Finally, the `waitfor{block,newblock,blockheight}` RPC methods are no longer hidden in `help`:
  - the changes in #30409 ensured these methods _could_ work in the GUI
  - #31785 removed the guards that prevented GUI users from using them
  - this PR makes `waitfornewblock` reliable

  So there's no more reason to hide them.

ACKs for top commit:
  TheCharlatan:
    Re-ACK c6e2c31c55
  ryanofsky:
    Code review ACK c6e2c31c55. Just rebased and tweaked documentation since last review.
  glozow:
    utACK c6e2c31c55

Tree-SHA512: 84a0c94cb9a2e4449e7a395cf3dce1650626bd852e30e0e238a1aafae19d57bf440bfac226fd4da44eaa8d1b2fa4a8c1177b6c716235ab862a72ff5bf8fc67ac
This commit is contained in:
merge-script
2025-07-30 14:30:22 -04:00
3 changed files with 26 additions and 7 deletions

5
doc/release-30635.md Normal file
View File

@@ -0,0 +1,5 @@
Updated RPCs
------------
- The waitfornewblock now takes an optional `current_tip` argument. It is also no longer hidden. (#30635)
- The waitforblock and waitforblockheight RPCs are no longer hidden. (#30635)

View File

@@ -271,6 +271,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."},
{"current_tip", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, "Method waits for the chain tip to differ from this."},
},
RPCResult{
RPCResult::Type::OBJ, "", "",
@@ -292,10 +293,22 @@ static RPCHelpMan waitfornewblock()
NodeContext& node = EnsureAnyNodeContext(request.context);
Mining& miner = EnsureMining(node);
// Abort if RPC came out of warmup too early
// If the caller provided a current_tip value, pass it to waitTipChanged().
//
// If the caller did not provide a current tip hash, call getTip() to get
// one and wait for the tip to be different from this value. This mode is
// less reliable because if the tip changed between waitfornewblock calls,
// it will need to change a second time before this call returns.
BlockRef current_block{CHECK_NONFATAL(miner.getTip()).value()};
std::optional<BlockRef> block = timeout ? miner.waitTipChanged(current_block.hash, std::chrono::milliseconds(timeout)) :
miner.waitTipChanged(current_block.hash);
uint256 tip_hash{request.params[1].isNull()
? current_block.hash
: ParseHashV(request.params[1], "current_tip")};
// If the user provided an invalid current_tip then this call immediately
// returns the current tip.
std::optional<BlockRef> block = timeout ? miner.waitTipChanged(tip_hash, std::chrono::milliseconds(timeout)) :
miner.waitTipChanged(tip_hash);
// Return current block upon shutdown
if (block) current_block = *block;
@@ -3461,9 +3474,9 @@ void RegisterBlockchainRPCCommands(CRPCTable& t)
{"blockchain", &getchainstates},
{"hidden", &invalidateblock},
{"hidden", &reconsiderblock},
{"hidden", &waitfornewblock},
{"hidden", &waitforblock},
{"hidden", &waitforblockheight},
{"blockchain", &waitfornewblock},
{"blockchain", &waitforblock},
{"blockchain", &waitforblockheight},
{"hidden", &syncwithvalidationinterfacequeue},
};
for (const auto& c : commands) {

View File

@@ -593,7 +593,8 @@ class BlockchainTest(BitcoinTestFramework):
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(current_tip=rollback_header['previousblockhash'])['hash'] == current_hash)
assert_raises_rpc_error(-1, "Negative timeout", node.waitfornewblock, -1)
def _test_waitforblockheight(self):