2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2015-2017 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.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <chain.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <zmq/zmqpublishnotifier.h>
|
|
|
|
#include <validation.h>
|
|
|
|
#include <util.h>
|
|
|
|
#include <rpc/server.h>
|
2014-11-18 18:06:32 +01:00
|
|
|
|
|
|
|
static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifiers;
|
|
|
|
|
2016-03-29 11:34:25 +02:00
|
|
|
static const char *MSG_HASHBLOCK = "hashblock";
|
|
|
|
static const char *MSG_HASHTX = "hashtx";
|
|
|
|
static const char *MSG_RAWBLOCK = "rawblock";
|
|
|
|
static const char *MSG_RAWTX = "rawtx";
|
|
|
|
|
2014-11-18 18:06:32 +01:00
|
|
|
// Internal function to send multipart message
|
|
|
|
static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, size);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
zmq_msg_t msg;
|
|
|
|
|
|
|
|
int rc = zmq_msg_init_size(&msg, size);
|
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
zmqError("Unable to initialize ZMQ msg");
|
2017-03-22 20:56:44 +01:00
|
|
|
va_end(args);
|
2014-11-18 18:06:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *buf = zmq_msg_data(&msg);
|
|
|
|
memcpy(buf, data, size);
|
|
|
|
|
|
|
|
data = va_arg(args, const void*);
|
|
|
|
|
|
|
|
rc = zmq_msg_send(&msg, sock, data ? ZMQ_SNDMORE : 0);
|
|
|
|
if (rc == -1)
|
|
|
|
{
|
|
|
|
zmqError("Unable to send ZMQ msg");
|
|
|
|
zmq_msg_close(&msg);
|
2017-03-22 20:56:44 +01:00
|
|
|
va_end(args);
|
2014-11-18 18:06:32 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
zmq_msg_close(&msg);
|
|
|
|
|
|
|
|
if (!data)
|
|
|
|
break;
|
|
|
|
|
|
|
|
size = va_arg(args, size_t);
|
|
|
|
}
|
2017-03-22 20:56:44 +01:00
|
|
|
va_end(args);
|
2014-11-18 18:06:32 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CZMQAbstractPublishNotifier::Initialize(void *pcontext)
|
|
|
|
{
|
|
|
|
assert(!psocket);
|
|
|
|
|
|
|
|
// check if address is being used by other publish notifier
|
|
|
|
std::multimap<std::string, CZMQAbstractPublishNotifier*>::iterator i = mapPublishNotifiers.find(address);
|
|
|
|
|
|
|
|
if (i==mapPublishNotifiers.end())
|
|
|
|
{
|
|
|
|
psocket = zmq_socket(pcontext, ZMQ_PUB);
|
|
|
|
if (!psocket)
|
|
|
|
{
|
|
|
|
zmqError("Failed to create socket");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rc = zmq_bind(psocket, address.c_str());
|
|
|
|
if (rc!=0)
|
|
|
|
{
|
|
|
|
zmqError("Failed to bind address");
|
2016-02-29 19:34:09 +01:00
|
|
|
zmq_close(psocket);
|
2014-11-18 18:06:32 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// register this notifier for the address, so it can be reused for other publish notifier
|
|
|
|
mapPublishNotifiers.insert(std::make_pair(address, this));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Reusing socket for address %s\n", address);
|
2014-11-18 18:06:32 +01:00
|
|
|
|
|
|
|
psocket = i->second->psocket;
|
|
|
|
mapPublishNotifiers.insert(std::make_pair(address, this));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CZMQAbstractPublishNotifier::Shutdown()
|
|
|
|
{
|
|
|
|
assert(psocket);
|
|
|
|
|
|
|
|
int count = mapPublishNotifiers.count(address);
|
|
|
|
|
|
|
|
// remove this notifier from the list of publishers using this address
|
|
|
|
typedef std::multimap<std::string, CZMQAbstractPublishNotifier*>::iterator iterator;
|
|
|
|
std::pair<iterator, iterator> iterpair = mapPublishNotifiers.equal_range(address);
|
|
|
|
|
|
|
|
for (iterator it = iterpair.first; it != iterpair.second; ++it)
|
|
|
|
{
|
|
|
|
if (it->second==this)
|
|
|
|
{
|
|
|
|
mapPublishNotifiers.erase(it);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count == 1)
|
|
|
|
{
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, "Close socket at address %s\n", address);
|
2014-11-18 18:06:32 +01:00
|
|
|
int linger = 0;
|
|
|
|
zmq_setsockopt(psocket, ZMQ_LINGER, &linger, sizeof(linger));
|
|
|
|
zmq_close(psocket);
|
|
|
|
}
|
|
|
|
|
2017-06-21 21:10:00 +02:00
|
|
|
psocket = nullptr;
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 14:30:02 +02:00
|
|
|
bool CZMQAbstractPublishNotifier::SendMessage(const char *command, const void* data, size_t size)
|
|
|
|
{
|
|
|
|
assert(psocket);
|
|
|
|
|
|
|
|
/* send three parts, command & data & a LE 4byte sequence number */
|
|
|
|
unsigned char msgseq[sizeof(uint32_t)];
|
|
|
|
WriteLE32(&msgseq[0], nSequence);
|
2017-06-21 21:10:00 +02:00
|
|
|
int rc = zmq_send_multipart(psocket, command, strlen(command), data, size, msgseq, (size_t)sizeof(uint32_t), nullptr);
|
2016-03-29 14:30:02 +02:00
|
|
|
if (rc == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* increment memory only sequence number after sending */
|
|
|
|
nSequence++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-16 16:42:23 +02:00
|
|
|
bool CZMQPublishHashBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2015-09-16 16:42:23 +02:00
|
|
|
uint256 hash = pindex->GetBlockHash();
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Publish hashblock %s\n", hash.GetHex());
|
2014-11-18 18:06:32 +01:00
|
|
|
char data[32];
|
|
|
|
for (unsigned int i = 0; i < 32; i++)
|
|
|
|
data[31 - i] = hash.begin()[i];
|
2016-03-29 14:30:02 +02:00
|
|
|
return SendMessage(MSG_HASHBLOCK, data, 32);
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CZMQPublishHashTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
|
|
|
|
{
|
|
|
|
uint256 hash = transaction.GetHash();
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Publish hashtx %s\n", hash.GetHex());
|
2014-11-18 18:06:32 +01:00
|
|
|
char data[32];
|
|
|
|
for (unsigned int i = 0; i < 32; i++)
|
|
|
|
data[31 - i] = hash.begin()[i];
|
2016-03-29 14:30:02 +02:00
|
|
|
return SendMessage(MSG_HASHTX, data, 32);
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
2015-09-16 16:42:23 +02:00
|
|
|
bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Publish rawblock %s\n", pindex->GetBlockHash().GetHex());
|
2014-11-18 18:06:32 +01:00
|
|
|
|
2015-04-17 14:19:21 +02:00
|
|
|
const Consensus::Params& consensusParams = Params().GetConsensus();
|
2016-11-22 21:47:07 +01:00
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
CBlock block;
|
2015-04-17 14:19:21 +02:00
|
|
|
if(!ReadBlockFromDisk(block, pindex, consensusParams))
|
2014-11-18 18:06:32 +01:00
|
|
|
{
|
|
|
|
zmqError("Can't read block from disk");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << block;
|
|
|
|
}
|
|
|
|
|
2016-03-29 14:30:02 +02:00
|
|
|
return SendMessage(MSG_RAWBLOCK, &(*ss.begin()), ss.size());
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
|
|
|
|
{
|
|
|
|
uint256 hash = transaction.GetHash();
|
2016-12-25 21:19:40 +01:00
|
|
|
LogPrint(BCLog::ZMQ, "zmq: Publish rawtx %s\n", hash.GetHex());
|
2016-11-22 21:47:07 +01:00
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags());
|
2014-11-18 18:06:32 +01:00
|
|
|
ss << transaction;
|
2016-03-29 14:30:02 +02:00
|
|
|
return SendMessage(MSG_RAWTX, &(*ss.begin()), ss.size());
|
2014-11-18 18:06:32 +01:00
|
|
|
}
|