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