mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 06:58:57 +01:00
Add findFork and findBlock to the Chain interface
And use them to remove uses of chainActive and mapBlockIndex in wallet code This commit does not change behavior. Co-authored-by: Ben Woosley <ben.woosley@gmail.com>
This commit is contained in:
@@ -5,6 +5,8 @@
|
||||
#include <interfaces/chain.h>
|
||||
|
||||
#include <chain.h>
|
||||
#include <chainparams.h>
|
||||
#include <primitives/block.h>
|
||||
#include <sync.h>
|
||||
#include <uint256.h>
|
||||
#include <util/system.h>
|
||||
@@ -58,6 +60,22 @@ class LockImpl : public Chain::Lock
|
||||
assert(block != nullptr);
|
||||
return block->GetMedianTimePast();
|
||||
}
|
||||
Optional<int> findFork(const uint256& hash, Optional<int>* height) override
|
||||
{
|
||||
const CBlockIndex* block = LookupBlockIndex(hash);
|
||||
const CBlockIndex* fork = block ? ::chainActive.FindFork(block) : nullptr;
|
||||
if (height) {
|
||||
if (block) {
|
||||
*height = block->nHeight;
|
||||
} else {
|
||||
height->reset();
|
||||
}
|
||||
}
|
||||
if (fork) {
|
||||
return fork->nHeight;
|
||||
}
|
||||
return nullopt;
|
||||
}
|
||||
};
|
||||
|
||||
class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
|
||||
@@ -77,6 +95,27 @@ public:
|
||||
return std::move(result);
|
||||
}
|
||||
std::unique_ptr<Chain::Lock> assumeLocked() override { return MakeUnique<LockImpl>(); }
|
||||
bool findBlock(const uint256& hash, CBlock* block, int64_t* time, int64_t* time_max) override
|
||||
{
|
||||
CBlockIndex* index;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
index = LookupBlockIndex(hash);
|
||||
if (!index) {
|
||||
return false;
|
||||
}
|
||||
if (time) {
|
||||
*time = index->GetBlockTime();
|
||||
}
|
||||
if (time_max) {
|
||||
*time_max = index->GetBlockTimeMax();
|
||||
}
|
||||
}
|
||||
if (block && !ReadBlockFromDisk(*block, index, Params().GetConsensus())) {
|
||||
block->SetNull();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user