@@ -266,6 +266,7 @@ static RPCHelpMan waitfornewblock()
266266 " \n Make sure to use no RPC timeout (bitcoin-cli -rpcclienttimeout=0)" ,
267267 {
268268 {" timeout" , RPCArg::Type::NUM, RPCArg::Default{0 }, " Time in milliseconds to wait for a response. 0 indicates no timeout." },
269+ {" current_tip" , RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, " Method waits for the chain tip to differ from this." },
269270 },
270271 RPCResult{
271272 RPCResult::Type::OBJ, " " , " " ,
@@ -287,10 +288,22 @@ static RPCHelpMan waitfornewblock()
287288 NodeContext& node = EnsureAnyNodeContext (request.context );
288289 Mining& miner = EnsureMining (node);
289290
290- // Abort if RPC came out of warmup too early
291+ // If the caller provided a current_tip value, pass it to waitTipChanged().
292+ //
293+ // If the caller did not provide a current tip hash, call getTip() to get
294+ // one and wait for the tip to be different from this value. This mode is
295+ // less reliable because if the tip changed between waitfornewblock calls,
296+ // it will need to change a second time before this call returns.
291297 BlockRef current_block{CHECK_NONFATAL (miner.getTip ()).value ()};
292- std::optional<BlockRef> block = timeout ? miner.waitTipChanged (current_block.hash , std::chrono::milliseconds (timeout)) :
293- miner.waitTipChanged (current_block.hash );
298+
299+ uint256 tip_hash{request.params [1 ].isNull ()
300+ ? current_block.hash
301+ : ParseHashV (request.params [1 ], " current_tip" )};
302+
303+ // If the user provided an invalid current_tip then this call immediately
304+ // returns the current tip.
305+ std::optional<BlockRef> block = timeout ? miner.waitTipChanged (tip_hash, std::chrono::milliseconds (timeout)) :
306+ miner.waitTipChanged (tip_hash);
294307
295308 // Return current block upon shutdown
296309 if (block) current_block = *block;
0 commit comments