iwyu: Fix warnings in `src/consensus` and treat them as errors
What changed, and why it matters
This commit is a routine code cleanup: it adjusts which C++ header files are included in several consensus-related source files and turns on a stricter compiler hygiene check (Include What You Use, or IWYU) for the src/consensus directory. There is no functional change to Bitcoin's rules, no bug fix, and no security-relevant behavior change.
No security action required. Treat as normal build/CI hygiene maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch is an IWYU enforcement change. It adds src/consensus to the set of directories where IWYU warnings are treated as errors in CI, and updates #include lists in consensus headers/sources to match IWYU recommendations (e.g., replacing
Changed components
src/consensus/consensus.hsrc/consensus/merkle.cppsrc/consensus/merkle.hsrc/consensus/params.hsrc/consensus/tx_check.cppsrc/consensus/tx_verify.cppsrc/consensus/tx_verify.hsrc/consensus/validation.hci/test/03_test_script.shInspect captured patch +42 / −7
diff --git a/ci/test/03_test_script.sh b/ci/test/03_test_script.sh
index 834473b1..abc501d1 100755
--- a/ci/test/03_test_script.sh
+++ b/ci/test/03_test_script.sh
@@ -234,7 +234,7 @@ fi
if [[ "${RUN_IWYU}" == true ]]; then
# TODO: Consider enforcing IWYU across the entire codebase.
- FILES_WITH_ENFORCED_IWYU='/src/((bench|common|crypto|index|kernel|primitives|script|univalue/(lib|test)|util|zmq)/.*|node/(blockstorage|interfaces|miner|mining_args|utxo_snapshot)|rpc/mining|clientversion|core_io|signet|init)\.cpp'
+ FILES_WITH_ENFORCED_IWYU='/src/((bench|common|consensus|crypto|index|kernel|primitives|script|univalue/(lib|test)|util|zmq)/.*|node/(blockstorage|interfaces|miner|mining_args|utxo_snapshot)|rpc/mining|clientversion|core_io|signet|init)\.cpp'
jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns)))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_errors.json"
jq --arg patterns "$FILES_WITH_ENFORCED_IWYU" 'map(select(.file | test($patterns) | not))' "${BASE_BUILD_DIR}/compile_commands.json" > "${BASE_BUILD_DIR}/compile_commands_iwyu_warnings.json"
@@ -248,6 +248,7 @@ if [[ "${RUN_IWYU}" == true ]]; then
-- -Xiwyu --cxx17ns -Xiwyu --mapping_file="${BASE_ROOT_DIR}/contrib/devtools/iwyu/bitcoin.core.imp" \
-Xiwyu --max_line_length=160 \
-Xiwyu --check_also='*/common/types\.h' \
+ -Xiwyu --check_also='*/consensus/*\.h' \
-Xiwyu --check_also='*/primitives/transaction_identifier\.h' \
2>&1 || true
} | tee /tmp/iwyu_ci.out
diff --git a/src/consensus/consensus.h b/src/consensus/consensus.h
index 71b5fe24..f1595f31 100644
--- a/src/consensus/consensus.h
+++ b/src/consensus/consensus.h
@@ -6,8 +6,8 @@
#ifndef BITCOIN_CONSENSUS_CONSENSUS_H
#define BITCOIN_CONSENSUS_CONSENSUS_H
+#include <cstddef>
#include <cstdint>
-#include <cstdlib>
/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
diff --git a/src/consensus/merkle.cpp b/src/consensus/merkle.cpp
index c6ae81c6..dfa23cf8 100644
--- a/src/consensus/merkle.cpp
+++ b/src/consensus/merkle.cpp
@@ -3,9 +3,17 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <consensus/merkle.h>
+
+#include <crypto/sha256.h>
#include <hash.h>
+#include <primitives/block.h>
+#include <primitives/transaction.h>
#include <util/check.h>
+#include <cstddef>
+#include <memory>
+#include <utility>
+
/* WARNING! If you're reading this because you're learning about crypto
and/or designing a new system that will use merkle trees, keep in mind
that the following merkle tree algorithm has a serious flaw related to
diff --git a/src/consensus/merkle.h b/src/consensus/merkle.h
index 03b5a1b5..446b2df7 100644
--- a/src/consensus/merkle.h
+++ b/src/consensus/merkle.h
@@ -5,10 +5,12 @@
#ifndef BITCOIN_CONSENSUS_MERKLE_H
#define BITCOIN_CONSENSUS_MERKLE_H
+#include <uint256.h>
+
+#include <cstdint>
#include <vector>
-#include <primitives/block.h>
-#include <uint256.h>
+class CBlock;
uint256 ComputeMerkleRoot(std::vector<uint256> hashes, bool* mutated = nullptr);
diff --git a/src/consensus/params.h b/src/consensus/params.h
index 7f567294..99096e06 100644
--- a/src/consensus/params.h
+++ b/src/consensus/params.h
@@ -11,6 +11,7 @@
#include <array>
#include <chrono>
+#include <cstdint>
#include <limits>
#include <map>
#include <vector>
diff --git a/src/consensus/tx_check.cpp b/src/consensus/tx_check.cpp
index 251417c8..473538e4 100644
--- a/src/consensus/tx_check.cpp
+++ b/src/consensus/tx_check.cpp
@@ -5,8 +5,16 @@
#include <consensus/tx_check.h>
#include <consensus/amount.h>
-#include <primitives/transaction.h>
+#include <consensus/consensus.h>
#include <consensus/validation.h>
+#include <primitives/transaction.h>
+#include <script/script.h>
+#include <serialize.h>
+
+#include <set>
+#include <string>
+#include <utility>
+#include <vector>
bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
{
diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp
index 4efed70f..e580a9d2 100644
--- a/src/consensus/tx_verify.cpp
+++ b/src/consensus/tx_verify.cpp
@@ -11,9 +11,15 @@
#include <consensus/validation.h>
#include <primitives/transaction.h>
#include <script/interpreter.h>
+#include <script/script.h>
+#include <tinyformat.h>
#include <util/check.h>
#include <util/moneystr.h>
+#include <algorithm>
+#include <cstddef>
+#include <string>
+
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
{
if (tx.nLockTime == 0)
diff --git a/src/consensus/tx_verify.h b/src/consensus/tx_verify.h
index ed44d435..76faeaea 100644
--- a/src/consensus/tx_verify.h
+++ b/src/consensus/tx_verify.h
@@ -9,6 +9,7 @@
#include <script/verify_flags.h>
#include <cstdint>
+#include <utility>
#include <vector>
class CBlockIndex;
diff --git a/src/consensus/validation.h b/src/consensus/validation.h
index 37a40e76..9e8a4ce5 100644
--- a/src/consensus/validation.h
+++ b/src/consensus/validation.h
@@ -6,10 +6,18 @@
#ifndef BITCOIN_CONSENSUS_VALIDATION_H
#define BITCOIN_CONSENSUS_VALIDATION_H
-#include <string>
#include <consensus/consensus.h>
-#include <primitives/transaction.h>
#include <primitives/block.h>
+#include <primitives/transaction.h>
+#include <script/script.h>
+#include <serialize.h>
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+#include <string>
+#include <vector>
+
/** Index marker for when no witness commitment is present in a coinbase transaction. */
static constexpr int NO_WITNESS_COMMITMENT{-1};
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.