LCOV - code coverage report
Current view: top level - src/zmq - zmqnotificationinterface.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 102 0
Test Date: 2024-08-28 04:44:32 Functions: 0.0 % 21 0
Branches: 0.0 % 116 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2015-2022 The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <zmq/zmqnotificationinterface.h>
       6                 :             : 
       7                 :             : #include <common/args.h>
       8                 :             : #include <kernel/chain.h>
       9                 :             : #include <kernel/mempool_entry.h>
      10                 :             : #include <logging.h>
      11                 :             : #include <netbase.h>
      12                 :             : #include <primitives/block.h>
      13                 :             : #include <primitives/transaction.h>
      14                 :             : #include <validationinterface.h>
      15                 :             : #include <zmq/zmqabstractnotifier.h>
      16                 :             : #include <zmq/zmqpublishnotifier.h>
      17                 :             : #include <zmq/zmqutil.h>
      18                 :             : 
      19                 :             : #include <zmq.h>
      20                 :             : 
      21                 :             : #include <cassert>
      22                 :             : #include <map>
      23                 :             : #include <string>
      24                 :             : #include <utility>
      25                 :             : #include <vector>
      26                 :             : 
      27                 :           0 : CZMQNotificationInterface::CZMQNotificationInterface() = default;
      28                 :             : 
      29                 :           0 : CZMQNotificationInterface::~CZMQNotificationInterface()
      30                 :             : {
      31                 :           0 :     Shutdown();
      32                 :           0 : }
      33                 :             : 
      34                 :           0 : std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers() const
      35                 :             : {
      36                 :           0 :     std::list<const CZMQAbstractNotifier*> result;
      37         [ #  # ]:           0 :     for (const auto& n : notifiers) {
      38         [ #  # ]:           0 :         result.push_back(n.get());
      39                 :             :     }
      40                 :           0 :     return result;
      41                 :           0 : }
      42                 :             : 
      43                 :           0 : std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std::function<bool(std::vector<uint8_t>&, const CBlockIndex&)> get_block_by_index)
      44                 :             : {
      45         [ #  # ]:           0 :     std::map<std::string, CZMQNotifierFactory> factories;
      46   [ #  #  #  # ]:           0 :     factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
      47   [ #  #  #  # ]:           0 :     factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
      48   [ #  #  #  # ]:           0 :     factories["pubrawblock"] = [&get_block_by_index]() -> std::unique_ptr<CZMQAbstractNotifier> {
      49                 :           0 :         return std::make_unique<CZMQPublishRawBlockNotifier>(get_block_by_index);
      50                 :           0 :     };
      51   [ #  #  #  # ]:           0 :     factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
      52   [ #  #  #  # ]:           0 :     factories["pubsequence"] = CZMQAbstractNotifier::Create<CZMQPublishSequenceNotifier>;
      53                 :             : 
      54                 :           0 :     std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
      55         [ #  # ]:           0 :     for (const auto& entry : factories)
      56                 :             :     {
      57         [ #  # ]:           0 :         std::string arg("-zmq" + entry.first);
      58                 :           0 :         const auto& factory = entry.second;
      59   [ #  #  #  # ]:           0 :         for (std::string& address : gArgs.GetArgs(arg)) {
      60                 :             :             // libzmq uses prefix "ipc://" for UNIX domain sockets
      61   [ #  #  #  # ]:           0 :             if (address.substr(0, ADDR_PREFIX_UNIX.length()) == ADDR_PREFIX_UNIX) {
      62         [ #  # ]:           0 :                 address.replace(0, ADDR_PREFIX_UNIX.length(), ADDR_PREFIX_IPC);
      63                 :             :             }
      64                 :             : 
      65         [ #  # ]:           0 :             std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
      66         [ #  # ]:           0 :             notifier->SetType(entry.first);
      67         [ #  # ]:           0 :             notifier->SetAddress(address);
      68   [ #  #  #  #  :           0 :             notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetIntArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
                   #  # ]
      69         [ #  # ]:           0 :             notifiers.push_back(std::move(notifier));
      70                 :           0 :         }
      71                 :           0 :     }
      72                 :             : 
      73         [ #  # ]:           0 :     if (!notifiers.empty())
      74                 :             :     {
      75         [ #  # ]:           0 :         std::unique_ptr<CZMQNotificationInterface> notificationInterface(new CZMQNotificationInterface());
      76                 :           0 :         notificationInterface->notifiers = std::move(notifiers);
      77                 :             : 
      78   [ #  #  #  # ]:           0 :         if (notificationInterface->Initialize()) {
      79                 :             :             return notificationInterface;
      80                 :             :         }
      81                 :           0 :     }
      82                 :             : 
      83                 :           0 :     return nullptr;
      84                 :           0 : }
      85                 :             : 
      86                 :             : // Called at startup to conditionally set up ZMQ socket(s)
      87                 :           0 : bool CZMQNotificationInterface::Initialize()
      88                 :             : {
      89                 :           0 :     int major = 0, minor = 0, patch = 0;
      90                 :           0 :     zmq_version(&major, &minor, &patch);
      91         [ #  # ]:           0 :     LogPrint(BCLog::ZMQ, "version %d.%d.%d\n", major, minor, patch);
      92                 :             : 
      93         [ #  # ]:           0 :     LogPrint(BCLog::ZMQ, "Initialize notification interface\n");
      94         [ #  # ]:           0 :     assert(!pcontext);
      95                 :             : 
      96                 :           0 :     pcontext = zmq_ctx_new();
      97                 :             : 
      98         [ #  # ]:           0 :     if (!pcontext)
      99                 :             :     {
     100         [ #  # ]:           0 :         zmqError("Unable to initialize context");
     101                 :           0 :         return false;
     102                 :             :     }
     103                 :             : 
     104         [ #  # ]:           0 :     for (auto& notifier : notifiers) {
     105         [ #  # ]:           0 :         if (notifier->Initialize(pcontext)) {
     106   [ #  #  #  #  :           0 :             LogPrint(BCLog::ZMQ, "Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
                   #  # ]
     107                 :             :         } else {
     108   [ #  #  #  #  :           0 :             LogPrint(BCLog::ZMQ, "Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
                   #  # ]
     109                 :           0 :             return false;
     110                 :             :         }
     111                 :             :     }
     112                 :             : 
     113                 :             :     return true;
     114                 :             : }
     115                 :             : 
     116                 :             : // Called during shutdown sequence
     117                 :           0 : void CZMQNotificationInterface::Shutdown()
     118                 :             : {
     119         [ #  # ]:           0 :     LogPrint(BCLog::ZMQ, "Shutdown notification interface\n");
     120         [ #  # ]:           0 :     if (pcontext)
     121                 :             :     {
     122         [ #  # ]:           0 :         for (auto& notifier : notifiers) {
     123   [ #  #  #  #  :           0 :             LogPrint(BCLog::ZMQ, "Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
                   #  # ]
     124                 :           0 :             notifier->Shutdown();
     125                 :             :         }
     126                 :           0 :         zmq_ctx_term(pcontext);
     127                 :             : 
     128                 :           0 :         pcontext = nullptr;
     129                 :             :     }
     130                 :           0 : }
     131                 :             : 
     132                 :             : namespace {
     133                 :             : 
     134                 :             : template <typename Function>
     135                 :           0 : void TryForEachAndRemoveFailed(std::list<std::unique_ptr<CZMQAbstractNotifier>>& notifiers, const Function& func)
     136                 :             : {
     137         [ #  # ]:           0 :     for (auto i = notifiers.begin(); i != notifiers.end(); ) {
     138                 :           0 :         CZMQAbstractNotifier* notifier = i->get();
     139         [ #  # ]:           0 :         if (func(notifier)) {
     140                 :           0 :             ++i;
     141                 :             :         } else {
     142                 :           0 :             notifier->Shutdown();
     143                 :           0 :             i = notifiers.erase(i);
     144                 :             :         }
     145                 :             :     }
     146                 :           0 : }
     147                 :             : 
     148                 :             : } // anonymous namespace
     149                 :             : 
     150                 :           0 : void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
     151                 :             : {
     152         [ #  # ]:           0 :     if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones
     153                 :             :         return;
     154                 :             : 
     155                 :           0 :     TryForEachAndRemoveFailed(notifiers, [pindexNew](CZMQAbstractNotifier* notifier) {
     156                 :           0 :         return notifier->NotifyBlock(pindexNew);
     157                 :             :     });
     158                 :             : }
     159                 :             : 
     160                 :           0 : void CZMQNotificationInterface::TransactionAddedToMempool(const NewMempoolTransactionInfo& ptx, uint64_t mempool_sequence)
     161                 :             : {
     162                 :           0 :     const CTransaction& tx = *(ptx.info.m_tx);
     163                 :             : 
     164                 :           0 :     TryForEachAndRemoveFailed(notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier* notifier) {
     165   [ #  #  #  # ]:           0 :         return notifier->NotifyTransaction(tx) && notifier->NotifyTransactionAcceptance(tx, mempool_sequence);
     166                 :             :     });
     167                 :           0 : }
     168                 :             : 
     169                 :           0 : void CZMQNotificationInterface::TransactionRemovedFromMempool(const CTransactionRef& ptx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
     170                 :             : {
     171                 :             :     // Called for all non-block inclusion reasons
     172                 :           0 :     const CTransaction& tx = *ptx;
     173                 :             : 
     174                 :           0 :     TryForEachAndRemoveFailed(notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier* notifier) {
     175                 :           0 :         return notifier->NotifyTransactionRemoval(tx, mempool_sequence);
     176                 :             :     });
     177                 :           0 : }
     178                 :             : 
     179                 :           0 : void CZMQNotificationInterface::BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected)
     180                 :             : {
     181         [ #  # ]:           0 :     if (role == ChainstateRole::BACKGROUND) {
     182                 :             :         return;
     183                 :             :     }
     184         [ #  # ]:           0 :     for (const CTransactionRef& ptx : pblock->vtx) {
     185                 :           0 :         const CTransaction& tx = *ptx;
     186                 :           0 :         TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
     187                 :           0 :             return notifier->NotifyTransaction(tx);
     188                 :             :         });
     189                 :             :     }
     190                 :             : 
     191                 :             :     // Next we notify BlockConnect listeners for *all* blocks
     192                 :           0 :     TryForEachAndRemoveFailed(notifiers, [pindexConnected](CZMQAbstractNotifier* notifier) {
     193                 :           0 :         return notifier->NotifyBlockConnect(pindexConnected);
     194                 :             :     });
     195                 :             : }
     196                 :             : 
     197                 :           0 : void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
     198                 :             : {
     199         [ #  # ]:           0 :     for (const CTransactionRef& ptx : pblock->vtx) {
     200                 :           0 :         const CTransaction& tx = *ptx;
     201                 :           0 :         TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
     202                 :           0 :             return notifier->NotifyTransaction(tx);
     203                 :             :         });
     204                 :             :     }
     205                 :             : 
     206                 :             :     // Next we notify BlockDisconnect listeners for *all* blocks
     207                 :           0 :     TryForEachAndRemoveFailed(notifiers, [pindexDisconnected](CZMQAbstractNotifier* notifier) {
     208                 :           0 :         return notifier->NotifyBlockDisconnect(pindexDisconnected);
     209                 :             :     });
     210                 :           0 : }
     211                 :             : 
     212                 :             : std::unique_ptr<CZMQNotificationInterface> g_zmq_notification_interface;
        

Generated by: LCOV version 2.0-1