| 222 | } |
| 223 | |
| 224 | static UniValue waitfornewblock(const JSONRPCRequest& request) |
| 225 | { |
| 226 | if (request.fHelp || request.params.size() > 1) |
| 227 | throw std::runtime_error( |
| 228 | "waitfornewblock (timeout)\n" |
| 229 | "\nWaits for a specific new block and returns useful info about it.\n" |
| 230 | "\nReturns the current block on timeout or exit.\n" |
| 231 | "\nArguments:\n" |
| 232 | "1. timeout (int, optional, default=0) Time in milliseconds to wait for a response. 0 indicates no timeout.\n" |
| 233 | "\nResult:\n" |
| 234 | "{ (json object)\n" |
| 235 | " \"hash\" : { (string) The blockhash\n" |
| 236 | " \"height\" : { (int) Block height\n" |
| 237 | "}\n" |
| 238 | "\nExamples:\n" |
| 239 | + HelpExampleCli("waitfornewblock", "1000") |
| 240 | + HelpExampleRpc("waitfornewblock", "1000") |
| 241 | ); |
| 242 | int timeout = 0; |
| 243 | if (!request.params[0].isNull()) |
| 244 | timeout = request.params[0].get_int(); |
| 245 | |
| 246 | CUpdatedBlock block; |
| 247 | { |
| 248 | std::unique_lock<std::mutex> lock(cs_blockchange); |
| 249 | block = latestblock; |
| 250 | if(timeout) |
| 251 | cond_blockchange.wait_for(lock, std::chrono::milliseconds(timeout), [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); }); |
| 252 | else |
| 253 | cond_blockchange.wait(lock, [&block]{return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning(); }); |
| 254 | block = latestblock; |
| 255 | } |
| 256 | UniValue ret(UniValue::VOBJ); |
| 257 | ret.pushKV("hash", block.hash.GetHex()); |
| 258 | ret.pushKV("height", block.height); |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static UniValue waitforblock(const JSONRPCRequest& request) |
| 263 | { |
nothing calls this directly
no test coverage detected