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.
|
|
|
|
|
|
|
|
#include "zmqnotificationinterface.h"
|
|
|
|
#include "zmqpublishnotifier.h"
|
|
|
|
|
|
|
|
#include "version.h"
|
2016-12-02 01:06:41 +01:00
|
|
|
#include "validation.h"
|
2014-11-18 18:06:32 +01:00
|
|
|
#include "streams.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
void zmqError(const char *str)
|
|
|
|
{
|
2015-11-19 03:27:18 +01:00
|
|
|
LogPrint("zmq", "zmq: Error: %s, errno=%s\n", str, zmq_strerror(errno));
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
CZMQNotificationInterface* notificationInterface = NULL;
|
|
|
|
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>;
|
|
|
|
|
|
|
|
for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
|
|
|
|
{
|
2016-11-30 03:43:29 +01:00
|
|
|
std::string arg("-zmq" + i->first);
|
|
|
|
if (IsArgSet(arg))
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
CZMQNotifierFactory factory = i->second;
|
2016-11-30 03:43:29 +01:00
|
|
|
std::string address = GetArg(arg, "");
|
2014-11-18 18:06:32 +01:00
|
|
|
CZMQAbstractNotifier *notifier = factory();
|
|
|
|
notifier->SetType(i->first);
|
|
|
|
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;
|
|
|
|
notificationInterface = NULL;
|
|
|
|
}
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return notificationInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called at startup to conditionally set up ZMQ socket(s)
|
|
|
|
bool CZMQNotificationInterface::Initialize()
|
|
|
|
{
|
2015-11-19 03:27:18 +01:00
|
|
|
LogPrint("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))
|
|
|
|
{
|
|
|
|
LogPrint("zmq", " Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint("zmq", " Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
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()
|
|
|
|
{
|
2015-11-19 03:27:18 +01:00
|
|
|
LogPrint("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;
|
|
|
|
LogPrint("zmq", " Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
notifier->Shutdown();
|
|
|
|
}
|
|
|
|
zmq_ctx_destroy(pcontext);
|
|
|
|
|
|
|
|
pcontext = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-26 13:55:13 +02:00
|
|
|
void CZMQNotificationInterface::SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock)
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|