mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
Add height, depth, and hash methods 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:
@@ -4,7 +4,9 @@
|
||||
|
||||
#include <interfaces/chain.h>
|
||||
|
||||
#include <chain.h>
|
||||
#include <sync.h>
|
||||
#include <uint256.h>
|
||||
#include <util/system.h>
|
||||
#include <validation.h>
|
||||
|
||||
@@ -16,6 +18,34 @@ namespace {
|
||||
|
||||
class LockImpl : public Chain::Lock
|
||||
{
|
||||
Optional<int> getHeight() override
|
||||
{
|
||||
int height = ::chainActive.Height();
|
||||
if (height >= 0) {
|
||||
return height;
|
||||
}
|
||||
return nullopt;
|
||||
}
|
||||
Optional<int> getBlockHeight(const uint256& hash) override
|
||||
{
|
||||
CBlockIndex* block = LookupBlockIndex(hash);
|
||||
if (block && ::chainActive.Contains(block)) {
|
||||
return block->nHeight;
|
||||
}
|
||||
return nullopt;
|
||||
}
|
||||
int getBlockDepth(const uint256& hash) override
|
||||
{
|
||||
const Optional<int> tip_height = getHeight();
|
||||
const Optional<int> height = getBlockHeight(hash);
|
||||
return tip_height && height ? *tip_height - *height + 1 : 0;
|
||||
}
|
||||
uint256 getBlockHash(int height) override
|
||||
{
|
||||
CBlockIndex* block = ::chainActive[height];
|
||||
assert(block != nullptr);
|
||||
return block->GetBlockHash();
|
||||
}
|
||||
};
|
||||
|
||||
class LockingStateImpl : public LockImpl, public UniqueLock<CCriticalSection>
|
||||
|
||||
Reference in New Issue
Block a user