Skip to content

Commit

Permalink
Revert "Use height based on hashPrevBlock (#932)" (#955)
Browse files Browse the repository at this point in the history
This reverts commit 458988d.
  • Loading branch information
Bushstar authored Dec 7, 2021
1 parent ad2f5f6 commit 639e8db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 deletions.
4 changes: 0 additions & 4 deletions src/test/blockencodings_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ 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 @@ -145,7 +144,6 @@ 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 @@ -274,7 +272,6 @@ 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 @@ -304,7 +301,6 @@ 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: 8 additions & 17 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4526,24 +4526,15 @@ 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(newBlock, consensusParams, pcustomcsview.get(), ctxState))
if (!fIsFakeNet && fCheckPOS && !pos::ContextualCheckProofOfStake(block, 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 && (height < consensusParams.EunosHeight
|| height >= consensusParams.EunosKampungHeight)) {
if (fCheckMerkleRoot && (block.height < consensusParams.EunosHeight
|| block.height >= consensusParams.EunosKampungHeight)) {
bool mutated;
uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
if (block.hashMerkleRoot != hashMerkleRoot2)
Expand Down Expand Up @@ -4575,8 +4566,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, height >= consensusParams.FortCanningHeight) &&
!IsAnchorRewardTxPlus(*block.vtx[i], dummy, height >= consensusParams.FortCanningHeight))
!IsAnchorRewardTx(*block.vtx[i], dummy, block.height >= consensusParams.FortCanningHeight) &&
!IsAnchorRewardTxPlus(*block.vtx[i], dummy, block.height >= consensusParams.FortCanningHeight))
return state.Invalid(ValidationInvalidReason::CONSENSUS, false, REJECT_INVALID, "bad-cb-multiple", "more than one coinbase");
}
}
Expand All @@ -4590,7 +4581,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 && height >= consensusParams.FortCanningHeight) {
if (!fIsFakeNet && fCheckPOS && block.height >= consensusParams.FortCanningHeight) {
CKeyID minter;
// this is safe cause pos::ContextualCheckProofOfStake checked
block.ExtractMinterKey(minter);
Expand Down Expand Up @@ -4715,15 +4706,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 && nHeight >= static_cast<uint64_t>(consensusParams.EunosPayaHeight)) {
if (Params().NetworkIDString() != CBaseChainParams::REGTEST && block.height >= 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 (nHeight >= static_cast<uint64_t>(consensusParams.DakotaCrescentHeight)) {
if (block.height >= 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 639e8db

Please sign in to comment.