Bitcoin Core  31.0.0
P2P Digital Currency
validationinterface.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present 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 <primitives/transaction.h>
11 #include <sync.h>
12 
13 #include <cstddef>
14 #include <cstdint>
15 #include <functional>
16 #include <memory>
17 #include <vector>
18 
19 namespace kernel {
20 struct ChainstateRole;
21 } // namespace kernel
22 namespace util {
23 class TaskRunnerInterface;
24 } // namespace util
25 
27 class CBlock;
28 class CBlockIndex;
29 struct CBlockLocator;
30 enum class MemPoolRemovalReason;
33 
50 protected:
55  ~CValidationInterface() = default;
65  virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
69  virtual void ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd) {};
75  virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) {}
76 
109  virtual void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
110  /*
111  * Notifies listeners of transactions removed from the mempool as
112  * as a result of new block being connected.
113  * MempoolTransactionsRemovedForBlock will be fired before BlockConnected.
114  *
115  * Called on a background thread.
116  */
117  virtual void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight) {}
123  virtual void BlockConnected(const kernel::ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) {}
131  virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
148  virtual void ChainStateFlushed(const kernel::ChainstateRole& role, const CBlockLocator& locator) {}
155  virtual void BlockChecked(const std::shared_ptr<const CBlock>&, const BlockValidationState&) {}
160  virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
161  friend class ValidationSignals;
163 };
164 
167 private:
168  std::unique_ptr<ValidationSignalsImpl> m_internals;
169 
170 public:
171  // The task runner will block validation if it calls its insert method's
172  // func argument synchronously. In this class func contains a loop that
173  // dispatches a single validation event to all subscribers sequentially.
174  explicit ValidationSignals(std::unique_ptr<util::TaskRunnerInterface> task_runner);
175 
177 
180 
181  size_t CallbacksPending();
182 
189 
190  // Alternate registration functions that release a shared_ptr after the last
191  // notification is sent. These are useful for race-free cleanup, since
192  // unregistration is nonblocking and can return before the last notification is
193  // processed.
195  void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
197  void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
198 
208  void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
209 
220 
221  void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
222  void ActiveTipChange(const CBlockIndex&, bool);
223  void TransactionAddedToMempool(const NewMempoolTransactionInfo&, uint64_t mempool_sequence);
224  void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
225  void MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>&, unsigned int nBlockHeight);
226  void BlockConnected(const kernel::ChainstateRole&, const std::shared_ptr<const CBlock>&, const CBlockIndex* pindex);
227  void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
228  void ChainStateFlushed(const kernel::ChainstateRole&, const CBlockLocator&);
229  void BlockChecked(const std::shared_ptr<const CBlock>&, const BlockValidationState&);
230  void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
231 };
232 
233 #endif // BITCOIN_VALIDATIONINTERFACE_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
std::unique_ptr< ValidationSignalsImpl > m_internals
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void TransactionAddedToMempool(const NewMempoolTransactionInfo &, uint64_t mempool_sequence)
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:116
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.
~CValidationInterface()=default
Protected destructor so that instances can only be deleted by derived classes.
Definition: block.h:73
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Information about chainstate that notifications are sent from.
Definition: types.h:18
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 ActiveTipChange(const CBlockIndex &new_tip, bool is_ibd)
Notifies listeners any time the block chain tip changes, synchronously.
Definition: common.h:29
virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
Implement this to subscribe to events generated in validation and mempool.
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
void ChainStateFlushed(const kernel::ChainstateRole &, const CBlockLocator &)
void BlockChecked(const std::shared_ptr< const CBlock > &, const BlockValidationState &)
void ActiveTipChange(const CBlockIndex &, bool)
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
virtual void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int nBlockHeight)
ValidationSignals(std::unique_ptr< util::TaskRunnerInterface > task_runner)
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
virtual void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
ValidationSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
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 MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &, unsigned int nBlockHeight)
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:50
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
virtual void BlockChecked(const std::shared_ptr< const CBlock > &, const BlockValidationState &)
Notifies listeners of a block validation result.
virtual void ChainStateFlushed(const kernel::ChainstateRole &role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
void BlockConnected(const kernel::ChainstateRole &, const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)