22
22
#include < hash.h>
23
23
#include < index/blockfilterindex.h>
24
24
#include < index/coinstatsindex.h>
25
+ #include < interfaces/mining.h>
25
26
#include < kernel/coinstats.h>
26
27
#include < logging/timer.h>
27
28
#include < net.h>
61
62
using kernel::CCoinsStats;
62
63
using kernel::CoinStatsHashType;
63
64
65
+ using interfaces::Mining;
64
66
using node::BlockManager;
65
67
using node::NodeContext;
66
68
using node::SnapshotMetadata;
67
69
using util::MakeUnorderedList;
68
70
69
- struct CUpdatedBlock
70
- {
71
- uint256 hash;
72
- int height;
73
- };
74
-
75
- static GlobalMutex cs_blockchange;
76
- static std::condition_variable cond_blockchange;
77
- static CUpdatedBlock latestblock GUARDED_BY (cs_blockchange);
78
-
79
71
std::tuple<std::unique_ptr<CCoinsViewCursor>, CCoinsStats, const CBlockIndex*>
80
72
PrepareUTXOSnapshot (
81
73
Chainstate& chainstate,
@@ -262,16 +254,6 @@ static RPCHelpMan getbestblockhash()
262
254
};
263
255
}
264
256
265
- void RPCNotifyBlockChange (const CBlockIndex* pindex)
266
- {
267
- if (pindex) {
268
- LOCK (cs_blockchange);
269
- latestblock.hash = pindex->GetBlockHash ();
270
- latestblock.height = pindex->nHeight ;
271
- }
272
- cond_blockchange.notify_all ();
273
- }
274
-
275
257
static RPCHelpMan waitfornewblock ()
276
258
{
277
259
return RPCHelpMan{" waitfornewblock" ,
@@ -298,16 +280,14 @@ static RPCHelpMan waitfornewblock()
298
280
timeout = request.params [0 ].getInt <int >();
299
281
if (timeout < 0 ) throw JSONRPCError (RPC_MISC_ERROR, " Negative timeout" );
300
282
301
- CUpdatedBlock block;
302
- {
303
- WAIT_LOCK (cs_blockchange, lock);
304
- block = latestblock;
305
- if (timeout)
306
- 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 (); });
307
- else
308
- cond_blockchange.wait (lock, [&block]() EXCLUSIVE_LOCKS_REQUIRED (cs_blockchange) {return latestblock.height != block.height || latestblock.hash != block.hash || !IsRPCRunning (); });
309
- block = latestblock;
283
+ NodeContext& node = EnsureAnyNodeContext (request.context );
284
+ Mining& miner = EnsureMining (node);
285
+
286
+ auto block{CHECK_NONFATAL (miner.getTip ()).value ()};
287
+ if (IsRPCRunning ()) {
288
+ block = timeout ? miner.waitTipChanged (block.hash , std::chrono::milliseconds (timeout)) : miner.waitTipChanged (block.hash );
310
289
}
290
+
311
291
UniValue ret (UniValue::VOBJ);
312
292
ret.pushKV (" hash" , block.hash .GetHex ());
313
293
ret.pushKV (" height" , block.height );
@@ -346,14 +326,20 @@ static RPCHelpMan waitforblock()
346
326
timeout = request.params [1 ].getInt <int >();
347
327
if (timeout < 0 ) throw JSONRPCError (RPC_MISC_ERROR, " Negative timeout" );
348
328
349
- CUpdatedBlock block;
350
- {
351
- WAIT_LOCK (cs_blockchange, lock);
352
- if (timeout)
353
- cond_blockchange.wait_for (lock, std::chrono::milliseconds (timeout), [&hash]() EXCLUSIVE_LOCKS_REQUIRED (cs_blockchange) {return latestblock.hash == hash || !IsRPCRunning ();});
354
- else
355
- cond_blockchange.wait (lock, [&hash]() EXCLUSIVE_LOCKS_REQUIRED (cs_blockchange) {return latestblock.hash == hash || !IsRPCRunning (); });
356
- block = latestblock;
329
+ NodeContext& node = EnsureAnyNodeContext (request.context );
330
+ Mining& miner = EnsureMining (node);
331
+
332
+ auto block{CHECK_NONFATAL (miner.getTip ()).value ()};
333
+ const auto deadline{std::chrono::steady_clock::now () + 1ms * timeout};
334
+ while (IsRPCRunning () && block.hash != hash) {
335
+ if (timeout) {
336
+ auto now{std::chrono::steady_clock::now ()};
337
+ if (now >= deadline) break ;
338
+ const MillisecondsDouble remaining{deadline - now};
339
+ block = miner.waitTipChanged (block.hash , remaining);
340
+ } else {
341
+ block = miner.waitTipChanged (block.hash );
342
+ }
357
343
}
358
344
359
345
UniValue ret (UniValue::VOBJ);
@@ -395,15 +381,23 @@ static RPCHelpMan waitforblockheight()
395
381
timeout = request.params [1 ].getInt <int >();
396
382
if (timeout < 0 ) throw JSONRPCError (RPC_MISC_ERROR, " Negative timeout" );
397
383
398
- CUpdatedBlock block;
399
- {
400
- WAIT_LOCK (cs_blockchange, lock);
401
- if (timeout)
402
- cond_blockchange.wait_for (lock, std::chrono::milliseconds (timeout), [&height]() EXCLUSIVE_LOCKS_REQUIRED (cs_blockchange) {return latestblock.height >= height || !IsRPCRunning ();});
403
- else
404
- cond_blockchange.wait (lock, [&height]() EXCLUSIVE_LOCKS_REQUIRED (cs_blockchange) {return latestblock.height >= height || !IsRPCRunning (); });
405
- block = latestblock;
384
+ NodeContext& node = EnsureAnyNodeContext (request.context );
385
+ Mining& miner = EnsureMining (node);
386
+
387
+ auto block{CHECK_NONFATAL (miner.getTip ()).value ()};
388
+ const auto deadline{std::chrono::steady_clock::now () + 1ms * timeout};
389
+
390
+ while (IsRPCRunning () && block.height < height) {
391
+ if (timeout) {
392
+ auto now{std::chrono::steady_clock::now ()};
393
+ if (now >= deadline) break ;
394
+ const MillisecondsDouble remaining{deadline - now};
395
+ block = miner.waitTipChanged (block.hash , remaining);
396
+ } else {
397
+ block = miner.waitTipChanged (block.hash );
398
+ }
406
399
}
400
+
407
401
UniValue ret (UniValue::VOBJ);
408
402
ret.pushKV (" hash" , block.hash .GetHex ());
409
403
ret.pushKV (" height" , block.height );
0 commit comments