Skip to content

Commit

Permalink
Use height based on hashPrevBlock (#932)
Browse files Browse the repository at this point in the history
* Use height based on hashPrevBlock
* Set hashPrevBlock in FillBlock tests

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Dec 2, 2021
1 parent 20f7eff commit 458988d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
CBlock block(BuildBlockTestCase());

LOCK2(cs_main, pool.cs);
block.hashPrevBlock = ::ChainActive().Tip()->GetBlockHash();
pool.addUnchecked(entry.FromTx(block.vtx[2]));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[2]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);

Expand Down Expand Up @@ -144,6 +145,7 @@ BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
BOOST_CHECK(block.hashMerkleRoot != BlockMerkleRoot(block2, &mutated));

CBlock block3;
block3.hashPrevBlock = ::ChainActive().Tip()->GetBlockHash();
BOOST_CHECK(partialBlock.FillBlock(block3, {block.vtx[1]}) == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block3.GetHash().ToString());
BOOST_CHECK_EQUAL(block.hashMerkleRoot.ToString(), BlockMerkleRoot(block3, &mutated).ToString());
Expand Down Expand Up @@ -272,6 +274,7 @@ BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
CBlock block(BuildBlockTestCase());

LOCK2(cs_main, pool.cs);
block.hashPrevBlock = ::ChainActive().Tip()->GetBlockHash();
pool.addUnchecked(entry.FromTx(block.vtx[1]));
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 0);

Expand Down Expand Up @@ -301,6 +304,7 @@ BOOST_AUTO_TEST_CASE(SufficientPreforwardRTTest)
BOOST_CHECK_EQUAL(pool.mapTx.find(block.vtx[1]->GetHash())->GetSharedTx().use_count(), SHARED_TX_OFFSET + 1);

CBlock block2;
block2.hashPrevBlock = ::ChainActive().Tip()->GetBlockHash();
PartiallyDownloadedBlock partialBlockCopy = partialBlock;
BOOST_CHECK(partialBlock.FillBlock(block2, {}) == READ_STATUS_OK);
BOOST_CHECK_EQUAL(block.GetHash().ToString(), block2.GetHash().ToString());
Expand Down
25 changes: 17 additions & 8 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4526,15 +4526,24 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
if (block.fChecked)
return true;

// Create block copy with height based on hashPrevBlock
auto newBlock = block;
const auto pindexPrev = LookupBlockIndex(block.hashPrevBlock);
if (!pindexPrev && block.GetHash() != consensusParams.hashGenesisBlock) {
return state.Invalid(ValidationInvalidReason::BLOCK_MISSING_PREV, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
}
const int height = pindexPrev ? pindexPrev->nHeight + 1 : 0;
newBlock.height = height;

// Check that the header is valid (particularly PoW). This is mostly
// redundant with the call in AcceptBlockHeader.
if (!fIsFakeNet && fCheckPOS && !pos::ContextualCheckProofOfStake(block, consensusParams, pcustomcsview.get(), ctxState))
if (!fIsFakeNet && fCheckPOS && !pos::ContextualCheckProofOfStake(newBlock, consensusParams, pcustomcsview.get(), ctxState))
return state.Invalid(ValidationInvalidReason::BLOCK_INVALID_HEADER, false, REJECT_INVALID, "high-hash", "proof of stake failed");

// Check the merkle root.
// block merkle root is delayed to ConnectBlock to ensure account changes
if (fCheckMerkleRoot && (block.height < consensusParams.EunosHeight
|| block.height >= consensusParams.EunosKampungHeight)) {
if (fCheckMerkleRoot && (height < consensusParams.EunosHeight
|| height >= consensusParams.EunosKampungHeight)) {
bool mutated;
uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
if (block.hashMerkleRoot != hashMerkleRoot2)
Expand Down Expand Up @@ -4566,8 +4575,8 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
TBytes dummy;
for (unsigned int i = 1; i < block.vtx.size(); i++) {
if (block.vtx[i]->IsCoinBase() &&
!IsAnchorRewardTx(*block.vtx[i], dummy, block.height >= consensusParams.FortCanningHeight) &&
!IsAnchorRewardTxPlus(*block.vtx[i], dummy, block.height >= consensusParams.FortCanningHeight))
!IsAnchorRewardTx(*block.vtx[i], dummy, height >= consensusParams.FortCanningHeight) &&
!IsAnchorRewardTxPlus(*block.vtx[i], dummy, height >= consensusParams.FortCanningHeight))
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-cb-multiple", "more than one coinbase");
}
}
Expand All @@ -4581,7 +4590,7 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));
}

if (!fIsFakeNet && fCheckPOS && block.height >= consensusParams.FortCanningHeight) {
if (!fIsFakeNet && fCheckPOS && height >= consensusParams.FortCanningHeight) {
CKeyID minter;
// this is safe cause pos::ContextualCheckProofOfStake checked
block.ExtractMinterKey(minter);
Expand Down Expand Up @@ -4706,15 +4715,15 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationSta
return state.Invalid(ValidationInvalidReason::BLOCK_INVALID_HEADER, false, REJECT_INVALID, "time-too-old", strprintf("block's timestamp is too early. Block time: %d Min time: %d", block.GetBlockTime(), pindexPrev->GetMedianTimePast()));

// Check timestamp
if (Params().NetworkIDString() != CBaseChainParams::REGTEST && block.height >= static_cast<uint64_t>(consensusParams.EunosPayaHeight)) {
if (Params().NetworkIDString() != CBaseChainParams::REGTEST && nHeight >= static_cast<uint64_t>(consensusParams.EunosPayaHeight)) {
if (block.GetBlockTime() > GetTime() + MAX_FUTURE_BLOCK_TIME_EUNOSPAYA)
return state.Invalid(ValidationInvalidReason::BLOCK_TIME_FUTURE, false, REJECT_INVALID, "time-too-new", strprintf("block timestamp too far in the future. Block time: %d Max time: %d", block.GetBlockTime(), GetTime() + MAX_FUTURE_BLOCK_TIME_EUNOSPAYA));
}

if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
return state.Invalid(ValidationInvalidReason::BLOCK_TIME_FUTURE, false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");

if (block.height >= static_cast<uint64_t>(consensusParams.DakotaCrescentHeight)) {
if (nHeight >= static_cast<uint64_t>(consensusParams.DakotaCrescentHeight)) {
if (block.GetBlockTime() > GetTime() + MAX_FUTURE_BLOCK_TIME_DAKOTACRESCENT)
return state.Invalid(ValidationInvalidReason::BLOCK_TIME_FUTURE, false, REJECT_INVALID, "time-too-new", strprintf("block timestamp too far in the future. Block time: %d Max time: %d", block.GetBlockTime(), GetTime() + MAX_FUTURE_BLOCK_TIME_DAKOTACRESCENT));
}
Expand Down

0 comments on commit 458988d

Please sign in to comment.