Bitcoin Core 22.99.0
P2P Digital Currency
validationinterface.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_VALIDATIONINTERFACE_H
7#define BITCOIN_VALIDATIONINTERFACE_H
8
9#include <primitives/transaction.h> // CTransaction(Ref)
10#include <sync.h>
11
12#include <functional>
13#include <memory>
14
17class CBlock;
18class CBlockIndex;
19struct CBlockLocator;
20class CConnman;
22class uint256;
23class CScheduler;
24enum class MemPoolRemovalReason;
25
32
33// Alternate registration functions that release a shared_ptr after the last
34// notification is sent. These are useful for race-free cleanup, since
35// unregistration is nonblocking and can return before the last notification is
36// processed.
38void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
40void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
41
51void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
62
79protected:
94 virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
100 virtual void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
101
134 virtual void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
141 virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex) {}
147 virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
164 virtual void ChainStateFlushed(const CBlockLocator &locator) {}
171 virtual void BlockChecked(const CBlock&, const BlockValidationState&) {}
175 virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
176 friend class CMainSignals;
177};
178
181private:
182 std::unique_ptr<MainSignalsInstance> m_internals;
183
184 friend void ::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface>);
187 friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
188
189public:
196
197 size_t CallbacksPending();
198
199
200 void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
201 void TransactionAddedToMempool(const CTransactionRef&, uint64_t mempool_sequence);
202 void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
203 void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
204 void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
205 void ChainStateFlushed(const CBlockLocator &);
206 void BlockChecked(const CBlock&, const BlockValidationState&);
207 void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
208};
209
211
212#endif // BITCOIN_VALIDATIONINTERFACE_H
Definition: block.h:63
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:146
Definition: net.h:741
std::unique_ptr< MainSignalsInstance > m_internals
void TransactionAddedToMempool(const CTransactionRef &, 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 &)
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 ChainStateFlushed(const CBlockLocator &)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:34
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 TransactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
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 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.
~CValidationInterface()=default
Protected destructor so that instances can only be deleted by derived classes.
256-bit opaque blob.
Definition: uint256.h:124
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:115
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:341
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 SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:118
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.