Bitcoin ABC  0.24.7
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 <primitives/transaction.h> // CTransaction(Ref)
10 #include <sync.h>
11 
12 #include <functional>
13 #include <memory>
14 
15 extern RecursiveMutex cs_main;
17 class CBlock;
18 class CBlockIndex;
19 struct CBlockLocator;
20 class CConnman;
22 class uint256;
23 class CScheduler;
24 enum class MemPoolRemovalReason;
25 
35 
36 // Alternate registration functions that release a shared_ptr after the last
37 // notification is sent. These are useful for race-free cleanup, since
38 // unregistration is nonblocking and can return before the last notification is
39 // processed.
42  std::shared_ptr<CValidationInterface> callbacks);
45  std::shared_ptr<CValidationInterface> callbacks);
46 
56 void CallFunctionInValidationInterfaceQueue(std::function<void()> func);
67 
84 protected:
90  ~CValidationInterface() = default;
100  virtual void UpdatedBlockTip(const CBlockIndex *pindexNew,
101  const CBlockIndex *pindexFork,
102  bool fInitialDownload) {}
109  uint64_t mempool_sequence) {}
110 
144  MemPoolRemovalReason reason,
145  uint64_t mempool_sequence) {}
152  virtual void BlockConnected(const std::shared_ptr<const CBlock> &block,
153  const CBlockIndex *pindex) {}
159  virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block,
160  const CBlockIndex *pindex) {}
177  virtual void ChainStateFlushed(const CBlockLocator &locator) {}
184  virtual void BlockChecked(const CBlock &, const BlockValidationState &) {}
190  virtual void NewPoWValidBlock(const CBlockIndex *pindex,
191  const std::shared_ptr<const CBlock> &block){};
192  friend class CMainSignals;
193 };
194 
195 struct MainSignalsInstance;
197 private:
198  std::unique_ptr<MainSignalsInstance> m_internals;
199 
201  std::shared_ptr<CValidationInterface>);
205  std::function<void()> func);
206 
207 public:
220 
221  size_t CallbacksPending();
222 
223  void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *,
224  bool fInitialDownload);
226  uint64_t mempool_sequence);
229  uint64_t mempool_sequence);
230  void BlockConnected(const std::shared_ptr<const CBlock> &,
231  const CBlockIndex *pindex);
232  void BlockDisconnected(const std::shared_ptr<const CBlock> &,
233  const CBlockIndex *pindex);
234  void ChainStateFlushed(const CBlockLocator &);
235  void BlockChecked(const CBlock &, const BlockValidationState &);
236  void NewPoWValidBlock(const CBlockIndex *,
237  const std::shared_ptr<const CBlock> &);
238 };
239 
241 
242 #endif // BITCOIN_VALIDATIONINTERFACE_H
CValidationInterface
Implement this to subscribe to events generated in validation.
Definition: validationinterface.h:83
CValidationInterface::TransactionRemovedFromMempool
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
Definition: validationinterface.h:143
CMainSignals::BlockConnected
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: validationinterface.cpp:230
CScheduler
Simple class for background tasks that should be run periodically or once "after a while".
Definition: scheduler.h:35
CMainSignals::CallbacksPending
size_t CallbacksPending()
Definition: validationinterface.cpp:115
CValidationInterface::NewPoWValidBlock
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...
Definition: validationinterface.h:190
BlockValidationState
Definition: validation.h:138
MainSignalsInstance
The MainSignalsInstance manages a list of shared_ptr<CValidationInterface> callbacks.
Definition: validationinterface.cpp:27
sync.h
transaction.h
CValidationInterface::BlockDisconnected
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected.
Definition: validationinterface.h:159
AnnotatedMixin< std::recursive_mutex >
CMainSignals::BlockChecked
void BlockChecked(const CBlock &, const BlockValidationState &)
Definition: validationinterface.cpp:263
CMainSignals::BlockDisconnected
void BlockDisconnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex)
Definition: validationinterface.cpp:241
UnregisterSharedValidationInterface
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:140
CMainSignals::UpdatedBlockTip
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
Definition: validationinterface.cpp:186
CValidationInterface::BlockConnected
virtual void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
Definition: validationinterface.h:152
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main)
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:162
CValidationInterface::ChainStateFlushed
virtual void ChainStateFlushed(const CBlockLocator &locator)
Notifies listeners of the new active block chain on-disk.
Definition: validationinterface.h:177
CMainSignals::TransactionRemovedFromMempool
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
Definition: validationinterface.cpp:217
CMainSignals::FlushBackgroundCallbacks
void FlushBackgroundCallbacks()
Call any remaining callbacks on the calling thread.
Definition: validationinterface.cpp:109
CallFunctionInValidationInterfaceQueue
void CallFunctionInValidationInterfaceQueue(std::function< void()> func)
Pushes a function to callback onto the notification queue, guaranteeing any callbacks generated prior...
Definition: validationinterface.cpp:158
RegisterSharedValidationInterface
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
Definition: validationinterface.cpp:126
CValidationInterface::TransactionAddedToMempool
virtual void TransactionAddedToMempool(const CTransactionRef &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
Definition: validationinterface.h:108
CMainSignals::RegisterBackgroundSignalScheduler
void RegisterBackgroundSignalScheduler(CScheduler &scheduler)
Register a CScheduler to give callbacks which should run in the background (may only be called once)
Definition: validationinterface.cpp:100
uint256
256-bit opaque blob.
Definition: uint256.h:127
cs_main
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:103
GetMainSignals
CMainSignals & GetMainSignals()
Definition: validationinterface.cpp:122
CMainSignals::UnregisterBackgroundSignalScheduler
void UnregisterBackgroundSignalScheduler()
Unregister a CScheduler to give callbacks which should run in the background - these callbacks will n...
Definition: validationinterface.cpp:105
CMainSignals::TransactionAddedToMempool
void TransactionAddedToMempool(const CTransactionRef &, uint64_t mempool_sequence)
Definition: validationinterface.cpp:206
CBlock
Definition: block.h:55
CConnman
Definition: net.h:221
LOCKS_EXCLUDED
#define LOCKS_EXCLUDED(...)
Definition: threadsafety.h:55
UnregisterAllValidationInterfaces
void UnregisterAllValidationInterfaces()
Unregister all subscribers.
Definition: validationinterface.cpp:151
MemPoolRemovalReason
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:400
UnregisterValidationInterface
void UnregisterValidationInterface(CValidationInterface *callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:145
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:319
CMainSignals
Definition: validationinterface.h:196
CMainSignals::m_internals
std::unique_ptr< MainSignalsInstance > m_internals
Definition: validationinterface.h:198
CMainSignals::NewPoWValidBlock
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
Definition: validationinterface.cpp:272
CBlockLocator
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
Definition: block.h:100
CMainSignals::ChainStateFlushed
void ChainStateFlushed(const CBlockLocator &)
Definition: validationinterface.cpp:252
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:23
CValidationInterface::BlockChecked
virtual void BlockChecked(const CBlock &, const BlockValidationState &)
Notifies listeners of a block validation result.
Definition: validationinterface.h:184
CValidationInterface::UpdatedBlockTip
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
Notifies listeners when the block chain tip advances.
Definition: validationinterface.h:100
RegisterValidationInterface
void RegisterValidationInterface(CValidationInterface *callbacks)
Register subscriber.
Definition: validationinterface.cpp:133