Skip to content

Commit 97d9871

Browse files
committed
clusterlin: add LinearizationChunking class
It encapsulates a given linearization in chunked form, permitting arbitrary subsets of transactions to be removed from the linearization. Its purpose is adding the Intersect function, which is a crucial operation that will be used in a further commit to make Linearize improve existing linearizations.
1 parent d5918dc commit 97d9871

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

src/cluster_linearize.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <utility>
1414

1515
#include <random.h>
16+
#include <span.h>
1617
#include <util/feefrac.h>
1718
#include <util/vecdeque.h>
1819

@@ -256,6 +257,114 @@ std::vector<FeeFrac> ChunkLinearization(const DepGraph<SetType>& depgraph, Span<
256257
return ret;
257258
}
258259

260+
/** Data structure encapsulating the chunking of a linearization, permitting removal of subsets. */
261+
template<typename SetType>
262+
class LinearizationChunking
263+
{
264+
/** The depgraph this linearization is for. */
265+
const DepGraph<SetType>& m_depgraph;
266+
267+
/** The linearization we started from. */
268+
Span<const ClusterIndex> m_linearization;
269+
270+
/** Chunk sets and their feerates, of what remains of the linearization. */
271+
std::vector<SetInfo<SetType>> m_chunks;
272+
273+
/** Which transactions remain in the linearization. */
274+
SetType m_todo;
275+
276+
/** Fill the m_chunks variable. */
277+
void BuildChunks() noexcept
278+
{
279+
// Caller must clear m_chunks.
280+
Assume(m_chunks.empty());
281+
282+
// Iterate over the entries in m_linearization. This is effectively the same
283+
// algorithm as ChunkLinearization, but supports skipping parts of the linearization and
284+
// keeps track of the sets themselves instead of just their feerates.
285+
for (auto idx : m_linearization) {
286+
if (!m_todo[idx]) continue;
287+
// Start with an initial chunk containing just element idx.
288+
SetInfo add(m_depgraph, idx);
289+
// Absorb existing final chunks into add while they have lower feerate.
290+
while (!m_chunks.empty() && add.feerate >> m_chunks.back().feerate) {
291+
add |= m_chunks.back();
292+
m_chunks.pop_back();
293+
}
294+
// Remember new chunk.
295+
m_chunks.push_back(std::move(add));
296+
}
297+
}
298+
299+
public:
300+
/** Initialize a LinearizationSubset object for a given length of linearization. */
301+
explicit LinearizationChunking(const DepGraph<SetType>& depgraph LIFETIMEBOUND, Span<const ClusterIndex> lin LIFETIMEBOUND) noexcept :
302+
m_depgraph(depgraph), m_linearization(lin)
303+
{
304+
// Mark everything in lin as todo still.
305+
for (auto i : m_linearization) m_todo.Set(i);
306+
// Compute the initial chunking.
307+
m_chunks.reserve(depgraph.TxCount());
308+
BuildChunks();
309+
}
310+
311+
/** Determine how many chunks remain in the linearization. */
312+
ClusterIndex NumChunksLeft() const noexcept { return m_chunks.size(); }
313+
314+
/** Access a chunk. Chunk 0 is the highest-feerate prefix of what remains. */
315+
const SetInfo<SetType>& GetChunk(ClusterIndex n) const noexcept
316+
{
317+
Assume(n < m_chunks.size());
318+
return m_chunks[n];
319+
}
320+
321+
/** Remove some subset of transactions from the linearization. */
322+
void MarkDone(SetType subset) noexcept
323+
{
324+
Assume(subset.Any());
325+
Assume(subset.IsSubsetOf(m_todo));
326+
m_todo -= subset;
327+
// Rechunk what remains of m_linearization.
328+
m_chunks.clear();
329+
BuildChunks();
330+
}
331+
332+
/** Find the shortest intersection between subset and the prefixes of remaining chunks
333+
* of the linearization that has a feerate not below subset's.
334+
*
335+
* This is a crucial operation in guaranteeing improvements to linearizations. If subset has
336+
* a feerate not below GetChunk(0)'s, then moving Intersect(subset) to the front of (what
337+
* remains of) the linearization is guaranteed not to make it worse at any point.
338+
*
339+
* See https://delvingbitcoin.org/t/introduction-to-cluster-linearization/1032 for background.
340+
*/
341+
SetInfo<SetType> Intersect(const SetInfo<SetType>& subset) const noexcept
342+
{
343+
Assume(subset.transactions.IsSubsetOf(m_todo));
344+
SetInfo<SetType> accumulator;
345+
// Iterate over all chunks of the remaining linearization.
346+
for (ClusterIndex i = 0; i < NumChunksLeft(); ++i) {
347+
// Find what (if any) intersection the chunk has with subset.
348+
const SetType to_add = GetChunk(i).transactions & subset.transactions;
349+
if (to_add.Any()) {
350+
// If adding that to accumulator makes us hit all of subset, we are done as no
351+
// shorter intersection with higher/equal feerate exists.
352+
accumulator.transactions |= to_add;
353+
if (accumulator.transactions == subset.transactions) break;
354+
// Otherwise update the accumulator feerate.
355+
accumulator.feerate += m_depgraph.FeeRate(to_add);
356+
// If that does result in something better, or something with the same feerate but
357+
// smaller, return that. Even if a longer, higher-feerate intersection exists, it
358+
// does not hurt to return the shorter one (the remainder of the longer intersection
359+
// will generally be found in the next call to Intersect, but even if not, it is not
360+
// required for the improvement guarantee this function makes).
361+
if (!(accumulator.feerate << subset.feerate)) return accumulator;
362+
}
363+
}
364+
return subset;
365+
}
366+
};
367+
259368
/** Class encapsulating the state needed to find the best remaining ancestor set.
260369
*
261370
* It is initialized for an entire DepGraph, and parts of the graph can be dropped by calling

src/test/fuzz/cluster_linearize.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,123 @@ FUZZ_TARGET(clusterlin_search_finder)
484484
assert(anc_finder.AllDone());
485485
}
486486

487+
FUZZ_TARGET(clusterlin_linearization_chunking)
488+
{
489+
// Verify the behavior of LinearizationChunking.
490+
491+
// Retrieve a depgraph from the fuzz input.
492+
SpanReader reader(buffer);
493+
DepGraph<TestBitSet> depgraph;
494+
try {
495+
reader >> Using<DepGraphFormatter>(depgraph);
496+
} catch (const std::ios_base::failure&) {}
497+
498+
// Retrieve a topologically-valid subset of depgraph.
499+
auto todo = TestBitSet::Fill(depgraph.TxCount());
500+
auto subset = SetInfo(depgraph, ReadTopologicalSet(depgraph, todo, reader));
501+
502+
// Retrieve a valid linearization for depgraph.
503+
auto linearization = ReadLinearization(depgraph, reader);
504+
505+
// Construct a LinearizationChunking object, initially for the whole linearization.
506+
LinearizationChunking chunking(depgraph, linearization);
507+
508+
// Incrementally remove transactions from the chunking object, and check various properties at
509+
// every step.
510+
while (todo.Any()) {
511+
assert(chunking.NumChunksLeft() > 0);
512+
513+
// Construct linearization with just todo.
514+
std::vector<ClusterIndex> linearization_left;
515+
for (auto i : linearization) {
516+
if (todo[i]) linearization_left.push_back(i);
517+
}
518+
519+
// Compute the chunking for linearization_left.
520+
auto chunking_left = ChunkLinearization(depgraph, linearization_left);
521+
522+
// Verify that it matches the feerates of the chunks of chunking.
523+
assert(chunking.NumChunksLeft() == chunking_left.size());
524+
for (ClusterIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
525+
assert(chunking.GetChunk(i).feerate == chunking_left[i]);
526+
}
527+
528+
// Check consistency of chunking.
529+
TestBitSet combined;
530+
for (ClusterIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
531+
const auto& chunk_info = chunking.GetChunk(i);
532+
// Chunks must be non-empty.
533+
assert(chunk_info.transactions.Any());
534+
// Chunk feerates must be monotonically non-increasing.
535+
if (i > 0) assert(!(chunk_info.feerate >> chunking.GetChunk(i - 1).feerate));
536+
// Chunks must be a subset of what is left of the linearization.
537+
assert(chunk_info.transactions.IsSubsetOf(todo));
538+
// Chunks' claimed feerates must match their transactions' aggregate feerate.
539+
assert(depgraph.FeeRate(chunk_info.transactions) == chunk_info.feerate);
540+
// Chunks must be the highest-feerate remaining prefix.
541+
SetInfo<TestBitSet> accumulator, best;
542+
for (auto j : linearization) {
543+
if (todo[j] && !combined[j]) {
544+
accumulator |= SetInfo(depgraph, j);
545+
if (best.feerate.IsEmpty() || accumulator.feerate > best.feerate) {
546+
best = accumulator;
547+
}
548+
}
549+
}
550+
assert(best.transactions == chunk_info.transactions);
551+
assert(best.feerate == chunk_info.feerate);
552+
// Chunks cannot overlap.
553+
assert(!chunk_info.transactions.Overlaps(combined));
554+
combined |= chunk_info.transactions;
555+
// Chunks must be topological.
556+
for (auto idx : chunk_info.transactions) {
557+
assert((depgraph.Ancestors(idx) & todo).IsSubsetOf(combined));
558+
}
559+
}
560+
assert(combined == todo);
561+
562+
// Verify the expected properties of LinearizationChunking::Intersect:
563+
auto intersect = chunking.Intersect(subset);
564+
// - Intersecting again doesn't change the result.
565+
assert(chunking.Intersect(intersect) == intersect);
566+
// - The intersection is topological.
567+
TestBitSet intersect_anc;
568+
for (auto idx : intersect.transactions) {
569+
intersect_anc |= (depgraph.Ancestors(idx) & todo);
570+
}
571+
assert(intersect.transactions == intersect_anc);
572+
// - The claimed intersection feerate matches its transactions.
573+
assert(intersect.feerate == depgraph.FeeRate(intersect.transactions));
574+
// - The intersection may only be empty if its input is empty.
575+
assert(intersect.transactions.Any() == subset.transactions.Any());
576+
// - The intersection feerate must be as high as the input.
577+
assert(intersect.feerate >= subset.feerate);
578+
// - No non-empty intersection between the intersection and a prefix of the chunks of the
579+
// remainder of the linearization may be better than the intersection.
580+
TestBitSet prefix;
581+
for (ClusterIndex i = 0; i < chunking.NumChunksLeft(); ++i) {
582+
prefix |= chunking.GetChunk(i).transactions;
583+
auto reintersect = SetInfo(depgraph, prefix & intersect.transactions);
584+
if (!reintersect.feerate.IsEmpty()) {
585+
assert(reintersect.feerate <= intersect.feerate);
586+
}
587+
}
588+
589+
// Find a subset to remove from linearization.
590+
auto done = ReadTopologicalSet(depgraph, todo, reader);
591+
if (done.None()) {
592+
// We need to remove a non-empty subset, so fall back to the unlinearized ancestors of
593+
// the first transaction in todo if done is empty.
594+
done = depgraph.Ancestors(todo.First()) & todo;
595+
}
596+
todo -= done;
597+
chunking.MarkDone(done);
598+
subset = SetInfo(depgraph, subset.transactions - done);
599+
}
600+
601+
assert(chunking.NumChunksLeft() == 0);
602+
}
603+
487604
FUZZ_TARGET(clusterlin_linearize)
488605
{
489606
// Verify the behavior of Linearize().

0 commit comments

Comments
 (0)