2018-07-09 17:05:14 +02:00
|
|
|
// Copyright (c) 2015-2018 The Bitcoin Core developers
|
2014-11-18 18:06:32 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <zmq/zmqnotificationinterface.h>
|
|
|
|
#include <zmq/zmqpublishnotifier.h>
|
2020-09-01 09:40:13 +02:00
|
|
|
#include <zmq/zmqutil.h>
|
|
|
|
|
|
|
|
#include <zmq.h>
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <validation.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/system.h>
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2019-08-06 05:08:33 +02:00
|
|
|
CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(nullptr)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CZMQNotificationInterface::~CZMQNotificationInterface()
|
|
|
|
{
|
2015-11-01 19:09:17 +01:00
|
|
|
Shutdown();
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 17:05:14 +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-07-09 17:05:14 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-12-27 19:11:21 +01:00
|
|
|
CZMQNotificationInterface* CZMQNotificationInterface::Create()
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
std::map<std::string, CZMQNotifierFactory> factories;
|
|
|
|
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
|
2019-05-08 11:12:54 +02:00
|
|
|
factories["pubhashchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashChainLockNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
2016-07-15 08:38:33 +02:00
|
|
|
factories["pubhashtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionLockNotifier>;
|
2018-07-12 11:06:30 +02:00
|
|
|
factories["pubhashgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceVoteNotifier>;
|
|
|
|
factories["pubhashgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceObjectNotifier>;
|
2018-09-12 13:12:44 +02:00
|
|
|
factories["pubhashinstantsenddoublespend"] = CZMQAbstractNotifier::Create<CZMQPublishHashInstantSendDoubleSpendNotifier>;
|
2020-11-17 20:41:30 +01:00
|
|
|
factories["pubhashrecoveredsig"] = CZMQAbstractNotifier::Create<CZMQPublishHashRecoveredSigNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
2019-05-08 11:12:54 +02:00
|
|
|
factories["pubrawchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockNotifier>;
|
2019-05-23 11:13:58 +02:00
|
|
|
factories["pubrawchainlocksig"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockSigNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
2016-07-15 08:38:33 +02:00
|
|
|
factories["pubrawtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockNotifier>;
|
2019-05-23 11:13:58 +02:00
|
|
|
factories["pubrawtxlocksig"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockSigNotifier>;
|
2018-07-12 11:06:30 +02:00
|
|
|
factories["pubrawgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceVoteNotifier>;
|
|
|
|
factories["pubrawgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceObjectNotifier>;
|
2018-09-12 13:12:44 +02:00
|
|
|
factories["pubrawinstantsenddoublespend"] = CZMQAbstractNotifier::Create<CZMQPublishRawInstantSendDoubleSpendNotifier>;
|
2020-11-17 20:41:30 +01:00
|
|
|
factories["pubrawrecoveredsig"] = CZMQAbstractNotifier::Create<CZMQPublishRawRecoveredSigNotifier>;
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
std::list<std::unique_ptr<CZMQAbstractNotifier>> notifiers;
|
2017-11-30 23:09:44 +01:00
|
|
|
for (const auto& entry : factories)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2017-11-30 23:09:44 +01:00
|
|
|
std::string arg("-zmq" + entry.first);
|
2019-06-24 18:44:27 +02:00
|
|
|
if (gArgs.IsArgSet(arg))
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
const auto& factory = entry.second;
|
|
|
|
const std::string address = gArgs.GetArg(arg, "");
|
|
|
|
std::unique_ptr<CZMQAbstractNotifier> notifier = factory();
|
2017-11-30 23:09:44 +01:00
|
|
|
notifier->SetType(entry.first);
|
2014-11-18 18:06:32 +01:00
|
|
|
notifier->SetAddress(address);
|
2021-09-08 18:39:06 +02:00
|
|
|
notifier->SetOutboundMessageHighWaterMark(static_cast<int>(gArgs.GetArg(arg + "hwm", CZMQAbstractNotifier::DEFAULT_ZMQ_SNDHWM)));
|
2020-09-01 09:40:13 +02:00
|
|
|
notifiers.push_back(std::move(notifier));
|
2014-11-18 18:06:32 +01: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 19:09:17 +01:00
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
if (notificationInterface->Initialize()) {
|
|
|
|
return notificationInterface.release();
|
2015-11-01 19:09:17 +01:00
|
|
|
}
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
return nullptr;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called at startup to conditionally set up ZMQ socket(s)
|
|
|
|
bool CZMQNotificationInterface::Initialize()
|
|
|
|
{
|
Merge #13578: [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (#4397)
f1bd03eb013b96ff040a8f835e4137fbd2a38cda [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (mruddy)
Pull request description:
Upgrade the ZeroMQ dependency from version 4.2.3 to the latest stable version 4.2.5.
This PR Follows the lead of https://github.com/bitcoin/bitcoin/pull/11986.
I upgraded both patch files to correspond to the version `4.2.5` libzmq files.
I assume doing so is still necessary and correct.
Without updating the patch line numbers, things appear to work, but you get extra log messages while building `depends` because things don't exactly match, e.g.:
```
/bitcoin/depends> make zeromq
Extracting zeromq...
/bitcoin/depends/sources/zeromq-4.2.5.tar.gz: OK
Preprocessing zeromq...
patching file src/windows.hpp
Hunk #1 succeeded at 58 (offset 3 lines).
patching file src/thread.cpp
Hunk #1 succeeded at 307 with fuzz 2 (offset 87 lines).
Hunk #2 succeeded at 323 with fuzz 2 (offset 90 lines).
```
Updating the patches seemed cleaner, so I did it. Note that libzmq had some whitespace changes, so that's why the updated patches do too.
More info: https://github.com/zeromq/libzmq/releases/tag/v4.2.5
tags: libzmq, zmq, 0mq
Tree-SHA512: 78659dd276b5311e40634b1bbebb802ddd6b69662ba3c84995ef1e3795c49a78b1635112c7fd72a405ea36e2cc3bdeb84e6d00d4e491a349bba1dafff50e2fa5
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
2021-09-08 18:25:20 +02: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);
|
|
|
|
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Initialize notification interface\n");
|
2014-11-18 18:06:32 +01:00
|
|
|
assert(!pcontext);
|
|
|
|
|
Merge #13578: [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (#4397)
f1bd03eb013b96ff040a8f835e4137fbd2a38cda [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (mruddy)
Pull request description:
Upgrade the ZeroMQ dependency from version 4.2.3 to the latest stable version 4.2.5.
This PR Follows the lead of https://github.com/bitcoin/bitcoin/pull/11986.
I upgraded both patch files to correspond to the version `4.2.5` libzmq files.
I assume doing so is still necessary and correct.
Without updating the patch line numbers, things appear to work, but you get extra log messages while building `depends` because things don't exactly match, e.g.:
```
/bitcoin/depends> make zeromq
Extracting zeromq...
/bitcoin/depends/sources/zeromq-4.2.5.tar.gz: OK
Preprocessing zeromq...
patching file src/windows.hpp
Hunk #1 succeeded at 58 (offset 3 lines).
patching file src/thread.cpp
Hunk #1 succeeded at 307 with fuzz 2 (offset 87 lines).
Hunk #2 succeeded at 323 with fuzz 2 (offset 90 lines).
```
Updating the patches seemed cleaner, so I did it. Note that libzmq had some whitespace changes, so that's why the updated patches do too.
More info: https://github.com/zeromq/libzmq/releases/tag/v4.2.5
tags: libzmq, zmq, 0mq
Tree-SHA512: 78659dd276b5311e40634b1bbebb802ddd6b69662ba3c84995ef1e3795c49a78b1635112c7fd72a405ea36e2cc3bdeb84e6d00d4e491a349bba1dafff50e2fa5
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
2021-09-08 18:25:20 +02:00
|
|
|
pcontext = zmq_ctx_new();
|
2014-11-18 18:06:32 +01: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)) {
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
} else {
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
|
|
|
|
return false;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:09:17 +01:00
|
|
|
return true;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called during shutdown sequence
|
|
|
|
void CZMQNotificationInterface::Shutdown()
|
|
|
|
{
|
2019-05-22 23:51:39 +02:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Shutdown notification interface\n");
|
2014-11-18 18:06:32 +01:00
|
|
|
if (pcontext)
|
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto& notifier : notifiers) {
|
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
|
2014-11-18 18:06:32 +01:00
|
|
|
notifier->Shutdown();
|
|
|
|
}
|
Merge #13578: [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (#4397)
f1bd03eb013b96ff040a8f835e4137fbd2a38cda [depends, zmq, doc] upgrade zeromq to 4.2.5 and avoid deprecated zeromq api functions (mruddy)
Pull request description:
Upgrade the ZeroMQ dependency from version 4.2.3 to the latest stable version 4.2.5.
This PR Follows the lead of https://github.com/bitcoin/bitcoin/pull/11986.
I upgraded both patch files to correspond to the version `4.2.5` libzmq files.
I assume doing so is still necessary and correct.
Without updating the patch line numbers, things appear to work, but you get extra log messages while building `depends` because things don't exactly match, e.g.:
```
/bitcoin/depends> make zeromq
Extracting zeromq...
/bitcoin/depends/sources/zeromq-4.2.5.tar.gz: OK
Preprocessing zeromq...
patching file src/windows.hpp
Hunk #1 succeeded at 58 (offset 3 lines).
patching file src/thread.cpp
Hunk #1 succeeded at 307 with fuzz 2 (offset 87 lines).
Hunk #2 succeeded at 323 with fuzz 2 (offset 90 lines).
```
Updating the patches seemed cleaner, so I did it. Note that libzmq had some whitespace changes, so that's why the updated patches do too.
More info: https://github.com/zeromq/libzmq/releases/tag/v4.2.5
tags: libzmq, zmq, 0mq
Tree-SHA512: 78659dd276b5311e40634b1bbebb802ddd6b69662ba3c84995ef1e3795c49a78b1635112c7fd72a405ea36e2cc3bdeb84e6d00d4e491a349bba1dafff50e2fa5
Co-authored-by: Wladimir J. van der Laan <laanwj@gmail.com>
2021-09-08 18:25:20 +02:00
|
|
|
zmq_ctx_term(pcontext);
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2017-08-16 15:54:51 +02:00
|
|
|
pcontext = nullptr;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
namespace {
|
|
|
|
template <typename Function>
|
|
|
|
void TryForEachAndRemoveFailed(std::list<std::unique_ptr<CZMQAbstractNotifier>>& notifiers, const Function& func)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
for (auto i = notifiers.begin(); i != notifiers.end(); ) {
|
|
|
|
CZMQAbstractNotifier* notifier = i->get();
|
|
|
|
if (func(notifier)) {
|
|
|
|
++i;
|
|
|
|
} else {
|
2014-11-18 18:06:32 +01:00
|
|
|
notifier->Shutdown();
|
|
|
|
i = notifiers.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 09:40:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyChainLock(const CBlockIndex *pindex, const std::shared_ptr<const llmq::CChainLockSig>& clsig)
|
2019-05-08 11:12:54 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [pindex, &clsig](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyChainLock(pindex, clsig);
|
|
|
|
});
|
2019-05-08 11:12:54 +02:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:56:17 +01:00
|
|
|
void CZMQNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx, int64_t nAcceptTime)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2017-04-10 21:06:42 +02:00
|
|
|
// Used by BlockConnected and BlockDisconnected as well, because they're
|
|
|
|
// all the same external callback.
|
|
|
|
const CTransaction& tx = *ptx;
|
|
|
|
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransaction(tx);
|
|
|
|
});
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
2016-07-15 08:38:33 +02:00
|
|
|
|
2017-04-10 21:06:42 +02:00
|
|
|
void CZMQNotificationInterface::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected, const std::vector<CTransactionRef>& vtxConflicted)
|
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
|
|
|
// Do a normal notify for each transaction added in the block
|
2019-12-07 11:56:17 +01:00
|
|
|
TransactionAddedToMempool(ptx, 0);
|
2017-04-10 21:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-27 14:55:29 +02:00
|
|
|
void CZMQNotificationInterface::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
|
2017-04-10 21:06:42 +02:00
|
|
|
{
|
|
|
|
for (const CTransactionRef& ptx : pblock->vtx) {
|
|
|
|
// Do a normal notify for each transaction removed in block disconnection
|
2019-12-07 11:56:17 +01:00
|
|
|
TransactionAddedToMempool(ptx, 0);
|
2017-04-10 21:06:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyTransactionLock(const CTransactionRef& tx, const std::shared_ptr<const llmq::CInstantSendLock>& islock)
|
2016-07-15 08:38:33 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&tx, &islock](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyTransactionLock(tx, islock);
|
|
|
|
});
|
2016-07-15 08:38:33 +02:00
|
|
|
}
|
2018-07-12 11:06:30 +02:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyGovernanceVote(const std::shared_ptr<const CGovernanceVote> &vote)
|
2018-07-12 11:06:30 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&vote](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyGovernanceVote(vote);
|
|
|
|
});
|
2018-07-12 11:06:30 +02:00
|
|
|
}
|
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyGovernanceObject(const std::shared_ptr<const CGovernanceObject> &object)
|
2018-07-12 11:06:30 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&object](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyGovernanceObject(object);
|
|
|
|
});
|
2018-07-12 11:06:30 +02:00
|
|
|
}
|
2018-09-12 13:12:44 +02:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyInstantSendDoubleSpendAttempt(const CTransactionRef& currentTx, const CTransactionRef& previousTx)
|
2018-09-12 13:12:44 +02:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [¤tTx, &previousTx](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyInstantSendDoubleSpendAttempt(currentTx, previousTx);
|
|
|
|
});
|
2018-09-12 13:12:44 +02:00
|
|
|
}
|
2018-07-09 17:05:14 +02:00
|
|
|
|
2021-01-22 05:32:15 +01:00
|
|
|
void CZMQNotificationInterface::NotifyRecoveredSig(const std::shared_ptr<const llmq::CRecoveredSig>& sig)
|
2020-11-17 20:41:30 +01:00
|
|
|
{
|
2020-09-01 09:40:13 +02:00
|
|
|
TryForEachAndRemoveFailed(notifiers, [&sig](CZMQAbstractNotifier* notifier) {
|
|
|
|
return notifier->NotifyRecoveredSig(sig);
|
|
|
|
});
|
2020-11-17 20:41:30 +01:00
|
|
|
}
|
|
|
|
|
2018-07-09 17:05:14 +02:00
|
|
|
CZMQNotificationInterface* g_zmq_notification_interface = nullptr;
|