mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-29 21:41:07 +02:00
rpc: add optional blockhash to waitfornewblock
This commit is contained in:
4
doc/release-30635.md
Normal file
4
doc/release-30635.md
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Updated RPCs
|
||||||
|
------------
|
||||||
|
|
||||||
|
- The waitfornewblock RPC takes an optional `current_tip` argument. (#30635)
|
@@ -266,6 +266,7 @@ static RPCHelpMan waitfornewblock()
|
|||||||
"\nMake sure to use no RPC timeout (bitcoin-cli -rpcclienttimeout=0)",
|
"\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."},
|
{"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{
|
||||||
RPCResult::Type::OBJ, "", "",
|
RPCResult::Type::OBJ, "", "",
|
||||||
@@ -287,10 +288,22 @@ static RPCHelpMan waitfornewblock()
|
|||||||
NodeContext& node = EnsureAnyNodeContext(request.context);
|
NodeContext& node = EnsureAnyNodeContext(request.context);
|
||||||
Mining& miner = EnsureMining(node);
|
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()};
|
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
|
// Return current block upon shutdown
|
||||||
if (block) current_block = *block;
|
if (block) current_block = *block;
|
||||||
|
@@ -579,7 +579,8 @@ class BlockchainTest(BitcoinTestFramework):
|
|||||||
node.reconsiderblock(rollback_hash)
|
node.reconsiderblock(rollback_hash)
|
||||||
# The chain has probably already been restored by the time reconsiderblock returns,
|
# The chain has probably already been restored by the time reconsiderblock returns,
|
||||||
# but poll anyway.
|
# 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)
|
assert_raises_rpc_error(-1, "Negative timeout", node.waitfornewblock, -1)
|
||||||
|
|
||||||
def _test_waitforblockheight(self):
|
def _test_waitforblockheight(self):
|
||||||
|
Reference in New Issue
Block a user