From a60a84276cb859dc848ad09deb0d24a407371c9e Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Sun, 26 Jan 2025 14:31:12 -0500 Subject: [PATCH] Add version validation for superblock and project contracts --- src/gridcoin/project.cpp | 11 +++++++++++ src/gridcoin/quorum.cpp | 29 +++++++++++++++++------------ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/gridcoin/project.cpp b/src/gridcoin/project.cpp index 2f37a1dc70..8da9502908 100644 --- a/src/gridcoin/project.cpp +++ b/src/gridcoin/project.cpp @@ -960,6 +960,8 @@ bool Whitelist::Validate(const Contract& contract, const CTransaction& tx, int & const auto payload = contract.SharePayloadAs(); + // Contract version 3 is tied to block v13. Payloads of less than v3 are not allowed at or above the v13 height with + // contract v3. if (contract.m_version >= 3 && payload->m_version < 3) { DoS = 25; error("%s: Project entry contract in contract v3 is wrong version.", __func__); @@ -977,6 +979,15 @@ bool Whitelist::Validate(const Contract& contract, const CTransaction& tx, int & bool Whitelist::BlockValidate(const ContractContext& ctx, int& DoS) const { + const auto payload = ctx.m_contract.SharePayloadAs(); + + // This ensures v4 projects do not appear before the v4 project height in protocol. + if (payload->m_version > 3 && !IsProjectV4Enabled(ctx.m_pindex->nHeight)) { + DoS = 25; + error("%s: Project entry contract in contract v3 with project v4 enabled is wrong version.", __func__); + return false; + } + return Validate(ctx.m_contract, ctx.m_tx, DoS); } diff --git a/src/gridcoin/quorum.cpp b/src/gridcoin/quorum.cpp index eecb8d46d8..6e7c1f3dff 100644 --- a/src/gridcoin/quorum.cpp +++ b/src/gridcoin/quorum.cpp @@ -1539,12 +1539,16 @@ bool Quorum::ValidateSuperblockClaim( const CBlockIndex* const pindex) { if (!SuperblockNeeded(pindex->nTime)) { - return error("ValidateSuperblockClaim(): superblock too early."); + return error("%s: superblock too early.", __func__); } if (pindex->nVersion >= 11) { if (superblock->m_version < 2) { - return error("ValidateSuperblockClaim(): rejected legacy version."); + return error("%s: rejected legacy version.", __func__); + } + + if (IsSuperblockV3Enabled(pindex->nHeight) && superblock->m_version < 3) { + return error("%s: superblock rejected: version < 3 with superblock v3 enabld.", __func__); } // Superblocks are not included in the input for the claim hash @@ -1552,7 +1556,7 @@ bool Quorum::ValidateSuperblockClaim( // protect the integrity of the superblock data: // if (superblock->GetHash() != claim.m_quorum_hash) { - return error("ValidateSuperblockClaim(): quorum hash mismatch."); + return error("%s: quorum hash mismatch.", __func__); } return ValidateSuperblock(superblock); @@ -1561,23 +1565,24 @@ bool Quorum::ValidateSuperblockClaim( const CTxDestination address = DecodeDestination(claim.m_quorum_address); if (!IsValidDestination(address)) { - return error("ValidateSuperblockClaim(): " - "invalid quorum address: %s", claim.m_quorum_address); + return error("%s: " + "invalid quorum address: %s", __func__, claim.m_quorum_address); } if (!Participating(claim.m_quorum_address, pindex->nTime)) { - return error("ValidateSuperblockClaim(): " - "ineligible quorum participant: %s", claim.m_quorum_address); + return error("%s: " + "ineligible quorum participant: %s", __func__, claim.m_quorum_address); } - // Popular hash search begins from the block before: + // Popular hash search begins from the block before: const QuorumHash popular_hash = FindPopularHash(pindex->pprev); if (superblock->GetHash() != popular_hash) { - return error("ValidateSuperblockClaim(): " - "superblock hash mismatch: %s popular: %s", - superblock->GetHash().ToString(), - popular_hash.ToString()); + return error("%s: " + "superblock hash mismatch: %s popular: %s", + __func__, + superblock->GetHash().ToString(), + popular_hash.ToString()); } return true;