2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2015-2016 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>
|
|
|
|
#include <util.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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
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()
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
pcontext = zmq_init(1);
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, " Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::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;
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, " Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
|
2014-11-18 18:06:32 +01:00
|
|
|
notifier->Shutdown();
|
|
|
|
}
|
|
|
|
zmq_ctx_destroy(pcontext);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|