Skip to content

Commit

Permalink
Cherry pick of dashpay#2833
Browse files Browse the repository at this point in the history
Implement quorum merkle roots for DIP4 coinbases and add quorums to MNLISTDIFF
  • Loading branch information
codablock authored and Duddino committed Nov 6, 2024
1 parent 901976a commit b70bfb3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/llmq/quorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "quorums.h"

#include "activemasternode.h"
#include "chain.h"
#include "chainparams.h"
#include "cxxtimer.h"
#include "evo/deterministicmns.h"
Expand Down Expand Up @@ -33,7 +34,7 @@ static uint256 MakeQuorumKey(const CQuorum& q)
{
CHashWriter hw(SER_NETWORK, 0);
hw << (uint8_t)q.params.type;
hw << q.pindexQuorum->GetBlockHash();
hw << q.qc.quorumHash;
for (const auto& dmn : q.members) {
hw << dmn->proTxHash;
}
Expand All @@ -51,13 +52,13 @@ CQuorum::~CQuorum()
}
}

void CQuorum::Init(const uint256& _minedBlockHash, const CBlockIndex* _pindexQuorum, const std::vector<CDeterministicMNCPtr>& _members, const std::vector<bool>& _validMembers, const CBLSPublicKey& _quorumPublicKey)
void CQuorum::Init(const CFinalCommitment& _qc, const CBlockIndex* _pindexQuorum, int _height, const uint256& _minedBlockHash, const std::vector<CDeterministicMNCPtr>& _members)
{
minedBlockHash = _minedBlockHash;
qc = _qc;
height = _height;
pindexQuorum = _pindexQuorum;
members = _members;
validMembers = _validMembers;
quorumPublicKey = _quorumPublicKey;
minedBlockHash = _minedBlockHash;
}

bool CQuorum::IsMember(const uint256& proTxHash) const
Expand All @@ -74,15 +75,15 @@ bool CQuorum::IsValidMember(const uint256& proTxHash) const
{
for (size_t i = 0; i < members.size(); i++) {
if (members[i]->proTxHash == proTxHash) {
return validMembers[i];
return qc.validMembers[i];
}
}
return false;
}

CBLSPublicKey CQuorum::GetPubKeyShare(size_t memberIdx) const
{
if (quorumVvec == nullptr || memberIdx >= members.size() || !validMembers[memberIdx]) {
if (quorumVvec == nullptr || memberIdx >= members.size() || !qc.validMembers[memberIdx]) {
return CBLSPublicKey();
}
auto& m = members[memberIdx];
Expand Down Expand Up @@ -147,7 +148,7 @@ void CQuorum::StartCachePopulatorThread(std::shared_ptr<CQuorum> _this)
// when then later some other thread tries to get keys, it will be much faster
_this->cachePopulatorThread = std::thread(&TraceThread<std::function<void()> >, "quorum-cachepop", [_this, t] {
for (size_t i = 0; i < _this->members.size() && !_this->stopCachePopulatorThread && !ShutdownRequested(); i++) {
if (_this->validMembers[i]) {
if (_this->qc.validMembers[i]) {
_this->GetPubKeyShare(i);
}
}
Expand Down Expand Up @@ -178,14 +179,13 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial
}
}


bool CQuorumManager::BuildQuorumFromCommitment(const CFinalCommitment& qc, const CBlockIndex* pindexQuorum, const uint256& minedBlockHash, std::shared_ptr<CQuorum>& quorum) const
{
assert(pindexQuorum);
assert(qc.quorumHash == pindexQuorum->GetBlockHash());

auto members = deterministicMNManager->GetAllQuorumMembers((Consensus::LLMQType)qc.llmqType, pindexQuorum);
quorum->Init(minedBlockHash, pindexQuorum, members, qc.validMembers, qc.quorumPublicKey);
quorum->Init(qc, pindexQuorum, pindexQuorum->nHeight, minedBlockHash, members);

bool hasValidVvec = false;
if (quorum->ReadContributions(evoDb)) {
Expand Down
9 changes: 5 additions & 4 deletions src/llmq/quorums.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef PIVX_LLMQ_QUORUMS_H
#define PIVX_LLMQ_QUORUMS_H

#include "llmq/quorums_commitment.h"
#include "bls/bls_worker.h"
#include "bls/bls_wrapper.h"
#include "consensus/params.h"
Expand Down Expand Up @@ -35,11 +36,11 @@ class CQuorum

public:
const Consensus::LLMQParams& params;
uint256 minedBlockHash;
const CBlockIndex* pindexQuorum;
CFinalCommitment qc;
int height;
uint256 minedBlockHash;
std::vector<CDeterministicMNCPtr> members;
std::vector<bool> validMembers;
CBLSPublicKey quorumPublicKey;

// These are only valid when we either participated in the DKG or fully watched it
BLSVerificationVectorPtr quorumVvec;
Expand All @@ -55,7 +56,7 @@ class CQuorum
public:
CQuorum(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker) : params(_params), blsCache(_blsWorker), stopCachePopulatorThread(false) {}
~CQuorum();
void Init(const uint256& minedBlockHash, const CBlockIndex* pindexQuorum, const std::vector<CDeterministicMNCPtr>& members, const std::vector<bool>& validMembers, const CBLSPublicKey& quorumPublicKey);
void Init(const CFinalCommitment& _qc, const CBlockIndex* pindexQuorum, int _height, const uint256& _minedBlockHash, const std::vector<CDeterministicMNCPtr>& _members);

bool IsMember(const uint256& proTxHash) const;
bool IsValidMember(const uint256& proTxHash) const;
Expand Down
6 changes: 3 additions & 3 deletions src/llmq/quorums_signing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ bool CSigningManager::ProcessPendingRecoveredSigs(CConnman& connman)

for (auto& recSig : v) {
const auto& quorum = quorums.at(std::make_pair((Consensus::LLMQType)recSig.llmqType, recSig.quorumHash));
batchVerifier.PushMessage(nodeId, recSig.GetHash(), llmq::utils::BuildSignHash(recSig), recSig.sig, quorum->quorumPublicKey);
batchVerifier.PushMessage(nodeId, recSig.GetHash(), llmq::utils::BuildSignHash(recSig), recSig.sig, quorum->qc.quorumPublicKey);
verifyCount++;
}
}
Expand Down Expand Up @@ -678,7 +678,7 @@ bool CSigningManager::VerifyRecoveredSig(Consensus::LLMQType llmqType, int signe
}

uint256 signHash = llmq::utils::BuildSignHash(llmqParams.type, quorum->pindexQuorum->GetBlockHash(), id, msgHash);
return sig.VerifyInsecure(quorum->quorumPublicKey, signHash);
return sig.VerifyInsecure(quorum->qc.quorumPublicKey, signHash);
}

} // namespace llmq
} // namespace llmq
6 changes: 3 additions & 3 deletions src/llmq/quorums_signing_shares.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ bool CSigSharesManager::PreVerifyBatchedSigShares(NodeId nodeId, const CSigShare
retBan = true;
return false;
}
if (!session.quorum->validMembers[quorumMember]) {
if (!session.quorum->qc.validMembers[quorumMember]) {
LogPrintf("CSigSharesManager::%s -- quorumMember not valid\n", __func__);
retBan = true;
return false;
Expand Down Expand Up @@ -757,7 +757,7 @@ void CSigSharesManager::TryRecoverSig(const CQuorumCPtr& quorum, const uint256&
rs.UpdateHash();

auto signHash = llmq::utils::BuildSignHash(rs);
bool valid = rs.sig.VerifyInsecure(quorum->quorumPublicKey, signHash);
bool valid = rs.sig.VerifyInsecure(quorum->qc.quorumPublicKey, signHash);
if (!valid) {
// this should really not happen as we have verified all signature shares before
LogPrintf("CSigSharesManager::%s -- own recovered signature is invalid. id=%s, msgHash=%s\n", __func__,
Expand Down Expand Up @@ -1424,4 +1424,4 @@ void CSigSharesManager::HandleNewRecoveredSig(const llmq::CRecoveredSig& recover
RemoveSigSharesForSession(llmq::utils::BuildSignHash(recoveredSig));
}

} // namespace llmq
} // namespace llmq
6 changes: 3 additions & 3 deletions src/rpc/rpcquorums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ UniValue getquoruminfo(const JSONRPCRequest& request)
auto& dmn = quorum->members[i];
UniValue mo(UniValue::VOBJ);
mo.pushKV("proTxHash", dmn->proTxHash.ToString());
mo.pushKV("valid", quorum->validMembers[i]);
if (quorum->validMembers[i]) {
mo.pushKV("valid", quorum->qc.validMembers[i]);
if (quorum->qc.validMembers[i]) {
CBLSPublicKey pubKey = quorum->GetPubKeyShare(i);
if (pubKey.IsValid()) {
mo.pushKV("pubKeyShare", pubKey.ToString());
Expand All @@ -218,7 +218,7 @@ UniValue getquoruminfo(const JSONRPCRequest& request)
}

ret.pushKV("members", membersArr);
ret.pushKV("quorumPublicKey", quorum->quorumPublicKey.ToString());
ret.pushKV("quorumPublicKey", quorum->qc.quorumPublicKey.ToString());
CBLSSecretKey skShare = quorum->GetSkShare();
if (includeSkShare && skShare.IsValid()) {
ret.pushKV("secretKeyShare", skShare.ToString());
Expand Down

0 comments on commit b70bfb3

Please sign in to comment.