 |
Bitcoin Core
21.99.0
P2P Digital Currency
|
Go to the documentation of this file.
16 #include <unordered_map>
35 std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map
GUARDED_BY(
m_mutex);
45 void Register(std::shared_ptr<CValidationInterface> callbacks)
48 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
49 if (inserted.second) inserted.first->second = m_list.emplace(m_list.end());
50 inserted.first->second->callbacks = std::move(callbacks);
56 auto it = m_map.find(callbacks);
57 if (
it != m_map.end()) {
58 if (!--
it->second->count) m_list.erase(
it->second);
70 for (
const auto& entry : m_map) {
71 if (!--entry.second->count) m_list.erase(entry.second);
79 for (
auto it = m_list.begin();
it != m_list.end();) {
85 it = --
it->count ? std::next(
it) : m_list.erase(
it);
113 return m_internals->m_schedulerClient.CallbacksPending();
164 std::promise<void> promise;
168 promise.get_future().wait();
175 #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
177 auto local_name = (name); \
178 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
179 m_internals->m_schedulerClient.AddToProcessQueue([=] { \
180 LOG_EVENT(fmt, local_name, __VA_ARGS__); \
185 #define LOG_EVENT(fmt, ...) \
186 LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
193 auto event = [pindexNew, pindexFork, fInitialDownload,
this] {
198 pindexFork ? pindexFork->GetBlockHash().ToString() :
"null",
203 auto event = [tx, mempool_sequence,
this] {
207 tx->GetHash().ToString(),
208 tx->GetWitnessHash().ToString());
212 auto event = [tx, reason, mempool_sequence,
this] {
216 tx->GetHash().ToString(),
217 tx->GetWitnessHash().ToString());
221 auto event = [pblock, pindex,
this] {
225 pblock->GetHash().ToString(),
231 auto event = [pblock, pindex,
this] {
235 pblock->GetHash().ToString(),
240 auto event = [locator,
this] {
244 locator.
IsNull() ?
"null" : locator.
vHave.front().ToString());
248 LOG_EVENT(
"%s: block hash=%s state=%s", __func__,
254 LOG_EVENT(
"%s: block hash=%s", __func__, block->GetHash().ToString());
Implement this to subscribe to events generated in validation.
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Simple class for background tasks that should be run periodically or once "after a while".
size_t CallbacksPending()
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
Notifies listeners that a block which builds directly on our current tip has been received and connec...
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
void Unregister(CValidationInterface *callbacks)
MainSignalsInstance(CScheduler *pscheduler)
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
SingleThreadedSchedulerClient m_schedulerClient
void BlockChecked(const CBlock &, const BlockValidationState &)
static CMainSignals g_signals
std::shared_ptr< const CTransaction > CTransactionRef
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
CMainSignals & GetMainSignals()
std::string ToString() const
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
#define AssertLockNotHeld(cs)
void Clear()
Clear unregisters every previously registered callback, erasing every map entry.
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
List entries consist of a callback pointer and reference count.
void Register(std::shared_ptr< CValidationInterface > callbacks)
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
virtual void TransactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once)
std::string ToString() const
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
uint256 GetBlockHash() const
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
void TransactionAddedToMempool(const CTransactionRef &, uint64_t mempool_sequence)
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
#define LOG_EVENT(fmt,...)
std::vector< uint256 > vHave
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
std::unique_ptr< MainSignalsInstance > m_internals
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
void ChainStateFlushed(const CBlockLocator &)
The block chain is a tree shaped structure starting with the genesis block at the root,...
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
#define WAIT_LOCK(cs, name)
std::shared_ptr< CValidationInterface > callbacks