0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

Various cleanups in zmqnotificationinterface.

This is a pure refactoring of zmqnotificationinterface to make the
code easier to read and maintain.  It replaces explicit iterators
with C++11 for-each loops where appropriate and uses std::unique_ptr
to make memory ownership more explicit.
This commit is contained in:
Daniel Kraft 2020-09-01 09:40:13 +02:00
parent 78cb45d722
commit e15b1cfc31
3 changed files with 28 additions and 41 deletions

View file

@ -20,33 +20,26 @@ CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(nullptr)
CZMQNotificationInterface::~CZMQNotificationInterface() CZMQNotificationInterface::~CZMQNotificationInterface()
{ {
Shutdown(); Shutdown();
for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
{
delete *i;
}
} }
std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers() const std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers() const
{ {
std::list<const CZMQAbstractNotifier*> result; std::list<const CZMQAbstractNotifier*> result;
for (const auto* n : notifiers) { for (const auto& n : notifiers) {
result.push_back(n); result.push_back(n.get());
} }
return result; return result;
} }
CZMQNotificationInterface* CZMQNotificationInterface::Create() CZMQNotificationInterface* CZMQNotificationInterface::Create()
{ {
CZMQNotificationInterface* notificationInterface = nullptr;
std::map<std::string, CZMQNotifierFactory> factories; std::map<std::string, CZMQNotifierFactory> factories;
std::list<CZMQAbstractNotifier*> notifiers;
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>; factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>; factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>; factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>; factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
for (const auto& entry : factories) for (const auto& entry : factories)
{ {
std::string arg("-zmq" + entry.first); std::string arg("-zmq" + entry.first);
@ -58,23 +51,21 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
notifier->SetType(entry.first); notifier->SetType(entry.first);
notifier->SetAddress(address); notifier->SetAddress(address);
notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM))); notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
notifiers.push_back(notifier); notifiers.emplace_back(notifier);
} }
} }
if (!notifiers.empty()) if (!notifiers.empty())
{ {
notificationInterface = new CZMQNotificationInterface(); std::unique_ptr<CZMQNotificationInterface> notificationInterface(new CZMQNotificationInterface());
notificationInterface->notifiers = notifiers; notificationInterface->notifiers = std::move(notifiers);
if (!notificationInterface->Initialize()) if (notificationInterface->Initialize()) {
{ return notificationInterface.release();
delete notificationInterface;
notificationInterface = nullptr;
} }
} }
return notificationInterface; return nullptr;
} }
// Called at startup to conditionally set up ZMQ socket(s) // Called at startup to conditionally set up ZMQ socket(s)
@ -95,26 +86,15 @@ bool CZMQNotificationInterface::Initialize()
return false; return false;
} }
std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); for (auto& notifier : notifiers) {
for (; i!=notifiers.end(); ++i) if (notifier->Initialize(pcontext)) {
{
CZMQAbstractNotifier *notifier = *i;
if (notifier->Initialize(pcontext))
{
LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress()); LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
} } else {
else
{
LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress()); LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
break; return false;
} }
} }
if (i!=notifiers.end())
{
return false;
}
return true; return true;
} }
@ -124,9 +104,7 @@ void CZMQNotificationInterface::Shutdown()
LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n"); LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n");
if (pcontext) if (pcontext)
{ {
for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i) for (auto& notifier : notifiers) {
{
CZMQAbstractNotifier *notifier = *i;
LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress()); LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
notifier->Shutdown(); notifier->Shutdown();
} }
@ -141,9 +119,9 @@ void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, co
if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones if (fInitialDownload || pindexNew == pindexFork) // In IBD or blocks were disconnected without any new ones
return; return;
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); ) for (auto i = notifiers.begin(); i!=notifiers.end(); )
{ {
CZMQAbstractNotifier *notifier = *i; CZMQAbstractNotifier *notifier = i->get();
if (notifier->NotifyBlock(pindexNew)) if (notifier->NotifyBlock(pindexNew))
{ {
i++; i++;
@ -162,9 +140,9 @@ void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef&
// all the same external callback. // all the same external callback.
const CTransaction& tx = *ptx; const CTransaction& tx = *ptx;
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); ) for (auto i = notifiers.begin(); i!=notifiers.end(); )
{ {
CZMQAbstractNotifier *notifier = *i; CZMQAbstractNotifier *notifier = i->get();
if (notifier->NotifyTransaction(tx)) if (notifier->NotifyTransaction(tx))
{ {
i++; i++;

View file

@ -7,6 +7,7 @@
#include <validationinterface.h> #include <validationinterface.h>
#include <list> #include <list>
#include <memory>
class CBlockIndex; class CBlockIndex;
class CZMQAbstractNotifier; class CZMQAbstractNotifier;
@ -34,7 +35,7 @@ private:
CZMQNotificationInterface(); CZMQNotificationInterface();
void *pcontext; void *pcontext;
std::list<CZMQAbstractNotifier*> notifiers; std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
}; };
extern CZMQNotificationInterface* g_zmq_notification_interface; extern CZMQNotificationInterface* g_zmq_notification_interface;

View file

@ -10,6 +10,14 @@
#include <util/system.h> #include <util/system.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <zmq.h>
#include <cstdarg>
#include <cstddef>
#include <map>
#include <string>
#include <utility>
static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifiers; static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifiers;
static const char *MSG_HASHBLOCK = "hashblock"; static const char *MSG_HASHBLOCK = "hashblock";