Bitcoin ABC 0.30.5
P2P Digital Currency
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/cs_main.h>
10#include <primitives/transaction.h> // CTransaction(Ref)
11#include <sync.h>
12
13#include <functional>
14#include <memory>
15
17class CBlock;
18class CBlockIndex;
19struct CBlockLocator;
20class Coin;
22class CScheduler;
23enum class MemPoolRemovalReason;
24
34
35// Alternate registration functions that release a shared_ptr after the last
36// notification is sent. These are useful for race-free cleanup, since
37// unregistration is nonblocking and can return before the last notification is
38// processed.
41 std::shared_ptr<CValidationInterface> callbacks);
44 std::shared_ptr<CValidationInterface> callbacks);
45
55void CallFunctionInValidationInterfaceQueue(std::function<void()> func);
66
83protected:
99 virtual void UpdatedBlockTip(const CBlockIndex *pindexNew,
100 const CBlockIndex *pindexFork,
101 bool fInitialDownload) {}
108 const CTransactionRef &tx,
109 std::shared_ptr<const std::vector<Coin>> spent_coins,
110 uint64_t mempool_sequence) {}
111
146 uint64_t mempool_sequence) {}
153 virtual void BlockConnected(const std::shared_ptr<const CBlock> &block,
154 const CBlockIndex *pindex) {}
160 virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block,
161 const CBlockIndex *pindex) {}
178 virtual void ChainStateFlushed(const CBlockLocator &locator) {}
185 virtual void BlockChecked(const CBlock &, const BlockValidationState &) {}
191 virtual void NewPoWValidBlock(const CBlockIndex *pindex,
192 const std::shared_ptr<const CBlock> &block){};
193
194 virtual void BlockFinalized(const CBlockIndex *pindex){};
195 virtual void BlockInvalidated(const CBlockIndex *pindex,
196 const std::shared_ptr<const CBlock> &block){};
197
198 friend class CMainSignals;
199 friend class ValidationInterfaceTest;
200};
201
202class MainSignalsImpl;
204private:
205 std::unique_ptr<MainSignalsImpl> m_internals;
206
208 std::shared_ptr<CValidationInterface>);
212 std::function<void()> func);
213
214public:
227
228 size_t CallbacksPending();
229
230 void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *,
231 bool fInitialDownload);
233 std::shared_ptr<const std::vector<Coin>>,
234 uint64_t mempool_sequence);
237 uint64_t mempool_sequence);
238 void BlockConnected(const std::shared_ptr<const CBlock> &,
239 const CBlockIndex *pindex);
240 void BlockDisconnected(const std::shared_ptr<const CBlock> &,
241 const CBlockIndex *pindex);
242 void ChainStateFlushed(const CBlockLocator &);
243 void BlockChecked(const CBlock &, const BlockValidationState &);
244 void NewPoWValidBlock(const CBlockIndex *,
245 const std::shared_ptr<const CBlock> &);
246 void BlockFinalized(const CBlockIndex *pindex);
247 void BlockInvalidated(const CBlockIndex *pindex,
248 const std::shared_ptr<const CBlock> &block);
249};
250
252
253#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
void TransactionAddedToMempool(const CTransactionRef &, std::shared_ptr< const std::vector< Coin > >, 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 &)
std::unique_ptr< MainSignalsImpl > m_internals
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 BlockInvalidated(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
void ChainStateFlushed(const CBlockLocator &)
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
void BlockFinalized(const CBlockIndex *pindex)
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:41
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(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 BlockFinalized(const CBlockIndex *pindex)
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.
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.
~CValidationInterface()=default
Protected destructor so that instances can only be deleted by derived classes.
A UTXO entry.
Definition: coins.h:28
MainSignalsImpl 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
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:105
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:55
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:151
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.
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.