20 #include <unordered_map> 41 std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map
GUARDED_BY(
m_mutex);
52 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
53 if (inserted.second) inserted.first->second = m_list.emplace(m_list.end());
54 inserted.first->second->callbacks = std::move(callbacks);
60 auto it = m_map.find(callbacks);
61 if (it != m_map.end()) {
62 if (!--it->second->count) m_list.erase(it->second);
74 for (
const auto& entry : m_map) {
75 if (!--entry.second->count) m_list.erase(entry.second);
83 for (
auto it = m_list.begin(); it != m_list.end();) {
89 it = --it->count ? std::next(it) : m_list.erase(it);
95 : m_internals{std::make_unique<ValidationSignalsImpl>(std::move(task_runner))} {}
140 m_internals->m_task_runner->insert(std::move(func));
147 std::promise<void> promise;
151 promise.get_future().wait();
158 #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \ 160 auto local_name = (name); \ 161 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \ 162 m_internals->m_task_runner->insert([=] { \ 163 LOG_EVENT(fmt, local_name, __VA_ARGS__); \ 168 #define LOG_EVENT(fmt, ...) \ 169 LogDebug(BCLog::VALIDATION, fmt "\n", __VA_ARGS__) 176 auto event = [pindexNew, pindexFork, fInitialDownload,
this] {
181 pindexFork ? pindexFork->GetBlockHash().ToString() :
"null",
193 auto event = [tx, mempool_sequence,
this] {
198 tx.
info.
m_tx->GetWitnessHash().ToString());
202 auto event = [tx, reason, mempool_sequence,
this] {
206 tx->GetHash().ToString(),
207 tx->GetWitnessHash().ToString(),
212 auto event = [role, pblock, pindex,
this] {
216 pblock->GetHash().ToString(),
222 auto event = [txs_removed_for_block, nBlockHeight,
this] {
227 txs_removed_for_block.size());
232 auto event = [pblock, pindex,
this] {
236 pblock->GetHash().ToString(),
241 auto event = [role, locator,
this] {
245 locator.IsNull() ?
"null" : locator.vHave.front().ToString());
249 LOG_EVENT(
"%s: block hash=%s state=%s", __func__,
255 LOG_EVENT(
"%s: block hash=%s", __func__, block->GetHash().ToString());
std::shared_ptr< const CTransaction > CTransactionRef
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
std::unique_ptr< ValidationSignalsImpl > m_internals
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void TransactionAddedToMempool(const NewMempoolTransactionInfo &, uint64_t mempool_sequence)
std::unique_ptr< util::TaskRunnerInterface > m_task_runner
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 Provides the block that was disconnected.
void Register(std::shared_ptr< CValidationInterface > callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
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()
virtual void ActiveTipChange(const CBlockIndex &new_tip, bool is_ibd)
Notifies listeners any time the block chain tip changes, synchronously.
void Unregister(CValidationInterface *callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
ValidationSignalsImpl(std::unique_ptr< util::TaskRunnerInterface > task_runner)
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Clear unregisters every previously registered callback, erasing every map entry.
virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
Implement this to subscribe to events generated in validation and mempool.
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
uint256 GetBlockHash() const
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
#define LOG_EVENT(fmt,...)
std::string ToString() const
void ActiveTipChange(const CBlockIndex &, bool)
List entries consist of a callback pointer and reference count.
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
virtual void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int nBlockHeight)
ValidationSignals(std::unique_ptr< util::TaskRunnerInterface > task_runner)
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
std::shared_ptr< CValidationInterface > callbacks
#define WAIT_LOCK(cs, name)
std::string ToString() const
This header provides an interface and simple implementation for a task runner.
ValidationSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &, unsigned int nBlockHeight)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
The block chain is a tree shaped structure starting with the genesis block at the root...
virtual void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
const CTransactionRef m_tx
#define AssertLockNotHeld(cs)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
virtual void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
int nHeight
height of the entry in the chain. The genesis block has height 0
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void Iterate(F &&f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
#define Assert(val)
Identity function.
void BlockChecked(const CBlock &, const BlockValidationState &)
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)