LCOV - code coverage report
Current view: top level - src - validationinterface.h (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 81.8 % 11 9
Test Date: 2026-11-27 05:37:43 Functions: 80.0 % 10 8
Branches: 50.0 % 2 1

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

Generated by: LCOV version 2.5.0-full