Changeset View
Changeset View
Standalone View
Standalone View
src/interfaces/chain.cpp
Show All 32 Lines | |||||
#include <memory> | #include <memory> | ||||
#include <utility> | #include <utility> | ||||
namespace interfaces { | namespace interfaces { | ||||
namespace { | namespace { | ||||
class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex> { | class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex> { | ||||
Optional<int> getHeight() override { | Optional<int> getHeight() override { | ||||
LockAnnotation lock(::cs_main); | |||||
int height = ::ChainActive().Height(); | int height = ::ChainActive().Height(); | ||||
if (height >= 0) { | if (height >= 0) { | ||||
return height; | return height; | ||||
} | } | ||||
return nullopt; | return nullopt; | ||||
} | } | ||||
Optional<int> getBlockHeight(const BlockHash &hash) override { | Optional<int> getBlockHeight(const BlockHash &hash) override { | ||||
LockAnnotation lock(::cs_main); | |||||
CBlockIndex *block = LookupBlockIndex(hash); | CBlockIndex *block = LookupBlockIndex(hash); | ||||
if (block && ::ChainActive().Contains(block)) { | if (block && ::ChainActive().Contains(block)) { | ||||
return block->nHeight; | return block->nHeight; | ||||
} | } | ||||
return nullopt; | return nullopt; | ||||
} | } | ||||
int getBlockDepth(const BlockHash &hash) override { | int getBlockDepth(const BlockHash &hash) override { | ||||
const Optional<int> tip_height = getHeight(); | const Optional<int> tip_height = getHeight(); | ||||
const Optional<int> height = getBlockHeight(hash); | const Optional<int> height = getBlockHeight(hash); | ||||
return tip_height && height ? *tip_height - *height + 1 : 0; | return tip_height && height ? *tip_height - *height + 1 : 0; | ||||
} | } | ||||
BlockHash getBlockHash(int height) override { | BlockHash getBlockHash(int height) override { | ||||
LockAnnotation lock(::cs_main); | |||||
CBlockIndex *block = ::ChainActive()[height]; | CBlockIndex *block = ::ChainActive()[height]; | ||||
assert(block != nullptr); | assert(block != nullptr); | ||||
return block->GetBlockHash(); | return block->GetBlockHash(); | ||||
} | } | ||||
int64_t getBlockTime(int height) override { | int64_t getBlockTime(int height) override { | ||||
LockAnnotation lock(::cs_main); | |||||
CBlockIndex *block = ::ChainActive()[height]; | CBlockIndex *block = ::ChainActive()[height]; | ||||
assert(block != nullptr); | assert(block != nullptr); | ||||
return block->GetBlockTime(); | return block->GetBlockTime(); | ||||
} | } | ||||
int64_t getBlockMedianTimePast(int height) override { | int64_t getBlockMedianTimePast(int height) override { | ||||
LockAnnotation lock(::cs_main); | |||||
CBlockIndex *block = ::ChainActive()[height]; | CBlockIndex *block = ::ChainActive()[height]; | ||||
assert(block != nullptr); | assert(block != nullptr); | ||||
return block->GetMedianTimePast(); | return block->GetMedianTimePast(); | ||||
} | } | ||||
bool haveBlockOnDisk(int height) override { | bool haveBlockOnDisk(int height) override { | ||||
LockAnnotation lock(::cs_main); | |||||
CBlockIndex *block = ::ChainActive()[height]; | CBlockIndex *block = ::ChainActive()[height]; | ||||
return block && (block->nStatus.hasData() != 0) && block->nTx > 0; | return block && (block->nStatus.hasData() != 0) && block->nTx > 0; | ||||
} | } | ||||
Optional<int> | Optional<int> | ||||
findFirstBlockWithTimeAndHeight(int64_t time, int height, | findFirstBlockWithTimeAndHeight(int64_t time, int height, | ||||
BlockHash *hash) override { | BlockHash *hash) override { | ||||
LockAnnotation lock(::cs_main); | |||||
CBlockIndex *block = | CBlockIndex *block = | ||||
::ChainActive().FindEarliestAtLeast(time, height); | ::ChainActive().FindEarliestAtLeast(time, height); | ||||
if (block) { | if (block) { | ||||
if (hash) { | if (hash) { | ||||
*hash = block->GetBlockHash(); | *hash = block->GetBlockHash(); | ||||
} | } | ||||
return block->nHeight; | return block->nHeight; | ||||
} | } | ||||
return nullopt; | return nullopt; | ||||
} | } | ||||
Optional<int> findPruned(int start_height, | Optional<int> findPruned(int start_height, | ||||
Optional<int> stop_height) override { | Optional<int> stop_height) override { | ||||
LockAnnotation lock(::cs_main); | |||||
if (::fPruneMode) { | if (::fPruneMode) { | ||||
CBlockIndex *block = stop_height ? ::ChainActive()[*stop_height] | CBlockIndex *block = stop_height ? ::ChainActive()[*stop_height] | ||||
: ::ChainActive().Tip(); | : ::ChainActive().Tip(); | ||||
while (block && block->nHeight >= start_height) { | while (block && block->nHeight >= start_height) { | ||||
if (block->nStatus.hasData() == 0) { | if (block->nStatus.hasData() == 0) { | ||||
return block->nHeight; | return block->nHeight; | ||||
} | } | ||||
block = block->pprev; | block = block->pprev; | ||||
} | } | ||||
} | } | ||||
return nullopt; | return nullopt; | ||||
} | } | ||||
Optional<int> findFork(const BlockHash &hash, | Optional<int> findFork(const BlockHash &hash, | ||||
Optional<int> *height) override { | Optional<int> *height) override { | ||||
LockAnnotation lock(::cs_main); | |||||
const CBlockIndex *block = LookupBlockIndex(hash); | const CBlockIndex *block = LookupBlockIndex(hash); | ||||
const CBlockIndex *fork = | const CBlockIndex *fork = | ||||
block ? ::ChainActive().FindFork(block) : nullptr; | block ? ::ChainActive().FindFork(block) : nullptr; | ||||
if (height) { | if (height) { | ||||
if (block) { | if (block) { | ||||
*height = block->nHeight; | *height = block->nHeight; | ||||
} else { | } else { | ||||
height->reset(); | height->reset(); | ||||
} | } | ||||
} | } | ||||
if (fork) { | if (fork) { | ||||
return fork->nHeight; | return fork->nHeight; | ||||
} | } | ||||
return nullopt; | return nullopt; | ||||
} | } | ||||
CBlockLocator getTipLocator() override { | CBlockLocator getTipLocator() override { | ||||
LockAnnotation lock(::cs_main); | |||||
return ::ChainActive().GetLocator(); | return ::ChainActive().GetLocator(); | ||||
} | } | ||||
Optional<int> findLocatorFork(const CBlockLocator &locator) override { | Optional<int> findLocatorFork(const CBlockLocator &locator) override { | ||||
LockAnnotation lock(::cs_main); | LockAnnotation lock(::cs_main); | ||||
if (CBlockIndex *fork = | if (CBlockIndex *fork = | ||||
FindForkInGlobalIndex(::ChainActive(), locator)) { | FindForkInGlobalIndex(::ChainActive(), locator)) { | ||||
return fork->nHeight; | return fork->nHeight; | ||||
} | } | ||||
Show All 10 Lines | class LockImpl : public Chain::Lock, public UniqueLock<RecursiveMutex> { | ||||
CValidationState &state) override { | CValidationState &state) override { | ||||
LockAnnotation lock(::cs_main); | LockAnnotation lock(::cs_main); | ||||
return AcceptToMemoryPool(config, ::g_mempool, state, tx, | return AcceptToMemoryPool(config, ::g_mempool, state, tx, | ||||
nullptr /* missing inputs */, | nullptr /* missing inputs */, | ||||
false /* bypass limits */, absurd_fee); | false /* bypass limits */, absurd_fee); | ||||
} | } | ||||
using UniqueLock::UniqueLock; | using UniqueLock::UniqueLock; | ||||
}; | }; // namespace interfaces | ||||
class NotificationsHandlerImpl : public Handler, CValidationInterface { | class NotificationsHandlerImpl : public Handler, CValidationInterface { | ||||
public: | public: | ||||
explicit NotificationsHandlerImpl(Chain &chain, | explicit NotificationsHandlerImpl(Chain &chain, | ||||
Chain::Notifications ¬ifications) | Chain::Notifications ¬ifications) | ||||
: m_chain(chain), m_notifications(¬ifications) { | : m_chain(chain), m_notifications(¬ifications) { | ||||
RegisterValidationInterface(this); | RegisterValidationInterface(this); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 237 Lines • Show Last 20 Lines |