Skip to content

Commit

Permalink
Implement Governance ZMQ notification messages (#2160)
Browse files Browse the repository at this point in the history
* fix whitespace

* added zmq stuff for governance objects and votes
it seems that zmq is currently not in a working state, need to figure that out.

Need to:
plug in the new methods added
possibly plug in the old methods, as it doesn't look like they are.

* formatting fix. Will probably need to revert for this PR

* continue linking new zmq messages in

* added comment, might need to revert

* fixes error of it not knowing about the classes

* Actually link in, all new govobjects and govvotes should be broadcast over zmq now.

* fix compile error, forgot to change params when copying

* fix compile error

* add imports to the header files in zmqconfig.h

* fixing linking(i think) error

* Revert "added comment, might need to revert"

This reverts commit 2918ea4.

* Revert "formatting fix. Will probably need to revert for this PR"

This reverts commit ca10558.

* fix tabs etc

* EOL added

* optimization of hash.begin() @nmarley thoughts?

* remove formatting changes

* iterator i -> it and removal of notifier

* typo in df879f5

* use auto for the iterators

* introduce hash prefix

* implement changes in zmq_sub.py, update the doc, and change argument name to fix typo

* deref iterators before calling methods

* continued e8a4c50

* missed one... continued e8a4c50

* killing some tabs

* fix spacing for setting or comparing iterators

* change order of new variables to match current setup

* re-add elif's I didn't realize got removed

* Revert "fix spacing for setting or comparing iterators"

This reverts commit 8ce2068.

* Revert "use auto for the iterators"

This reverts commit cb16cf0.

* Revert "missed one... continued e8a4c50"

This reverts commit 2087cf8.

* Revert "continued e8a4c50"

This reverts commit a78c8ad.

* Revert "deref iterators before calling methods"

This reverts commit e8a4c50.

* Revert "iterator i -> it and removal of notifier"

This reverts commit 2957424.

* Revert "fix whitespace"

This reverts commit 612be48.

* Revert "typo in df879f5"

* Revert "Optimization of hash.begin()"

* fixes problem with revert

* Udjin complain's a lot ;)

* help text, init.cpp

* add signals in validationinterface.cpp

* Change location of vote notification call.

* remain consistent

* remove unneeded include due to change of notification location

* implement raw notifications
  • Loading branch information
PastaPastaPasta authored and UdjinM6 committed Jul 12, 2018
1 parent 448e92f commit 0e68934
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 1 deletion.
17 changes: 17 additions & 0 deletions contrib/zmq/zmq_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtxlock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashgovernancevote")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashgovernanceobject")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawblock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawtxlock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawgovernancevote")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawgovernanceobject")
zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)

try:
Expand Down Expand Up @@ -48,6 +52,19 @@
elif topic == "rawtxlock":
print('- RAW TX LOCK ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "hashgovernancevote":
print('- HASH GOVERNANCE VOTE ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "hashgovernanceobject":
print('- HASH GOVERNANCE OBJECT ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "rawgovernancevote":
print('- RAW GOVERNANCE VOTE ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))
elif topic == "rawgovernanceobject":
print('- RAW GOVERNANCE OBJECT ('+sequence+') -')
print(binascii.hexlify(body).decode("utf-8"))


except KeyboardInterrupt:
zmqContext.destroy()
4 changes: 4 additions & 0 deletions doc/zmq.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Currently, the following notifications are supported:
-zmqpubrawblock=address
-zmqpubrawtx=address
-zmqpubrawtxlock=address
-zmqpubhashgovernancevote=address
-zmqpubhashgovernanceobject=address
-zmqpubrawgovernancevote=address
-zmqpubhashgovernanceobject=address

The socket type is PUB and the address must be a valid ZeroMQ socket
address. The same address can be used in more than one notification.
Expand Down
8 changes: 7 additions & 1 deletion src/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "consensus/validation.h"
#include "validationinterface.h"
#include "governance.h"
#include "governance-object.h"
#include "governance-validators.h"
Expand Down Expand Up @@ -264,7 +265,8 @@ void CGovernanceManager::ProcessMessage(CNode* pfrom, const std::string& strComm
}
return;
}

// SEND NOTIFICATION TO SCRIPT/ZMQ
GetMainSignals().NotifyGovernanceVote(vote);
}
}

Expand Down Expand Up @@ -361,6 +363,10 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman
CGovernanceException exception;
CheckOrphanVotes(govobj, exception, connman);

// SEND NOTIFICATION TO SCRIPT/ZMQ
GetMainSignals().NotifyGovernanceObject(govobj);


DBG( std::cout << "CGovernanceManager::AddGovernanceObject END" << std::endl; );
}

Expand Down
2 changes: 2 additions & 0 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,8 @@ std::string HelpMessage(HelpMessageMode mode)
strUsage += HelpMessageOpt("-zmqpubhashblock=<address>", _("Enable publish hash block in <address>"));
strUsage += HelpMessageOpt("-zmqpubhashtx=<address>", _("Enable publish hash transaction in <address>"));
strUsage += HelpMessageOpt("-zmqpubhashtxlock=<address>", _("Enable publish hash transaction (locked via InstantSend) in <address>"));
strUsage += HelpMessageOpt("-zmqpubhashgovernancevote=<address>", _("Enable publish hash of governance votes in <address>"));
strUsage += HelpMessageOpt("-zmqpubhashgovernanceobject=<address>", _("Enable publish hash of governance objects (like proposals) in <address>"));
strUsage += HelpMessageOpt("-zmqpubrawblock=<address>", _("Enable publish raw block in <address>"));
strUsage += HelpMessageOpt("-zmqpubrawtx=<address>", _("Enable publish raw transaction in <address>"));
strUsage += HelpMessageOpt("-zmqpubrawtxlock=<address>", _("Enable publish raw transaction (locked via InstantSend) in <address>"));
Expand Down
6 changes: 6 additions & 0 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
g_signals.ScriptForMining.connect(boost::bind(&CValidationInterface::GetScriptForMining, pwalletIn, _1));
g_signals.BlockFound.connect(boost::bind(&CValidationInterface::ResetRequestCount, pwalletIn, _1));
g_signals.NewPoWValidBlock.connect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
g_signals.NotifyGovernanceObject.connect(boost::bind(&CValidationInterface::NotifyGovernanceObject, pwalletIn, _1));
g_signals.NotifyGovernanceVote.connect(boost::bind(&CValidationInterface::NotifyGovernanceVote, pwalletIn, _1));
}

void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
Expand All @@ -42,6 +44,8 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
g_signals.NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
g_signals.NotifyHeaderTip.disconnect(boost::bind(&CValidationInterface::NotifyHeaderTip, pwalletIn, _1, _2));
g_signals.AcceptedBlockHeader.disconnect(boost::bind(&CValidationInterface::AcceptedBlockHeader, pwalletIn, _1));
g_signals.NotifyGovernanceObject.disconnect(boost::bind(&CValidationInterface::NotifyGovernanceObject, pwalletIn, _1));
g_signals.NotifyGovernanceVote.disconnect(boost::bind(&CValidationInterface::NotifyGovernanceVote, pwalletIn, _1));
}

void UnregisterAllValidationInterfaces() {
Expand All @@ -58,4 +62,6 @@ void UnregisterAllValidationInterfaces() {
g_signals.NewPoWValidBlock.disconnect_all_slots();
g_signals.NotifyHeaderTip.disconnect_all_slots();
g_signals.AcceptedBlockHeader.disconnect_all_slots();
g_signals.NotifyGovernanceObject.disconnect_all_slots();
g_signals.NotifyGovernanceVote.disconnect_all_slots();
}
8 changes: 8 additions & 0 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class CReserveScript;
class CTransaction;
class CValidationInterface;
class CValidationState;
class CGovernanceVote;
class CGovernanceObject;
class uint256;

// These functions dispatch to one or all registered wallets
Expand All @@ -36,6 +38,8 @@ class CValidationInterface {
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
virtual void SyncTransaction(const CTransaction &tx, const CBlockIndex *pindex, int posInBlock) {}
virtual void NotifyTransactionLock(const CTransaction &tx) {}
virtual void NotifyGovernanceVote(const CGovernanceVote &vote) {}
virtual void NotifyGovernanceObject(const CGovernanceObject &object) {}
virtual void SetBestChain(const CBlockLocator &locator) {}
virtual bool UpdatedTransaction(const uint256 &hash) { return false;}
virtual void Inventory(const uint256 &hash) {}
Expand Down Expand Up @@ -69,6 +73,10 @@ struct CMainSignals {
boost::signals2::signal<void (const CTransaction &, const CBlockIndex *pindex, int posInBlock)> SyncTransaction;
/** Notifies listeners of an updated transaction lock without new data. */
boost::signals2::signal<void (const CTransaction &)> NotifyTransactionLock;
/** Notifies listeners of a new governance vote. */
boost::signals2::signal<void (const CGovernanceVote &)> NotifyGovernanceVote;
/** Notifies listeners of a new governance object. */
boost::signals2::signal<void (const CGovernanceObject &)> NotifyGovernanceObject;
/** Notifies listeners of an updated transaction without new data (for now: a coinbase potentially becoming visible). */
boost::signals2::signal<bool (const uint256 &)> UpdatedTransaction;
/** Notifies listeners of a new active block chain. */
Expand Down
10 changes: 10 additions & 0 deletions src/zmq/zmqabstractnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ bool CZMQAbstractNotifier::NotifyTransactionLock(const CTransaction &/*transacti
{
return true;
}

bool CZMQAbstractNotifier::NotifyGovernanceVote(const CGovernanceVote& /*vote*/)
{
return true;
}

bool CZMQAbstractNotifier::NotifyGovernanceObject(const CGovernanceObject& /*object*/)
{
return true;
}
5 changes: 5 additions & 0 deletions src/zmq/zmqabstractnotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "zmqconfig.h"

class CBlockIndex;
class CGovernanceObject;
class CGovernanceVote;
class CZMQAbstractNotifier;

typedef CZMQAbstractNotifier* (*CZMQNotifierFactory)();
Expand Down Expand Up @@ -35,6 +37,9 @@ class CZMQAbstractNotifier
virtual bool NotifyBlock(const CBlockIndex *pindex);
virtual bool NotifyTransaction(const CTransaction &transaction);
virtual bool NotifyTransactionLock(const CTransaction &transaction);
virtual bool NotifyGovernanceVote(const CGovernanceVote &vote);
virtual bool NotifyGovernanceObject(const CGovernanceObject &object);


protected:
void *psocket;
Expand Down
3 changes: 3 additions & 0 deletions src/zmq/zmqconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#include "primitives/block.h"
#include "primitives/transaction.h"

#include "governance-object.h"
#include "governance-vote.h"

void zmqError(const char *str);

#endif // BITCOIN_ZMQ_ZMQCONFIG_H
38 changes: 38 additions & 0 deletions src/zmq/zmqnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
factories["pubhashtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionLockNotifier>;
factories["pubhashgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceVoteNotifier>;
factories["pubhashgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishHashGovernanceObjectNotifier>;
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
factories["pubrawtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockNotifier>;
factories["pubrawgovernancevote"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceVoteNotifier>;
factories["pubrawgovernanceobject"] = CZMQAbstractNotifier::Create<CZMQPublishRawGovernanceObjectNotifier>;

for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
{
Expand Down Expand Up @@ -179,3 +183,37 @@ void CZMQNotificationInterface::NotifyTransactionLock(const CTransaction &tx)
}
}
}

void CZMQNotificationInterface::NotifyGovernanceVote(const CGovernanceVote &vote)
{
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i != notifiers.end(); )
{
CZMQAbstractNotifier *notifier = *i;
if (notifier->NotifyGovernanceVote(vote))
{
i++;
}
else
{
notifier->Shutdown();
i = notifiers.erase(i);
}
}
}

void CZMQNotificationInterface::NotifyGovernanceObject(const CGovernanceObject &object)
{
for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i != notifiers.end(); )
{
CZMQAbstractNotifier *notifier = *i;
if (notifier->NotifyGovernanceObject(object))
{
i++;
}
else
{
notifier->Shutdown();
i = notifiers.erase(i);
}
}
}
3 changes: 3 additions & 0 deletions src/zmq/zmqnotificationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class CZMQNotificationInterface : public CValidationInterface
void SyncTransaction(const CTransaction& tx, const CBlockIndex *pindex, int posInBlock) override;
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
void NotifyTransactionLock(const CTransaction &tx) override;
void NotifyGovernanceVote(const CGovernanceVote& vote) override;
void NotifyGovernanceObject(const CGovernanceObject& object) override;


private:
CZMQNotificationInterface();
Expand Down
42 changes: 42 additions & 0 deletions src/zmq/zmqpublishnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifi
static const char *MSG_HASHBLOCK = "hashblock";
static const char *MSG_HASHTX = "hashtx";
static const char *MSG_HASHTXLOCK = "hashtxlock";
static const char *MSG_HASHGVOTE = "hashgovernancevote";
static const char *MSG_HASHGOBJ = "hashgovernanceobject";
static const char *MSG_RAWBLOCK = "rawblock";
static const char *MSG_RAWTX = "rawtx";
static const char *MSG_RAWTXLOCK = "rawtxlock";
static const char *MSG_RAWGVOTE = "rawgovernancevote";
static const char *MSG_RAWGOBJ = "rawgovernanceobject";

// Internal function to send multipart message
static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
Expand Down Expand Up @@ -173,6 +177,26 @@ bool CZMQPublishHashTransactionLockNotifier::NotifyTransactionLock(const CTransa
return SendMessage(MSG_HASHTXLOCK, data, 32);
}

bool CZMQPublishHashGovernanceVoteNotifier::NotifyGovernanceVote(const CGovernanceVote &vote)
{
uint256 hash = vote.GetHash();
LogPrint("zmq", "zmq: Publish hashgovernancevote %s\n", hash.GetHex());
char data[32];
for (unsigned int i = 0; i < 32; i++)
data[31 - i] = hash.begin()[i];
return SendMessage(MSG_HASHGVOTE, data, 32);
}

bool CZMQPublishHashGovernanceObjectNotifier::NotifyGovernanceObject(const CGovernanceObject &object)
{
uint256 hash = object.GetHash();
LogPrint("zmq", "zmq: Publish hashgovernanceobject %s\n", hash.GetHex());
char data[32];
for (unsigned int i = 0; i < 32; i++)
data[31 - i] = hash.begin()[i];
return SendMessage(MSG_HASHGOBJ, data, 32);
}

bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
{
LogPrint("zmq", "zmq: Publish rawblock %s\n", pindex->GetBlockHash().GetHex());
Expand Down Expand Up @@ -211,3 +235,21 @@ bool CZMQPublishRawTransactionLockNotifier::NotifyTransactionLock(const CTransac
ss << transaction;
return SendMessage(MSG_RAWTXLOCK, &(*ss.begin()), ss.size());
}

bool CZMQPublishRawGovernanceVoteNotifier::NotifyGovernanceVote(const CGovernanceVote &vote)
{
uint256 nHash = vote.GetHash();
LogPrint("gobject", "gobject: Publish rawgovernanceobject: hash = %s, vote = %d\n", nHash.ToString(), vote.ToString());
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << vote;
return SendMessage(MSG_RAWGVOTE, &(*ss.begin()), ss.size());
}

bool CZMQPublishRawGovernanceObjectNotifier::NotifyGovernanceObject(const CGovernanceObject &govobj)
{
uint256 nHash = govobj.GetHash();
LogPrint("gobject", "gobject: Publish rawgovernanceobject: hash = %s, type = %d\n", nHash.ToString(), govobj.GetObjectType());
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << govobj;
return SendMessage(MSG_RAWGOBJ, &(*ss.begin()), ss.size());
}
25 changes: 25 additions & 0 deletions src/zmq/zmqpublishnotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "zmqabstractnotifier.h"

class CBlockIndex;
class CGovernanceVote;
class CGovernanceObject;

class CZMQAbstractPublishNotifier : public CZMQAbstractNotifier
{
Expand Down Expand Up @@ -46,6 +48,18 @@ class CZMQPublishHashTransactionLockNotifier : public CZMQAbstractPublishNotifie
bool NotifyTransactionLock(const CTransaction &transaction) override;
};

class CZMQPublishHashGovernanceVoteNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyGovernanceVote(const CGovernanceVote &vote) override;
};

class CZMQPublishHashGovernanceObjectNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyGovernanceObject(const CGovernanceObject &object) override;
};

class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier
{
public:
Expand All @@ -64,4 +78,15 @@ class CZMQPublishRawTransactionLockNotifier : public CZMQAbstractPublishNotifier
bool NotifyTransactionLock(const CTransaction &transaction) override;
};

class CZMQPublishRawGovernanceVoteNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyGovernanceVote(const CGovernanceVote &vote) override;
};

class CZMQPublishRawGovernanceObjectNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyGovernanceObject(const CGovernanceObject &object) override;
};
#endif // BITCOIN_ZMQ_ZMQPUBLISHNOTIFIER_H

0 comments on commit 0e68934

Please sign in to comment.