Bitcoin ABC  0.29.2
P2P Digital Currency
zmqnotificationinterface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2018 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 
7 #include <zmq/zmqutil.h>
8 
9 #include <zmq.h>
10 
11 #include <primitives/block.h>
12 #include <util/system.h>
13 
15 
17  Shutdown();
18 }
19 
20 std::list<const CZMQAbstractNotifier *>
22  std::list<const CZMQAbstractNotifier *> result;
23  for (const auto &n : notifiers) {
24  result.push_back(n.get());
25  }
26  return result;
27 }
28 
30  std::map<std::string, CZMQNotifierFactory> factories;
31  factories["pubhashblock"] =
32  CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
33  factories["pubhashtx"] =
34  CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
35  factories["pubrawblock"] =
36  CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
37  factories["pubrawtx"] =
38  CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
39  factories["pubsequence"] =
40  CZMQAbstractNotifier::Create<CZMQPublishSequenceNotifier>;
41 
42  std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
43  for (const auto &entry : factories) {
44  std::string arg("-zmq" + entry.first);
45  const auto &factory = entry.second;
46  for (const std::string &address : gArgs.GetArgs(arg)) {
47  std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
48  notifier->SetType(entry.first);
49  notifier->SetAddress(address);
50  notifier->SetOutboundMessageHighWaterMark(
51  static_cast<int>(gArgs.GetIntArg(
53  notifiers.push_back(std::move(notifier));
54  }
55  }
56 
57  if (!notifiers.empty()) {
58  std::unique_ptr<CZMQNotificationInterface> notificationInterface(
60  notificationInterface->notifiers = std::move(notifiers);
61 
62  if (notificationInterface->Initialize()) {
63  return notificationInterface.release();
64  }
65  }
66 
67  return nullptr;
68 }
69 
70 // Called at startup to conditionally set up ZMQ socket(s)
72  int major = 0, minor = 0, patch = 0;
73  zmq_version(&major, &minor, &patch);
74  LogPrint(BCLog::ZMQ, "zmq: version %d.%d.%d\n", major, minor, patch);
75 
76  LogPrint(BCLog::ZMQ, "zmq: Initialize notification interface\n");
77  assert(!pcontext);
78 
79  pcontext = zmq_ctx_new();
80 
81  if (!pcontext) {
82  zmqError("Unable to initialize context");
83  return false;
84  }
85 
86  for (auto &notifier : notifiers) {
87  if (notifier->Initialize(pcontext)) {
88  LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n",
89  notifier->GetType(), notifier->GetAddress());
90  } else {
91  LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n",
92  notifier->GetType(), notifier->GetAddress());
93  return false;
94  }
95  }
96 
97  return true;
98 }
99 
100 // Called during shutdown sequence
102  LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n");
103  if (pcontext) {
104  for (auto &notifier : notifiers) {
105  LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n",
106  notifier->GetType(), notifier->GetAddress());
107  notifier->Shutdown();
108  }
109  zmq_ctx_term(pcontext);
110 
111  pcontext = nullptr;
112  }
113 }
114 
115 namespace {
116 
117 template <typename Function>
118 void TryForEachAndRemoveFailed(
119  std::list<std::unique_ptr<CZMQAbstractNotifier>> &notifiers,
120  const Function &func) {
121  for (auto i = notifiers.begin(); i != notifiers.end();) {
122  CZMQAbstractNotifier *notifier = i->get();
123  if (func(notifier)) {
124  ++i;
125  } else {
126  notifier->Shutdown();
127  i = notifiers.erase(i);
128  }
129  }
130 }
131 
132 } // anonymous namespace
133 
135  const CBlockIndex *pindexFork,
136  bool fInitialDownload) {
137  // In IBD or blocks were disconnected without any new ones
138  if (fInitialDownload || pindexNew == pindexFork) {
139  return;
140  }
141 
142  TryForEachAndRemoveFailed(notifiers,
143  [pindexNew](CZMQAbstractNotifier *notifier) {
144  return notifier->NotifyBlock(pindexNew);
145  });
146 }
147 
149  const CTransactionRef &ptx, std::shared_ptr<const std::vector<Coin>>,
150  uint64_t mempool_sequence) {
151  const CTransaction &tx = *ptx;
152 
153  TryForEachAndRemoveFailed(
154  notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier *notifier) {
155  return notifier->NotifyTransaction(tx) &&
156  notifier->NotifyTransactionAcceptance(tx, mempool_sequence);
157  });
158 }
159 
161  const CTransactionRef &ptx, MemPoolRemovalReason reason,
162  uint64_t mempool_sequence) {
163  // Called for all non-block inclusion reasons
164  const CTransaction &tx = *ptx;
165 
166  TryForEachAndRemoveFailed(
167  notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier *notifier) {
168  return notifier->NotifyTransactionRemoval(tx, mempool_sequence);
169  });
170 }
171 
173  const std::shared_ptr<const CBlock> &pblock,
174  const CBlockIndex *pindexConnected) {
175  for (const CTransactionRef &ptx : pblock->vtx) {
176  const CTransaction &tx = *ptx;
177  TryForEachAndRemoveFailed(notifiers,
178  [&tx](CZMQAbstractNotifier *notifier) {
179  return notifier->NotifyTransaction(tx);
180  });
181  }
182 
183  // Next we notify BlockConnect listeners for *all* blocks
184  TryForEachAndRemoveFailed(
185  notifiers, [pindexConnected](CZMQAbstractNotifier *notifier) {
186  return notifier->NotifyBlockConnect(pindexConnected);
187  });
188 }
189 
191  const std::shared_ptr<const CBlock> &pblock,
192  const CBlockIndex *pindexDisconnected) {
193  for (const CTransactionRef &ptx : pblock->vtx) {
194  const CTransaction &tx = *ptx;
195  TryForEachAndRemoveFailed(notifiers,
196  [&tx](CZMQAbstractNotifier *notifier) {
197  return notifier->NotifyTransaction(tx);
198  });
199  }
200 
201  // Next we notify BlockDisconnect listeners for *all* blocks
202  TryForEachAndRemoveFailed(
203  notifiers, [pindexDisconnected](CZMQAbstractNotifier *notifier) {
204  return notifier->NotifyBlockDisconnect(pindexDisconnected);
205  });
206 }
207 
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: system.cpp:480
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: system.cpp:635
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:26
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
virtual void Shutdown()=0
virtual bool NotifyBlockConnect(const CBlockIndex *pindex)
static const int DEFAULT_ZMQ_SNDHWM
virtual bool NotifyTransactionRemoval(const CTransaction &transaction, uint64_t mempool_sequence)
virtual bool NotifyBlock(const CBlockIndex *pindex)
virtual bool NotifyTransaction(const CTransaction &transaction)
virtual bool NotifyBlockDisconnect(const CBlockIndex *pindex)
virtual bool NotifyTransactionAcceptance(const CTransaction &transaction, uint64_t mempool_sequence)
void TransactionAddedToMempool(const CTransactionRef &tx, std::shared_ptr< const std::vector< Coin >>, uint64_t mempool_sequence) override
Notifies listeners of a transaction having been added to mempool.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexConnected) override
Notifies listeners of a block being connected.
static CZMQNotificationInterface * Create()
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
std::list< std::unique_ptr< CZMQAbstractNotifier > > notifiers
void BlockDisconnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexDisconnected) override
Notifies listeners of a block being disconnected.
void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override
Notifies listeners of a transaction leaving mempool.
std::list< const CZMQAbstractNotifier * > GetActiveNotifiers() const
#define LogPrint(category,...)
Definition: logging.h:210
@ ZMQ
Definition: logging.h:45
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
ArgsManager gArgs
Definition: system.cpp:80
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
Definition: txmempool.h:148
assert(!tx.IsCoinBase())
CZMQNotificationInterface * g_zmq_notification_interface
void zmqError(const char *str)
Definition: zmqutil.cpp:11