Bitcoin Core  27.1.0
P2P Digital Currency
validationinterface.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 <kernel/cs_main.h>
10 #include <kernel/chain.h>
11 #include <primitives/transaction.h> // CTransaction(Ref)
12 #include <sync.h>
13 
14 #include <functional>
15 #include <memory>
16 
18 class CBlock;
19 class CBlockIndex;
20 struct CBlockLocator;
22 class CScheduler;
23 enum class MemPoolRemovalReason;
26 
33 
34 // Alternate registration functions that release a shared_ptr after the last
35 // notification is sent. These are useful for race-free cleanup, since
36 // unregistration is nonblocking and can return before the last notification is
37 // processed.
39 void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
41 void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
42 
52 void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
63 
80 protected:
85  ~CValidationInterface() = default;
95  virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
101  virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) {}
102 
135  virtual void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
136  /*
137  * Notifies listeners of transactions removed from the mempool as
138  * as a result of new block being connected.
139  * MempoolTransactionsRemovedForBlock will be fired before BlockConnected.
140  *
141  * Called on a background thread.
142  */
143  virtual void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight) {}
150  virtual void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex) {}
158  virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
175  virtual void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator) {}
182  virtual void BlockChecked(const CBlock&, const BlockValidationState&) {}
187  virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
188  friend class CMainSignals;
190 };
191 
192 class MainSignalsImpl;
194 private:
195  std::unique_ptr<MainSignalsImpl> m_internals;
196 
197  friend void ::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface>);
200  friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
201 
202 public:
209 
210  size_t CallbacksPending();
211 
212 
213  void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
214  void TransactionAddedToMempool(const NewMempoolTransactionInfo&, uint64_t mempool_sequence);
215  void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
216  void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>&, unsigned int nBlockHeight);
217  void BlockConnected(ChainstateRole, const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
218  void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
220  void BlockChecked(const CBlock&, const BlockValidationState&);
221  void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
222 };
223 
225 
226 #endif // BITCOIN_VALIDATIONINTERFACE_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
std::unique_ptr< MainSignalsImpl > m_internals
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:123
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 BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: block.h:68
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...
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
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.
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition: chain.h:25
void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &, unsigned int nBlockHeight)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
void TransactionAddedToMempool(const NewMempoolTransactionInfo &, uint64_t mempool_sequence)
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
virtual void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int nBlockHeight)
void BlockConnected(ChainstateRole, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
CMainSignals & GetMainSignals()
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once) ...
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:149
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
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.
MainSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
Simple class for background tasks that should be run periodically or once "after a while"...
Definition: scheduler.h:38
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void BlockChecked(const CBlock &, const BlockValidationState &)
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)