2018-07-09 17:05:14 +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.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <zmq/zmqnotificationinterface.h>
|
|
|
|
#include <zmq/zmqpublishnotifier.h>
|
2020-09-01 09:40:13 +02:00
|
|
|
#include <zmq/zmqutil.h>
|
|
|
|
|
|
|
|
#include <zmq.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>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
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
|
|
|
}
|
|
|
|
|
2018-07-09 17:05:14 +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-07-09 17:05:14 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-27 19:11:21 +01:00
|
|
|
CZMQNotificationInterface* CZMQNotificationInterface::Create()
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
std::map<std::string, CZMQNotifierFactory> factories;
|
|
|
|
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>;
|
2020-11-17 20:41:30 +01:00
|
|
|
factories["pubhashrecoveredsig"] = CZMQAbstractNotifier::Create<CZMQPublishHashRecoveredSigNotifier>;
|
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>;
|
2020-11-17 20:41:30 +01:00
|
|
|
factories["pubrawrecoveredsig"] = CZMQAbstractNotifier::Create<CZMQPublishRawRecoveredSigNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
|
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
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
const auto& factory = entry.second;
|
|
|
|
const std::string address = gArgs.GetArg(arg, "");
|
|
|
|
std::unique_ptr<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);
|
2020-09-01 09:40:13 +02:00
|
|
|
notifiers.push_back(std::move(notifier));
|
2014-11-18 18:06:32 +01: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 19:09:17 +01:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
if (notificationInterface->Initialize()) {
|
|
|
|
return notificationInterface.release();
|
2015-11-01 19:09:17 +01:00
|
|
|
}
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
return nullptr;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
|
|
|
if (notifier->Initialize(pcontext)) {
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
} else {
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
return false;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
|
|
|
LogPrint(BCLog::ZMQ, "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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
namespace {
|
|
|
|
template <typename Function>
|
|
|
|
void TryForEachAndRemoveFailed(std::list<std::unique_ptr<CZMQAbstractNotifier>>& notifiers, const Function& func)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto i = notifiers.begin(); i != notifiers.end(); ) {
|
|
|
|
CZMQAbstractNotifier* notifier = i->get();
|
|
|
|
if (func(notifier)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
2014-11-18 18:06:32 +01:00
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyChainLock(const CBlockIndex *pindex, const std::shared_ptr<const llmq::CChainLockSig>& clsig)
|
2019-05-08 11:12:54 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [pindex, &clsig](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyChainLock(pindex, clsig);
|
|
|
|
});
|
2019-05-08 11:12:54 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransaction(tx);
|
|
|
|
});
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyTransactionLock(const CTransactionRef& tx, const std::shared_ptr<const llmq::CInstantSendLock>& islock)
|
2016-07-15 08:38:33 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx, &islock](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransactionLock(tx, islock);
|
|
|
|
});
|
2016-07-15 08:38:33 +02:00
|
|
|
}
|
2018-07-12 11:06:30 +02:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyGovernanceVote(const std::shared_ptr<const CGovernanceVote> &vote)
|
2018-07-12 11:06:30 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&vote](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyGovernanceVote(vote);
|
|
|
|
});
|
2018-07-12 11:06:30 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyGovernanceObject(const std::shared_ptr<const CGovernanceObject> &object)
|
2018-07-12 11:06:30 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&object](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyGovernanceObject(object);
|
|
|
|
});
|
2018-07-12 11:06:30 +02:00
|
|
|
}
|
2018-09-12 13:12:44 +02:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyInstantSendDoubleSpendAttempt(const CTransactionRef& currentTx, const CTransactionRef& previousTx)
|
2018-09-12 13:12:44 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [¤tTx, &previousTx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyInstantSendDoubleSpendAttempt(currentTx, previousTx);
|
|
|
|
});
|
2018-09-12 13:12:44 +02:00
|
|
|
}
|
2018-07-09 17:05:14 +02:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyRecoveredSig(const std::shared_ptr<const llmq::CRecoveredSig>& sig)
|
2020-11-17 20:41:30 +01:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&sig](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyRecoveredSig(sig);
|
|
|
|
});
|
2020-11-17 20:41:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 17:05:14 +02:00
|
|
|
CZMQNotificationInterface* g_zmq_notification_interface = nullptr;
|