19#include <unordered_map>
45 std::unordered_map<CValidationInterface *, std::list<ListEntry>::iterator>
52 std::unique_ptr<util::TaskRunnerInterface> task_runner)
55 void Register(std::shared_ptr<CValidationInterface> callbacks)
58 auto inserted = m_map.emplace(callbacks.get(), m_list.end());
59 if (inserted.second) {
60 inserted.first->second = m_list.emplace(m_list.end());
62 inserted.first->second->callbacks = std::move(callbacks);
68 auto it = m_map.find(callbacks);
69 if (it != m_map.end()) {
70 if (!--it->second->count) {
71 m_list.erase(it->second);
83 for (
const auto &entry : m_map) {
84 if (!--entry.second->count) {
85 m_list.erase(entry.second);
94 for (
auto it = m_list.begin(); it != m_list.end();) {
100 it = --it->count ? std::next(it) : m_list.erase(it);
106 std::unique_ptr<util::TaskRunnerInterface> task_runner)
121 std::shared_ptr<CValidationInterface> callbacks) {
136 std::shared_ptr<CValidationInterface> callbacks) {
150 std::function<
void()> func) {
151 m_internals->m_task_runner->insert(std::move(func));
157 std::promise<void> promise;
159 promise.get_future().wait();
166#define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...) \
168 auto local_name = (name); \
169 LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__); \
170 m_internals->m_task_runner->insert([=] { \
171 LOG_EVENT(fmt, local_name, __VA_ARGS__); \
176#define LOG_EVENT(fmt, ...) LogPrint(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
180 bool fInitialDownload) {
186 auto event = [pindexNew, pindexFork, fInitialDownload,
this] {
192 event,
"%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
200 std::shared_ptr<
const std::vector<Coin>> spent_coins,
201 uint64_t mempool_sequence) {
202 auto event = [tx, spent_coins, mempool_sequence,
this] {
209 tx->GetHash().ToString());
214 uint64_t mempool_sequence) {
215 auto event = [tx, reason, mempool_sequence,
this] {
222 tx->GetHash().ToString(),
229 auto event = [role, pblock, pindex,
this] {
235 pblock->GetHash().ToString(), pindex->
nHeight);
239 const std::shared_ptr<const CBlock> &pblock,
const CBlockIndex *pindex) {
240 auto event = [pblock, pindex,
this] {
246 pblock->GetHash().ToString());
251 auto event = [role, locator,
this] {
258 : locator.
vHave.front().ToString());
263 LOG_EVENT(
"%s: block hash=%s state=%s", __func__,
271 const CBlockIndex *pindex,
const std::shared_ptr<const CBlock> &block) {
272 LOG_EVENT(
"%s: block hash=%s", __func__, block->GetHash().ToString());
279 auto event = [pindex,
this] {
289 const CBlockIndex *pindex,
const std::shared_ptr<const CBlock> &block) {
290 auto event = [pindex, block,
this] {
296 block ? block->GetHash().ToString() :
"null");
300 auto event = [tx,
this] {
306 tx->GetId().ToString());
311 std::shared_ptr<
const std::vector<Coin>> spent_coins) {
312 auto event = [tx, spent_coins,
this] {
318 tx->GetId().ToString());
#define Assert(val)
Identity function.
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
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(ChainstateRole role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
virtual void BlockConnected(ChainstateRole role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
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 TransactionInvalidated(const CTransactionRef &tx, std::shared_ptr< const std::vector< Coin > > spent_coins)
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.
virtual void TransactionFinalized(const CTransactionRef &tx)
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
void BlockInvalidated(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
void TransactionInvalidated(const CTransactionRef &tx, std::shared_ptr< const std::vector< Coin > > spent_coins)
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, uint64_t mempool_sequence)
void BlockFinalized(const CBlockIndex *pindex)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
void TransactionFinalized(const CTransactionRef &tx)
ValidationSignals(std::unique_ptr< util::TaskRunnerInterface > task_runner)
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
size_t CallbacksPending()
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void BlockChecked(const CBlock &, const BlockValidationState &)
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
std::unique_ptr< ValidationSignalsImpl > m_internals
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
ValidationSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
void Register(std::shared_ptr< CValidationInterface > callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
std::unique_ptr< util::TaskRunnerInterface > m_task_runner
void Unregister(CValidationInterface *callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
std::unordered_map< CValidationInterface *, std::list< ListEntry >::iterator > m_map GUARDED_BY(m_mutex)
std::list< ListEntry > m_list GUARDED_BY(m_mutex)
void Iterate(F &&f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
ValidationSignalsImpl(std::unique_ptr< util::TaskRunnerInterface > task_runner)
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Clear unregisters every previously registered callback, erasing every map entry.
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.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
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)
This header provides an interface and simple implementation for a task runner.
#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,...)
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
#define ENQUEUE_AND_LOG_EVENT(event, fmt, name,...)