txgraph: move some sanity checks from Cluster to TxGraphImpl (refactor)
What changed, and why it matters
This is a small internal code cleanup in Bitcoin Core's transaction graph module. It moves several self-consistency checks from one internal helper function to another. The checks themselves remain present and unchanged in effect; only their location in the source code differs. There is no user-facing change and no indication of a security fix.
No security action required. Treat as normal code-quality refactor during review.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors Cluster::SanityCheck and TxGraphImpl::SanityCheck in src/txgraph.cpp. Assertions enforcing cluster size/count limits, quality/level consistency, and oversized-singleton invariants are relocated from Cluster::SanityCheck (which takes a graph argument) into the cluster-iteration loop inside TxGraphImpl::SanityCheck. Equivalent checks are preserved, using accessor methods (GetTxCount, IsOversized, GetLevel) instead of direct member access where appropriate. This is a pure refactor with no functional change to runtime behavior beyond assertion placement.
Changed components
src/txgraph.cppCluster::SanityCheckTxGraphImpl::SanityCheckInspect captured patch +13 / −13
diff --git a/src/txgraph.cpp b/src/txgraph.cpp
index 41764ff8..4dee1d02 100644
--- a/src/txgraph.cpp
+++ b/src/txgraph.cpp
@@ -2286,19 +2286,6 @@ void Cluster::SanityCheck(const TxGraphImpl& graph, int level) const
assert(m_depgraph.PositionRange() == m_mapping.size());
// The linearization for this Cluster must contain every transaction once.
assert(m_depgraph.TxCount() == m_linearization.size());
- // The number of transactions in a Cluster cannot exceed m_max_cluster_count.
- assert(m_linearization.size() <= graph.m_max_cluster_count);
- // The level must match the Cluster's own idea of its level (but GetLevel can only be called
- // for non-empty Clusters).
- assert(GetTxCount() == 0 || level == GetLevel(graph));
- // The sum of their sizes cannot exceed m_max_cluster_size, unless it is an individually
- // oversized transaction singleton. Note that groups of to-be-merged clusters which would
- // exceed this limit are marked oversized, which means they are never applied.
- assert(m_quality == QualityLevel::OVERSIZED_SINGLETON || GetTotalTxSize() <= graph.m_max_cluster_size);
- // m_quality and m_setindex are checked in TxGraphImpl::SanityCheck.
-
- // OVERSIZED clusters are singletons.
- assert(m_quality != QualityLevel::OVERSIZED_SINGLETON || m_linearization.size() == 1);
// Compute the chunking of m_linearization.
LinearizationChunking linchunking(m_depgraph, m_linearization);
@@ -2416,6 +2403,19 @@ void TxGraphImpl::SanityCheck() const
// ... for all clusters in them ...
for (ClusterSetIndex setindex = 0; setindex < quality_clusters.size(); ++setindex) {
const auto& cluster = *quality_clusters[setindex];
+ // The number of transactions in a Cluster cannot exceed m_max_cluster_count.
+ assert(cluster.GetTxCount() <= m_max_cluster_count);
+ // The level must match the Cluster's own idea of what level it is in (but GetLevel
+ // can only be called for non-empty Clusters).
+ assert(cluster.GetTxCount() == 0 || level == cluster.GetLevel(*this));
+ // The sum of their sizes cannot exceed m_max_cluster_size, unless it is an
+ // individually oversized transaction singleton. Note that groups of to-be-merged
+ // clusters which would exceed this limit are marked oversized, which means they
+ // are never applied.
+ assert(cluster.IsOversized() || cluster.GetTotalTxSize() <= m_max_cluster_size);
+ // OVERSIZED clusters are singletons.
+ assert(!cluster.IsOversized() || cluster.GetTxCount() == 1);
+
// Check the sequence number.
assert(cluster.m_sequence < m_next_sequence_counter);
assert(sequences.count(cluster.m_sequence) == 0);
Why this scored 12/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.