2020-12-31 09:48:25 +01:00
|
|
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
2014-11-18 12:06:32 -05:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <zmq/zmqnotificationinterface.h>
|
|
|
|
#include <zmq/zmqpublishnotifier.h>
|
2018-07-17 12:51:23 +02:00
|
|
|
#include <zmq/zmqutil.h>
|
|
|
|
|
|
|
|
#include <zmq.h>
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <validation.h>
|
2018-10-22 15:51:11 -07:00
|
|
|
#include <util/system.h>
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2017-08-07 07:36:37 +02:00
|
|
|
CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(nullptr)
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CZMQNotificationInterface::~CZMQNotificationInterface()
|
|
|
|
{
|
2015-11-01 18:09:17 +00:00
|
|
|
Shutdown();
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
2018-06-29 16:10:01 +02:00
|
|
|
std::list<const CZMQAbstractNotifier*> CZMQNotificationInterface::GetActiveNotifiers() const
|
|
|
|
{
|
|
|
|
std::list<const CZMQAbstractNotifier*> result;
|
2020-09-01 09:40:13 +02:00
|
|
|
for (const auto& n : notifiers) {
|
|
|
|
result.push_back(n.get());
|
2018-06-29 16:10:01 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-29 18:43:29 -08:00
|
|
|
CZMQNotificationInterface* CZMQNotificationInterface::Create()
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
|
|
|
std::map<std::string, CZMQNotifierFactory> factories;
|
|
|
|
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
|
|
|
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
|
|
|
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
|
|
|
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
factories["pubsequence"] = CZMQAbstractNotifier::Create<CZMQPublishSequenceNotifier>;
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
|
2017-06-04 22:02:43 +02:00
|
|
|
for (const auto& entry : factories)
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
2017-06-04 22:02:43 +02:00
|
|
|
std::string arg("-zmq" + entry.first);
|
2020-03-10 16:29:45 +01:00
|
|
|
const auto& factory = entry.second;
|
|
|
|
for (const std::string& address : gArgs.GetArgs(arg)) {
|
2018-07-17 12:36:22 +02:00
|
|
|
std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
|
2017-06-04 22:02:43 +02:00
|
|
|
notifier->SetType(entry.first);
|
2014-11-18 12:06:32 -05:00
|
|
|
notifier->SetAddress(address);
|
2019-08-22 21:40:41 -04:00
|
|
|
notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetIntArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
|
2018-07-17 12:36:22 +02:00
|
|
|
notifiers.push_back(std::move(notifier));
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!notifiers.empty())
|
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
std::unique_ptr<CZMQNotificationInterface> notificationInterface(new CZMQNotificationInterface());
|
|
|
|
notificationInterface->notifiers = std::move(notifiers);
|
2015-11-01 18:09:17 +00:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
if (notificationInterface->Initialize()) {
|
|
|
|
return notificationInterface.release();
|
2015-11-01 18:09:17 +00:00
|
|
|
}
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
return nullptr;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called at startup to conditionally set up ZMQ socket(s)
|
|
|
|
bool CZMQNotificationInterface::Initialize()
|
|
|
|
{
|
2018-06-30 10:32:33 -04:00
|
|
|
int major = 0, minor = 0, patch = 0;
|
|
|
|
zmq_version(&major, &minor, &patch);
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: version %d.%d.%d\n", major, minor, patch);
|
|
|
|
|
2016-12-25 20:19:40 +00:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Initialize notification interface\n");
|
2014-11-18 12:06:32 -05:00
|
|
|
assert(!pcontext);
|
|
|
|
|
2018-06-30 10:32:33 -04:00
|
|
|
pcontext = zmq_ctx_new();
|
2014-11-18 12:06:32 -05:00
|
|
|
|
|
|
|
if (!pcontext)
|
|
|
|
{
|
|
|
|
zmqError("Unable to initialize context");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
|
|
|
if (notifier->Initialize(pcontext)) {
|
2018-08-24 20:42:03 -04:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
2020-09-01 09:40:13 +02:00
|
|
|
} else {
|
2018-08-24 20:42:03 -04:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
2020-09-01 09:40:13 +02:00
|
|
|
return false;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 18:09:17 +00:00
|
|
|
return true;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called during shutdown sequence
|
|
|
|
void CZMQNotificationInterface::Shutdown()
|
|
|
|
{
|
2016-12-25 20:19:40 +00:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n");
|
2014-11-18 12:06:32 -05:00
|
|
|
if (pcontext)
|
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
2018-08-24 20:42:03 -04:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
|
2014-11-18 12:06:32 -05:00
|
|
|
notifier->Shutdown();
|
|
|
|
}
|
2018-06-30 10:32:33 -04:00
|
|
|
zmq_ctx_term(pcontext);
|
2014-11-18 12:06:32 -05:00
|
|
|
|
2017-06-21 21:10:00 +02:00
|
|
|
pcontext = nullptr;
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
namespace {
|
2016-09-30 17:40:03 -04:00
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
template <typename Function>
|
|
|
|
void TryForEachAndRemoveFailed(std::list<std::unique_ptr<CZMQAbstractNotifier>>& notifiers, const Function& func)
|
|
|
|
{
|
|
|
|
for (auto i = notifiers.begin(); i != notifiers.end(); ) {
|
|
|
|
CZMQAbstractNotifier* notifier = i->get();
|
|
|
|
if (func(notifier)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
2014-11-18 12:06:32 -05:00
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:43 +02:00
|
|
|
} // 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;
|
|
|
|
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [pindexNew](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyBlock(pindexNew);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx, uint64_t mempool_sequence)
|
2014-11-18 12:06:32 -05:00
|
|
|
{
|
2017-03-29 21:12:42 -04:00
|
|
|
const CTransaction& tx = *ptx;
|
|
|
|
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransaction(tx) && notifier->NotifyTransactionAcceptance(tx, mempool_sequence);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void CZMQNotificationInterface::TransactionRemovedFromMempool(const CTransactionRef& ptx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
|
|
|
|
{
|
|
|
|
// Called for all non-block inclusion reasons
|
|
|
|
const CTransaction& tx = *ptx;
|
|
|
|
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx, mempool_sequence](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransactionRemoval(tx, mempool_sequence);
|
2020-09-01 09:40:43 +02:00
|
|
|
});
|
2014-11-18 12:06:32 -05:00
|
|
|
}
|
2017-03-29 21:12:42 -04:00
|
|
|
|
2019-11-11 10:43:34 -05:00
|
|
|
void CZMQNotificationInterface::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected)
|
2017-03-29 21:12:42 -04:00
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
const CTransaction& tx = *ptx;
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransaction(tx);
|
|
|
|
});
|
2017-03-29 21:12:42 -04:00
|
|
|
}
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
|
|
|
|
// Next we notify BlockConnect listeners for *all* blocks
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [pindexConnected](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyBlockConnect(pindexConnected);
|
|
|
|
});
|
2017-03-29 21:12:42 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 15:41:41 -04:00
|
|
|
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
|
2017-03-29 21:12:42 -04:00
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
const CTransaction& tx = *ptx;
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransaction(tx);
|
|
|
|
});
|
2017-03-29 21:12:42 -04:00
|
|
|
}
|
Add 'sequence' zmq publisher to track all block (dis)connects, mempool deltas
Using the zmq notifications to avoid excessive mempool polling can be difficult
given the current notifications available. It announces all transactions
being added to mempool or included in blocks, but announces no evictions
and gives no indication if the transaction is in the mempool or a block.
Block notifications for zmq are also substandard, in that it only announces
block tips, while all block transactions are still announced.
This commit adds a unified stream which can be used to closely track mempool:
1) getrawmempool to fill out mempool knowledge
2) if txhash is announced, add or remove from set
based on add/remove flag
3) if blockhash is announced, get block txn list,
remove from those transactions local view of mempool
4) if we drop a sequence number, go to (1)
The mempool sequence number starts at the value 1, and
increments each time a transaction enters the mempool,
or is evicted from the mempool for any reason, including
block inclusion. The mempool sequence number is published
via ZMQ for any transaction-related notification.
These features allow for ZMQ/RPC consumer to track mempool
state in a more exacting way, without unnecesarily polling
getrawmempool. See interface_zmq.py::test_mempool_sync for
example usage.
2020-09-04 11:55:58 -04:00
|
|
|
|
|
|
|
// Next we notify BlockDisconnect listeners for *all* blocks
|
|
|
|
TryForEachAndRemoveFailed(notifiers, [pindexDisconnected](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyBlockDisconnect(pindexDisconnected);
|
|
|
|
});
|
2017-03-29 21:12:42 -04:00
|
|
|
}
|
2018-06-29 15:16:31 +02:00
|
|
|
|
|
|
|
CZMQNotificationInterface* g_zmq_notification_interface = nullptr;
|