@@ -303,14 +303,16 @@ class NodeImpl : public Node
303
303
util::Ref m_context_ref;
304
304
};
305
305
306
- bool FillBlock (const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock)
306
+ bool FillBlock (const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active )
307
307
{
308
308
if (!index) return false ;
309
309
if (block.m_hash ) *block.m_hash = index->GetBlockHash ();
310
310
if (block.m_height ) *block.m_height = index->nHeight ;
311
311
if (block.m_time ) *block.m_time = index->GetBlockTime ();
312
312
if (block.m_max_time ) *block.m_max_time = index->GetBlockTimeMax ();
313
313
if (block.m_mtp_time ) *block.m_mtp_time = index->GetMedianTimePast ();
314
+ if (block.m_in_active_chain ) *block.m_in_active_chain = active[index->nHeight ] == index;
315
+ if (block.m_next_block ) FillBlock (active[index->nHeight ] == index ? active[index->nHeight + 1 ] : nullptr , *block.m_next_block , lock, active);
314
316
if (block.m_data ) {
315
317
REVERSE_LOCK (lock);
316
318
if (!ReadBlockFromDisk (*block.m_data , index, Params ().GetConsensus ())) block.m_data ->SetNull ();
@@ -413,48 +415,33 @@ class ChainImpl : public Chain
413
415
Optional<int > getHeight () override
414
416
{
415
417
LOCK (::cs_main);
416
- int height = ::ChainActive ().Height ();
418
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
419
+ int height = active.Height ();
417
420
if (height >= 0 ) {
418
421
return height;
419
422
}
420
423
return nullopt;
421
424
}
422
- Optional<int > getBlockHeight (const uint256& hash) override
423
- {
424
- LOCK (::cs_main);
425
- CBlockIndex* block = LookupBlockIndex (hash);
426
- if (block && ::ChainActive ().Contains (block)) {
427
- return block->nHeight ;
428
- }
429
- return nullopt;
430
- }
431
425
uint256 getBlockHash (int height) override
432
426
{
433
427
LOCK (::cs_main);
434
- CBlockIndex* block = ::ChainActive ()[height];
428
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
429
+ CBlockIndex* block = active[height];
435
430
assert (block);
436
431
return block->GetBlockHash ();
437
432
}
438
433
bool haveBlockOnDisk (int height) override
439
434
{
440
435
LOCK (cs_main);
441
- CBlockIndex* block = ::ChainActive ()[height];
436
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
437
+ CBlockIndex* block = active[height];
442
438
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0 ) && block->nTx > 0 ;
443
439
}
444
- Optional<int > findFirstBlockWithTimeAndHeight (int64_t time, int height, uint256* hash) override
445
- {
446
- LOCK (cs_main);
447
- CBlockIndex* block = ::ChainActive ().FindEarliestAtLeast (time, height);
448
- if (block) {
449
- if (hash) *hash = block->GetBlockHash ();
450
- return block->nHeight ;
451
- }
452
- return nullopt;
453
- }
454
440
CBlockLocator getTipLocator () override
455
441
{
456
442
LOCK (cs_main);
457
- return ::ChainActive ().GetLocator ();
443
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
444
+ return active.GetLocator ();
458
445
}
459
446
bool checkFinalTx (const CTransaction& tx) override
460
447
{
@@ -464,55 +451,54 @@ class ChainImpl : public Chain
464
451
Optional<int > findLocatorFork (const CBlockLocator& locator) override
465
452
{
466
453
LOCK (cs_main);
467
- if (CBlockIndex* fork = FindForkInGlobalIndex (::ChainActive (), locator)) {
454
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
455
+ if (CBlockIndex* fork = FindForkInGlobalIndex (active, locator)) {
468
456
return fork->nHeight ;
469
457
}
470
458
return nullopt;
471
459
}
472
460
bool findBlock (const uint256& hash, const FoundBlock& block) override
473
461
{
474
462
WAIT_LOCK (cs_main, lock);
475
- return FillBlock (LookupBlockIndex (hash), block, lock);
463
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
464
+ return FillBlock (LookupBlockIndex (hash), block, lock, active);
476
465
}
477
466
bool findFirstBlockWithTimeAndHeight (int64_t min_time, int min_height, const FoundBlock& block) override
478
467
{
479
468
WAIT_LOCK (cs_main, lock);
480
- return FillBlock (ChainActive ().FindEarliestAtLeast (min_time, min_height), block, lock);
481
- }
482
- bool findNextBlock (const uint256& block_hash, int block_height, const FoundBlock& next, bool * reorg) override {
483
- WAIT_LOCK (cs_main, lock);
484
- CBlockIndex* block = ChainActive ()[block_height];
485
- if (block && block->GetBlockHash () != block_hash) block = nullptr ;
486
- if (reorg) *reorg = !block;
487
- return FillBlock (block ? ChainActive ()[block_height + 1 ] : nullptr , next, lock);
469
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
470
+ return FillBlock (active.FindEarliestAtLeast (min_time, min_height), block, lock, active);
488
471
}
489
472
bool findAncestorByHeight (const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override
490
473
{
491
474
WAIT_LOCK (cs_main, lock);
475
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
492
476
if (const CBlockIndex* block = LookupBlockIndex (block_hash)) {
493
477
if (const CBlockIndex* ancestor = block->GetAncestor (ancestor_height)) {
494
- return FillBlock (ancestor, ancestor_out, lock);
478
+ return FillBlock (ancestor, ancestor_out, lock, active );
495
479
}
496
480
}
497
- return FillBlock (nullptr , ancestor_out, lock);
481
+ return FillBlock (nullptr , ancestor_out, lock, active );
498
482
}
499
483
bool findAncestorByHash (const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
500
484
{
501
485
WAIT_LOCK (cs_main, lock);
486
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
502
487
const CBlockIndex* block = LookupBlockIndex (block_hash);
503
488
const CBlockIndex* ancestor = LookupBlockIndex (ancestor_hash);
504
489
if (block && ancestor && block->GetAncestor (ancestor->nHeight ) != ancestor) ancestor = nullptr ;
505
- return FillBlock (ancestor, ancestor_out, lock);
490
+ return FillBlock (ancestor, ancestor_out, lock, active );
506
491
}
507
492
bool findCommonAncestor (const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
508
493
{
509
494
WAIT_LOCK (cs_main, lock);
495
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
510
496
const CBlockIndex* block1 = LookupBlockIndex (block_hash1);
511
497
const CBlockIndex* block2 = LookupBlockIndex (block_hash2);
512
498
const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor (block1, block2) : nullptr ;
513
499
// Using & instead of && below to avoid short circuiting and leaving
514
500
// output uninitialized.
515
- return FillBlock (ancestor, ancestor_out, lock) & FillBlock (block1, block1_out, lock) & FillBlock (block2, block2_out, lock);
501
+ return FillBlock (ancestor, ancestor_out, lock, active ) & FillBlock (block1, block1_out, lock, active ) & FillBlock (block2, block2_out, lock, active );
516
502
}
517
503
void findCoins (std::map<COutPoint, Coin>& coins) override { return FindCoins (m_node, coins); }
518
504
double guessVerificationProgress (const uint256& block_hash) override
@@ -632,7 +618,8 @@ class ChainImpl : public Chain
632
618
{
633
619
if (!old_tip.IsNull ()) {
634
620
LOCK (::cs_main);
635
- if (old_tip == ::ChainActive ().Tip ()->GetBlockHash ()) return ;
621
+ const CChain& active = Assert (m_node.chainman )->ActiveChain ();
622
+ if (old_tip == active.Tip ()->GetBlockHash ()) return ;
636
623
}
637
624
SyncWithValidationInterfaceQueue ();
638
625
}
0 commit comments