diff --git a/src/zmq/zmqnotificationinterface.cpp b/src/zmq/zmqnotificationinterface.cpp index 96e5b3c039..449651e65f 100644 --- a/src/zmq/zmqnotificationinterface.cpp +++ b/src/zmq/zmqnotificationinterface.cpp @@ -114,24 +114,32 @@ void CZMQNotificationInterface::Shutdown() } } +namespace { + +template +void TryForEachAndRemoveFailed(std::list>& notifiers, const Function& func) +{ + for (auto i = notifiers.begin(); i != notifiers.end(); ) { + CZMQAbstractNotifier* notifier = i->get(); + if (func(notifier)) { + ++i; + } else { + notifier->Shutdown(); + i = notifiers.erase(i); + } + } +} + +} // anonymous namespace + 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 (auto i = notifiers.begin(); i!=notifiers.end(); ) - { - CZMQAbstractNotifier *notifier = i->get(); - if (notifier->NotifyBlock(pindexNew)) - { - i++; - } - else - { - notifier->Shutdown(); - i = notifiers.erase(i); - } - } + TryForEachAndRemoveFailed(notifiers, [pindexNew](CZMQAbstractNotifier* notifier) { + return notifier->NotifyBlock(pindexNew); + }); } void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx) @@ -140,19 +148,9 @@ void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& // all the same external callback. const CTransaction& tx = *ptx; - for (auto i = notifiers.begin(); i!=notifiers.end(); ) - { - CZMQAbstractNotifier *notifier = i->get(); - if (notifier->NotifyTransaction(tx)) - { - i++; - } - else - { - notifier->Shutdown(); - i = notifiers.erase(i); - } - } + TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) { + return notifier->NotifyTransaction(tx); + }); } void CZMQNotificationInterface::BlockConnected(const std::shared_ptr& pblock, const CBlockIndex* pindexConnected)