2015-02-05 01:11:44 +01:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2015-02-05 01:11:44 +01: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
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <primitives/transaction.h> // CTransaction(Ref)
|
2021-07-07 18:02:11 +02:00
|
|
|
#include <sync.h>
|
2017-04-10 21:06:42 +02:00
|
|
|
|
2017-11-15 14:14:20 +01:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
|
2021-07-07 18:02:11 +02:00
|
|
|
extern CCriticalSection cs_main;
|
2015-02-05 01:11:44 +01:00
|
|
|
class CBlock;
|
2016-02-04 17:02:57 +01:00
|
|
|
class CBlockIndex;
|
2015-03-29 13:45:05 +02:00
|
|
|
struct CBlockLocator;
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
class CConnman;
|
2015-07-01 08:32:30 +02:00
|
|
|
class CReserveScript;
|
2015-02-05 01:11:44 +01:00
|
|
|
class CValidationInterface;
|
|
|
|
class CValidationState;
|
2018-07-12 11:06:30 +02:00
|
|
|
class CGovernanceVote;
|
|
|
|
class CGovernanceObject;
|
2019-01-03 10:17:43 +01:00
|
|
|
class CDeterministicMNList;
|
2019-04-09 13:26:33 +02:00
|
|
|
class CDeterministicMNListDiff;
|
2015-02-05 01:11:44 +01:00
|
|
|
class uint256;
|
2017-07-11 09:30:36 +02:00
|
|
|
class CScheduler;
|
2017-11-15 14:14:20 +01:00
|
|
|
class CTxMemPool;
|
|
|
|
enum class MemPoolRemovalReason;
|
2015-02-05 01:11:44 +01:00
|
|
|
|
2019-05-23 11:13:58 +02:00
|
|
|
namespace llmq {
|
|
|
|
class CChainLockSig;
|
|
|
|
class CInstantSendLock;
|
2020-11-17 20:41:30 +01:00
|
|
|
class CRecoveredSig;
|
2019-07-15 20:55:01 +02:00
|
|
|
} // namespace llmq
|
2019-05-23 11:13:58 +02:00
|
|
|
|
2015-02-05 01:11:44 +01: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();
|
2017-11-15 14:14:20 +01: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);
|
2020-03-24 15:21:59 +01: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();
|
|
|
|
*/
|
2021-07-07 18:02:11 +02:00
|
|
|
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main);
|
2015-02-05 01:11:44 +01:00
|
|
|
|
2018-08-01 02:50:18 +02: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-05 01:11:44 +01:00
|
|
|
class CValidationInterface {
|
|
|
|
protected:
|
2017-09-03 15:30:08 +02:00
|
|
|
virtual void AcceptedBlockHeader(const CBlockIndex *pindexNew) {}
|
|
|
|
virtual void NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload) {}
|
2018-03-14 10:16:41 +01: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-11-15 14:14:20 +01:00
|
|
|
/**
|
2021-04-14 02:15:23 +02: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-11-15 14:14:20 +01:00
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2017-07-28 16:10:10 +02:00
|
|
|
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
|
2020-03-20 17:11:54 +01:00
|
|
|
/**
|
|
|
|
* Same as UpdatedBlockTip, but called from the caller's thread
|
|
|
|
*/
|
|
|
|
virtual void SynchronousUpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
|
2017-11-15 14:14:20 +01:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a transaction having been added to mempool.
|
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2019-12-07 11:56:17 +01:00
|
|
|
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn, int64_t nAcceptTime) {}
|
2017-11-15 14:14:20 +01:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a transaction leaving mempool.
|
|
|
|
*
|
|
|
|
* This only fires for transactions which leave mempool because of expiry,
|
|
|
|
* size limiting, reorg (changes in lock times/coinbase maturity), or
|
|
|
|
* replacement. This does not include any transactions which are included
|
|
|
|
* in BlockConnectedDisconnected either in block->vtx or in txnConflicted.
|
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2021-05-19 01:02:31 +02:00
|
|
|
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx, MemPoolRemovalReason reason) {}
|
2017-07-11 09:30:36 +02:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a block being connected.
|
|
|
|
* Provides a vector of transactions evicted from the mempool as a result.
|
2017-11-15 14:14:20 +01:00
|
|
|
*
|
|
|
|
* Called on a background thread.
|
2017-07-11 09:30:36 +02:00
|
|
|
*/
|
2017-04-10 21:06:42 +02:00
|
|
|
virtual void BlockConnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex, const std::vector<CTransactionRef> &txnConflicted) {}
|
2017-11-15 14:14:20 +01:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a block being disconnected
|
|
|
|
*
|
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2019-05-27 14:55:29 +02:00
|
|
|
virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindexDisconnected) {}
|
2021-01-22 05:32:15 +01:00
|
|
|
virtual void NotifyTransactionLock(const CTransactionRef &tx, const std::shared_ptr<const llmq::CInstantSendLock>& islock) {}
|
|
|
|
virtual void NotifyChainLock(const CBlockIndex* pindex, const std::shared_ptr<const llmq::CChainLockSig>& clsig) {}
|
|
|
|
virtual void NotifyGovernanceVote(const std::shared_ptr<const CGovernanceVote>& vote) {}
|
|
|
|
virtual void NotifyGovernanceObject(const std::shared_ptr<const CGovernanceObject>& object) {}
|
|
|
|
virtual void NotifyInstantSendDoubleSpendAttempt(const CTransactionRef& currentTx, const CTransactionRef& previousTx) {}
|
|
|
|
virtual void NotifyRecoveredSig(const std::shared_ptr<const llmq::CRecoveredSig>& sig) {}
|
2019-04-09 13:26:33 +02:00
|
|
|
virtual void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff) {}
|
2017-11-15 14:14:20 +01:00
|
|
|
/**
|
|
|
|
* Notifies listeners of the new active block chain on-disk.
|
|
|
|
*
|
2018-05-02 13:04:02 +02: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.
|
|
|
|
*
|
2017-11-15 14:14:20 +01:00
|
|
|
* Called on a background thread.
|
|
|
|
*/
|
2018-05-02 13:04:02 +02:00
|
|
|
virtual void ChainStateFlushed(const CBlockLocator &locator) {}
|
2015-02-05 01:11:44 +01:00
|
|
|
/** Tells listeners to broadcast their data. */
|
2017-07-11 09:30:36 +02:00
|
|
|
virtual void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) {}
|
2017-03-23 08:18:35 +01:00
|
|
|
/**
|
|
|
|
* Notifies listeners of a block validation result.
|
|
|
|
* If the provided CValidationState IsValid, the provided block
|
|
|
|
* is guaranteed to be the current best block at the time the
|
|
|
|
* callback was generated (not necessarily now)
|
|
|
|
*/
|
2017-07-11 09:30:36 +02:00
|
|
|
virtual void BlockChecked(const CBlock&, const CValidationState&) {}
|
2016-12-19 08:26:20 +01: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-07-11 09:30:36 +02:00
|
|
|
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
|
|
|
|
friend void ::RegisterValidationInterface(CValidationInterface*);
|
|
|
|
friend void ::UnregisterValidationInterface(CValidationInterface*);
|
|
|
|
friend void ::UnregisterAllValidationInterfaces();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct MainSignalsInstance;
|
|
|
|
class CMainSignals {
|
|
|
|
private:
|
|
|
|
std::unique_ptr<MainSignalsInstance> m_internals;
|
|
|
|
|
|
|
|
friend void ::RegisterValidationInterface(CValidationInterface*);
|
|
|
|
friend void ::UnregisterValidationInterface(CValidationInterface*);
|
|
|
|
friend void ::UnregisterAllValidationInterfaces();
|
2017-11-15 14:14:20 +01:00
|
|
|
friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
|
|
|
|
|
|
|
|
void MempoolEntryRemoved(CTransactionRef tx, MemPoolRemovalReason reason);
|
2017-07-11 09:30:36 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/** 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();
|
|
|
|
/** Call any remaining callbacks on the calling thread */
|
|
|
|
void FlushBackgroundCallbacks();
|
|
|
|
|
2020-03-24 15:21:59 +01:00
|
|
|
size_t CallbacksPending();
|
|
|
|
|
2017-11-15 14:14:20 +01:00
|
|
|
/** Register with mempool to call TransactionRemovedFromMempool callbacks */
|
|
|
|
void RegisterWithMempoolSignals(CTxMemPool& pool);
|
|
|
|
/** Unregister with mempool */
|
|
|
|
void UnregisterWithMempoolSignals(CTxMemPool& pool);
|
|
|
|
|
2019-09-04 03:07:05 +02:00
|
|
|
void AcceptedBlockHeader(const CBlockIndex *pindexNew);
|
|
|
|
void NotifyHeaderTip(const CBlockIndex *pindexNew, bool fInitialDownload);
|
2017-07-11 09:30:36 +02:00
|
|
|
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
|
2020-03-20 17:11:54 +01:00
|
|
|
void SynchronousUpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
|
2019-12-07 11:56:17 +01:00
|
|
|
void TransactionAddedToMempool(const CTransactionRef &, int64_t);
|
2017-11-15 14:14:20 +01:00
|
|
|
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
|
2019-09-07 22:16:59 +02:00
|
|
|
void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindexDisconnected);
|
2021-01-22 05:32:15 +01:00
|
|
|
void NotifyTransactionLock(const CTransactionRef &tx, const std::shared_ptr<const llmq::CInstantSendLock>& islock);
|
|
|
|
void NotifyChainLock(const CBlockIndex* pindex, const std::shared_ptr<const llmq::CChainLockSig>& clsig);
|
|
|
|
void NotifyGovernanceVote(const std::shared_ptr<const CGovernanceVote>& vote);
|
|
|
|
void NotifyGovernanceObject(const std::shared_ptr<const CGovernanceObject>& object);
|
|
|
|
void NotifyInstantSendDoubleSpendAttempt(const CTransactionRef ¤tTx, const CTransactionRef &previousTx);
|
|
|
|
void NotifyRecoveredSig(const std::shared_ptr<const llmq::CRecoveredSig> &sig);
|
2019-09-04 03:07:05 +02:00
|
|
|
void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff);
|
2018-05-02 13:04:02 +02:00
|
|
|
void ChainStateFlushed(const CBlockLocator &);
|
2017-07-11 09:30:36 +02:00
|
|
|
void Broadcast(int64_t nBestBlockTime, CConnman* connman);
|
|
|
|
void BlockChecked(const CBlock&, const CValidationState&);
|
|
|
|
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
|
2015-02-05 01:11:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
CMainSignals& GetMainSignals();
|
|
|
|
|
|
|
|
#endif // BITCOIN_VALIDATIONINTERFACE_H
|