Skip to content

Commit cfbb212

Browse files
committed
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.
1 parent 8ed4ff8 commit cfbb212

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

src/init.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1424,7 +1424,11 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
14241424
}
14251425

14261426
#if ENABLE_ZMQ
1427-
g_zmq_notification_interface = CZMQNotificationInterface::Create();
1427+
g_zmq_notification_interface = CZMQNotificationInterface::Create(
1428+
[&chainman = node.chainman](CBlock& block, const CBlockIndex& index) {
1429+
assert(chainman);
1430+
return node::ReadBlockFromDisk(block, &index, chainman->GetConsensus());
1431+
});
14281432

14291433
if (g_zmq_notification_interface) {
14301434
RegisterValidationInterface(g_zmq_notification_interface.get());

src/zmq/zmqabstractnotifier.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
#define BITCOIN_ZMQ_ZMQABSTRACTNOTIFIER_H
77

88
#include <cstdint>
9+
#include <functional>
910
#include <memory>
1011
#include <string>
1112

1213
class CBlockIndex;
1314
class CTransaction;
1415
class CZMQAbstractNotifier;
1516

16-
using CZMQNotifierFactory = std::unique_ptr<CZMQAbstractNotifier> (*)();
17+
using CZMQNotifierFactory = std::function<std::unique_ptr<CZMQAbstractNotifier>()>;
1718

1819
class CZMQAbstractNotifier
1920
{

src/zmq/zmqnotificationinterface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotif
3939
return result;
4040
}
4141

42-
std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create()
42+
std::unique_ptr<CZMQNotificationInterface> CZMQNotificationInterface::Create(std::function<bool(CBlock&, const CBlockIndex&)> get_block_by_index)
4343
{
4444
std::map<std::string, CZMQNotifierFactory> factories;
4545
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
4646
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
47-
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
47+
factories["pubrawblock"] = [&get_block_by_index]() -> std::unique_ptr<CZMQAbstractNotifier> {
48+
return std::make_unique<CZMQPublishRawBlockNotifier>(get_block_by_index);
49+
};
4850
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
4951
factories["pubsequence"] = CZMQAbstractNotifier::Create<CZMQPublishSequenceNotifier>;
5052

src/zmq/zmqnotificationinterface.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <validationinterface.h>
1010

1111
#include <cstdint>
12+
#include <functional>
1213
#include <list>
1314
#include <memory>
1415

@@ -23,7 +24,7 @@ class CZMQNotificationInterface final : public CValidationInterface
2324

2425
std::list<const CZMQAbstractNotifier*> GetActiveNotifiers() const;
2526

26-
static std::unique_ptr<CZMQNotificationInterface> Create();
27+
static std::unique_ptr<CZMQNotificationInterface> Create(std::function<bool(CBlock&, const CBlockIndex&)> get_block_by_index);
2728

2829
protected:
2930
bool Initialize();

src/zmq/zmqpublishnotifier.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ namespace Consensus {
3939
struct Params;
4040
}
4141

42-
using node::ReadBlockFromDisk;
43-
4442
static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifiers;
4543

4644
static const char *MSG_HASHBLOCK = "hashblock";
@@ -247,10 +245,9 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
247245
{
248246
LogPrint(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address);
249247

250-
const Consensus::Params& consensusParams = Params().GetConsensus();
251248
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
252249
CBlock block;
253-
if (!ReadBlockFromDisk(block, pindex, consensusParams)) {
250+
if (!m_get_block_by_index(block, *pindex)) {
254251
zmqError("Can't read block from disk");
255252
return false;
256253
}

src/zmq/zmqpublishnotifier.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
#include <cstddef>
1111
#include <cstdint>
12+
#include <functional>
1213

14+
class CBlock;
1315
class CBlockIndex;
1416
class CTransaction;
1517

@@ -46,7 +48,12 @@ class CZMQPublishHashTransactionNotifier : public CZMQAbstractPublishNotifier
4648

4749
class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier
4850
{
51+
private:
52+
const std::function<bool(CBlock&, const CBlockIndex&)> m_get_block_by_index;
53+
4954
public:
55+
CZMQPublishRawBlockNotifier(std::function<bool(CBlock&, const CBlockIndex&)> get_block_by_index)
56+
: m_get_block_by_index{std::move(get_block_by_index)} {}
5057
bool NotifyBlock(const CBlockIndex *pindex) override;
5158
};
5259

0 commit comments

Comments
 (0)