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.
6 #ifndef BITCOIN_VALIDATIONINTERFACE_H
7 #define BITCOIN_VALIDATIONINTERFACE_H
9 #include <primitives/transaction.h> // CTransaction(Ref)
20 class CValidationInterface
;
21 class CValidationState
;
25 enum class MemPoolRemovalReason
;
27 // These functions dispatch to one or all registered wallets
29 /** Register a wallet to receive updates from core */
30 void RegisterValidationInterface(CValidationInterface
* pwalletIn
);
31 /** Unregister a wallet from core */
32 void UnregisterValidationInterface(CValidationInterface
* pwalletIn
);
33 /** Unregister all wallets from core */
34 void UnregisterAllValidationInterfaces();
36 * Pushes a function to callback onto the notification queue, guaranteeing any
37 * callbacks generated prior to now are finished when the function is called.
39 * Be very careful blocking on func to be called if any locks are held -
40 * validation interface clients may not be able to make progress as they often
41 * wait for things like cs_main, so blocking until func is called with cs_main
42 * will result in a deadlock (that DEBUG_LOCKORDER will miss).
44 void CallFunctionInValidationInterfaceQueue(std::function
<void ()> func
);
46 class CValidationInterface
{
49 * Notifies listeners of updated block chain tip
51 * Called on a background thread.
53 virtual void UpdatedBlockTip(const CBlockIndex
*pindexNew
, const CBlockIndex
*pindexFork
, bool fInitialDownload
) {}
55 * Notifies listeners of a transaction having been added to mempool.
57 * Called on a background thread.
59 virtual void TransactionAddedToMempool(const CTransactionRef
&ptxn
) {}
61 * Notifies listeners of a transaction leaving mempool.
63 * This only fires for transactions which leave mempool because of expiry,
64 * size limiting, reorg (changes in lock times/coinbase maturity), or
65 * replacement. This does not include any transactions which are included
66 * in BlockConnectedDisconnected either in block->vtx or in txnConflicted.
68 * Called on a background thread.
70 virtual void TransactionRemovedFromMempool(const CTransactionRef
&ptx
) {}
72 * Notifies listeners of a block being connected.
73 * Provides a vector of transactions evicted from the mempool as a result.
75 * Called on a background thread.
77 virtual void BlockConnected(const std::shared_ptr
<const CBlock
> &block
, const CBlockIndex
*pindex
, const std::vector
<CTransactionRef
> &txnConflicted
) {}
79 * Notifies listeners of a block being disconnected
81 * Called on a background thread.
83 virtual void BlockDisconnected(const std::shared_ptr
<const CBlock
> &block
) {}
85 * Notifies listeners of the new active block chain on-disk.
87 * Called on a background thread.
89 virtual void SetBestChain(const CBlockLocator
&locator
) {}
91 * Notifies listeners about an inventory item being seen on the network.
93 * Called on a background thread.
95 virtual void Inventory(const uint256
&hash
) {}
96 /** Tells listeners to broadcast their data. */
97 virtual void ResendWalletTransactions(int64_t nBestBlockTime
, CConnman
* connman
) {}
99 * Notifies listeners of a block validation result.
100 * If the provided CValidationState IsValid, the provided block
101 * is guaranteed to be the current best block at the time the
102 * callback was generated (not necessarily now)
104 virtual void BlockChecked(const CBlock
&, const CValidationState
&) {}
106 * Notifies listeners that a block which builds directly on our current tip
107 * has been received and connected to the headers tree, though not validated yet */
108 virtual void NewPoWValidBlock(const CBlockIndex
*pindex
, const std::shared_ptr
<const CBlock
>& block
) {};
109 friend void ::RegisterValidationInterface(CValidationInterface
*);
110 friend void ::UnregisterValidationInterface(CValidationInterface
*);
111 friend void ::UnregisterAllValidationInterfaces();
114 struct MainSignalsInstance
;
117 std::unique_ptr
<MainSignalsInstance
> m_internals
;
119 friend void ::RegisterValidationInterface(CValidationInterface
*);
120 friend void ::UnregisterValidationInterface(CValidationInterface
*);
121 friend void ::UnregisterAllValidationInterfaces();
122 friend void ::CallFunctionInValidationInterfaceQueue(std::function
<void ()> func
);
124 void MempoolEntryRemoved(CTransactionRef tx
, MemPoolRemovalReason reason
);
127 /** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
128 void RegisterBackgroundSignalScheduler(CScheduler
& scheduler
);
129 /** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
130 void UnregisterBackgroundSignalScheduler();
131 /** Call any remaining callbacks on the calling thread */
132 void FlushBackgroundCallbacks();
134 /** Register with mempool to call TransactionRemovedFromMempool callbacks */
135 void RegisterWithMempoolSignals(CTxMemPool
& pool
);
136 /** Unregister with mempool */
137 void UnregisterWithMempoolSignals(CTxMemPool
& pool
);
139 void UpdatedBlockTip(const CBlockIndex
*, const CBlockIndex
*, bool fInitialDownload
);
140 void TransactionAddedToMempool(const CTransactionRef
&);
141 void BlockConnected(const std::shared_ptr
<const CBlock
> &, const CBlockIndex
*pindex
, const std::shared_ptr
<const std::vector
<CTransactionRef
>> &);
142 void BlockDisconnected(const std::shared_ptr
<const CBlock
> &);
143 void SetBestChain(const CBlockLocator
&);
144 void Inventory(const uint256
&);
145 void Broadcast(int64_t nBestBlockTime
, CConnman
* connman
);
146 void BlockChecked(const CBlock
&, const CValidationState
&);
147 void NewPoWValidBlock(const CBlockIndex
*, const std::shared_ptr
<const CBlock
>&);
150 CMainSignals
& GetMainSignals();
152 #endif // BITCOIN_VALIDATIONINTERFACE_H