| 383 | } |
| 384 | |
| 385 | static RPCHelpMan waitforblock() |
| 386 | { |
| 387 | return RPCHelpMan{"waitforblock", |
| 388 | "\nWaits for a specific new block and returns useful info about it.\n" |
| 389 | "\nReturns the current block on timeout or exit.\n", |
| 390 | { |
| 391 | {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "Block hash to wait for."}, |
| 392 | {"timeout", RPCArg::Type::NUM, RPCArg::Default{0}, "Time in milliseconds to wait for a response. 0 indicates no timeout."}, |
| 393 | }, |
| 394 | RPCResult{ |
| 395 | RPCResult::Type::OBJ, "", "", |
| 396 | { |
| 397 | {RPCResult::Type::STR_HEX, "hash", "The blockhash"}, |
| 398 | {RPCResult::Type::NUM, "height", "Block height"}, |
| 399 | }}, |
| 400 | RPCExamples{ |
| 401 | HelpExampleCli("waitforblock", "\"0000000000079f8ef3d2c688c244eb7a4570b24c9ed7b4a8c619eb02596f8862\" 1000") |
| 402 | + HelpExampleRpc("waitforblock", "\"0000000000079f8ef3d2c688c244eb7a4570b24c9ed7b4a8c619eb02596f8862\", 1000") |
| 403 | }, |
| 404 | [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 405 | { |
| 406 | int timeout = 0; |
| 407 | |
| 408 | uint256 hash(ParseHashV(request.params[0], "blockhash")); |
| 409 | |
| 410 | if (!request.params[1].isNull()) |
| 411 | timeout = request.params[1].get_int(); |
| 412 | |
| 413 | CUpdatedBlock block; |
| 414 | { |
| 415 | WAIT_LOCK(cs_blockchange, lock); |
| 416 | if(timeout) |
| 417 | cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&hash]() EXCLUSIVE_LOCKS_REQUIRED(cs_blockchange) {return latestblock.hash == hash || !IsRPCRunning();}); |
| 418 | else |
| 419 | cond_blockchange.wait(lock, [&hash]() EXCLUSIVE_LOCKS_REQUIRED(cs_blockchange) {return latestblock.hash == hash || !IsRPCRunning(); }); |
| 420 | block = latestblock; |
| 421 | } |
| 422 | |
| 423 | UniValue ret(UniValue::VOBJ); |
| 424 | ret.pushKV("hash", block.hash.GetHex()); |
| 425 | ret.pushKV("height", block.height); |
| 426 | return ret; |
| 427 | }, |
| 428 | }; |
| 429 | } |
| 430 | |
| 431 | static RPCHelpMan waitforblockheight() |
| 432 | { |
nothing calls this directly
no test coverage detected