Merge bitcoin/bitcoin#32191: Make TxGraph fuzz tests more deterministic

2835216ec0 txgraph: make GroupClusters use partition numbers directly (optimization) (Pieter Wuille)
c72c8d5d45 txgraph: compare sequence numbers instead of Cluster* (bugfix) (Pieter Wuille)

Pull request description:

  Part of cluster mempool: #30289

  The implicit transaction ordering for transactions in a TxGraphImpl is defined by:
  1. higher chunk feerate first
  2. lower Cluster* object pointer first
  3. lower position within cluster linearization first.

  Number (2) is not deterministic, as it intricately depends on the heap allocation algorithm. Fix this by giving each Cluster a unique `uint64_t m_sequence` value, and sorting by those instead.

  The second commit then uses this new approach to optimize GroupClusters a bit more, avoiding some repeated checks and dereferences, by making a local copy of the involved sequence numbers.

  Thanks to @dergoegge for pointing this out.

ACKs for top commit:
  instagibbs:
    reACK 2835216ec0
  marcofleon:
    ACK 2835216ec0
  glozow:
    utACK 2835216ec0

Tree-SHA512: d772a55b9ed620159b934a42a39fca7f900d4aa89c099a280a0c61ea0bd7c4fc39b388281ffc775064ea77b0b17263871b4c9763aa71c710a79287d5eb2cd4b4
This commit is contained in:
merge-script
2025-04-17 13:50:48 -04:00
2 changed files with 101 additions and 75 deletions

View File

@@ -206,11 +206,14 @@ struct SimTxGraph
ret.push_back(ptr);
}
}
// Deduplicate.
std::sort(ret.begin(), ret.end());
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
// Replace input.
arg = std::move(ret);
// Construct deduplicated version in input (do not use std::sort/std::unique for
// deduplication as it'd rely on non-deterministic pointer comparison).
arg.clear();
for (auto ptr : ret) {
if (std::find(arg.begin(), arg.end(), ptr) == arg.end()) {
arg.push_back(ptr);
}
}
}
};