Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Implement rawchainlocksig and rawtxlocksig (#2930) #2950

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
26 changes: 15 additions & 11 deletions contrib/zmq/zmq_sub.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
zmqSubSocket = zmqContext.socket(zmq.SUB)
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashblock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"hashtxlock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawblock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawtx")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawtxlock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawchainlock")
zmqSubSocket.setsockopt(zmq.SUBSCRIBE, b"rawchainlocksig")
zmqSubSocket.connect("tcp://127.0.0.1:%i" % port)

try:
Expand All @@ -25,26 +26,29 @@
sequence = "Unknown";

if len(msg[-1]) == 4:
msgSequence = struct.unpack('<I', msg[-1])[-1]
sequence = str(msgSequence)
msgSequence = struct.unpack('<I', msg[-1])[-1]
sequence = str(msgSequence)

if topic == "hashblock":
print('- HASH BLOCK ('+sequence+') -')
print(body.hex())
elif topic == "hashtx":
print ('- HASH TX ('+sequence+') -')
print(body.hex())
elif topic == "hashtxlock":
print('- HASH TX LOCK ('+sequence+') -')
print('- HASH TX ('+sequence+') -')
print(body.hex())
elif topic == "rawblock":
print('- RAW BLOCK HEADER ('+sequence+') -')
print(body[:80].hex())
elif topic == "rawtx":
print(binascii.hexlify(body[:80]).decode("utf-8"))
elif topic == b"rawchainlock":
print('- RAW CHAINLOCK ('+sequence+') -')
print(binascii.hexlify(body[:80]).decode("utf-8"))
elif topic == b"rawchainlocksig":
print('- RAW CHAINLOCK SIG ('+sequence+') -')
print(binascii.hexlify(body[:80]).decode("utf-8"))
elif topic == b"rawtx":
print('- RAW TX ('+sequence+') -')
print(body.hex())
elif topic == "rawtxlock":
print('- RAW TX LOCK ('+sequence+') -')
elif topic == b"rawtxlock":
print('- RAW TX LOCK('+sequence+') -')
print(body.hex())

except KeyboardInterrupt:
Expand Down
4 changes: 2 additions & 2 deletions doc/zmq.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ the command line or in the configuration file.
Currently, the following notifications are supported:

-zmqpubhashtx=address
-zmqpubhashtxlock=address
-zmqpubhashblock=address
-zmqpubrawblock=address
-zmqpubrawchainlock=address
-zmqpubrawchainlocksig=address
-zmqpubrawtx=address
-zmqpubrawtxlock=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
14 changes: 14 additions & 0 deletions src/llmq/quorums_chainlocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,20 @@ void CChainLocksHandler::EnforceBestChainLock()
if (activateNeeded && !ActivateBestChain(state)) {
LogPrintf("CChainLocksHandler::%s -- ActivateBestChain failed: %s\n", __func__, state.GetRejectReason());
}

const CBlockIndex* pindexNotify = nullptr;
{
LOCK(cs_main);
if (lastNotifyChainLockBlockIndex != currentBestChainLockBlockIndex &&
chainActive.Tip()->GetAncestor(currentBestChainLockBlockIndex->nHeight) == currentBestChainLockBlockIndex) {
lastNotifyChainLockBlockIndex = currentBestChainLockBlockIndex;
pindexNotify = currentBestChainLockBlockIndex;
}
}

if (pindexNotify) {
GetMainSignals().NotifyChainLock(pindexNotify, clsig);
}
}

void CChainLocksHandler::HandleNewRecoveredSig(const llmq::CRecoveredSig& recoveredSig)
Expand Down
1 change: 1 addition & 0 deletions src/llmq/quorums_chainlocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class CChainLocksHandler : public CRecoveredSigsListener

CChainLockSig bestChainLockWithKnownBlock;
const CBlockIndex* bestChainLockBlockIndex{nullptr};
const CBlockIndex* lastNotifyChainLockBlockIndex{nullptr};

int32_t lastSignedHeight{-1};
uint256 lastSignedRequestId;
Expand Down
6 changes: 6 additions & 0 deletions src/validationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class uint256;
class CScheduler;
enum class MemPoolRemovalReason;

namespace llmq {
class CChainLockSig;
}

// These functions dispatch to one or all registered wallets

/** Register a wallet to receive updates from core */
Expand Down Expand Up @@ -160,6 +164,7 @@ class CValidationInterface {
friend void ::UnregisterAllValidationInterfaces();
/** Notifies listeners of updated deterministic masternode list */
virtual void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff) {}
virtual void NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLockSig& clsig) {}
};

struct MainSignalsInstance;
Expand Down Expand Up @@ -192,6 +197,7 @@ class CMainSignals {
void Broadcast(CConnman* connman);
void BlockChecked(const CBlock&, const CValidationState&);
void NotifyMasternodeListChanged(bool undo, const CDeterministicMNList& oldMNList, const CDeterministicMNListDiff& diff);
void NotifyChainLock(const CBlockIndex* pindex, const llmq::CChainLockSig& clsig);
};

CMainSignals& GetMainSignals();
Expand Down
1 change: 1 addition & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

/** notify stake-split threshold changed */
boost::signals2::signal<void (const CAmount stakeSplitThreshold)> NotifySSTChanged;
void NotifyChainLock(const CBlockIndex* pindexChainLock, const llmq::CChainLockSig& clsig) override;
};

/** A key allocated from the key pool. */
Expand Down
5 changes: 5 additions & 0 deletions src/zmq/zmqabstractnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ bool CZMQAbstractNotifier::NotifyBlock(const CBlockIndex * /*CBlockIndex*/)
return true;
}

bool CZMQAbstractNotifier::NotifyChainLock(const CBlockIndex * /*CBlockIndex*/, const llmq::CChainLockSig& /*clsig*/)
{
return true;
}

bool CZMQAbstractNotifier::NotifyTransaction(const CTransaction &/*transaction*/)
{
return true;
Expand Down
5 changes: 5 additions & 0 deletions src/zmq/zmqabstractnotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
class CBlockIndex;
class CZMQAbstractNotifier;

namespace llmq {
class CChainLockSig;
}

typedef CZMQAbstractNotifier* (*CZMQNotifierFactory)();

class CZMQAbstractNotifier
Expand All @@ -33,6 +37,7 @@ class CZMQAbstractNotifier
virtual void Shutdown() = 0;

virtual bool NotifyBlock(const CBlockIndex *pindex);
virtual bool NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig);
virtual bool NotifyTransaction(const CTransaction &transaction);

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

#include "llmq/quorums_chainlocks.h"

void zmqError(const char *str);

#endif // PIVX_ZMQ_ZMQCONFIG_H
2 changes: 2 additions & 0 deletions src/zmq/zmqnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ CZMQNotificationInterface* CZMQNotificationInterface::Create()
factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
factories["pubrawchainlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockNotifier>;
factories["pubrawchainlocksig"] = CZMQAbstractNotifier::Create<CZMQPublishRawChainLockSigNotifier>;
factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;

for (const auto& entry : factories)
Expand Down
9 changes: 5 additions & 4 deletions src/zmq/zmqnotificationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class CZMQNotificationInterface : public CValidationInterface
void Shutdown();

// CValidationInterface
void TransactionAddedToMempool(const CTransactionRef& tx) override;
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const uint256& blockHash, int nBlockHeight, int64_t blockTime) override;
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
void TransactionAddedToMempool(const CTransactionRef& tx);
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected);
void BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const uint256& blockHash, int nBlockHeight, int64_t blockTime);
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload);
void NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig);

private:
CZMQNotificationInterface();
Expand Down
62 changes: 58 additions & 4 deletions src/zmq/zmqpublishnotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

static std::multimap<std::string, CZMQAbstractPublishNotifier*> mapPublishNotifiers;

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_HASHBLOCK = "hashblock";
static const char *MSG_HASHCHAINLOCK = "hashchainlock";
static const char *MSG_HASHTX = "hashtx";
static const char *MSG_RAWBLOCK = "rawblock";
static const char *MSG_RAWCHAINLOCK = "rawchainlock";
static const char *MSG_RAWCLSIG = "rawchainlocksig";
static const char *MSG_RAWTX = "rawtx";

// Internal function to send multipart message
static int zmq_send_multipart(void *sock, const void* data, size_t size, ...)
Expand Down Expand Up @@ -155,6 +158,16 @@ bool CZMQPublishHashBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
return SendMessage(MSG_HASHBLOCK, data, 32);
}

bool CZMQPublishHashChainLockNotifier::NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig)
{
uint256 hash = pindex->GetBlockHash();
LogPrint(BCLog::ZMQ, "zmq: Publish hashchainlock %s\n", hash.GetHex());
char data[32];
for (unsigned int i = 0; i < 32; i++)
data[31 - i] = hash.begin()[i];
return SendMessage(MSG_HASHCHAINLOCK, data, 32);
}

bool CZMQPublishHashTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
{
uint256 hash = transaction.GetHash();
Expand Down Expand Up @@ -185,6 +198,47 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
return SendMessage(MSG_RAWBLOCK, &(*ss.begin()), ss.size());
}

bool CZMQPublishRawChainLockNotifier::NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig)
{
LogPrint(BCLog::ZMQ, "zmq: Publish rawchainlock %s\n", pindex->GetBlockHash().GetHex());

CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
{
LOCK(cs_main);
CBlock block;
if(!ReadBlockFromDisk(block, pindex))
{
zmqError("Can't read block from disk");
return false;
}

ss << block;
}

return SendMessage(MSG_RAWCHAINLOCK, &(*ss.begin()), ss.size());
}

bool CZMQPublishRawChainLockSigNotifier::NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig)
{
LogPrint(BCLog::ZMQ, "zmq: Publish rawchainlocksig %s\n", pindex->GetBlockHash().GetHex());

CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
{
LOCK(cs_main);
CBlock block;
if(!ReadBlockFromDisk(block, pindex))
{
zmqError("Can't read block from disk");
return false;
}

ss << block;
ss << clsig;
}

return SendMessage(MSG_RAWCLSIG, &(*ss.begin()), ss.size());
}

bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &transaction)
{
uint256 hash = transaction.GetHash();
Expand Down
18 changes: 18 additions & 0 deletions src/zmq/zmqpublishnotifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class CZMQPublishHashBlockNotifier : public CZMQAbstractPublishNotifier
bool NotifyBlock(const CBlockIndex *pindex);
};

class CZMQPublishHashChainLockNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig) override;
};

class CZMQPublishHashTransactionNotifier : public CZMQAbstractPublishNotifier
{
public:
Expand All @@ -46,6 +52,18 @@ class CZMQPublishRawBlockNotifier : public CZMQAbstractPublishNotifier
bool NotifyBlock(const CBlockIndex *pindex);
};

class CZMQPublishRawChainLockNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig) override;
};

class CZMQPublishRawChainLockSigNotifier : public CZMQAbstractPublishNotifier
{
public:
bool NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig& clsig) override;
};

class CZMQPublishRawTransactionNotifier : public CZMQAbstractPublishNotifier
{
public:
Expand Down
Loading