|
64 | 64 | using kernel::CCoinsStats;
|
65 | 65 | using kernel::CoinStatsHashType;
|
66 | 66 |
|
| 67 | +using interfaces::BlockRef; |
67 | 68 | using interfaces::Mining;
|
68 | 69 | using node::BlockManager;
|
69 | 70 | using node::NodeContext;
|
@@ -286,12 +287,17 @@ static RPCHelpMan waitfornewblock()
|
286 | 287 | NodeContext& node = EnsureAnyNodeContext(request.context);
|
287 | 288 | Mining& miner = EnsureMining(node);
|
288 | 289 |
|
289 |
| - auto block{CHECK_NONFATAL(miner.getTip()).value()}; |
290 |
| - block = timeout ? miner.waitTipChanged(block.hash, std::chrono::milliseconds(timeout)) : miner.waitTipChanged(block.hash); |
| 290 | + // Abort if RPC came out of warmup too early |
| 291 | + 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); |
| 294 | + |
| 295 | + // Return current block upon shutdown |
| 296 | + if (block) current_block = *block; |
291 | 297 |
|
292 | 298 | UniValue ret(UniValue::VOBJ);
|
293 |
| - ret.pushKV("hash", block.hash.GetHex()); |
294 |
| - ret.pushKV("height", block.height); |
| 299 | + ret.pushKV("hash", current_block.hash.GetHex()); |
| 300 | + ret.pushKV("height", current_block.height); |
295 | 301 | return ret;
|
296 | 302 | },
|
297 | 303 | };
|
@@ -330,22 +336,28 @@ static RPCHelpMan waitforblock()
|
330 | 336 | NodeContext& node = EnsureAnyNodeContext(request.context);
|
331 | 337 | Mining& miner = EnsureMining(node);
|
332 | 338 |
|
333 |
| - auto block{CHECK_NONFATAL(miner.getTip()).value()}; |
| 339 | + // Abort if RPC came out of warmup too early |
| 340 | + BlockRef current_block{CHECK_NONFATAL(miner.getTip()).value()}; |
| 341 | + |
334 | 342 | const auto deadline{std::chrono::steady_clock::now() + 1ms * timeout};
|
335 |
| - while (block.hash != hash) { |
| 343 | + while (current_block.hash != hash) { |
| 344 | + std::optional<BlockRef> block; |
336 | 345 | if (timeout) {
|
337 | 346 | auto now{std::chrono::steady_clock::now()};
|
338 | 347 | if (now >= deadline) break;
|
339 | 348 | const MillisecondsDouble remaining{deadline - now};
|
340 |
| - block = miner.waitTipChanged(block.hash, remaining); |
| 349 | + block = miner.waitTipChanged(current_block.hash, remaining); |
341 | 350 | } else {
|
342 |
| - block = miner.waitTipChanged(block.hash); |
| 351 | + block = miner.waitTipChanged(current_block.hash); |
343 | 352 | }
|
| 353 | + // Return current block upon shutdown |
| 354 | + if (!block) break; |
| 355 | + current_block = *block; |
344 | 356 | }
|
345 | 357 |
|
346 | 358 | UniValue ret(UniValue::VOBJ);
|
347 |
| - ret.pushKV("hash", block.hash.GetHex()); |
348 |
| - ret.pushKV("height", block.height); |
| 359 | + ret.pushKV("hash", current_block.hash.GetHex()); |
| 360 | + ret.pushKV("height", current_block.height); |
349 | 361 | return ret;
|
350 | 362 | },
|
351 | 363 | };
|
@@ -385,23 +397,29 @@ static RPCHelpMan waitforblockheight()
|
385 | 397 | NodeContext& node = EnsureAnyNodeContext(request.context);
|
386 | 398 | Mining& miner = EnsureMining(node);
|
387 | 399 |
|
388 |
| - auto block{CHECK_NONFATAL(miner.getTip()).value()}; |
| 400 | + // Abort if RPC came out of warmup too early |
| 401 | + BlockRef current_block{CHECK_NONFATAL(miner.getTip()).value()}; |
| 402 | + |
389 | 403 | const auto deadline{std::chrono::steady_clock::now() + 1ms * timeout};
|
390 | 404 |
|
391 |
| - while (block.height < height) { |
| 405 | + while (current_block.height < height) { |
| 406 | + std::optional<BlockRef> block; |
392 | 407 | if (timeout) {
|
393 | 408 | auto now{std::chrono::steady_clock::now()};
|
394 | 409 | if (now >= deadline) break;
|
395 | 410 | const MillisecondsDouble remaining{deadline - now};
|
396 |
| - block = miner.waitTipChanged(block.hash, remaining); |
| 411 | + block = miner.waitTipChanged(current_block.hash, remaining); |
397 | 412 | } else {
|
398 |
| - block = miner.waitTipChanged(block.hash); |
| 413 | + block = miner.waitTipChanged(current_block.hash); |
399 | 414 | }
|
| 415 | + // Return current block on shutdown |
| 416 | + if (!block) break; |
| 417 | + current_block = *block; |
400 | 418 | }
|
401 | 419 |
|
402 | 420 | UniValue ret(UniValue::VOBJ);
|
403 |
| - ret.pushKV("hash", block.hash.GetHex()); |
404 |
| - ret.pushKV("height", block.height); |
| 421 | + ret.pushKV("hash", current_block.hash.GetHex()); |
| 422 | + ret.pushKV("height", current_block.height); |
405 | 423 | return ret;
|
406 | 424 | },
|
407 | 425 | };
|
|
0 commit comments