mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
clusterlin: add reordering support for DepGraph
Add a DepGraph(depgraph, reordering) function that constructs a new DepGraph corresponding to an old one, but with its transactions is a modified order (given as a vector from old to new positions). Also use this reordering feature inside DepGraphFormatter::Unser, which needs a small modification so that its reordering mapping is old-to-new (rather than the new-to-old it used before).
This commit is contained in:
parent
85a285a306
commit
b80e6dfe78
2 changed files with 37 additions and 23 deletions
|
@ -118,6 +118,28 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Construct a DepGraph object given another DepGraph and a mapping from old to new.
|
||||||
|
*
|
||||||
|
* Complexity: O(N^2) where N=depgraph.TxCount().
|
||||||
|
*/
|
||||||
|
DepGraph(const DepGraph<SetType>& depgraph, Span<const ClusterIndex> mapping) noexcept : entries(depgraph.TxCount())
|
||||||
|
{
|
||||||
|
Assert(mapping.size() == depgraph.TxCount());
|
||||||
|
// Fill in fee, size, ancestors.
|
||||||
|
for (ClusterIndex i = 0; i < depgraph.TxCount(); ++i) {
|
||||||
|
const auto& input = depgraph.entries[i];
|
||||||
|
auto& output = entries[mapping[i]];
|
||||||
|
output.feerate = input.feerate;
|
||||||
|
for (auto j : input.ancestors) output.ancestors.Set(mapping[j]);
|
||||||
|
}
|
||||||
|
// Fill in descendant information.
|
||||||
|
for (ClusterIndex i = 0; i < entries.size(); ++i) {
|
||||||
|
for (auto j : entries[i].ancestors) {
|
||||||
|
entries[j].descendants.Set(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Get the number of transactions in the graph. Complexity: O(1). */
|
/** Get the number of transactions in the graph. Complexity: O(1). */
|
||||||
auto TxCount() const noexcept { return entries.size(); }
|
auto TxCount() const noexcept { return entries.size(); }
|
||||||
/** Get the feerate of a given transaction i. Complexity: O(1). */
|
/** Get the feerate of a given transaction i. Complexity: O(1). */
|
||||||
|
|
|
@ -186,7 +186,7 @@ struct DepGraphFormatter
|
||||||
/** The dependency graph which we deserialize into first, with transactions in
|
/** The dependency graph which we deserialize into first, with transactions in
|
||||||
* topological serialization order, not original cluster order. */
|
* topological serialization order, not original cluster order. */
|
||||||
DepGraph<SetType> topo_depgraph;
|
DepGraph<SetType> topo_depgraph;
|
||||||
/** Mapping from cluster order to serialization order, used later to reconstruct the
|
/** Mapping from serialization order to cluster order, used later to reconstruct the
|
||||||
* cluster order. */
|
* cluster order. */
|
||||||
std::vector<ClusterIndex> reordering;
|
std::vector<ClusterIndex> reordering;
|
||||||
|
|
||||||
|
@ -205,9 +205,9 @@ struct DepGraphFormatter
|
||||||
coded_fee &= 0xFFFFFFFFFFFFF; // Enough for fee between -21M...21M BTC.
|
coded_fee &= 0xFFFFFFFFFFFFF; // Enough for fee between -21M...21M BTC.
|
||||||
static_assert(0xFFFFFFFFFFFFF > uint64_t{2} * 21000000 * 100000000);
|
static_assert(0xFFFFFFFFFFFFF > uint64_t{2} * 21000000 * 100000000);
|
||||||
auto fee = UnsignedToSigned(coded_fee);
|
auto fee = UnsignedToSigned(coded_fee);
|
||||||
// Extend topo_depgraph with the new transaction (at the end).
|
// Extend topo_depgraph with the new transaction (preliminarily at the end).
|
||||||
auto topo_idx = topo_depgraph.AddTransaction({fee, size});
|
auto topo_idx = topo_depgraph.AddTransaction({fee, size});
|
||||||
reordering.push_back(topo_idx);
|
reordering.push_back(reordering.size());
|
||||||
// Read dependency information.
|
// Read dependency information.
|
||||||
uint64_t diff = 0; //!< How many potential parents we have to skip.
|
uint64_t diff = 0; //!< How many potential parents we have to skip.
|
||||||
s >> VARINT(diff);
|
s >> VARINT(diff);
|
||||||
|
@ -226,31 +226,23 @@ struct DepGraphFormatter
|
||||||
--diff;
|
--diff;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If we reach this point, we can interpret the remaining skip value as how far from the
|
// If we reach this point, we can interpret the remaining skip value as how far
|
||||||
// end of reordering topo_idx should be placed (wrapping around), so move it to its
|
// from the end of reordering the new transaction should be placed (wrapping
|
||||||
// correct location. The preliminary reordering.push_back(topo_idx) above was to make
|
// around), so remove the preliminary position it was put in above (which was to
|
||||||
// sure that if a deserialization exception occurs, topo_idx still appears somewhere.
|
// make sure that if a deserialization exception occurs, the new transaction still
|
||||||
|
// has some entry in reordering).
|
||||||
reordering.pop_back();
|
reordering.pop_back();
|
||||||
reordering.insert(reordering.end() - (diff % (reordering.size() + 1)), topo_idx);
|
ClusterIndex insert_distance = diff % (reordering.size() + 1);
|
||||||
|
// And then update reordering to reflect this new transaction's insertion.
|
||||||
|
for (auto& pos : reordering) {
|
||||||
|
pos += (pos >= reordering.size() - insert_distance);
|
||||||
|
}
|
||||||
|
reordering.push_back(reordering.size() - insert_distance);
|
||||||
}
|
}
|
||||||
} catch (const std::ios_base::failure&) {}
|
} catch (const std::ios_base::failure&) {}
|
||||||
|
|
||||||
// Construct the original cluster order depgraph.
|
// Construct the original cluster order depgraph.
|
||||||
depgraph = {};
|
depgraph = DepGraph(topo_depgraph, reordering);
|
||||||
// Add transactions to depgraph in the original cluster order.
|
|
||||||
for (auto topo_idx : reordering) {
|
|
||||||
depgraph.AddTransaction(topo_depgraph.FeeRate(topo_idx));
|
|
||||||
}
|
|
||||||
// Translate dependencies from topological to cluster order.
|
|
||||||
for (ClusterIndex idx = 0; idx < reordering.size(); ++idx) {
|
|
||||||
ClusterIndex topo_idx = reordering[idx];
|
|
||||||
for (ClusterIndex dep_idx = 0; dep_idx < reordering.size(); ++dep_idx) {
|
|
||||||
ClusterIndex dep_topo_idx = reordering[dep_idx];
|
|
||||||
if (topo_depgraph.Ancestors(topo_idx)[dep_topo_idx]) {
|
|
||||||
depgraph.AddDependency(dep_idx, idx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue