2014-11-18 18:06:32 +01:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <zmq/zmqnotificationinterface.h>
|
|
|
|
#include <zmq/zmqpublishnotifier.h>
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2020-03-19 23:46:56 +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)
|
|
|
|
{
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Error: %s, errno=%s\n", str, zmq_strerror(errno));
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
2019-08-06 05:08:33 +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-12-27 19:11:21 +01:00
|
|
|
CZMQNotificationInterface* CZMQNotificationInterface::Create()
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2019-08-06 05:08:33 +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>;
|
2019-05-08 11:12:54 +02:00
|
|
|
factories["pubhashchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashChainLockNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
2016-07-15 08:38:33 +02:00
|
|
|
factories["pubhashtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionLockNotifier>;
|
2018-07-12 11:06:30 +02:00
|
|
|
factories["pubhashgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceVoteNotifier>;
|
|
|
|
factories["pubhashgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceObjectNotifier>;
|
2018-09-12 13:12:44 +02:00
|
|
|
factories["pubhashinstantsenddoublespend"] = CZMQAbstractNotifier::Create<CZMQPublishHashInstantSendDoubleSpendNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
2019-05-08 11:12:54 +02:00
|
|
|
factories["pubrawchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockNotifier>;
|
2019-05-23 11:13:58 +02:00
|
|
|
factories["pubrawchainlocksig"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockSigNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
2016-07-15 08:38:33 +02:00
|
|
|
factories["pubrawtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockNotifier>;
|
2019-05-23 11:13:58 +02:00
|
|
|
factories["pubrawtxlocksig"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockSigNotifier>;
|
2018-07-12 11:06:30 +02:00
|
|
|
factories["pubrawgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceVoteNotifier>;
|
|
|
|
factories["pubrawgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceObjectNotifier>;
|
2018-09-12 13:12:44 +02:00
|
|
|
factories["pubrawinstantsenddoublespend"] = CZMQAbstractNotifier::Create<CZMQPublishRawInstantSendDoubleSpendNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2017-11-30 23:09:44 +01:00
|
|
|
for (const auto& entry : factories)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2017-11-30 23:09:44 +01:00
|
|
|
std::string arg("-zmq" + entry.first);
|
2019-06-24 18:44:27 +02:00
|
|
|
if (gArgs.IsArgSet(arg))
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2017-11-30 23:09:44 +01:00
|
|
|
CZMQNotifierFactory factory = entry.second;
|
2019-06-24 18:44:27 +02:00
|
|
|
std::string address = gArgs.GetArg(arg, "");
|
2014-11-18 18:06:32 +01:00
|
|
|
CZMQAbstractNotifier *notifier = factory();
|
2017-11-30 23:09:44 +01: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;
|
2019-08-06 05:08:33 +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()
|
|
|
|
{
|
2019-05-22 23:51:39 +02: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))
|
|
|
|
{
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::ZMQ, " Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-22 23:51:39 +02: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()
|
|
|
|
{
|
2019-05-22 23:51:39 +02: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;
|
2019-05-22 23:51:39 +02: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-08-16 15:54:51 +02:00
|
|
|
pcontext = nullptr;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 16:10:10 +02:00
|
|
|
void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2017-07-28 16:10:10 +02:00
|
|
|
if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones
|
|
|
|
return;
|
|
|
|
|
2014-11-18 18:06:32 +01:00
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
|
|
|
{
|
|
|
|
CZMQAbstractNotifier *notifier = *i;
|
2017-07-28 16:10:10 +02:00
|
|
|
if (notifier->NotifyBlock(pindexNew))
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:13:58 +02:00
|
|
|
void CZMQNotificationInterface::NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig)
|
2019-05-08 11:12:54 +02:00
|
|
|
{
|
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
|
|
|
{
|
|
|
|
CZMQAbstractNotifier *notifier = *i;
|
2019-05-23 11:13:58 +02:00
|
|
|
if (notifier->NotifyChainLock(pindex, clsig))
|
2019-05-08 11:12:54 +02:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:56:17 +01:00
|
|
|
void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx, int64_t nAcceptTime)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2017-04-10 21:06: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-07-15 08:38:33 +02:00
|
|
|
|
2017-04-10 21:06: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
|
2019-12-07 11:56:17 +01:00
|
|
|
TransactionAddedToMempool(ptx, 0);
|
2017-04-10 21:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:55:29 +02:00
|
|
|
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
|
2017-04-10 21:06:42 +02:00
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
|
|
|
// Do a normal notify for each transaction removed in block disconnection
|
2019-12-07 11:56:17 +01:00
|
|
|
TransactionAddedToMempool(ptx, 0);
|
2017-04-10 21:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:13:58 +02:00
|
|
|
void CZMQNotificationInterface::NotifyTransactionLock(const CTransaction &tx, const llmq::CInstantSendLock& islock)
|
2016-07-15 08:38:33 +02:00
|
|
|
{
|
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
|
|
|
{
|
|
|
|
CZMQAbstractNotifier *notifier = *i;
|
2019-05-23 11:13:58 +02:00
|
|
|
if (notifier->NotifyTransactionLock(tx, islock))
|
2016-07-15 08:38:33 +02:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-12 11:06:30 +02:00
|
|
|
|
|
|
|
void CZMQNotificationInterface::NotifyGovernanceVote(const CGovernanceVote &vote)
|
|
|
|
{
|
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i != notifiers.end(); )
|
|
|
|
{
|
|
|
|
CZMQAbstractNotifier *notifier = *i;
|
|
|
|
if (notifier->NotifyGovernanceVote(vote))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CZMQNotificationInterface::NotifyGovernanceObject(const CGovernanceObject &object)
|
|
|
|
{
|
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i != notifiers.end(); )
|
|
|
|
{
|
|
|
|
CZMQAbstractNotifier *notifier = *i;
|
|
|
|
if (notifier->NotifyGovernanceObject(object))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:12:44 +02:00
|
|
|
|
|
|
|
void CZMQNotificationInterface::NotifyInstantSendDoubleSpendAttempt(const CTransaction ¤tTx, const CTransaction &previousTx)
|
|
|
|
{
|
|
|
|
for (auto it = notifiers.begin(); it != notifiers.end();) {
|
|
|
|
CZMQAbstractNotifier *notifier = *it;
|
|
|
|
if (notifier->NotifyInstantSendDoubleSpendAttempt(currentTx, previousTx)) {
|
|
|
|
++it;
|
|
|
|
} else {
|
|
|
|
notifier->Shutdown();
|
|
|
|
it = notifiers.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|