diff --git a/src/zmq/zmqnotificationinterface.cpp b/src/zmq/zmqnotificationinterface.cpp --- a/src/zmq/zmqnotificationinterface.cpp +++ b/src/zmq/zmqnotificationinterface.cpp @@ -113,18 +113,16 @@ } } -void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, - const CBlockIndex *pindexFork, - bool fInitialDownload) { - // In IBD or blocks were disconnected without any new ones - if (fInitialDownload || pindexNew == pindexFork) { - return; - } +namespace { +template +void TryForEachAndRemoveFailed( + std::list> ¬ifiers, + const Function &func) { for (auto i = notifiers.begin(); i != notifiers.end();) { CZMQAbstractNotifier *notifier = i->get(); - if (notifier->NotifyBlock(pindexNew)) { - i++; + if (func(notifier)) { + ++i; } else { notifier->Shutdown(); i = notifiers.erase(i); @@ -132,21 +130,31 @@ } } +} // anonymous namespace + +void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, + const CBlockIndex *pindexFork, + bool fInitialDownload) { + // In IBD or blocks were disconnected without any new ones + if (fInitialDownload || pindexNew == pindexFork) { + return; + } + + TryForEachAndRemoveFailed(notifiers, + [pindexNew](CZMQAbstractNotifier *notifier) { + return notifier->NotifyBlock(pindexNew); + }); +} + void CZMQNotificationInterface::TransactionAddedToMempool( const CTransactionRef &ptx) { // Used by BlockConnected and BlockDisconnected as well, because they're 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(