2015-02-04 21:11:44 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-01-14 16:17:38 -03:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2015-02-04 21:11:44 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_VALIDATIONINTERFACE_H
|
|
|
|
#define BITCOIN_VALIDATIONINTERFACE_H
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <primitives/transaction.h> // CTransaction(Ref)
|
2018-08-13 08:24:52 -03:00
|
|
|
#include <sync.h>
|
2017-03-29 22:12:42 -03:00
|
|
|
|
2017-06-08 11:13:46 -04:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
2020-01-07 13:14:15 -03:00
|
|
|
extern RecursiveMutex cs_main;
|
2019-10-24 12:35:42 -03:00
|
|
|
class BlockValidationState;
|
2015-02-04 21:11:44 -03:00
|
|
|
class CBlock;
|
2015-07-27 10:33:03 -03:00
|
|
|
class CBlockIndex;
|
2015-03-29 08:45:05 -03:00
|
|
|
struct CBlockLocator;
|
2016-05-24 20:56:17 -04:00
|
|
|
class CConnman;
|
2015-02-04 21:11:44 -03:00
|
|
|
class CValidationInterface;
|
|
|
|
class uint256;
|
2017-01-19 18:49:22 -03:00
|
|
|
class CScheduler;
|
2015-02-04 21:11:44 -03:00
|
|
|
|
|
|
|
// These functions dispatch to one or all registered wallets
|
|
|
|
|
|
|
|
/** Register a wallet to receive updates from core */
|
|
|
|
void RegisterValidationInterface(CValidationInterface* pwalletIn);
|
|
|
|
/** Unregister a wallet from core */
|
|
|
|
void UnregisterValidationInterface(CValidationInterface* pwalletIn);
|
|
|
|
/** Unregister all wallets from core */
|
|
|
|
void UnregisterAllValidationInterfaces();
|
2020-03-10 16:46:20 -03:00
|
|
|
|
|
|
|
// Alternate registration functions that release a shared_ptr after the last
|
|
|
|
// notification is sent. These are useful for race-free cleanup, since
|
|
|
|
// unregistration is nonblocking and can return before the last notification is
|
|
|
|
// processed.
|
|
|
|
void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
|
|
|
|
void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
|
|
|
|
|
2017-06-08 11:13:46 -04:00
|
|
|
/**
|
|
|
|
* Pushes a function to callback onto the notification queue, guaranteeing any
|
|
|
|
* callbacks generated prior to now are finished when the function is called.
|
|
|
|
*
|
|
|
|
* Be very careful blocking on func to be called if any locks are held -
|
|
|
|
* validation interface clients may not be able to make progress as they often
|
|
|
|
* wait for things like cs_main, so blocking until func is called with cs_main
|
|
|
|
* will result in a deadlock (that DEBUG_LOCKORDER will miss).
|
|
|
|
*/
|
|
|
|
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
|
2017-12-24 14:13:13 -03:00
|
|
|
/**
|
|
|
|
* This is a synonym for the following, which asserts certain locks are not
|
|
|
|
* held:
|
|
|
|
* std::promise<void> promise;
|
|
|
|
* CallFunctionInValidationInterfaceQueue([&promise] {
|
|
|
|
* promise.set_value();
|
|
|
|
* });
|
|
|
|
* promise.get_future().wait();
|
|
|
|
*/
|
2018-08-13 08:24:52 -03:00
|
|
|
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main);
|
2015-02-04 21:11:44 -03:00
|
|
|
|
2018-05-16 16:20:37 -04:00
|
|
|
/**
|
|
|
|
* Implement this to subscribe to events generated in validation
|
|
|
|
*
|
|
|
|
* Each CValidationInterface() subscriber will receive event callbacks
|
|
|
|
* in the order in which the events were generated by validation.
|
|
|
|
* Furthermore, each ValidationInterface() subscriber may assume that
|
|
|
|
* callbacks effectively run in a single thread with single-threaded
|
|
|
|
* memory consistency. That is, for a given ValidationInterface()
|
|
|
|
* instantiation, each callback will complete before the next one is
|
|
|
|
* invoked. This means, for example when a block is connected that the
|
|
|
|
* UpdatedBlockTip() callback may depend on an operation performed in
|
|
|
|
* the BlockConnected() callback without worrying about explicit
|
|
|
|
* synchronization. No ordering should be assumed across
|
|
|
|
* ValidationInterface() subscribers.
|
|
|
|
*/
|
2015-02-04 21:11:44 -03:00
|
|
|
class CValidationInterface {
|
|
|
|
protected:
|
2018-03-14 02:04:42 -03:00
|
|
|
/**
|
|
|
|
* Protected destructor so that instances can only be deleted by derived classes.
|
|
|
|
* If that restriction is no longer desired, this should be made public and virtual.
|
|
|
|
*/
|
|
|
|
~CValidationInterface() = default;
|
2017-10-01 00:49:59 -03:00
|
|
|
/**
|
2017-10-09 12:19:10 -03:00
|
|
|
* Notifies listeners when the block chain tip advances.
|
|
|
|
*
|
|
|
|
* When multiple blocks are connected at once, UpdatedBlockTip will be called on the final tip
|
|
|
|
* but may not be called on every intermediate tip. If the latter behavior is desired,
|
|
|
|
* subscribe to BlockConnected() instead.
|
2017-10-01 00:49:59 -03:00
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2017-01-19 18:17:14 -03:00
|
|
|
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
|
Also call other wallet notify callbacks in scheduler thread
This runs Block{Connected,Disconnected}, SetBestChain, Inventory,
and TransactionAddedToMempool on the background scheduler thread.
Of those, only BlockConnected is used outside of Wallet/ZMQ, and
is used only for orphan transaction removal in net_processing,
something which does not need to be synchronous with anything
else.
This partially reverts #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 11:08:53 -04:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a transaction having been added to mempool.
|
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2017-01-19 18:17:14 -03:00
|
|
|
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
|
2017-01-20 17:08:14 -03:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a transaction leaving mempool.
|
|
|
|
*
|
2019-11-11 12:34:31 -03:00
|
|
|
* This notification fires for transactions that are removed from the
|
|
|
|
* mempool for the following reasons:
|
|
|
|
*
|
|
|
|
* - EXPIRY (expired from mempool after -mempoolexpiry hours)
|
|
|
|
* - SIZELIMIT (removed in size limiting if the mempool exceeds -maxmempool megabytes)
|
|
|
|
* - REORG (removed during a reorg)
|
|
|
|
* - CONFLICT (removed because it conflicts with in-block transaction)
|
|
|
|
* - REPLACED (removed due to RBF replacement)
|
|
|
|
*
|
|
|
|
* This does not fire for transactions that are removed from the mempool
|
|
|
|
* because they have been included in a block. Any client that is interested
|
|
|
|
* in transactions removed from the mempool for inclusion in a block can learn
|
|
|
|
* about those transactions from the BlockConnected notification.
|
|
|
|
*
|
|
|
|
* Transactions that are removed from the mempool because they conflict
|
|
|
|
* with a transaction in the new block will have
|
|
|
|
* TransactionRemovedFromMempool events fired *before* the BlockConnected
|
|
|
|
* event is fired. If multiple blocks are connected in one step, then the
|
|
|
|
* ordering could be:
|
|
|
|
*
|
|
|
|
* - TransactionRemovedFromMempool(tx1 from block A)
|
|
|
|
* - TransactionRemovedFromMempool(tx2 from block A)
|
|
|
|
* - TransactionRemovedFromMempool(tx1 from block B)
|
|
|
|
* - TransactionRemovedFromMempool(tx2 from block B)
|
|
|
|
* - BlockConnected(A)
|
|
|
|
* - BlockConnected(B)
|
2017-06-08 11:05:18 -04:00
|
|
|
*
|
|
|
|
* Called on a background thread.
|
2017-01-20 17:08:14 -03:00
|
|
|
*/
|
|
|
|
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx) {}
|
2017-03-29 22:12:42 -03:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a block being connected.
|
|
|
|
* Provides a vector of transactions evicted from the mempool as a result.
|
Also call other wallet notify callbacks in scheduler thread
This runs Block{Connected,Disconnected}, SetBestChain, Inventory,
and TransactionAddedToMempool on the background scheduler thread.
Of those, only BlockConnected is used outside of Wallet/ZMQ, and
is used only for orphan transaction removal in net_processing,
something which does not need to be synchronous with anything
else.
This partially reverts #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 11:08:53 -04:00
|
|
|
*
|
|
|
|
* Called on a background thread.
|
2017-03-29 22:12:42 -03:00
|
|
|
*/
|
2019-11-11 12:43:34 -03:00
|
|
|
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex) {}
|
Also call other wallet notify callbacks in scheduler thread
This runs Block{Connected,Disconnected}, SetBestChain, Inventory,
and TransactionAddedToMempool on the background scheduler thread.
Of those, only BlockConnected is used outside of Wallet/ZMQ, and
is used only for orphan transaction removal in net_processing,
something which does not need to be synchronous with anything
else.
This partially reverts #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 11:08:53 -04:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a block being disconnected
|
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2019-07-24 15:41:41 -04:00
|
|
|
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
|
Also call other wallet notify callbacks in scheduler thread
This runs Block{Connected,Disconnected}, SetBestChain, Inventory,
and TransactionAddedToMempool on the background scheduler thread.
Of those, only BlockConnected is used outside of Wallet/ZMQ, and
is used only for orphan transaction removal in net_processing,
something which does not need to be synchronous with anything
else.
This partially reverts #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 11:08:53 -04:00
|
|
|
/**
|
|
|
|
* Notifies listeners of the new active block chain on-disk.
|
|
|
|
*
|
2018-04-27 15:08:39 -03:00
|
|
|
* Prior to this callback, any updates are not guaranteed to persist on disk
|
|
|
|
* (ie clients need to handle shutdown/restart safety by being able to
|
|
|
|
* understand when some updates were lost due to unclean shutdown).
|
|
|
|
*
|
|
|
|
* When this callback is invoked, the validation changes done by any prior
|
|
|
|
* callback are guaranteed to exist on disk and survive a restart, including
|
|
|
|
* an unclean shutdown.
|
|
|
|
*
|
|
|
|
* Provides a locator describing the best chain, which is likely useful for
|
|
|
|
* storing current state on disk in client DBs.
|
|
|
|
*
|
Also call other wallet notify callbacks in scheduler thread
This runs Block{Connected,Disconnected}, SetBestChain, Inventory,
and TransactionAddedToMempool on the background scheduler thread.
Of those, only BlockConnected is used outside of Wallet/ZMQ, and
is used only for orphan transaction removal in net_processing,
something which does not need to be synchronous with anything
else.
This partially reverts #9583, re-enabling some of the gains from
#7946. This does not, however, re-enable the gains achieved by
repeatedly releasing cs_main between each transaction processed.
2017-06-08 11:08:53 -04:00
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2018-04-27 15:01:02 -03:00
|
|
|
virtual void ChainStateFlushed(const CBlockLocator &locator) {}
|
2017-01-14 18:40:46 -03:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a block validation result.
|
2019-10-24 12:35:42 -03:00
|
|
|
* If the provided BlockValidationState IsValid, the provided block
|
2017-01-14 18:40:46 -03:00
|
|
|
* is guaranteed to be the current best block at the time the
|
|
|
|
* callback was generated (not necessarily now)
|
|
|
|
*/
|
2019-10-24 12:35:42 -03:00
|
|
|
virtual void BlockChecked(const CBlock&, const BlockValidationState&) {}
|
2016-12-19 04:26:20 -03:00
|
|
|
/**
|
|
|
|
* Notifies listeners that a block which builds directly on our current tip
|
|
|
|
* has been received and connected to the headers tree, though not validated yet */
|
2017-01-19 18:17:14 -03:00
|
|
|
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
|
2020-03-10 16:46:20 -03:00
|
|
|
friend void ::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface>);
|
2017-01-19 18:17:14 -03:00
|
|
|
friend void ::UnregisterValidationInterface(CValidationInterface*);
|
|
|
|
friend void ::UnregisterAllValidationInterfaces();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MainSignalsInstance;
|
|
|
|
class CMainSignals {
|
|
|
|
private:
|
|
|
|
std::unique_ptr<MainSignalsInstance> m_internals;
|
|
|
|
|
2020-03-10 16:46:20 -03:00
|
|
|
friend void ::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface>);
|
2017-01-19 18:17:14 -03:00
|
|
|
friend void ::UnregisterValidationInterface(CValidationInterface*);
|
|
|
|
friend void ::UnregisterAllValidationInterfaces();
|
2017-06-08 11:13:46 -04:00
|
|
|
friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
|
2017-01-19 18:49:22 -03:00
|
|
|
|
2017-01-19 18:17:14 -03:00
|
|
|
public:
|
2017-01-19 18:49:22 -03:00
|
|
|
/** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
|
|
|
|
void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
|
|
|
|
/** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
|
|
|
|
void UnregisterBackgroundSignalScheduler();
|
2017-06-27 19:07:52 -04:00
|
|
|
/** Call any remaining callbacks on the calling thread */
|
|
|
|
void FlushBackgroundCallbacks();
|
2017-01-19 18:49:22 -03:00
|
|
|
|
2017-12-04 20:31:36 -03:00
|
|
|
size_t CallbacksPending();
|
|
|
|
|
2017-01-20 17:08:14 -03:00
|
|
|
|
2017-01-19 18:17:14 -03:00
|
|
|
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
|
|
|
|
void TransactionAddedToMempool(const CTransactionRef &);
|
2019-07-23 17:47:17 -04:00
|
|
|
void TransactionRemovedFromMempool(const CTransactionRef &);
|
2019-11-11 12:43:34 -03:00
|
|
|
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
|
2019-07-24 15:41:41 -04:00
|
|
|
void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
|
2018-04-27 15:01:02 -03:00
|
|
|
void ChainStateFlushed(const CBlockLocator &);
|
2019-10-24 12:35:42 -03:00
|
|
|
void BlockChecked(const CBlock&, const BlockValidationState&);
|
2017-01-19 18:17:14 -03:00
|
|
|
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
|
2015-02-04 21:11:44 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
CMainSignals& GetMainSignals();
|
|
|
|
|
|
|
|
#endif // BITCOIN_VALIDATIONINTERFACE_H
|