18#include <unordered_map>
44 std::unordered_map<CValidationInterface *, std::list<ListEntry>::iterator>
56 void Register(std::shared_ptr<CValidationInterface> callbacks)
59 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
60 if (inserted.second) {
61 inserted.first->second = m_list.emplace(m_list.end());
63 inserted.first->second->callbacks = std::move(callbacks);
69 auto it = m_map.find(callbacks);
70 if (it != m_map.end()) {
71 if (!--it->second->count) {
72 m_list.erase(it->second);
84 for (
const auto &entry : m_map) {
85 if (!--entry.second->count) {
86 m_list.erase(entry.second);
95 for (
auto it = m_list.begin(); it != m_list.end();) {
101 it = --it->count ? std::next(it) : m_list.erase(it);
110 m_internals = std::make_unique<MainSignalsImpl>(scheduler);
127 return m_internals->m_schedulerClient.CallbacksPending();
135 std::shared_ptr<CValidationInterface> callbacks) {
149 std::shared_ptr<CValidationInterface> callbacks) {
173 std::promise<void> promise;
175 promise.get_future().wait();
182#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
184 auto local_name = (name); \
185 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
186 m_internals->m_schedulerClient.AddToProcessQueue([=] { \
187 LOG_EVENT(fmt, local_name, __VA_ARGS__); \
192#define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
196 bool fInitialDownload) {
202 auto event = [pindexNew, pindexFork, fInitialDownload,
this] {
208 event,
"%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
216 std::shared_ptr<
const std::vector<Coin>> spent_coins,
217 uint64_t mempool_sequence) {
218 auto event = [tx, spent_coins, mempool_sequence,
this] {
225 tx->GetHash().ToString());
230 uint64_t mempool_sequence) {
231 auto event = [tx, reason, mempool_sequence,
this] {
238 tx->GetHash().ToString(),
244 auto event = [pblock, pindex,
this] {
250 pblock->GetHash().ToString(), pindex->
nHeight);
254 const std::shared_ptr<const CBlock> &pblock,
const CBlockIndex *pindex) {
255 auto event = [pblock, pindex,
this] {
261 pblock->GetHash().ToString());
265 auto event = [locator,
this] {
272 : locator.
vHave.front().ToString());
277 LOG_EVENT(
"%s: block hash=%s state=%s", __func__,
285 const CBlockIndex *pindex,
const std::shared_ptr<const CBlock> &block) {
286 LOG_EVENT(
"%s: block hash=%s", __func__, block->GetHash().ToString());
293 auto event = [pindex,
this] {
303 const CBlockIndex *pindex,
const std::shared_ptr<const CBlock> &block) {
304 auto event = [pindex, block,
this] {
310 block ? block->GetHash().ToString() :
"null");
The block chain is a tree shaped structure starting with the genesis block at the root,...
BlockHash GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
size_t CallbacksPending()
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, uint64_t mempool_sequence)
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void BlockChecked(const CBlock &, const BlockValidationState &)
std::unique_ptr< MainSignalsImpl > m_internals
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once)
void BlockInvalidated(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
void ChainStateFlushed(const CBlockLocator &)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void BlockFinalized(const CBlockIndex *pindex)
Simple class for background tasks that should be run periodically or once "after a while".
Implement this to subscribe to events generated in validation.
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...
virtual void BlockInvalidated(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
virtual void BlockFinalized(const CBlockIndex *pindex)
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
virtual void TransactionAddedToMempool(const CTransactionRef &tx, std::shared_ptr< const std::vector< Coin > > spent_coins, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
MainSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Clear unregisters every previously registered callback, erasing every map entry.
void Iterate(F &&f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void Unregister(CValidationInterface *callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void Register(std::shared_ptr< CValidationInterface > callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
std::unordered_map< CValidationInterface *, std::list< ListEntry >::iterator > m_map GUARDED_BY(m_mutex)
SingleThreadedSchedulerClient m_schedulerClient
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
MainSignalsImpl(CScheduler &scheduler LIFETIMEBOUND)
Class used by CScheduler clients which may schedule multiple jobs which are required to be run serial...
std::string ToString() const
std::string ToString() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
std::shared_ptr< const CTransaction > CTransactionRef
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< BlockHash > vHave
List entries consist of a callback pointer and reference count.
std::shared_ptr< CValidationInterface > callbacks
#define WAIT_LOCK(cs, name)
#define AssertLockNotHeld(cs)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
#define LOG_EVENT(fmt,...)
static CMainSignals g_signals
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
CMainSignals & GetMainSignals()
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)
const std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.