From cfbb2124939822e95265a39242ffca3d86bac6e8 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Wed, 3 May 2023 22:24:21 +0200 Subject: [PATCH] zmq: Pass lambda to zmq's ZMQPublishRawBlockNotifier The lambda captures a reference to the chainman unique_ptr to retrieve block data. An assert is added on the chainman to ensure that the lambda is not used while the chainman is uninitialized. This is done in preparation for the following commits where blockstorage functions are made BlockManager methods. --- src/init.cpp | 6 +++++- src/zmq/zmqabstractnotifier.h | 3 ++- src/zmq/zmqnotificationinterface.cpp | 6 ++++-- src/zmq/zmqnotificationinterface.h | 3 ++- src/zmq/zmqpublishnotifier.cpp | 5 +---- src/zmq/zmqpublishnotifier.h | 7 +++++++ 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 5c05b55f0d4..2f3521a1032 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1424,7 +1424,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) } #if ENABLE_ZMQ - g_zmq_notification_interface = CZMQNotificationInterface::Create(); + g_zmq_notification_interface = CZMQNotificationInterface::Create( + [&chainman = node.chainman](CBlock& block, const CBlockIndex& index) { + assert(chainman); + return node::ReadBlockFromDisk(block, &index, chainman->GetConsensus()); + }); if (g_zmq_notification_interface) { RegisterValidationInterface(g_zmq_notification_interface.get()); diff --git a/src/zmq/zmqabstractnotifier.h b/src/zmq/zmqabstractnotifier.h index cf0ee48f47e..17fa7bbaa91 100644 --- a/src/zmq/zmqabstractnotifier.h +++ b/src/zmq/zmqabstractnotifier.h @@ -6,6 +6,7 @@ #define BITCOIN_ZMQ_ZMQABSTRACTNOTIFIER_H #include +#include #include #include @@ -13,7 +14,7 @@ class CBlockIndex; class CTransaction; class CZMQAbstractNotifier; -using CZMQNotifierFactory = std::unique_ptr (*)(); +using CZMQNotifierFactory = std::function()>; class CZMQAbstractNotifier { diff --git a/src/zmq/zmqnotificationinterface.cpp b/src/zmq/zmqnotificationinterface.cpp index 2ed0c93fc32..6755368249f 100644 --- a/src/zmq/zmqnotificationinterface.cpp +++ b/src/zmq/zmqnotificationinterface.cpp @@ -39,12 +39,14 @@ std::list CZMQNotificationInterface::GetActiveNotif return result; } -std::unique_ptr CZMQNotificationInterface::Create() +std::unique_ptr CZMQNotificationInterface::Create(std::function get_block_by_index) { std::map factories; factories["pubhashblock"] = CZMQAbstractNotifier::Create; factories["pubhashtx"] = CZMQAbstractNotifier::Create; - factories["pubrawblock"] = CZMQAbstractNotifier::Create; + factories["pubrawblock"] = [&get_block_by_index]() -> std::unique_ptr { + return std::make_unique(get_block_by_index); + }; factories["pubrawtx"] = CZMQAbstractNotifier::Create; factories["pubsequence"] = CZMQAbstractNotifier::Create; diff --git a/src/zmq/zmqnotificationinterface.h b/src/zmq/zmqnotificationinterface.h index 4aef87c5a44..ce67633b30f 100644 --- a/src/zmq/zmqnotificationinterface.h +++ b/src/zmq/zmqnotificationinterface.h @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -23,7 +24,7 @@ public: std::list GetActiveNotifiers() const; - static std::unique_ptr Create(); + static std::unique_ptr Create(std::function get_block_by_index); protected: bool Initialize(); diff --git a/src/zmq/zmqpublishnotifier.cpp b/src/zmq/zmqpublishnotifier.cpp index 55f3d4e9345..21aa44c309f 100644 --- a/src/zmq/zmqpublishnotifier.cpp +++ b/src/zmq/zmqpublishnotifier.cpp @@ -39,8 +39,6 @@ namespace Consensus { struct Params; } -using node::ReadBlockFromDisk; - static std::multimap mapPublishNotifiers; static const char *MSG_HASHBLOCK = "hashblock"; @@ -247,10 +245,9 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex) { LogPrint(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address); - const Consensus::Params& consensusParams = Params().GetConsensus(); CDataStream ss(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); CBlock block; - if (!ReadBlockFromDisk(block, pindex, consensusParams)) { + if (!m_get_block_by_index(block, *pindex)) { zmqError("Can't read block from disk"); return false; } diff --git a/src/zmq/zmqpublishnotifier.h b/src/zmq/zmqpublishnotifier.h index 18336a5eb02..a5cd4337615 100644 --- a/src/zmq/zmqpublishnotifier.h +++ b/src/zmq/zmqpublishnotifier.h @@ -9,7 +9,9 @@ #include #include +#include +class CBlock; class CBlockIndex; class CTransaction; @@ -46,7 +48,12 @@ public: class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier { +private: + const std::function m_get_block_by_index; + public: + CZMQPublishRawBlockNotifier(std::function get_block_by_index) + : m_get_block_by_index{std::move(get_block_by_index)} {} bool NotifyBlock(const CBlockIndex *pindex) override; };