2019-12-30 22:39:22 +13:00
|
|
|
// Copyright (c) 2015-2019 The Bitcoin Core developers
|
2014-11-18 12:06:32 -05:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <zmq/zmqnotificationinterface.h>
|
|
|
|
#include <zmq/zmqpublishnotifier.h>
|
2018-07-17 12:51:23 +02:00
|
|
|
#include <zmq/zmqutil.h>
|
|
|
|
|
|
|
|
#include <zmq.h>
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <validation.h>
|
2018-10-22 15:51:11 -07:00
|
|
|
#include <util/system.h>
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2017-08-07 07:36:37 +02:00
|
|
|
CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(nullptr)
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CZMQNotificationInterface::~CZMQNotificationInterface()
|
|
|
|
{
|
2015-11-01 18:09:17 +00:00
|
|
|
Shutdown();
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:10:01 +02:00
|
|
|
std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers() const
|
|
|
|
{
|
|
|
|
std::list<const CZMQAbstractNotifier*> result;
|
2020-09-01 09:40:13 +02:00
|
|
|
for (const auto& n : notifiers) {
|
|
|
|
result.push_back(n.get());
|
2018-06-29 16:10:01 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:43:29 -08:00
|
|
|
CZMQNotificationInterface* CZMQNotificationInterface::Create()
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
|
|
|
std::map<std::string, CZMQNotifierFactory> factories;
|
|
|
|
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
|
|
|
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
|
|
|
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
|
|
|
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : factories)
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
2017-06-04 22:02:43 +02:00
|
|
|
std::string arg("-zmq" + entry.first);
|
2017-08-01 21:17:40 +02:00
|
|
|
if (gArgs.IsArgSet(arg))
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
2018-07-17 12:36:22 +02:00
|
|
|
const auto& factory = entry.second;
|
|
|
|
const std::string address = gArgs.GetArg(arg, "");
|
|
|
|
std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
|
2017-06-04 22:02:43 +02:00
|
|
|
notifier->SetType(entry.first);
|
2014-11-18 12:06:32 -05:00
|
|
|
notifier->SetAddress(address);
|
2018-08-24 20:42:03 -04:00
|
|
|
notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
|
2018-07-17 12:36:22 +02:00
|
|
|
notifiers.push_back(std::move(notifier));
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!notifiers.empty())
|
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
std::unique_ptr<CZMQNotificationInterface> notificationInterface(new CZMQNotificationInterface());
|
|
|
|
notificationInterface->notifiers = std::move(notifiers);
|
2015-11-01 18:09:17 +00:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
if (notificationInterface->Initialize()) {
|
|
|
|
return notificationInterface.release();
|
2015-11-01 18:09:17 +00:00
|
|
|
}
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
return nullptr;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called at startup to conditionally set up ZMQ socket(s)
|
|
|
|
bool CZMQNotificationInterface::Initialize()
|
|
|
|
{
|
2018-06-30 10:32:33 -04:00
|
|
|
int major = 0, minor = 0, patch = 0;
|
|
|
|
zmq_version(&major, &minor, &patch);
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: version %d.%d.%d\n", major, minor, patch);
|
|
|
|
|
2016-12-25 20:19:40 +00:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Initialize notification interface\n");
|
2014-11-18 12:06:32 -05:00
|
|
|
assert(!pcontext);
|
|
|
|
|
2018-06-30 10:32:33 -04:00
|
|
|
pcontext = zmq_ctx_new();
|
2014-11-18 12:06:32 -05:00
|
|
|
|
|
|
|
if (!pcontext)
|
|
|
|
{
|
|
|
|
zmqError("Unable to initialize context");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
|
|
|
if (notifier->Initialize(pcontext)) {
|
2018-08-24 20:42:03 -04:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
2020-09-01 09:40:13 +02:00
|
|
|
} else {
|
2018-08-24 20:42:03 -04:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
2020-09-01 09:40:13 +02:00
|
|
|
return false;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:09:17 +00:00
|
|
|
return true;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called during shutdown sequence
|
|
|
|
void CZMQNotificationInterface::Shutdown()
|
|
|
|
{
|
2016-12-25 20:19:40 +00:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n");
|
2014-11-18 12:06:32 -05:00
|
|
|
if (pcontext)
|
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
2018-08-24 20:42:03 -04:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
|
2014-11-18 12:06:32 -05:00
|
|
|
notifier->Shutdown();
|
|
|
|
}
|
2018-06-30 10:32:33 -04:00
|
|
|
zmq_ctx_term(pcontext);
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2017-06-21 21:10:00 +02:00
|
|
|
pcontext = nullptr;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
namespace {
|
2016-09-30 17:40:03 -04:00
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
template <typename Function>
|
|
|
|
void TryForEachAndRemoveFailed(std::list<std::unique_ptr<CZMQAbstractNotifier>>& notifiers, const Function& func)
|
|
|
|
{
|
|
|
|
for (auto i = notifiers.begin(); i != notifiers.end(); ) {
|
|
|
|
CZMQAbstractNotifier* notifier = i->get();
|
|
|
|
if (func(notifier)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
2014-11-18 12:06:32 -05:00
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
|
|
|
|
{
|
|
|
|
if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones
|
|
|
|
return;
|
|
|
|
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [pindexNew](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyBlock(pindexNew);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-29 21:12:42 -04:00
|
|
|
void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx)
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
2017-03-29 21:12:42 -04:00
|
|
|
// Used by BlockConnected and BlockDisconnected as well, because they're
|
|
|
|
// all the same external callback.
|
|
|
|
const CTransaction& tx = *ptx;
|
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransaction(tx);
|
|
|
|
});
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
2017-03-29 21:12:42 -04:00
|
|
|
|
2019-11-11 10:43:34 -05:00
|
|
|
void CZMQNotificationInterface::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected)
|
2017-03-29 21:12:42 -04:00
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
|
|
|
// Do a normal notify for each transaction added in the block
|
|
|
|
TransactionAddedToMempool(ptx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 15:41:41 -04:00
|
|
|
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
|
2017-03-29 21:12:42 -04:00
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
|
|
|
// Do a normal notify for each transaction removed in block disconnection
|
|
|
|
TransactionAddedToMempool(ptx);
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 15:16:31 +02:00
|
|
|
|
|
|
|
CZMQNotificationInterface* g_zmq_notification_interface = nullptr;
|