scripted-diff: (refactor) ClusterIndex -> DepGraphIndex

Since cluster_linearize.h does not actually have a Cluster type anymore, it is more
appropriate to rename the index type to DepGraphIndex.

-BEGIN VERIFY SCRIPT-
sed -i 's/Data type to represent transaction indices in clusters./Data type to represent transaction indices in DepGraphs and the clusters they represent./' $(git grep -l 'using ClusterIndex')
sed -i 's|\<ClusterIndex\>|DepGraphIndex|g' $(git grep -l 'ClusterIndex')
-END VERIFY SCRIPT-
This commit is contained in:
Pieter Wuille
2025-01-31 16:26:06 -05:00
parent bfeb69f6e0
commit d449773899
5 changed files with 143 additions and 143 deletions

View File

@@ -28,11 +28,11 @@ void TestDepGraphSerialization(const std::vector<std::pair<FeeFrac, SetType>>& c
// Construct DepGraph from cluster argument.
DepGraph<SetType> depgraph;
SetType holes;
for (ClusterIndex i = 0; i < cluster.size(); ++i) {
for (DepGraphIndex i = 0; i < cluster.size(); ++i) {
depgraph.AddTransaction(cluster[i].first);
if (cluster[i] == HOLE) holes.Set(i);
}
for (ClusterIndex i = 0; i < cluster.size(); ++i) {
for (DepGraphIndex i = 0; i < cluster.size(); ++i) {
depgraph.AddDependencies(cluster[i].second, i);
}
depgraph.RemoveTransactions(holes);

View File

@@ -149,9 +149,9 @@ public:
* than AncestorCandidateFinder and SearchCandidateFinder.
*/
template<typename SetType>
std::pair<std::vector<ClusterIndex>, bool> SimpleLinearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations)
std::pair<std::vector<DepGraphIndex>, bool> SimpleLinearize(const DepGraph<SetType>& depgraph, uint64_t max_iterations)
{
std::vector<ClusterIndex> linearization;
std::vector<DepGraphIndex> linearization;
SimpleCandidateFinder finder(depgraph);
SetType todo = depgraph.Positions();
bool optimal = true;
@@ -203,9 +203,9 @@ SetType ReadTopologicalSet(const DepGraph<SetType>& depgraph, const SetType& tod
/** Given a dependency graph, construct any valid linearization for it, reading from a SpanReader. */
template<typename BS>
std::vector<ClusterIndex> ReadLinearization(const DepGraph<BS>& depgraph, SpanReader& reader)
std::vector<DepGraphIndex> ReadLinearization(const DepGraph<BS>& depgraph, SpanReader& reader)
{
std::vector<ClusterIndex> linearization;
std::vector<DepGraphIndex> linearization;
TestBitSet todo = depgraph.Positions();
// In every iteration one topologically-valid transaction is appended to linearization.
while (todo.Any()) {
@@ -253,18 +253,18 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
* sim[i]->first is its individual feerate, and sim[i]->second is its set of ancestors. */
std::array<std::optional<std::pair<FeeFrac, TestBitSet>>, TestBitSet::Size()> sim;
/** The number of non-nullopt position in sim. */
ClusterIndex num_tx_sim{0};
DepGraphIndex num_tx_sim{0};
/** Read a valid index of a transaction from the provider. */
auto idx_fn = [&]() {
auto offset = provider.ConsumeIntegralInRange<ClusterIndex>(0, num_tx_sim - 1);
for (ClusterIndex i = 0; i < sim.size(); ++i) {
auto offset = provider.ConsumeIntegralInRange<DepGraphIndex>(0, num_tx_sim - 1);
for (DepGraphIndex i = 0; i < sim.size(); ++i) {
if (!sim[i].has_value()) continue;
if (offset == 0) return i;
--offset;
}
assert(false);
return ClusterIndex(-1);
return DepGraphIndex(-1);
};
/** Read a valid subset of the transactions from the provider. */
@@ -273,7 +273,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
const auto mask = provider.ConsumeIntegralInRange<uint64_t>(0, range);
auto mask_shifted = mask;
TestBitSet subset;
for (ClusterIndex i = 0; i < sim.size(); ++i) {
for (DepGraphIndex i = 0; i < sim.size(); ++i) {
if (!sim[i].has_value()) continue;
if (mask_shifted & 1) {
subset.Set(i);
@@ -289,7 +289,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
auto range = (uint64_t{1} << sim.size()) - 1;
const auto mask = provider.ConsumeIntegralInRange<uint64_t>(0, range);
TestBitSet set;
for (ClusterIndex i = 0; i < sim.size(); ++i) {
for (DepGraphIndex i = 0; i < sim.size(); ++i) {
if ((mask >> i) & 1) {
set.Set(i);
}
@@ -301,7 +301,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
auto anc_update_fn = [&]() {
while (true) {
bool updates{false};
for (ClusterIndex chl = 0; chl < sim.size(); ++chl) {
for (DepGraphIndex chl = 0; chl < sim.size(); ++chl) {
if (!sim[chl].has_value()) continue;
for (auto par : sim[chl]->second) {
if (!sim[chl]->second.IsSupersetOf(sim[par]->second)) {
@@ -315,7 +315,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
};
/** Compare the state of transaction i in the simulation with the real one. */
auto check_fn = [&](ClusterIndex i) {
auto check_fn = [&](DepGraphIndex i) {
// Compare used positions.
assert(real.Positions()[i] == sim[i].has_value());
if (sim[i].has_value()) {
@@ -338,7 +338,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
auto idx = real.AddTransaction(feerate);
// Verify that the returned index is correct.
assert(!sim[idx].has_value());
for (ClusterIndex i = 0; i < TestBitSet::Size(); ++i) {
for (DepGraphIndex i = 0; i < TestBitSet::Size(); ++i) {
if (!sim[i].has_value()) {
assert(idx == i);
break;
@@ -351,7 +351,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
}
if ((command % 3) <= 1 && num_tx_sim > 0) {
// AddDependencies.
ClusterIndex child = idx_fn();
DepGraphIndex child = idx_fn();
auto parents = subset_fn();
// Apply to DepGraph.
real.AddDependencies(parents, child);
@@ -370,7 +370,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
// Apply to DepGraph.
real.RemoveTransactions(del);
// Apply to sim.
for (ClusterIndex i = 0; i < sim.size(); ++i) {
for (DepGraphIndex i = 0; i < sim.size(); ++i) {
if (sim[i].has_value()) {
if (del[i]) {
--num_tx_sim;
@@ -388,7 +388,7 @@ FUZZ_TARGET(clusterlin_depgraph_sim)
// Compare the real obtained depgraph against the simulation.
anc_update_fn();
for (ClusterIndex i = 0; i < sim.size(); ++i) check_fn(i);
for (DepGraphIndex i = 0; i < sim.size(); ++i) check_fn(i);
assert(real.TxCount() == num_tx_sim);
// Sanity check the result (which includes round-tripping serialization, if applicable).
SanityCheck(real);
@@ -401,7 +401,7 @@ FUZZ_TARGET(clusterlin_depgraph_serialization)
// Construct a graph by deserializing.
SpanReader reader(buffer);
DepGraph<TestBitSet> depgraph;
ClusterIndex par_code{0}, chl_code{0};
DepGraphIndex par_code{0}, chl_code{0};
try {
reader >> Using<DepGraphFormatter>(depgraph) >> VARINT(par_code) >> VARINT(chl_code);
} catch (const std::ios_base::failure&) {}
@@ -412,7 +412,7 @@ FUZZ_TARGET(clusterlin_depgraph_serialization)
// Introduce a cycle, and then test that IsAcyclic returns false.
if (depgraph.TxCount() < 2) return;
ClusterIndex par(0), chl(0);
DepGraphIndex par(0), chl(0);
// Pick any transaction of depgraph as parent.
par_code %= depgraph.TxCount();
for (auto i : depgraph.Positions()) {
@@ -498,7 +498,7 @@ FUZZ_TARGET(clusterlin_components)
reader >> VARINT(subset_bits);
} catch (const std::ios_base::failure&) {}
TestBitSet subset;
for (ClusterIndex i : depgraph.Positions()) {
for (DepGraphIndex i : depgraph.Positions()) {
if (todo[i]) {
if (subset_bits & 1) subset.Set(i);
subset_bits >>= 1;
@@ -555,7 +555,7 @@ FUZZ_TARGET(clusterlin_chunking)
for (const auto& chunk_feerate : chunking) {
assert(todo.Any());
SetInfo<TestBitSet> accumulator, best;
for (ClusterIndex idx : linearization) {
for (DepGraphIndex idx : linearization) {
if (todo[idx]) {
accumulator.Set(depgraph, idx);
if (best.feerate.IsEmpty() || accumulator.feerate >> best.feerate) {
@@ -766,7 +766,7 @@ FUZZ_TARGET(clusterlin_linearization_chunking)
assert(chunking.NumChunksLeft() > 0);
// Construct linearization with just todo.
std::vector<ClusterIndex> linearization_left;
std::vector<DepGraphIndex> linearization_left;
for (auto i : linearization) {
if (todo[i]) linearization_left.push_back(i);
}
@@ -776,13 +776,13 @@ FUZZ_TARGET(clusterlin_linearization_chunking)
// Verify that it matches the feerates of the chunks of chunking.
assert(chunking.NumChunksLeft() == chunking_left.size());
for (ClusterIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
for (DepGraphIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
assert(chunking.GetChunk(i).feerate == chunking_left[i]);
}
// Check consistency of chunking.
TestBitSet combined;
for (ClusterIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
for (DepGraphIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
const auto& chunk_info = chunking.GetChunk(i);
// Chunks must be non-empty.
assert(chunk_info.transactions.Any());
@@ -833,7 +833,7 @@ FUZZ_TARGET(clusterlin_linearization_chunking)
// - No non-empty intersection between the intersection and a prefix of the chunks of the
// remainder of the linearization may be better than the intersection.
TestBitSet prefix;
for (ClusterIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
for (DepGraphIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
prefix |= chunking.GetChunk(i).transactions;
auto reintersect = SetInfo(depgraph, prefix & intersect.transactions);
if (!reintersect.feerate.IsEmpty()) {
@@ -875,7 +875,7 @@ FUZZ_TARGET(clusterlin_linearize)
if (make_connected) MakeConnected(depgraph);
// Optionally construct an old linearization for it.
std::vector<ClusterIndex> old_linearization;
std::vector<DepGraphIndex> old_linearization;
{
uint8_t have_old_linearization{0};
try {
@@ -934,8 +934,8 @@ FUZZ_TARGET(clusterlin_linearize)
// Only for very small clusters, test every topologically-valid permutation.
if (depgraph.TxCount() <= 7) {
std::vector<ClusterIndex> perm_linearization;
for (ClusterIndex i : depgraph.Positions()) perm_linearization.push_back(i);
std::vector<DepGraphIndex> perm_linearization;
for (DepGraphIndex i : depgraph.Positions()) perm_linearization.push_back(i);
// Iterate over all valid permutations.
do {
// Determine whether perm_linearization is topological.
@@ -971,7 +971,7 @@ FUZZ_TARGET(clusterlin_postlinearize)
} catch (const std::ios_base::failure&) {}
// Retrieve a linearization from the fuzz input.
std::vector<ClusterIndex> linearization;
std::vector<DepGraphIndex> linearization;
linearization = ReadLinearization(depgraph, reader);
SanityCheck(depgraph, linearization);
@@ -1019,7 +1019,7 @@ FUZZ_TARGET(clusterlin_postlinearize_tree)
// Now construct a new graph, copying the nodes, but leaving only the first parent (even
// direction) or the first child (odd direction).
DepGraph<TestBitSet> depgraph_tree;
for (ClusterIndex i = 0; i < depgraph_gen.PositionRange(); ++i) {
for (DepGraphIndex i = 0; i < depgraph_gen.PositionRange(); ++i) {
if (depgraph_gen.Positions()[i]) {
depgraph_tree.AddTransaction(depgraph_gen.FeeRate(i));
} else {
@@ -1031,14 +1031,14 @@ FUZZ_TARGET(clusterlin_postlinearize_tree)
depgraph_tree.RemoveTransactions(TestBitSet::Fill(depgraph_gen.PositionRange()) - depgraph_gen.Positions());
if (direction & 1) {
for (ClusterIndex i = 0; i < depgraph_gen.TxCount(); ++i) {
for (DepGraphIndex i = 0; i < depgraph_gen.TxCount(); ++i) {
auto children = depgraph_gen.GetReducedChildren(i);
if (children.Any()) {
depgraph_tree.AddDependencies(TestBitSet::Singleton(i), children.First());
}
}
} else {
for (ClusterIndex i = 0; i < depgraph_gen.TxCount(); ++i) {
for (DepGraphIndex i = 0; i < depgraph_gen.TxCount(); ++i) {
auto parents = depgraph_gen.GetReducedParents(i);
if (parents.Any()) {
depgraph_tree.AddDependencies(TestBitSet::Singleton(parents.First()), i);
@@ -1047,7 +1047,7 @@ FUZZ_TARGET(clusterlin_postlinearize_tree)
}
// Retrieve a linearization from the fuzz input.
std::vector<ClusterIndex> linearization;
std::vector<DepGraphIndex> linearization;
linearization = ReadLinearization(depgraph_tree, reader);
SanityCheck(depgraph_tree, linearization);
@@ -1104,7 +1104,7 @@ FUZZ_TARGET(clusterlin_postlinearize_moved_leaf)
// Construct a linearization identical to lin, but with the tail end of lin_leaf moved to the
// back.
std::vector<ClusterIndex> lin_moved;
std::vector<DepGraphIndex> lin_moved;
for (auto i : lin) {
if (i != lin_leaf.back()) lin_moved.push_back(i);
}
@@ -1160,7 +1160,7 @@ FUZZ_TARGET(clusterlin_fix_linearization)
} catch (const std::ios_base::failure&) {}
// Construct an arbitrary linearization (not necessarily topological for depgraph).
std::vector<ClusterIndex> linearization;
std::vector<DepGraphIndex> linearization;
/** Which transactions of depgraph are yet to be included in linearization. */
TestBitSet todo = depgraph.Positions();
while (todo.Any()) {
@@ -1188,7 +1188,7 @@ FUZZ_TARGET(clusterlin_fix_linearization)
size_t topo_prefix = 0;
todo = depgraph.Positions();
while (topo_prefix < linearization.size()) {
ClusterIndex idx = linearization[topo_prefix];
DepGraphIndex idx = linearization[topo_prefix];
todo.Reset(idx);
if (todo.Overlaps(depgraph.Ancestors(idx))) break;
++topo_prefix;

View File

@@ -122,10 +122,10 @@ struct DepGraphFormatter
static void Ser(Stream& s, const DepGraph<SetType>& depgraph)
{
/** Construct a topological order to serialize the transactions in. */
std::vector<ClusterIndex> topo_order;
std::vector<DepGraphIndex> topo_order;
topo_order.reserve(depgraph.TxCount());
for (auto i : depgraph.Positions()) topo_order.push_back(i);
std::sort(topo_order.begin(), topo_order.end(), [&](ClusterIndex a, ClusterIndex b) {
std::sort(topo_order.begin(), topo_order.end(), [&](DepGraphIndex a, DepGraphIndex b) {
auto anc_a = depgraph.Ancestors(a).Count(), anc_b = depgraph.Ancestors(b).Count();
if (anc_a != anc_b) return anc_a < anc_b;
return a < b;
@@ -136,9 +136,9 @@ struct DepGraphFormatter
SetType done;
// Loop over the transactions in topological order.
for (ClusterIndex topo_idx = 0; topo_idx < topo_order.size(); ++topo_idx) {
for (DepGraphIndex topo_idx = 0; topo_idx < topo_order.size(); ++topo_idx) {
/** Which depgraph index we are currently writing. */
ClusterIndex idx = topo_order[topo_idx];
DepGraphIndex idx = topo_order[topo_idx];
// Write size, which must be larger than 0.
s << VARINT_MODE(depgraph.FeeRate(idx).size, VarIntMode::NONNEGATIVE_SIGNED);
// Write fee, encoded as an unsigned varint (odd=negative, even=non-negative).
@@ -146,9 +146,9 @@ struct DepGraphFormatter
// Write dependency information.
SetType written_parents;
uint64_t diff = 0; //!< How many potential parent/child relations we have skipped over.
for (ClusterIndex dep_dist = 0; dep_dist < topo_idx; ++dep_dist) {
for (DepGraphIndex dep_dist = 0; dep_dist < topo_idx; ++dep_dist) {
/** Which depgraph index we are currently considering as parent of idx. */
ClusterIndex dep_idx = topo_order[topo_idx - 1 - dep_dist];
DepGraphIndex dep_idx = topo_order[topo_idx - 1 - dep_dist];
// Ignore transactions which are already known to be ancestors.
if (depgraph.Descendants(dep_idx).Overlaps(written_parents)) continue;
if (depgraph.Ancestors(idx)[dep_idx]) {
@@ -191,9 +191,9 @@ struct DepGraphFormatter
DepGraph<SetType> topo_depgraph;
/** Mapping from serialization order to cluster order, used later to reconstruct the
* cluster order. */
std::vector<ClusterIndex> reordering;
std::vector<DepGraphIndex> reordering;
/** How big the entries vector in the reconstructed depgraph will be (including holes). */
ClusterIndex total_size{0};
DepGraphIndex total_size{0};
// Read transactions in topological order.
while (true) {
@@ -217,9 +217,9 @@ struct DepGraphFormatter
// Read dependency information.
auto topo_idx = reordering.size();
s >> VARINT(diff);
for (ClusterIndex dep_dist = 0; dep_dist < topo_idx; ++dep_dist) {
for (DepGraphIndex dep_dist = 0; dep_dist < topo_idx; ++dep_dist) {
/** Which topo_depgraph index we are currently considering as parent of topo_idx. */
ClusterIndex dep_topo_idx = topo_idx - 1 - dep_dist;
DepGraphIndex dep_topo_idx = topo_idx - 1 - dep_dist;
// Ignore transactions which are already known ancestors of topo_idx.
if (new_ancestors[dep_topo_idx]) continue;
if (diff == 0) {
@@ -286,9 +286,9 @@ template<typename SetType>
void SanityCheck(const DepGraph<SetType>& depgraph)
{
// Verify Positions and PositionRange consistency.
ClusterIndex num_positions{0};
ClusterIndex position_range{0};
for (ClusterIndex i : depgraph.Positions()) {
DepGraphIndex num_positions{0};
DepGraphIndex position_range{0};
for (DepGraphIndex i : depgraph.Positions()) {
++num_positions;
position_range = i + 1;
}
@@ -297,7 +297,7 @@ void SanityCheck(const DepGraph<SetType>& depgraph)
assert(position_range >= num_positions);
assert(position_range <= SetType::Size());
// Consistency check between ancestors internally.
for (ClusterIndex i : depgraph.Positions()) {
for (DepGraphIndex i : depgraph.Positions()) {
// Transactions include themselves as ancestors.
assert(depgraph.Ancestors(i)[i]);
// If a is an ancestor of b, then b's ancestors must include all of a's ancestors.
@@ -306,8 +306,8 @@ void SanityCheck(const DepGraph<SetType>& depgraph)
}
}
// Consistency check between ancestors and descendants.
for (ClusterIndex i : depgraph.Positions()) {
for (ClusterIndex j : depgraph.Positions()) {
for (DepGraphIndex i : depgraph.Positions()) {
for (DepGraphIndex j : depgraph.Positions()) {
assert(depgraph.Ancestors(i)[j] == depgraph.Descendants(j)[i]);
}
// No transaction is a parent or child of itself.
@@ -348,7 +348,7 @@ void SanityCheck(const DepGraph<SetType>& depgraph)
// In acyclic graphs, the union of parents with parents of parents etc. yields the
// full ancestor set (and similar for children and descendants).
std::vector<SetType> parents(depgraph.PositionRange()), children(depgraph.PositionRange());
for (ClusterIndex i : depgraph.Positions()) {
for (DepGraphIndex i : depgraph.Positions()) {
parents[i] = depgraph.GetReducedParents(i);
children[i] = depgraph.GetReducedChildren(i);
}
@@ -380,7 +380,7 @@ void SanityCheck(const DepGraph<SetType>& depgraph)
/** Perform a sanity check on a linearization. */
template<typename SetType>
void SanityCheck(const DepGraph<SetType>& depgraph, std::span<const ClusterIndex> linearization)
void SanityCheck(const DepGraph<SetType>& depgraph, std::span<const DepGraphIndex> linearization)
{
// Check completeness.
assert(linearization.size() == depgraph.TxCount());