16 #include <unordered_map> 34 std::list<ListEntry> m_list
GUARDED_BY(m_mutex);
35 std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map
GUARDED_BY(m_mutex);
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);
100 m_internals.reset(
nullptr);
106 m_internals->m_schedulerClient.EmptyQueue();
112 if (!m_internals)
return 0;
113 return m_internals->m_schedulerClient.CallbacksPending();
125 g_signals.
m_internals->Register(std::move(callbacks));
157 g_signals.
m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
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());
std::shared_ptr< const CTransaction > CTransactionRef
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
std::unique_ptr< MainSignalsInstance > m_internals
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
std::deque< CInv >::iterator it
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
std::shared_ptr< CValidationInterface > callbacks
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
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...
size_t CallbacksPending()
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
Implement this to subscribe to events generated in validation.
void TransactionAddedToMempool(const CTransactionRef &, uint64_t mempool_sequence)
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
uint256 GetBlockHash() const
static CMainSignals g_signals
#define LOG_EVENT(fmt,...)
std::string ToString() const
SingleThreadedSchedulerClient m_schedulerClient
MainSignalsInstance(CScheduler *pscheduler)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
CMainSignals & GetMainSignals()
#define WAIT_LOCK(cs, name)
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
std::string ToString() const
std::vector< uint256 > vHave
void ChainStateFlushed(const CBlockLocator &)
void Unregister(CValidationInterface *callbacks)
void Clear()
Clear unregisters every previously registered callback, erasing every map entry.
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once) ...
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
The block chain is a tree shaped structure starting with the genesis block at the root...
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
#define AssertLockNotHeld(cs)
virtual void TransactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
Simple class for background tasks that should be run periodically or once "after a while"...
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void BlockChecked(const CBlock &, const BlockValidationState &)
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void Register(std::shared_ptr< CValidationInterface > callbacks)
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
List entries consist of a callback pointer and reference count.