Skip to content

Commit

Permalink
[validation] merge all ConnectBlock debug logging code paths
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Dec 2, 2024
1 parent b49df70 commit 492e1f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
44 changes: 22 additions & 22 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2595,9 +2595,8 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
for (const auto& tx : block.vtx) {
for (size_t o = 0; o < tx->vout.size(); o++) {
if (view.HaveCoin(COutPoint(tx->GetHash(), o))) {
LogPrintf("ERROR: ConnectBlock(): tried to overwrite transaction\n");
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-BIP30",
"tried to overwrite transaction");
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-BIP30",
"tried to overwrite transaction");
}
}
}
Expand Down Expand Up @@ -2636,6 +2635,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
blockundo.vtxundo.reserve(block.vtx.size() - 1);
for (unsigned int i = 0; i < block.vtx.size(); i++)
{
if (!state.IsValid()) break;
const CTransaction &tx = *(block.vtx[i]);

nInputs += tx.vin.size();
Expand All @@ -2649,14 +2649,13 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
tx_state.GetRejectReason(),
tx_state.GetDebugMessage() + " in transaction " + tx.GetHash().ToString());
LogError("%s: Consensus::CheckTxInputs: %s, %s\n", __func__, tx.GetHash().ToString(), state.ToString());
return false;
break;
}
nFees += txfee;
if (!MoneyRange(nFees)) {
LogPrintf("ERROR: %s: accumulated fee in the block out of range.\n", __func__);
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-accumulated-fee-outofrange",
"accumulated fee in the block out of range");
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-accumulated-fee-outofrange",
"accumulated fee in the block out of range");
break;
}

// Check that transaction is BIP68 final
Expand All @@ -2668,9 +2667,9 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
}

if (!SequenceLocks(tx, nLockTimeFlags, prevheights, *pindex)) {
LogPrintf("ERROR: %s: contains a non-BIP68-final transaction\n", __func__);
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal",
"contains a non-BIP68-final transaction " + tx.GetHash().ToString());
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-txns-nonfinal",
"contains a non-BIP68-final transaction " + tx.GetHash().ToString());
break;
}
}

Expand All @@ -2680,8 +2679,8 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
// * witness (when witness enabled in flags and excludes coinbase)
nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST) {
LogPrintf("ERROR: ConnectBlock(): too many sigops\n");
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "too many sigops");
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-sigops", "too many sigops");
break;
}

if (!tx.IsCoinBase())
Expand All @@ -2693,8 +2692,7 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
// Any transaction validation failure in ConnectBlock is a block consensus failure
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS,
tx_state.GetRejectReason(), tx_state.GetDebugMessage());
LogInfo("Script validation error in block: %s\n", state.ToString());
return false;
break;
}
control.Add(std::move(vChecks));
}
Expand All @@ -2714,16 +2712,17 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
Ticks<MillisecondsDouble>(m_chainman.time_connect) / m_chainman.num_blocks_total);

CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, params.GetConsensus());
if (block.vtx[0]->GetValueOut() > blockReward) {
LogPrintf("ERROR: ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)\n", block.vtx[0]->GetValueOut(), blockReward);
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-amount",
strprintf("coinbase pays too much (actual=%d vs limit=%d)", block.vtx[0]->GetValueOut(), blockReward));
if (block.vtx[0]->GetValueOut() > blockReward && state.IsValid()) {
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-cb-amount",
strprintf("coinbase pays too much (actual=%d vs limit=%d)", block.vtx[0]->GetValueOut(), blockReward));
}

auto parallel_result = control.Complete();
if (parallel_result.has_value()) {
if (parallel_result.has_value() && state.IsValid()) {
state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(parallel_result->first)), parallel_result->second);
LogInfo("Script validation error in block: %s", state.ToString());
}
if (!state.IsValid()) {
LogInfo("Block validation error: %s", state.ToString());
return false;
}
const auto time_4{SteadyClock::now()};
Expand All @@ -2734,8 +2733,9 @@ bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state,
Ticks<SecondsDouble>(m_chainman.time_verify),
Ticks<MillisecondsDouble>(m_chainman.time_verify) / m_chainman.num_blocks_total);

if (fJustCheck)
if (fJustCheck) {
return true;
}

if (!m_blockman.WriteUndoDataForBlock(blockundo, state, *pindex)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_cltv.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def run_test(self):
block.hashMerkleRoot = block.calc_merkle_root()
block.solve()

with self.nodes[0].assert_debug_log(expected_msgs=[f'Script validation error in block: {expected_cltv_reject_reason}']):
with self.nodes[0].assert_debug_log(expected_msgs=[f'Block validation error: {expected_cltv_reject_reason}']):
peer.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
peer.sync_with_ping()
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_dersig.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def run_test(self):
block.hashMerkleRoot = block.calc_merkle_root()
block.solve()

with self.nodes[0].assert_debug_log(expected_msgs=[f'Script validation error in block: mandatory-script-verify-flag-failed (Non-canonical DER signature)']):
with self.nodes[0].assert_debug_log(expected_msgs=[f'Block validation error: mandatory-script-verify-flag-failed (Non-canonical DER signature)']):
peer.send_and_ping(msg_block(block))
assert_equal(int(self.nodes[0].getbestblockhash(), 16), tip)
peer.sync_with_ping()
Expand Down

0 comments on commit 492e1f0

Please sign in to comment.