Auto merge of #3737 - gtank:zmq_checkedblock, r=str4d
zmq: add flag to publish all checked blocks This change adds a hook for the BlockChecked signal to the zmq publisher. This is useful for light wallet daemon initialization (see https://github.com/zcash/zcash/issues/3638 for context, and [lightwalletd](https://github.com/zcash-hackworks/lightwalletd) for implementation). The new flag is `-zmqpubcheckedblock=address`, in keeping with the established style.
This commit is contained in:
commit
bf8ba762d6
|
@ -16,6 +16,7 @@ zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashblock")
|
||||||
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashtx")
|
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashtx")
|
||||||
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "rawblock")
|
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "rawblock")
|
||||||
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "rawtx")
|
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "rawtx")
|
||||||
|
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "checkedblock")
|
||||||
zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)
|
zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -39,6 +40,9 @@ try:
|
||||||
elif topic == "rawtx":
|
elif topic == "rawtx":
|
||||||
print '- RAW TX ('+sequence+') -'
|
print '- RAW TX ('+sequence+') -'
|
||||||
print binascii.hexlify(body)
|
print binascii.hexlify(body)
|
||||||
|
elif topic == "checkedblock":
|
||||||
|
print '- CHECKED BLOCK ('+sequence+') -'
|
||||||
|
print binascii.hexlify(body[:80])
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
zmqContext.destroy()
|
zmqContext.destroy()
|
||||||
|
|
|
@ -16,6 +16,11 @@ bool CZMQAbstractNotifier::NotifyBlock(const CBlockIndex * /*CBlockIndex*/)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CZMQAbstractNotifier::NotifyBlock(const CBlock &)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool CZMQAbstractNotifier::NotifyTransaction(const CTransaction &/*transaction*/)
|
bool CZMQAbstractNotifier::NotifyTransaction(const CTransaction &/*transaction*/)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -33,6 +33,7 @@ public:
|
||||||
virtual void Shutdown() = 0;
|
virtual void Shutdown() = 0;
|
||||||
|
|
||||||
virtual bool NotifyBlock(const CBlockIndex *pindex);
|
virtual bool NotifyBlock(const CBlockIndex *pindex);
|
||||||
|
virtual bool NotifyBlock(const CBlock& pblock);
|
||||||
virtual bool NotifyTransaction(const CTransaction &transaction);
|
virtual bool NotifyTransaction(const CTransaction &transaction);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
|
@ -39,6 +39,7 @@ CZMQNotificationInterface* CZMQNotificationInterface::CreateWithArguments(const
|
||||||
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
|
||||||
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
|
||||||
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
|
||||||
|
factories["pubcheckedblock"] = CZMQAbstractNotifier::Create<CZMQPublishCheckedBlockNotifier>;
|
||||||
|
|
||||||
for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
|
for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
|
||||||
{
|
{
|
||||||
|
@ -141,6 +142,27 @@ void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CZMQNotificationInterface::BlockChecked(const CBlock& block, const CValidationState& state)
|
||||||
|
{
|
||||||
|
if (state.IsInvalid()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
||||||
|
{
|
||||||
|
CZMQAbstractNotifier *notifier = *i;
|
||||||
|
if (notifier->NotifyBlock(block))
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
notifier->Shutdown();
|
||||||
|
i = notifiers.erase(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CZMQNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock)
|
void CZMQNotificationInterface::SyncTransaction(const CTransaction &tx, const CBlock *pblock)
|
||||||
{
|
{
|
||||||
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#define BITCOIN_ZMQ_ZMQNOTIFICATIONINTERFACE_H
|
#define BITCOIN_ZMQ_ZMQNOTIFICATIONINTERFACE_H
|
||||||
|
|
||||||
#include "validationinterface.h"
|
#include "validationinterface.h"
|
||||||
|
#include "consensus/validation.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ protected:
|
||||||
// CValidationInterface
|
// CValidationInterface
|
||||||
void SyncTransaction(const CTransaction &tx, const CBlock *pblock);
|
void SyncTransaction(const CTransaction &tx, const CBlock *pblock);
|
||||||
void UpdatedBlockTip(const CBlockIndex *pindex);
|
void UpdatedBlockTip(const CBlockIndex *pindex);
|
||||||
|
void BlockChecked(const CBlock& block, const CValidationState& state);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
CZMQNotificationInterface();
|
CZMQNotificationInterface();
|
||||||
|
|
|
@ -12,6 +12,7 @@ static const char *MSG_HASHBLOCK = "hashblock";
|
||||||
static const char *MSG_HASHTX = "hashtx";
|
static const char *MSG_HASHTX = "hashtx";
|
||||||
static const char *MSG_RAWBLOCK = "rawblock";
|
static const char *MSG_RAWBLOCK = "rawblock";
|
||||||
static const char *MSG_RAWTX = "rawtx";
|
static const char *MSG_RAWTX = "rawtx";
|
||||||
|
static const char *MSG_CHECKEDBLOCK = "checkedblock";
|
||||||
|
|
||||||
// Internal function to send multipart message
|
// Internal function to send multipart message
|
||||||
static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
|
static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
|
||||||
|
@ -179,6 +180,19 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
|
||||||
return SendMessage(MSG_RAWBLOCK, &(*ss.begin()), ss.size());
|
return SendMessage(MSG_RAWBLOCK, &(*ss.begin()), ss.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CZMQPublishCheckedBlockNotifier::NotifyBlock(const CBlock& block)
|
||||||
|
{
|
||||||
|
LogPrint("zmq", "zmq: Publish checkedblock %s\n", block.GetHash().GetHex());
|
||||||
|
|
||||||
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
|
{
|
||||||
|
LOCK(cs_main);
|
||||||
|
ss << block;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SendMessage(MSG_CHECKEDBLOCK, &(*ss.begin()), ss.size());
|
||||||
|
}
|
||||||
|
|
||||||
bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
|
bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
|
||||||
{
|
{
|
||||||
uint256 hash = transaction.GetHash();
|
uint256 hash = transaction.GetHash();
|
||||||
|
|
|
@ -52,4 +52,10 @@ public:
|
||||||
bool NotifyTransaction(const CTransaction &transaction);
|
bool NotifyTransaction(const CTransaction &transaction);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class CZMQPublishCheckedBlockNotifier : public CZMQAbstractPublishNotifier
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool NotifyBlock(const CBlock &block);
|
||||||
|
};
|
||||||
|
|
||||||
#endif // BITCOIN_ZMQ_ZMQPUBLISHNOTIFIER_H
|
#endif // BITCOIN_ZMQ_ZMQPUBLISHNOTIFIER_H
|
||||||
|
|
Loading…
Reference in New Issue