2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2018-2021 The Dash Core developers
|
2018-11-23 15:42:09 +01:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <llmq/quorums_commitment.h>
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2021-04-16 05:41:16 +02:00
|
|
|
#include <evo/deterministicmns.h>
|
|
|
|
#include <evo/specialtx.h>
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <validation.h>
|
2018-11-27 08:04:08 +01:00
|
|
|
|
2018-11-23 15:42:09 +01:00
|
|
|
namespace llmq
|
|
|
|
{
|
|
|
|
|
|
|
|
CFinalCommitment::CFinalCommitment(const Consensus::LLMQParams& params, const uint256& _quorumHash) :
|
|
|
|
llmqType(params.type),
|
|
|
|
quorumHash(_quorumHash),
|
|
|
|
signers(params.size),
|
|
|
|
validMembers(params.size)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LogPrintfFinalCommitment(...) do { \
|
|
|
|
LogPrintStr(strprintf("CFinalCommitment::%s -- %s", __func__, tinyformat::format(__VA_ARGS__))); \
|
|
|
|
} while(0)
|
|
|
|
|
2021-03-18 00:49:58 +01:00
|
|
|
bool CFinalCommitment::Verify(const CBlockIndex* pQuorumIndex, bool checkSigs) const
|
2018-11-23 15:42:09 +01:00
|
|
|
{
|
|
|
|
if (nVersion == 0 || nVersion > CURRENT_VERSION) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Params().GetConsensus().llmqs.count((Consensus::LLMQType)llmqType)) {
|
|
|
|
LogPrintfFinalCommitment("invalid llmqType=%d\n", llmqType);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto& params = Params().GetConsensus().llmqs.at((Consensus::LLMQType)llmqType);
|
|
|
|
|
|
|
|
if (!VerifySizes(params)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CountValidMembers() < params.minSize) {
|
|
|
|
LogPrintfFinalCommitment("invalid validMembers count. validMembersCount=%d\n", CountValidMembers());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (CountSigners() < params.minSize) {
|
|
|
|
LogPrintfFinalCommitment("invalid signers count. signersCount=%d\n", CountSigners());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!quorumPublicKey.IsValid()) {
|
|
|
|
LogPrintfFinalCommitment("invalid quorumPublicKey\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (quorumVvecHash.IsNull()) {
|
|
|
|
LogPrintfFinalCommitment("invalid quorumVvecHash\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!membersSig.IsValid()) {
|
|
|
|
LogPrintfFinalCommitment("invalid membersSig\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!quorumSig.IsValid()) {
|
|
|
|
LogPrintfFinalCommitment("invalid vvecSig\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-18 00:49:58 +01:00
|
|
|
auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pQuorumIndex);
|
2018-11-23 15:42:09 +01:00
|
|
|
for (size_t i = members.size(); i < params.size; i++) {
|
|
|
|
if (validMembers[i]) {
|
|
|
|
LogPrintfFinalCommitment("invalid validMembers bitset. bit %d should not be set\n", i);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (signers[i]) {
|
|
|
|
LogPrintfFinalCommitment("invalid signers bitset. bit %d should not be set\n", i);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 08:04:08 +01:00
|
|
|
// sigs are only checked when the block is processed
|
|
|
|
if (checkSigs) {
|
2019-05-28 15:34:41 +02:00
|
|
|
uint256 commitmentHash = CLLMQUtils::BuildCommitmentHash(params.type, quorumHash, validMembers, quorumPublicKey, quorumVvecHash);
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2018-11-27 08:04:08 +01:00
|
|
|
std::vector<CBLSPublicKey> memberPubKeys;
|
|
|
|
for (size_t i = 0; i < members.size(); i++) {
|
|
|
|
if (!signers[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-13 11:01:26 +02:00
|
|
|
memberPubKeys.emplace_back(members[i]->pdmnState->pubKeyOperator.Get());
|
2018-11-23 15:42:09 +01:00
|
|
|
}
|
|
|
|
|
2018-11-27 08:04:08 +01:00
|
|
|
if (!membersSig.VerifySecureAggregated(memberPubKeys, commitmentHash)) {
|
|
|
|
LogPrintfFinalCommitment("invalid aggregated members signature\n");
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-23 15:42:09 +01:00
|
|
|
|
2018-11-27 08:04:08 +01:00
|
|
|
if (!quorumSig.VerifyInsecure(quorumPublicKey, commitmentHash)) {
|
|
|
|
LogPrintfFinalCommitment("invalid quorum signature\n");
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-23 15:42:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-27 08:04:08 +01:00
|
|
|
bool CFinalCommitment::VerifyNull() const
|
2018-11-23 15:42:09 +01:00
|
|
|
{
|
|
|
|
if (!Params().GetConsensus().llmqs.count((Consensus::LLMQType)llmqType)) {
|
|
|
|
LogPrintfFinalCommitment("invalid llmqType=%d\n", llmqType);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const auto& params = Params().GetConsensus().llmqs.at((Consensus::LLMQType)llmqType);
|
|
|
|
|
|
|
|
if (!IsNull() || !VerifySizes(params)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CFinalCommitment::VerifySizes(const Consensus::LLMQParams& params) const
|
|
|
|
{
|
|
|
|
if (signers.size() != params.size) {
|
|
|
|
LogPrintfFinalCommitment("invalid signers.size=%d\n", signers.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (validMembers.size() != params.size) {
|
|
|
|
LogPrintfFinalCommitment("invalid signers.size=%d\n", signers.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-27 08:04:08 +01:00
|
|
|
bool CheckLLMQCommitment(const CTransaction& tx, const CBlockIndex* pindexPrev, CValidationState& state)
|
|
|
|
{
|
|
|
|
CFinalCommitmentTxPayload qcTx;
|
|
|
|
if (!GetTxPayload(tx, qcTx)) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-payload");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qcTx.nVersion == 0 || qcTx.nVersion > CFinalCommitmentTxPayload::CURRENT_VERSION) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-version");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qcTx.nHeight != pindexPrev->nHeight + 1) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-height");
|
|
|
|
}
|
|
|
|
|
2018-03-13 19:04:28 +01:00
|
|
|
const CBlockIndex* pindexQuorum = LookupBlockIndex(qcTx.commitment.quorumHash);
|
|
|
|
if (!pindexQuorum) {
|
2018-11-27 08:04:08 +01:00
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-quorum-hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (pindexQuorum != pindexPrev->GetAncestor(pindexQuorum->nHeight)) {
|
|
|
|
// not part of active chain
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-quorum-hash");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Params().GetConsensus().llmqs.count((Consensus::LLMQType)qcTx.commitment.llmqType)) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qcTx.commitment.IsNull()) {
|
|
|
|
if (!qcTx.commitment.VerifyNull()) {
|
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-invalid-null");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-18 00:49:58 +01:00
|
|
|
if (!qcTx.commitment.Verify(pindexQuorum, false)) {
|
2018-11-27 08:04:08 +01:00
|
|
|
return state.DoS(100, false, REJECT_INVALID, "bad-qc-invalid");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-15 20:55:01 +02:00
|
|
|
} // namespace llmq
|