Bitcoin Core 31.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-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>
11#include <sync.h>
12#include <uint256.h>
13
14#include <cstddef>
15#include <cstdint>
16#include <functional>
17#include <memory>
18#include <vector>
19
20namespace kernel {
21struct ChainstateRole;
22} // namespace kernel
23namespace util {
24class TaskRunnerInterface;
25} // namespace util
26
28class CBlock;
29class CBlockIndex;
30struct CBlockLocator;
31enum class MemPoolRemovalReason;
34
51protected:
66 virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
70 virtual void ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd) {};
76 virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence) {}
77
110 virtual void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
111 /*
112 * Notifies listeners of transactions removed from the mempool as
113 * as a result of new block being connected.
114 * MempoolTransactionsRemovedForBlock will be fired before BlockConnected.
115 *
116 * Not fired while initial block download is active.
117 *
118 * Called on a background thread.
119 */
120 virtual void MempoolTransactionsRemovedForBlock(const std::shared_ptr<const CBlock>& block, const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int block_height) {}
126 virtual void BlockConnected(const kernel::ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) {}
134 virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
151 virtual void ChainStateFlushed(const kernel::ChainstateRole& role, const CBlockLocator& locator) {}
158 virtual void BlockChecked(const std::shared_ptr<const CBlock>&, const BlockValidationState&) {}
163 virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
164 friend class ValidationSignals;
166};
167
170private:
171 std::unique_ptr<ValidationSignalsImpl> m_internals;
172
173public:
174 // The task runner will block validation if it calls its insert method's
175 // func argument synchronously. In this class func contains a loop that
176 // dispatches a single validation event to all subscribers sequentially.
177 explicit ValidationSignals(std::unique_ptr<util::TaskRunnerInterface> task_runner);
178
180
183
184 size_t CallbacksPending();
185
192
193 // Alternate registration functions that release a shared_ptr after the last
194 // notification is sent. These are useful for race-free cleanup, since
195 // unregistration is nonblocking and can return before the last notification is
196 // processed.
198 void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
200 void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
201
211 void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
212
223
224 void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
225 void ActiveTipChange(const CBlockIndex&, bool);
226 void TransactionAddedToMempool(const NewMempoolTransactionInfo&, uint64_t mempool_sequence);
227 void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
228 void MempoolTransactionsRemovedForBlock(std::shared_ptr<const CBlock>, std::vector<RemovedMempoolTransactionInfo>, unsigned int block_height);
229 void BlockConnected(const kernel::ChainstateRole&, std::shared_ptr<const CBlock>, const CBlockIndex* pindex);
230 void BlockDisconnected(std::shared_ptr<const CBlock>, const CBlockIndex* pindex);
231 void ChainStateFlushed(const kernel::ChainstateRole&, const CBlockLocator&);
232 void BlockChecked(const std::shared_ptr<const CBlock>&, const BlockValidationState&);
233 void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
234};
235
236#endif // BITCOIN_VALIDATIONINTERFACE_H
Definition: block.h:74
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:94
Implement this to subscribe to events generated in validation and mempool.
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 ChainStateFlushed(const kernel::ChainstateRole &role, const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
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 TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
virtual void BlockChecked(const std::shared_ptr< const CBlock > &, const BlockValidationState &)
Notifies listeners of a block validation result.
virtual void MempoolTransactionsRemovedForBlock(const std::shared_ptr< const CBlock > &block, const std::vector< RemovedMempoolTransactionInfo > &txs_removed_for_block, unsigned int block_height)
virtual void ActiveTipChange(const CBlockIndex &new_tip, bool is_ibd)
Notifies listeners any time the block chain tip changes, synchronously.
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.
virtual void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
~CValidationInterface()=default
Protected destructor so that instances can only be deleted by derived classes.
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
void BlockConnected(const kernel::ChainstateRole &, std::shared_ptr< const CBlock >, const CBlockIndex *pindex)
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
ValidationSignals(std::unique_ptr< util::TaskRunnerInterface > task_runner)
void BlockChecked(const std::shared_ptr< const CBlock > &, const BlockValidationState &)
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void ChainStateFlushed(const kernel::ChainstateRole &, const CBlockLocator &)
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
void TransactionAddedToMempool(const NewMempoolTransactionInfo &, uint64_t mempool_sequence)
void ActiveTipChange(const CBlockIndex &, bool)
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(std::shared_ptr< const CBlock >, std::vector< RemovedMempoolTransactionInfo >, unsigned int block_height)
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 BlockDisconnected(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.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:117
Information about chainstate that notifications are sent from.
Definition: types.h:18
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:48