Skip to content

Commit

Permalink
Add version validation for superblock and project contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jan 28, 2025
1 parent f55c405 commit a60a842
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/gridcoin/project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,8 @@ bool Whitelist::Validate(const Contract& contract, const CTransaction& tx, int &

const auto payload = contract.SharePayloadAs<Project>();

// 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__);
Expand All @@ -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<Project>();

// 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);
}

Expand Down
29 changes: 17 additions & 12 deletions src/gridcoin/quorum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1539,20 +1539,24 @@ 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
// so we need to compare the computed hash to the claim hash to
// 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);
Expand All @@ -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;
Expand Down

0 comments on commit a60a842

Please sign in to comment.