MCPcopy Create free account
hub / github.com/ElementsProject/elements / waitfornewblock

Function waitfornewblock

src/rpc/blockchain.cpp:343–383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

341}
342
343static RPCHelpMan waitfornewblock()
344{
345 return RPCHelpMan{"waitfornewblock",
346 "\nWaits for a specific new block and returns useful info about it.\n"
347 "\nReturns the current block on timeout or exit.\n",
348 {
349 {"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."},
350 },
351 RPCResult{
352 RPCResult::Type::OBJ, "", "",
353 {
354 {RPCResult::Type::STR_HEX, "hash", "The blockhash"},
355 {RPCResult::Type::NUM, "height", "Block height"},
356 }},
357 RPCExamples{
358 HelpExampleCli("waitfornewblock", "1000")
359 + HelpExampleRpc("waitfornewblock", "1000")
360 },
361 [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
362{
363 int timeout = 0;
364 if (!request.params[0].isNull())
365 timeout = request.params[0].get_int();
366
367 CUpdatedBlock block;
368 {
369 WAIT_LOCK(cs_blockchange, lock);
370 block = latestblock;
371 if(timeout)
372 cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]() EXCLUSIVE_LOCKS_REQUIRED(cs_blockchange) {return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
373 else
374 cond_blockchange.wait(lock, [&block]() EXCLUSIVE_LOCKS_REQUIRED(cs_blockchange) {return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); });
375 block = latestblock;
376 }
377 UniValue ret(UniValue::VOBJ);
378 ret.pushKV("hash", block.hash.GetHex());
379 ret.pushKV("height", block.height);
380 return ret;
381},
382 };
383}
384
385static RPCHelpMan waitforblock()
386{

Callers

nothing calls this directly

Calls 9

HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
IsRPCRunningFunction · 0.85
get_intMethod · 0.80
waitMethod · 0.80
EXCLUSIVE_LOCKS_REQUIREDFunction · 0.70
isNullMethod · 0.45
pushKVMethod · 0.45
GetHexMethod · 0.45

Tested by

no test coverage detected