LCOV - code coverage report
Current view: top level - src - validationinterface.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 83.9 % 118 99
Test Date: 2025-12-19 04:17:13 Functions: 87.7 % 57 50
Branches: 42.4 % 238 101

             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                 :             : #include <validationinterface.h>
       7                 :             : 
       8                 :             : #include <chain.h>
       9                 :             : #include <consensus/validation.h>
      10                 :             : #include <kernel/mempool_entry.h>
      11                 :             : #include <kernel/mempool_removal_reason.h>
      12                 :             : #include <kernel/types.h>
      13                 :             : #include <logging.h>
      14                 :             : #include <primitives/block.h>
      15                 :             : #include <primitives/transaction.h>
      16                 :             : #include <util/check.h>
      17                 :             : #include <util/task_runner.h>
      18                 :             : 
      19                 :             : #include <future>
      20                 :             : #include <memory>
      21                 :             : #include <unordered_map>
      22                 :             : #include <utility>
      23                 :             : 
      24                 :             : using kernel::ChainstateRole;
      25                 :             : 
      26                 :             : /**
      27                 :             :  * ValidationSignalsImpl manages a list of shared_ptr<CValidationInterface> callbacks.
      28                 :             :  *
      29                 :             :  * A std::unordered_map is used to track what callbacks are currently
      30                 :             :  * registered, and a std::list is used to store the callbacks that are
      31                 :             :  * currently registered as well as any callbacks that are just unregistered
      32                 :             :  * and about to be deleted when they are done executing.
      33                 :             :  */
      34                 :             : class ValidationSignalsImpl
      35                 :             : {
      36                 :             : private:
      37                 :             :     Mutex m_mutex;
      38                 :             :     //! List entries consist of a callback pointer and reference count. The
      39                 :             :     //! count is equal to the number of current executions of that entry, plus 1
      40                 :             :     //! if it's registered. It cannot be 0 because that would imply it is
      41                 :             :     //! unregistered and also not being executed (so shouldn't exist).
      42         [ +  - ]:     1059945 :     struct ListEntry { std::shared_ptr<CValidationInterface> callbacks; int count = 1; };
      43                 :             :     std::list<ListEntry> m_list GUARDED_BY(m_mutex);
      44                 :             :     std::unordered_map<CValidationInterface*, std::list<ListEntry>::iterator> m_map GUARDED_BY(m_mutex);
      45                 :             : 
      46                 :             : public:
      47                 :             :     std::unique_ptr<util::TaskRunnerInterface> m_task_runner;
      48                 :             : 
      49                 :        1258 :     explicit ValidationSignalsImpl(std::unique_ptr<util::TaskRunnerInterface> task_runner)
      50         [ -  + ]:        1258 :         : m_task_runner{std::move(Assert(task_runner))} {}
      51                 :             : 
      52                 :      353315 :     void Register(std::shared_ptr<CValidationInterface> callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      53                 :             :     {
      54                 :      353315 :         LOCK(m_mutex);
      55         [ +  - ]:      353315 :         auto inserted = m_map.emplace(callbacks.get(), m_list.end());
      56   [ +  -  +  - ]:      353315 :         if (inserted.second) inserted.first->second = m_list.emplace(m_list.end());
      57         [ +  - ]:      353315 :         inserted.first->second->callbacks = std::move(callbacks);
      58                 :      353315 :     }
      59                 :             : 
      60                 :      353315 :     void Unregister(CValidationInterface* callbacks) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      61                 :             :     {
      62                 :      353315 :         LOCK(m_mutex);
      63                 :      353315 :         auto it = m_map.find(callbacks);
      64   [ +  -  +  - ]:      706630 :         if (it != m_map.end()) {
      65         [ +  - ]:      353315 :             if (!--it->second->count) m_list.erase(it->second);
      66                 :      353315 :             m_map.erase(it);
      67                 :             :         }
      68                 :      353315 :     }
      69                 :             : 
      70                 :             :     //! Clear unregisters every previously registered callback, erasing every
      71                 :             :     //! map entry. After this call, the list may still contain callbacks that
      72                 :             :     //! are currently executing, but it will be cleared when they are done
      73                 :             :     //! executing.
      74                 :           0 :     void Clear() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      75                 :             :     {
      76                 :           0 :         LOCK(m_mutex);
      77   [ #  #  #  # ]:           0 :         for (const auto& entry : m_map) {
      78         [ #  # ]:           0 :             if (!--entry.second->count) m_list.erase(entry.second);
      79                 :             :         }
      80         [ #  # ]:           0 :         m_map.clear();
      81                 :           0 :     }
      82                 :             : 
      83                 :     1219510 :     template<typename F> void Iterate(F&& f) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
      84                 :             :     {
      85                 :     1219510 :         WAIT_LOCK(m_mutex, lock);
      86         [ +  + ]:     2278083 :         for (auto it = m_list.begin(); it != m_list.end();) {
      87                 :     1058573 :             ++it->count;
      88                 :             :             {
      89         [ +  - ]:     1058573 :                 REVERSE_LOCK(lock, m_mutex);
      90         [ +  - ]:     1058573 :                 f(*it->callbacks);
      91         [ +  - ]:     1058573 :             }
      92         [ +  - ]:     2117146 :             it = --it->count ? std::next(it) : m_list.erase(it);
      93                 :             :         }
      94                 :     1219510 :     }
      95                 :             : };
      96                 :             : 
      97                 :        1258 : ValidationSignals::ValidationSignals(std::unique_ptr<util::TaskRunnerInterface> task_runner)
      98                 :        1258 :     : m_internals{std::make_unique<ValidationSignalsImpl>(std::move(task_runner))} {}
      99                 :             : 
     100                 :        1258 : ValidationSignals::~ValidationSignals() = default;
     101                 :             : 
     102                 :        1258 : void ValidationSignals::FlushBackgroundCallbacks()
     103                 :             : {
     104                 :        1258 :     m_internals->m_task_runner->flush();
     105                 :        1258 : }
     106                 :             : 
     107                 :      177584 : size_t ValidationSignals::CallbacksPending()
     108                 :             : {
     109                 :      177584 :     return m_internals->m_task_runner->size();
     110                 :             : }
     111                 :             : 
     112                 :      353315 : void ValidationSignals::RegisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
     113                 :             : {
     114                 :             :     // Each connection captures the shared_ptr to ensure that each callback is
     115                 :             :     // executed before the subscriber is destroyed. For more details see #18338.
     116         [ +  - ]:      353315 :     m_internals->Register(std::move(callbacks));
     117                 :      353315 : }
     118                 :             : 
     119                 :      173997 : void ValidationSignals::RegisterValidationInterface(CValidationInterface* callbacks)
     120                 :             : {
     121                 :             :     // Create a shared_ptr with a no-op deleter - CValidationInterface lifecycle
     122                 :             :     // is managed by the caller.
     123         [ +  - ]:      173997 :     RegisterSharedValidationInterface({callbacks, [](CValidationInterface*){}});
     124                 :      173997 : }
     125                 :             : 
     126                 :      179318 : void ValidationSignals::UnregisterSharedValidationInterface(std::shared_ptr<CValidationInterface> callbacks)
     127                 :             : {
     128                 :      179318 :     UnregisterValidationInterface(callbacks.get());
     129                 :      179318 : }
     130                 :             : 
     131                 :      353315 : void ValidationSignals::UnregisterValidationInterface(CValidationInterface* callbacks)
     132                 :             : {
     133                 :      353315 :     m_internals->Unregister(callbacks);
     134                 :      353315 : }
     135                 :             : 
     136                 :           0 : void ValidationSignals::UnregisterAllValidationInterfaces()
     137                 :             : {
     138                 :           0 :     m_internals->Clear();
     139                 :           0 : }
     140                 :             : 
     141                 :      739817 : void ValidationSignals::CallFunctionInValidationInterfaceQueue(std::function<void()> func)
     142                 :             : {
     143         [ +  - ]:      739817 :     m_internals->m_task_runner->insert(std::move(func));
     144                 :      739817 : }
     145                 :             : 
     146                 :      739817 : void ValidationSignals::SyncWithValidationInterfaceQueue()
     147                 :             : {
     148                 :      739817 :     AssertLockNotHeld(cs_main);
     149                 :             :     // Block until the validation queue drains
     150                 :      739817 :     std::promise<void> promise;
     151         [ +  - ]:      739817 :     CallFunctionInValidationInterfaceQueue([&promise] {
     152                 :      739817 :         promise.set_value();
     153                 :             :     });
     154   [ +  -  +  - ]:     1479634 :     promise.get_future().wait();
     155                 :      739817 : }
     156                 :             : 
     157                 :             : // Use a macro instead of a function for conditional logging to prevent
     158                 :             : // evaluating arguments when logging is not enabled.
     159                 :             : //
     160                 :             : // NOTE: The lambda captures all local variables by value.
     161                 :             : #define ENQUEUE_AND_LOG_EVENT(event, fmt, name, ...)           \
     162                 :             :     do {                                                       \
     163                 :             :         auto local_name = (name);                              \
     164                 :             :         LOG_EVENT("Enqueuing " fmt, local_name, __VA_ARGS__);  \
     165                 :             :         m_internals->m_task_runner->insert([=] { \
     166                 :             :             LOG_EVENT(fmt, local_name, __VA_ARGS__);           \
     167                 :             :             event();                                           \
     168                 :             :         });                                                    \
     169                 :             :     } while (0)
     170                 :             : 
     171                 :             : #define LOG_EVENT(fmt, ...) \
     172                 :             :     LogDebug(BCLog::VALIDATION, fmt "\n", __VA_ARGS__)
     173                 :             : 
     174                 :      148905 : void ValidationSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
     175                 :             :     // Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
     176                 :             :     // the chain actually updates. One way to ensure this is for the caller to invoke this signal
     177                 :             :     // in the same critical section where the chain is updated
     178                 :             : 
     179                 :      297810 :     auto event = [pindexNew, pindexFork, fInitialDownload, this] {
     180                 :      296235 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload); });
     181                 :      297810 :     };
     182   [ +  +  +  +  :      465738 :     ENQUEUE_AND_LOG_EVENT(event, "%s: new block hash=%s fork block hash=%s (in IBD=%s)", __func__,
          +  -  +  -  +  
          -  +  +  +  +  
             +  -  +  - ]
     183                 :             :                           pindexNew->GetBlockHash().ToString(),
     184                 :             :                           pindexFork ? pindexFork->GetBlockHash().ToString() : "null",
     185                 :             :                           fInitialDownload);
     186                 :      148905 : }
     187                 :             : 
     188                 :      159756 : void ValidationSignals::ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd)
     189                 :             : {
     190   [ +  +  +  - ]:      254571 :     LOG_EVENT("%s: new block hash=%s block height=%d", __func__, new_tip.GetBlockHash().ToString(), new_tip.nHeight);
     191                 :      317937 :     m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ActiveTipChange(new_tip, is_ibd); });
     192                 :      159756 : }
     193                 :             : 
     194                 :      183312 : void ValidationSignals::TransactionAddedToMempool(const NewMempoolTransactionInfo& tx, uint64_t mempool_sequence)
     195                 :             : {
     196         [ -  - ]:           0 :     auto event = [tx, mempool_sequence, this] {
     197                 :      333720 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionAddedToMempool(tx, mempool_sequence); });
     198                 :      183312 :     };
     199   [ +  -  -  +  :      549936 :     ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s", __func__,
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  -  +  -  
                -  -  - ]
     200                 :             :                           tx.info.m_tx->GetHash().ToString(),
     201                 :             :                           tx.info.m_tx->GetWitnessHash().ToString());
     202                 :      183312 : }
     203                 :             : 
     204                 :       58349 : void ValidationSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {
     205   [ +  -  -  - ]:       58349 :     auto event = [tx, reason, mempool_sequence, this] {
     206                 :      117438 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason, mempool_sequence); });
     207                 :      116698 :     };
     208   [ +  -  -  +  :      233396 :     ENQUEUE_AND_LOG_EVENT(event, "%s: txid=%s wtxid=%s reason=%s", __func__,
          -  -  -  -  -  
          -  +  -  +  -  
          +  -  +  -  -  
          +  -  -  -  -  
                   -  - ]
     209                 :             :                           tx->GetHash().ToString(),
     210                 :             :                           tx->GetWitnessHash().ToString(),
     211                 :             :                           RemovalReasonToString(reason));
     212                 :       58349 : }
     213                 :             : 
     214                 :      148905 : void ValidationSignals::BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
     215                 :             : {
     216         [ -  - ]:           0 :     auto event = [role, pblock, pindex, this] {
     217                 :      296235 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockConnected(role, pblock, pindex); });
     218         [ +  - ]:      148905 :     };
     219   [ +  -  +  +  :      763548 :     ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
                +  +  - ]
     220                 :             :                           pblock->GetHash().ToString(),
     221                 :             :                           pindex->nHeight);
     222                 :      148905 : }
     223                 :             : 
     224                 :      157322 : void ValidationSignals::MempoolTransactionsRemovedForBlock(const std::vector<RemovedMempoolTransactionInfo>& txs_removed_for_block, unsigned int nBlockHeight)
     225                 :             : {
     226                 :           0 :     auto event = [txs_removed_for_block, nBlockHeight, this] {
     227                 :      304652 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.MempoolTransactionsRemovedForBlock(txs_removed_for_block, nBlockHeight); });
     228                 :      157322 :     };
     229   [ +  -  +  +  :      629288 :     ENQUEUE_AND_LOG_EVENT(event, "%s: block height=%s txs removed=%s", __func__,
          -  +  +  -  +  
          -  +  -  +  -  
          +  -  +  +  -  
                      + ]
     230                 :             :                           nBlockHeight,
     231                 :             :                           txs_removed_for_block.size());
     232                 :      157322 : }
     233                 :             : 
     234                 :           0 : void ValidationSignals::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
     235                 :             : {
     236   [ #  #  #  # ]:           0 :     auto event = [pblock, pindex, this] {
     237                 :           0 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockDisconnected(pblock, pindex); });
     238                 :           0 :     };
     239   [ #  #  #  #  :           0 :     ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s block height=%d", __func__,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
     240                 :             :                           pblock->GetHash().ToString(),
     241                 :             :                           pindex->nHeight);
     242                 :           0 : }
     243                 :             : 
     244                 :      110957 : void ValidationSignals::ChainStateFlushed(const ChainstateRole& role, const CBlockLocator& locator)
     245                 :             : {
     246                 :           0 :     auto event = [role, locator, this] {
     247         [ +  - ]:      110981 :         m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.ChainStateFlushed(role, locator); });
     248                 :      110957 :     };
     249   [ +  -  +  +  :      665392 :     ENQUEUE_AND_LOG_EVENT(event, "%s: block hash=%s", __func__,
          -  +  -  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  +  -  +  
                   +  - ]
     250                 :             :                           locator.IsNull() ? "null" : locator.vHave.front().ToString());
     251                 :      110957 : }
     252                 :             : 
     253                 :      177215 : void ValidationSignals::BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& state)
     254                 :             : {
     255   [ +  +  +  -  :      287898 :     LOG_EVENT("%s: block hash=%s state=%s", __func__,
             +  -  +  - ]
     256                 :             :               block->GetHash().ToString(), state.ToString());
     257         [ +  - ]:      351307 :     m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.BlockChecked(block, state); });
     258                 :      177215 : }
     259                 :             : 
     260                 :       74789 : void ValidationSignals::NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock> &block) {
     261   [ +  +  +  - ]:       84978 :     LOG_EVENT("%s: block hash=%s", __func__, block->GetHash().ToString());
     262                 :      149578 :     m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.NewPoWValidBlock(pindex, block); });
     263                 :       74789 : }
        

Generated by: LCOV version 2.0-1