-
Notifications
You must be signed in to change notification settings - Fork 6
Prevent 64 byte txs at consensus level #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 28.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2401,6 +2401,7 @@ unsigned int GetBlockScriptFlags(const CBlockIndex& block_index, const Chainstat | |||||||
return flags; | ||||||||
} | ||||||||
|
||||||||
static bool ContextualBlockPreCheck(const CBlock& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev); | ||||||||
|
||||||||
/** Apply the effects of this block (with given index) on the UTXO set represented by coins. | ||||||||
* Validity checks that depend on the UTXO set are also done; ConnectBlock() | ||||||||
|
@@ -2418,6 +2419,11 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, | |||||||
const auto time_start{SteadyClock::now()}; | ||||||||
const CChainParams& params{m_chainman.GetParams()}; | ||||||||
|
||||||||
if (!ContextualBlockPreCheck(block, state, m_chainman, pindex->pprev)) { | ||||||||
LogError("%s: Consensus::ContextualBlockPreCheck: %s", __func__, state.ToString()); | ||||||||
return false; | ||||||||
} | ||||||||
|
||||||||
// Check it again in case a previous version let a bad block in | ||||||||
// NOTE: We don't currently (re-)invoke ContextualCheckBlock() or | ||||||||
// ContextualCheckBlockHeader() here. This means that if we add a new | ||||||||
|
@@ -4196,6 +4202,28 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio | |||||||
return true; | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* We want to enforce certain rules (specifically the 64-byte transaction check) | ||||||||
* before we call CheckBlock to check the merkle root. This allows us to enforce | ||||||||
* malleability checks which may interact with other CheckBlock checks. | ||||||||
* This is currently called both in AcceptBlock prior to writing the block to | ||||||||
* disk and in ConnectBlock. | ||||||||
* Note that as this is called before merkle-tree checks so must never return a | ||||||||
* non-malleable error condition. | ||||||||
*/ | ||||||||
static bool ContextualBlockPreCheck(const CBlock& block, BlockValidationState& state, const ChainstateManager& chainman, const CBlockIndex* pindexPrev) | ||||||||
{ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throwing this out there in case it makes sense based on the docs
Suggested change
is it important to run this check specifically before the merkle checks? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to remove the call to If you don't want to remove that call, then it matters a lot less where the 64b tx check is made. As far as |
||||||||
if (DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_64BYTETX)) { | ||||||||
for (const auto& tx : block.vtx) { | ||||||||
if (::GetSerializeSize(TX_NO_WITNESS(tx)) == 64) { | ||||||||
return state.Invalid(BlockValidationResult::BLOCK_MUTATED, "64-byte-transaction", strprintf("size of tx %s without witness is 64 bytes", tx->GetHash().ToString())); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return true; | ||||||||
} | ||||||||
|
||||||||
/** NOTE: This function is not currently invoked by ConnectBlock(), so we | ||||||||
* should consider upgrade issues if we change which consensus rules are | ||||||||
* enforced in this function (eg by adding a new consensus rule). See comment | ||||||||
|
@@ -4478,7 +4506,8 @@ bool ChainstateManager::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, | |||||||
|
||||||||
const CChainParams& params{GetParams()}; | ||||||||
|
||||||||
if (!CheckBlock(block, state, params.GetConsensus()) || | ||||||||
if (!ContextualBlockPreCheck(block, state, *this, pindex->pprev) || | ||||||||
!CheckBlock(block, state, params.GetConsensus()) || | ||||||||
!ContextualCheckBlock(block, state, *this, pindex->pprev)) { | ||||||||
if (state.IsInvalid() && state.GetResult() != BlockValidationResult::BLOCK_MUTATED) { | ||||||||
pindex->nStatus |= BLOCK_FAILED_VALID; | ||||||||
|
@@ -4613,6 +4642,10 @@ bool TestBlockValidity(BlockValidationState& state, | |||||||
LogError("%s: Consensus::ContextualCheckBlockHeader: %s\n", __func__, state.ToString()); | ||||||||
return false; | ||||||||
} | ||||||||
if (!ContextualBlockPreCheck(block, state, chainstate.m_chainman, pindexPrev)) { | ||||||||
LogError("%s: Consensus::ContextualBlockPreCheck: %s", __func__, state.ToString()); | ||||||||
return false; | ||||||||
} | ||||||||
if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot)) { | ||||||||
LogError("%s: Consensus::CheckBlock: %s\n", __func__, state.ToString()); | ||||||||
return false; | ||||||||
|
@@ -4733,6 +4766,11 @@ VerifyDBResult CVerifyDB::VerifyDB( | |||||||
return VerifyDBResult::CORRUPTED_BLOCK_DB; | ||||||||
} | ||||||||
// check level 1: verify block validity | ||||||||
if (nCheckLevel >= 1 && !ContextualBlockPreCheck(block, state, chainstate.m_chainman, pindex->pprev)) { | ||||||||
LogPrintf("Verification error: found bad block at %d due to soft-fork, hash=%s (%s)\n", | ||||||||
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString()); | ||||||||
return VerifyDBResult::CORRUPTED_BLOCK_DB; | ||||||||
} | ||||||||
if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params)) { | ||||||||
LogPrintf("Verification error: found bad block at %d, hash=%s (%s)\n", | ||||||||
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString()); | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this complexity of performing contextual checks before
CheckBlock
necessary? I don't see a reason why we would need to not persist blocks invalid because they contain 64 bytes transactions as opposed to any other reason for invalidity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this is just to be able to call it from inside
ConnectBlock
, and since it's there it might as well be called also along withCheckBlock
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a sketch of two alternative approaches:
CheckBlock()
(ie a chainman and pindexPrev pointer)CheckBlock()
inProcessNewBlock()
ContextualCheckBlock
CheckBlock()
inProcessNewBlock()
in order to downgrade those failures to as-if they were BLOCK_MUTATED)