Browse Source

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.
pull/245/head
Homu 5 years ago
parent
commit
bf8ba762d6
  1. 4
      contrib/zmq/zmq_sub.py
  2. 5
      src/zmq/zmqabstractnotifier.cpp
  3. 1
      src/zmq/zmqabstractnotifier.h
  4. 22
      src/zmq/zmqnotificationinterface.cpp
  5. 2
      src/zmq/zmqnotificationinterface.h
  6. 14
      src/zmq/zmqpublishnotifier.cpp
  7. 6
      src/zmq/zmqpublishnotifier.h

4
contrib/zmq/zmq_sub.py

@ -16,6 +16,7 @@ zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashblock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "hashtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "rawblock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "rawtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, "checkedblock")
zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)
try:
@ -39,6 +40,9 @@ try:
elif topic == "rawtx":
print '- RAW TX ('+sequence+') -'
print binascii.hexlify(body)
elif topic == "checkedblock":
print '- CHECKED BLOCK ('+sequence+') -'
print binascii.hexlify(body[:80])
except KeyboardInterrupt:
zmqContext.destroy()

5
src/zmq/zmqabstractnotifier.cpp

@ -16,6 +16,11 @@ bool CZMQAbstractNotifier::NotifyBlock(const CBlockIndex * /*CBlockIndex*/)
return true;
}
bool CZMQAbstractNotifier::NotifyBlock(const CBlock &)
{
return true;
}
bool CZMQAbstractNotifier::NotifyTransaction(const CTransaction &/*transaction*/)
{
return true;

1
src/zmq/zmqabstractnotifier.h

@ -33,6 +33,7 @@ public:
virtual void Shutdown() = 0;
virtual bool NotifyBlock(const CBlockIndex *pindex);
virtual bool NotifyBlock(const CBlock& pblock);
virtual bool NotifyTransaction(const CTransaction &transaction);
protected:

22
src/zmq/zmqnotificationinterface.cpp

@ -39,6 +39,7 @@ CZMQNotificationInterface* CZMQNotificationInterface::CreateWithArguments(const
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
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)
{
@ -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)
{
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )

2
src/zmq/zmqnotificationinterface.h

@ -6,6 +6,7 @@
#define BITCOIN_ZMQ_ZMQNOTIFICATIONINTERFACE_H
#include "validationinterface.h"
#include "consensus/validation.h"
#include <string>
#include <map>
@ -26,6 +27,7 @@ protected:
// CValidationInterface
void SyncTransaction(const CTransaction &tx, const CBlock *pblock);
void UpdatedBlockTip(const CBlockIndex *pindex);
void BlockChecked(const CBlock& block, const CValidationState& state);
private:
CZMQNotificationInterface();

14
src/zmq/zmqpublishnotifier.cpp

@ -12,6 +12,7 @@ static const char *MSG_HASHBLOCK = "hashblock";
static const char *MSG_HASHTX = "hashtx";
static const char *MSG_RAWBLOCK = "rawblock";
static const char *MSG_RAWTX = "rawtx";
static const char *MSG_CHECKEDBLOCK = "checkedblock";
// Internal function to send multipart message
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());
}
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)
{
uint256 hash = transaction.GetHash();

6
src/zmq/zmqpublishnotifier.h

@ -52,4 +52,10 @@ public:
bool NotifyTransaction(const CTransaction &transaction);
};
class CZMQPublishCheckedBlockNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyBlock(const CBlock &block);
};
#endif // BITCOIN_ZMQ_ZMQPUBLISHNOTIFIER_H

Loading…
Cancel
Save