17 #include <unordered_map> 38 std::list<ListEntry> m_list
GUARDED_BY(m_mutex);
39 std::unordered_map<CValidationInterface *, std::list<ListEntry>::iterator>
49 : m_schedulerClient(pscheduler) {}
53 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
54 if (inserted.second) {
55 inserted.first->second = m_list.emplace(m_list.end());
57 inserted.first->second->callbacks = std::move(callbacks);
62 auto it = m_map.find(callbacks);
63 if (it != m_map.end()) {
64 if (!--it->second->count) {
65 m_list.erase(it->second);
77 for (
const auto &entry : m_map) {
78 if (!--entry.second->count) {
79 m_list.erase(entry.second);
85 template <
typename F>
void Iterate(F &&f) {
87 for (
auto it = m_list.begin(); it != m_list.end();) {
93 it = --it->count ? std::next(it) : m_list.erase(it);
101 assert(!m_internals);
106 m_internals.reset(
nullptr);
111 m_internals->m_schedulerClient.EmptyQueue();
119 return m_internals->m_schedulerClient.CallbacksPending();
127 std::shared_ptr<CValidationInterface> pwalletIn) {
130 g_signals.
m_internals->Register(std::move(pwalletIn));
141 std::shared_ptr<CValidationInterface>
callbacks) {
159 g_signals.
m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
165 std::promise<void> promise;
167 promise.get_future().wait();
174 #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \ 176 auto local_name = (name); \ 177 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \ 178 m_internals->m_schedulerClient.AddToProcessQueue([=] { \ 179 LOG_EVENT(fmt, local_name, __VA_ARGS__); \ 184 #define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__) 188 bool fInitialDownload) {
194 auto event = [pindexNew, pindexFork, fInitialDownload,
this] {
200 event,
"%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
202 pindexFork ? pindexFork->GetBlockHash().ToString() :
"null",
207 auto event = [ptx,
this] {
213 ptx->GetHash().ToString());
217 auto event = [ptx,
this] {
223 ptx->GetHash().ToString());
228 auto event = [pblock, pindex,
this] {
234 pblock->GetHash().ToString(), pindex->nHeight);
238 const std::shared_ptr<const CBlock> &pblock,
const CBlockIndex *pindex) {
239 auto event = [pblock, pindex,
this] {
245 pblock->GetHash().ToString());
249 auto event = [locator,
this] {
256 : locator.
vHave.front().ToString());
261 LOG_EVENT(
"%s: block hash=%s state=%s", __func__,
269 const CBlockIndex *pindex,
const std::shared_ptr<const CBlock> &block) {
270 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...
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)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > pwalletIn)
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...
std::vector< BlockHash > vHave
void TransactionRemovedFromMempool(const CTransactionRef &)
void UnregisterAllValidationInterfaces()
Unregister all wallets from core.
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
void UnregisterValidationInterface(CValidationInterface *pwalletIn)
Unregister a wallet from core.
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Implement this to subscribe to events generated in validation.
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
BlockHash GetBlockHash() const
static CMainSignals g_signals
#define LOG_EVENT(fmt,...)
std::string ToString() const
SingleThreadedSchedulerClient m_schedulerClient
MainSignalsInstance(CScheduler *pscheduler)
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
RecursiveMutex cs_main
Global state.
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
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) ...
#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.
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx)
Notifies listeners of a transaction leaving mempool.
#define AssertLockNotHeld(cs)
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn)
Notifies listeners of a transaction having been added to mempool.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register a wallet to receive updates from core.
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
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)
List entries consist of a callback pointer and reference count.
void TransactionAddedToMempool(const CTransactionRef &)