LCOV - code coverage report
Current view: top level - src - validationinterface.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 2 10 20.0 %
Date: 2023-11-12 01:39:15 Functions: 3 10 30.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 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 <kernel/chain.h>
      11                 :            : #include <primitives/transaction.h> // CTransaction(Ref)
      12                 :            : #include <sync.h>
      13                 :            : 
      14                 :            : #include <functional>
      15                 :            : #include <memory>
      16                 :            : 
      17                 :            : class BlockValidationState;
      18                 :            : class CBlock;
      19                 :            : class CBlockIndex;
      20                 :            : struct CBlockLocator;
      21                 :            : class CValidationInterface;
      22                 :            : class CScheduler;
      23                 :            : enum class MemPoolRemovalReason;
      24                 :            : 
      25                 :            : /** Register subscriber */
      26                 :            : void RegisterValidationInterface(CValidationInterface* callbacks);
      27                 :            : /** Unregister subscriber. DEPRECATED. This is not safe to use when the RPC server or main message handler thread is running. */
      28                 :            : void UnregisterValidationInterface(CValidationInterface* callbacks);
      29                 :            : /** Unregister all subscribers */
      30                 :            : void UnregisterAllValidationInterfaces();
      31                 :            : 
      32                 :            : // Alternate registration functions that release a shared_ptr after the last
      33                 :            : // notification is sent. These are useful for race-free cleanup, since
      34                 :            : // unregistration is nonblocking and can return before the last notification is
      35                 :            : // processed.
      36                 :            : /** Register subscriber */
      37                 :            : void RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
      38                 :            : /** Unregister subscriber */
      39                 :            : void UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks);
      40                 :            : 
      41                 :            : /**
      42                 :            :  * Pushes a function to callback onto the notification queue, guaranteeing any
      43                 :            :  * callbacks generated prior to now are finished when the function is called.
      44                 :            :  *
      45                 :            :  * Be very careful blocking on func to be called if any locks are held -
      46                 :            :  * validation interface clients may not be able to make progress as they often
      47                 :            :  * wait for things like cs_main, so blocking until func is called with cs_main
      48                 :            :  * will result in a deadlock (that DEBUG_LOCKORDER will miss).
      49                 :            :  */
      50                 :            : void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
      51                 :            : /**
      52                 :            :  * This is a synonym for the following, which asserts certain locks are not
      53                 :            :  * held:
      54                 :            :  *     std::promise<void> promise;
      55                 :            :  *     CallFunctionInValidationInterfaceQueue([&promise] {
      56                 :            :  *         promise.set_value();
      57                 :            :  *     });
      58                 :            :  *     promise.get_future().wait();
      59                 :            :  */
      60                 :            : void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main);
      61                 :            : 
      62                 :            : /**
      63                 :            :  * Implement this to subscribe to events generated in validation
      64                 :            :  *
      65                 :            :  * Each CValidationInterface() subscriber will receive event callbacks
      66                 :            :  * in the order in which the events were generated by validation.
      67                 :            :  * Furthermore, each ValidationInterface() subscriber may assume that
      68                 :            :  * callbacks effectively run in a single thread with single-threaded
      69                 :            :  * memory consistency. That is, for a given ValidationInterface()
      70                 :            :  * instantiation, each callback will complete before the next one is
      71                 :            :  * invoked. This means, for example when a block is connected that the
      72                 :            :  * UpdatedBlockTip() callback may depend on an operation performed in
      73                 :            :  * the BlockConnected() callback without worrying about explicit
      74                 :            :  * synchronization. No ordering should be assumed across
      75                 :            :  * ValidationInterface() subscribers.
      76                 :            :  */
      77                 :          0 : class CValidationInterface {
      78                 :            : protected:
      79                 :            :     /**
      80                 :            :      * Protected destructor so that instances can only be deleted by derived classes.
      81                 :            :      * If that restriction is no longer desired, this should be made public and virtual.
      82                 :            :      */
      83                 :            :     ~CValidationInterface() = default;
      84                 :            :     /**
      85                 :            :      * Notifies listeners when the block chain tip advances.
      86                 :            :      *
      87                 :            :      * When multiple blocks are connected at once, UpdatedBlockTip will be called on the final tip
      88                 :            :      * but may not be called on every intermediate tip. If the latter behavior is desired,
      89                 :            :      * subscribe to BlockConnected() instead.
      90                 :            :      *
      91                 :            :      * Called on a background thread. Only called for the active chainstate.
      92                 :            :      */
      93                 :        167 :     virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
      94                 :            :     /**
      95                 :            :      * Notifies listeners of a transaction having been added to mempool.
      96                 :            :      *
      97                 :            :      * Called on a background thread.
      98                 :            :      */
      99                 :          0 :     virtual void TransactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) {}
     100                 :            : 
     101                 :            :     /**
     102                 :            :      * Notifies listeners of a transaction leaving mempool.
     103                 :            :      *
     104                 :            :      * This notification fires for transactions that are removed from the
     105                 :            :      * mempool for the following reasons:
     106                 :            :      *
     107                 :            :      * - EXPIRY (expired from mempool after -mempoolexpiry hours)
     108                 :            :      * - SIZELIMIT (removed in size limiting if the mempool exceeds -maxmempool megabytes)
     109                 :            :      * - REORG (removed during a reorg)
     110                 :            :      * - CONFLICT (removed because it conflicts with in-block transaction)
     111                 :            :      * - REPLACED (removed due to RBF replacement)
     112                 :            :      *
     113                 :            :      * This does not fire for transactions that are removed from the mempool
     114                 :            :      * because they have been included in a block. Any client that is interested
     115                 :            :      * in transactions removed from the mempool for inclusion in a block can learn
     116                 :            :      * about those transactions from the BlockConnected notification.
     117                 :            :      *
     118                 :            :      * Transactions that are removed from the mempool because they conflict
     119                 :            :      * with a transaction in the new block will have
     120                 :            :      * TransactionRemovedFromMempool events fired *before* the BlockConnected
     121                 :            :      * event is fired. If multiple blocks are connected in one step, then the
     122                 :            :      * ordering could be:
     123                 :            :      *
     124                 :            :      * - TransactionRemovedFromMempool(tx1 from block A)
     125                 :            :      * - TransactionRemovedFromMempool(tx2 from block A)
     126                 :            :      * - TransactionRemovedFromMempool(tx1 from block B)
     127                 :            :      * - TransactionRemovedFromMempool(tx2 from block B)
     128                 :            :      * - BlockConnected(A)
     129                 :            :      * - BlockConnected(B)
     130                 :            :      *
     131                 :            :      * Called on a background thread.
     132                 :            :      */
     133                 :          0 :     virtual void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
     134                 :            :     /**
     135                 :            :      * Notifies listeners of a block being connected.
     136                 :            :      * Provides a vector of transactions evicted from the mempool as a result.
     137                 :            :      *
     138                 :            :      * Called on a background thread.
     139                 :            :      */
     140                 :        177 :     virtual void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock> &block, const CBlockIndex *pindex) {}
     141                 :            :     /**
     142                 :            :      * Notifies listeners of a block being disconnected
     143                 :            :      *
     144                 :            :      * Called on a background thread. Only called for the active chainstate, since
     145                 :            :      * background chainstates should never disconnect blocks.
     146                 :            :      */
     147                 :          0 :     virtual void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) {}
     148                 :            :     /**
     149                 :            :      * Notifies listeners of the new active block chain on-disk.
     150                 :            :      *
     151                 :            :      * Prior to this callback, any updates are not guaranteed to persist on disk
     152                 :            :      * (ie clients need to handle shutdown/restart safety by being able to
     153                 :            :      * understand when some updates were lost due to unclean shutdown).
     154                 :            :      *
     155                 :            :      * When this callback is invoked, the validation changes done by any prior
     156                 :            :      * callback are guaranteed to exist on disk and survive a restart, including
     157                 :            :      * an unclean shutdown.
     158                 :            :      *
     159                 :            :      * Provides a locator describing the best chain, which is likely useful for
     160                 :            :      * storing current state on disk in client DBs.
     161                 :            :      *
     162                 :            :      * Called on a background thread.
     163                 :            :      */
     164                 :          0 :     virtual void ChainStateFlushed(ChainstateRole role, const CBlockLocator &locator) {}
     165                 :            :     /**
     166                 :            :      * Notifies listeners of a block validation result.
     167                 :            :      * If the provided BlockValidationState IsValid, the provided block
     168                 :            :      * is guaranteed to be the current best block at the time the
     169                 :            :      * callback was generated (not necessarily now).
     170                 :            :      */
     171                 :          0 :     virtual void BlockChecked(const CBlock&, const BlockValidationState&) {}
     172                 :            :     /**
     173                 :            :      * Notifies listeners that a block which builds directly on our current tip
     174                 :            :      * has been received and connected to the headers tree, though not validated yet.
     175                 :            :      */
     176                 :          0 :     virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& block) {};
     177                 :            :     friend class CMainSignals;
     178                 :            :     friend class ValidationInterfaceTest;
     179                 :            : };
     180                 :            : 
     181                 :            : class MainSignalsImpl;
     182                 :          0 : class CMainSignals {
     183                 :            : private:
     184                 :            :     std::unique_ptr<MainSignalsImpl> m_internals;
     185                 :            : 
     186                 :            :     friend void ::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface>);
     187                 :            :     friend void ::UnregisterValidationInterface(CValidationInterface*);
     188                 :            :     friend void ::UnregisterAllValidationInterfaces();
     189                 :            :     friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
     190                 :            : 
     191                 :            : public:
     192                 :            :     /** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
     193                 :            :     void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
     194                 :            :     /** Unregister a CScheduler to give callbacks which should run in the background - these callbacks will now be dropped! */
     195                 :            :     void UnregisterBackgroundSignalScheduler();
     196                 :            :     /** Call any remaining callbacks on the calling thread */
     197                 :            :     void FlushBackgroundCallbacks();
     198                 :            : 
     199                 :            :     size_t CallbacksPending();
     200                 :            : 
     201                 :            : 
     202                 :            :     void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
     203                 :            :     void TransactionAddedToMempool(const CTransactionRef&, uint64_t mempool_sequence);
     204                 :            :     void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
     205                 :            :     void BlockConnected(ChainstateRole, const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
     206                 :            :     void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
     207                 :            :     void ChainStateFlushed(ChainstateRole, const CBlockLocator &);
     208                 :            :     void BlockChecked(const CBlock&, const BlockValidationState&);
     209                 :            :     void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr<const CBlock>&);
     210                 :            : };
     211                 :            : 
     212                 :            : CMainSignals& GetMainSignals();
     213                 :            : 
     214                 :            : #endif // BITCOIN_VALIDATIONINTERFACE_H

Generated by: LCOV version 1.14