mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 05:23:01 +01:00
89f6f75910
* Unify zmq message order * Implement `zmqpubhashchainlock` and `zmqpubrawchainlock`
254 lines
7.8 KiB
C++
254 lines
7.8 KiB
C++
// 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.
|
|
|
|
#include "zmqnotificationinterface.h"
|
|
#include "zmqpublishnotifier.h"
|
|
|
|
#include "version.h"
|
|
#include "validation.h"
|
|
#include "streams.h"
|
|
#include "util.h"
|
|
|
|
void zmqError(const char *str)
|
|
{
|
|
LogPrint("zmq", "zmq: Error: %s, errno=%s\n", str, zmq_strerror(errno));
|
|
}
|
|
|
|
CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(NULL)
|
|
{
|
|
}
|
|
|
|
CZMQNotificationInterface::~CZMQNotificationInterface()
|
|
{
|
|
Shutdown();
|
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
|
|
{
|
|
delete *i;
|
|
}
|
|
}
|
|
|
|
CZMQNotificationInterface* CZMQNotificationInterface::Create()
|
|
{
|
|
CZMQNotificationInterface* notificationInterface = NULL;
|
|
std::map<std::string, CZMQNotifierFactory> factories;
|
|
std::list<CZMQAbstractNotifier*> notifiers;
|
|
|
|
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
|
factories["pubhashchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashChainLockNotifier>;
|
|
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
|
factories["pubhashtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionLockNotifier>;
|
|
factories["pubhashgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceVoteNotifier>;
|
|
factories["pubhashgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceObjectNotifier>;
|
|
factories["pubhashinstantsenddoublespend"] = CZMQAbstractNotifier::Create<CZMQPublishHashInstantSendDoubleSpendNotifier>;
|
|
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
|
factories["pubrawchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockNotifier>;
|
|
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
|
factories["pubrawtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockNotifier>;
|
|
factories["pubrawgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceVoteNotifier>;
|
|
factories["pubrawgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceObjectNotifier>;
|
|
factories["pubrawinstantsenddoublespend"] = CZMQAbstractNotifier::Create<CZMQPublishRawInstantSendDoubleSpendNotifier>;
|
|
|
|
for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
|
|
{
|
|
std::string arg("-zmq" + i->first);
|
|
if (IsArgSet(arg))
|
|
{
|
|
CZMQNotifierFactory factory = i->second;
|
|
std::string address = GetArg(arg, "");
|
|
CZMQAbstractNotifier *notifier = factory();
|
|
notifier->SetType(i->first);
|
|
notifier->SetAddress(address);
|
|
notifiers.push_back(notifier);
|
|
}
|
|
}
|
|
|
|
if (!notifiers.empty())
|
|
{
|
|
notificationInterface = new CZMQNotificationInterface();
|
|
notificationInterface->notifiers = notifiers;
|
|
|
|
if (!notificationInterface->Initialize())
|
|
{
|
|
delete notificationInterface;
|
|
notificationInterface = NULL;
|
|
}
|
|
}
|
|
|
|
return notificationInterface;
|
|
}
|
|
|
|
// Called at startup to conditionally set up ZMQ socket(s)
|
|
bool CZMQNotificationInterface::Initialize()
|
|
{
|
|
LogPrint("zmq", "zmq: Initialize notification interface\n");
|
|
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;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
// Called during shutdown sequence
|
|
void CZMQNotificationInterface::Shutdown()
|
|
{
|
|
LogPrint("zmq", "zmq: Shutdown notification interface\n");
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
|
{
|
|
CZMQAbstractNotifier *notifier = *i;
|
|
if (notifier->NotifyBlock(pindexNew))
|
|
{
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
notifier->Shutdown();
|
|
i = notifiers.erase(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
void CZMQNotificationInterface::NotifyChainLock(const CBlockIndex *pindex)
|
|
{
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
|
{
|
|
CZMQAbstractNotifier *notifier = *i;
|
|
if (notifier->NotifyChainLock(pindex))
|
|
{
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
notifier->Shutdown();
|
|
i = notifiers.erase(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
void CZMQNotificationInterface::SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock)
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
void CZMQNotificationInterface::NotifyTransactionLock(const CTransaction &tx)
|
|
{
|
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
|
{
|
|
CZMQAbstractNotifier *notifier = *i;
|
|
if (notifier->NotifyTransactionLock(tx))
|
|
{
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
notifier->Shutdown();
|
|
i = notifiers.erase(i);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|