mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
refactor: Replace uses ChainActive() in interfaces/chain.cpp
Suggested https://github.com/bitcoin/bitcoin/pull/19425#discussion_r456236407
This commit is contained in:
@@ -303,7 +303,7 @@ public:
|
||||
util::Ref m_context_ref;
|
||||
};
|
||||
|
||||
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock)
|
||||
bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<RecursiveMutex>& lock, const CChain& active)
|
||||
{
|
||||
if (!index) return false;
|
||||
if (block.m_hash) *block.m_hash = index->GetBlockHash();
|
||||
@@ -311,8 +311,8 @@ bool FillBlock(const CBlockIndex* index, const FoundBlock& block, UniqueLock<Rec
|
||||
if (block.m_time) *block.m_time = index->GetBlockTime();
|
||||
if (block.m_max_time) *block.m_max_time = index->GetBlockTimeMax();
|
||||
if (block.m_mtp_time) *block.m_mtp_time = index->GetMedianTimePast();
|
||||
if (block.m_in_active_chain) *block.m_in_active_chain = ChainActive()[index->nHeight] == index;
|
||||
if (block.m_next_block) FillBlock(ChainActive()[index->nHeight] == index ? ChainActive()[index->nHeight + 1] : nullptr, *block.m_next_block, lock);
|
||||
if (block.m_in_active_chain) *block.m_in_active_chain = active[index->nHeight] == index;
|
||||
if (block.m_next_block) FillBlock(active[index->nHeight] == index ? active[index->nHeight + 1] : nullptr, *block.m_next_block, lock, active);
|
||||
if (block.m_data) {
|
||||
REVERSE_LOCK(lock);
|
||||
if (!ReadBlockFromDisk(*block.m_data, index, Params().GetConsensus())) block.m_data->SetNull();
|
||||
@@ -415,7 +415,8 @@ public:
|
||||
Optional<int> getHeight() override
|
||||
{
|
||||
LOCK(::cs_main);
|
||||
int height = ::ChainActive().Height();
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
int height = active.Height();
|
||||
if (height >= 0) {
|
||||
return height;
|
||||
}
|
||||
@@ -424,20 +425,23 @@ public:
|
||||
uint256 getBlockHash(int height) override
|
||||
{
|
||||
LOCK(::cs_main);
|
||||
CBlockIndex* block = ::ChainActive()[height];
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
CBlockIndex* block = active[height];
|
||||
assert(block);
|
||||
return block->GetBlockHash();
|
||||
}
|
||||
bool haveBlockOnDisk(int height) override
|
||||
{
|
||||
LOCK(cs_main);
|
||||
CBlockIndex* block = ::ChainActive()[height];
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
CBlockIndex* block = active[height];
|
||||
return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0;
|
||||
}
|
||||
CBlockLocator getTipLocator() override
|
||||
{
|
||||
LOCK(cs_main);
|
||||
return ::ChainActive().GetLocator();
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
return active.GetLocator();
|
||||
}
|
||||
bool checkFinalTx(const CTransaction& tx) override
|
||||
{
|
||||
@@ -447,7 +451,8 @@ public:
|
||||
Optional<int> findLocatorFork(const CBlockLocator& locator) override
|
||||
{
|
||||
LOCK(cs_main);
|
||||
if (CBlockIndex* fork = FindForkInGlobalIndex(::ChainActive(), locator)) {
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
if (CBlockIndex* fork = FindForkInGlobalIndex(active, locator)) {
|
||||
return fork->nHeight;
|
||||
}
|
||||
return nullopt;
|
||||
@@ -455,40 +460,45 @@ public:
|
||||
bool findBlock(const uint256& hash, const FoundBlock& block) override
|
||||
{
|
||||
WAIT_LOCK(cs_main, lock);
|
||||
return FillBlock(LookupBlockIndex(hash), block, lock);
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
return FillBlock(LookupBlockIndex(hash), block, lock, active);
|
||||
}
|
||||
bool findFirstBlockWithTimeAndHeight(int64_t min_time, int min_height, const FoundBlock& block) override
|
||||
{
|
||||
WAIT_LOCK(cs_main, lock);
|
||||
return FillBlock(ChainActive().FindEarliestAtLeast(min_time, min_height), block, lock);
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
return FillBlock(active.FindEarliestAtLeast(min_time, min_height), block, lock, active);
|
||||
}
|
||||
bool findAncestorByHeight(const uint256& block_hash, int ancestor_height, const FoundBlock& ancestor_out) override
|
||||
{
|
||||
WAIT_LOCK(cs_main, lock);
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
if (const CBlockIndex* block = LookupBlockIndex(block_hash)) {
|
||||
if (const CBlockIndex* ancestor = block->GetAncestor(ancestor_height)) {
|
||||
return FillBlock(ancestor, ancestor_out, lock);
|
||||
return FillBlock(ancestor, ancestor_out, lock, active);
|
||||
}
|
||||
}
|
||||
return FillBlock(nullptr, ancestor_out, lock);
|
||||
return FillBlock(nullptr, ancestor_out, lock, active);
|
||||
}
|
||||
bool findAncestorByHash(const uint256& block_hash, const uint256& ancestor_hash, const FoundBlock& ancestor_out) override
|
||||
{
|
||||
WAIT_LOCK(cs_main, lock);
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
const CBlockIndex* block = LookupBlockIndex(block_hash);
|
||||
const CBlockIndex* ancestor = LookupBlockIndex(ancestor_hash);
|
||||
if (block && ancestor && block->GetAncestor(ancestor->nHeight) != ancestor) ancestor = nullptr;
|
||||
return FillBlock(ancestor, ancestor_out, lock);
|
||||
return FillBlock(ancestor, ancestor_out, lock, active);
|
||||
}
|
||||
bool findCommonAncestor(const uint256& block_hash1, const uint256& block_hash2, const FoundBlock& ancestor_out, const FoundBlock& block1_out, const FoundBlock& block2_out) override
|
||||
{
|
||||
WAIT_LOCK(cs_main, lock);
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
const CBlockIndex* block1 = LookupBlockIndex(block_hash1);
|
||||
const CBlockIndex* block2 = LookupBlockIndex(block_hash2);
|
||||
const CBlockIndex* ancestor = block1 && block2 ? LastCommonAncestor(block1, block2) : nullptr;
|
||||
// Using & instead of && below to avoid short circuiting and leaving
|
||||
// output uninitialized.
|
||||
return FillBlock(ancestor, ancestor_out, lock) & FillBlock(block1, block1_out, lock) & FillBlock(block2, block2_out, lock);
|
||||
return FillBlock(ancestor, ancestor_out, lock, active) & FillBlock(block1, block1_out, lock, active) & FillBlock(block2, block2_out, lock, active);
|
||||
}
|
||||
void findCoins(std::map<COutPoint, Coin>& coins) override { return FindCoins(m_node, coins); }
|
||||
double guessVerificationProgress(const uint256& block_hash) override
|
||||
@@ -608,7 +618,8 @@ public:
|
||||
{
|
||||
if (!old_tip.IsNull()) {
|
||||
LOCK(::cs_main);
|
||||
if (old_tip == ::ChainActive().Tip()->GetBlockHash()) return;
|
||||
const CChain& active = Assert(m_node.chainman)->ActiveChain();
|
||||
if (old_tip == active.Tip()->GetBlockHash()) return;
|
||||
}
|
||||
SyncWithValidationInterfaceQueue();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user