Bitcoin ABC 0.34.5
P2P Digital Currency
Loading...
Searching...
No Matches
validationinterface.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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/chain.h>
10#include <kernel/cs_main.h>
11#include <primitives/transaction.h> // CTransaction(Ref)
12#include <sync.h>
13
14#include <cstddef>
15#include <cstdint>
16#include <functional>
17#include <memory>
18#include <vector>
19
20namespace util {
21class TaskRunnerInterface;
22} // namespace util
23
25class CBlock;
26class CBlockIndex;
27struct CBlockLocator;
28class Coin;
29enum class MemPoolRemovalReason;
30
47protected:
63 virtual void UpdatedBlockTip(const CBlockIndex *pindexNew,
64 const CBlockIndex *pindexFork,
65 bool fInitialDownload) {}
69 virtual void ActiveTipChange(const CBlockIndex &new_tip, bool is_ibd) {};
76 const CTransactionRef &tx,
77 std::shared_ptr<const std::vector<Coin>> spent_coins,
78 uint64_t mempool_sequence) {}
79
114 uint64_t mempool_sequence) {}
122 const std::shared_ptr<const CBlock> &block,
123 const CBlockIndex *pindex) {}
130 virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block,
131 const CBlockIndex *pindex) {}
149 const CBlockLocator &locator) {}
156 virtual void BlockChecked(const CBlock &, const BlockValidationState &) {}
162 virtual void NewPoWValidBlock(const CBlockIndex *pindex,
163 const std::shared_ptr<const CBlock> &block) {
164 };
165 virtual void BlockFinalized(const CBlockIndex *pindex) {};
166 virtual void BlockInvalidated(const CBlockIndex *pindex,
167 const std::shared_ptr<const CBlock> &block) {
168 };
169
170 virtual void TransactionFinalized(const CTransactionRef &tx) {};
172 const CTransactionRef &tx,
173 std::shared_ptr<const std::vector<Coin>> spent_coins) {};
174
175 friend class ValidationSignals;
177};
178
181private:
182 std::unique_ptr<ValidationSignalsImpl> m_internals;
183
184public:
185 // The task runner will block validation if it calls its insert method's
186 // func argument synchronously. In this class func contains a loop that
187 // dispatches a single validation event to all subscribers sequentially.
188 explicit ValidationSignals(
189 std::unique_ptr<util::TaskRunnerInterface> task_runner);
190
192
195
196 size_t CallbacksPending();
197
207
208 // Alternate registration functions that release a shared_ptr after the last
209 // notification is sent. These are useful for race-free cleanup, since
210 // unregistration is nonblocking and can return before the last notification
211 // is processed.
214 std::shared_ptr<CValidationInterface> callbacks);
217 std::shared_ptr<CValidationInterface> callbacks);
218
229 void CallFunctionInValidationInterfaceQueue(std::function<void()> func);
230
241
242 void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *,
243 bool fInitialDownload);
244 void ActiveTipChange(const CBlockIndex &, bool);
246 std::shared_ptr<const std::vector<Coin>>,
247 uint64_t mempool_sequence);
250 uint64_t mempool_sequence);
251 void BlockConnected(ChainstateRole, const std::shared_ptr<const CBlock> &,
252 const CBlockIndex *pindex);
253 void BlockDisconnected(const std::shared_ptr<const CBlock> &,
254 const CBlockIndex *pindex);
256 void BlockChecked(const CBlock &, const BlockValidationState &);
257 void NewPoWValidBlock(const CBlockIndex *,
258 const std::shared_ptr<const CBlock> &);
259 void BlockFinalized(const CBlockIndex *pindex);
260 void BlockInvalidated(const CBlockIndex *pindex,
261 const std::shared_ptr<const CBlock> &block);
264 const CTransactionRef &tx,
265 std::shared_ptr<const std::vector<Coin>> spent_coins);
266};
267
268#endif // BITCOIN_VALIDATIONINTERFACE_H
Definition block.h:60
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition blockindex.h:25
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)
friend class ValidationInterfaceTest
virtual void TransactionInvalidated(const CTransactionRef &tx, std::shared_ptr< const std::vector< Coin > > spent_coins)
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.
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)
~CValidationInterface()=default
Protected destructor so that instances can only be deleted by derived classes.
A UTXO entry.
Definition coins.h:31
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)
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)
void ChainStateFlushed(ChainstateRole, const CBlockLocator &)
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 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.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:7
ChainstateRole
This enum describes the various roles a specific Chainstate instance can take.
Definition chain.h:14
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition rcu.h:259
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...
Definition block.h:108
#define LOCKS_EXCLUDED(...)
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition txmempool.h:159