Skip to content

Commit 64733ca

Browse files
committed
feat(chain)!: Add run_until_finished methods
Add `run_until_finished` methods for `TxAncestors` and `TxDescendants`. This is useful for traversing until the internal closure returns `None`. Signatures of `TxAncestors` and `TxDescendants` are changed to enforce generic bounds in the type definition.
1 parent 5700c5e commit 64733ca

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

crates/chain/src/canonical_iter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
123123
None
124124
}
125125
})
126-
.for_each(|_| {})
126+
.run_until_finished()
127127
}
128128

129129
fn mark_canonical(&mut self, tx: Arc<Transaction>, reason: CanonicalReason<A>) {
@@ -150,7 +150,7 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
150150
}
151151
},
152152
)
153-
.for_each(|_| {})
153+
.run_until_finished()
154154
}
155155
}
156156

crates/chain/src/tx_graph.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl<A: Clone + Ord> TxGraph<A> {
437437
///
438438
/// The supplied closure returns an `Option<T>`, allowing the caller to map each `Transaction`
439439
/// it visits and decide whether to visit ancestors.
440-
pub fn walk_ancestors<'g, T, F, O>(&'g self, tx: T, walk_map: F) -> TxAncestors<'g, A, F>
440+
pub fn walk_ancestors<'g, T, F, O>(&'g self, tx: T, walk_map: F) -> TxAncestors<'g, A, F, O>
441441
where
442442
T: Into<Arc<Transaction>>,
443443
F: FnMut(usize, Arc<Transaction>) -> Option<O> + 'g,
@@ -455,7 +455,7 @@ impl<A: Clone + Ord> TxGraph<A> {
455455
///
456456
/// The supplied closure returns an `Option<T>`, allowing the caller to map each node it visits
457457
/// and decide whether to visit descendants.
458-
pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants<A, F>
458+
pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants<A, F, O>
459459
where
460460
F: FnMut(usize, Txid) -> Option<O> + 'g,
461461
{
@@ -472,7 +472,7 @@ impl<A> TxGraph<A> {
472472
&'g self,
473473
tx: &'g Transaction,
474474
walk_map: F,
475-
) -> TxDescendants<A, F>
475+
) -> TxDescendants<A, F, O>
476476
where
477477
F: FnMut(usize, Txid) -> Option<O> + 'g,
478478
{
@@ -1151,14 +1151,20 @@ impl<A> AsRef<TxGraph<A>> for TxGraph<A> {
11511151
/// Returned by the [`walk_ancestors`] method of [`TxGraph`].
11521152
///
11531153
/// [`walk_ancestors`]: TxGraph::walk_ancestors
1154-
pub struct TxAncestors<'g, A, F> {
1154+
pub struct TxAncestors<'g, A, F, O>
1155+
where
1156+
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
1157+
{
11551158
graph: &'g TxGraph<A>,
11561159
visited: HashSet<Txid>,
11571160
queue: VecDeque<(usize, Arc<Transaction>)>,
11581161
filter_map: F,
11591162
}
11601163

1161-
impl<'g, A, F> TxAncestors<'g, A, F> {
1164+
impl<'g, A, F, O> TxAncestors<'g, A, F, O>
1165+
where
1166+
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
1167+
{
11621168
/// Creates a `TxAncestors` that includes the starting `Transaction` when iterating.
11631169
pub(crate) fn new_include_root(
11641170
graph: &'g TxGraph<A>,
@@ -1233,6 +1239,11 @@ impl<'g, A, F> TxAncestors<'g, A, F> {
12331239
ancestors
12341240
}
12351241

1242+
/// Traverse all ancestors that are not filtered out by the provided closure.
1243+
pub fn run_until_finished(self) {
1244+
self.for_each(|_| {})
1245+
}
1246+
12361247
fn populate_queue(&mut self, depth: usize, tx: Arc<Transaction>) {
12371248
let ancestors = tx
12381249
.input
@@ -1245,7 +1256,7 @@ impl<'g, A, F> TxAncestors<'g, A, F> {
12451256
}
12461257
}
12471258

1248-
impl<'g, A, F, O> Iterator for TxAncestors<'g, A, F>
1259+
impl<'g, A, F, O> Iterator for TxAncestors<'g, A, F, O>
12491260
where
12501261
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
12511262
{
@@ -1271,14 +1282,20 @@ where
12711282
/// Returned by the [`walk_descendants`] method of [`TxGraph`].
12721283
///
12731284
/// [`walk_descendants`]: TxGraph::walk_descendants
1274-
pub struct TxDescendants<'g, A, F> {
1285+
pub struct TxDescendants<'g, A, F, O>
1286+
where
1287+
F: FnMut(usize, Txid) -> Option<O>,
1288+
{
12751289
graph: &'g TxGraph<A>,
12761290
visited: HashSet<Txid>,
12771291
queue: VecDeque<(usize, Txid)>,
12781292
filter_map: F,
12791293
}
12801294

1281-
impl<'g, A, F> TxDescendants<'g, A, F> {
1295+
impl<'g, A, F, O> TxDescendants<'g, A, F, O>
1296+
where
1297+
F: FnMut(usize, Txid) -> Option<O>,
1298+
{
12821299
/// Creates a `TxDescendants` that includes the starting `txid` when iterating.
12831300
#[allow(unused)]
12841301
pub(crate) fn new_include_root(graph: &'g TxGraph<A>, txid: Txid, filter_map: F) -> Self {
@@ -1342,9 +1359,12 @@ impl<'g, A, F> TxDescendants<'g, A, F> {
13421359
}
13431360
descendants
13441361
}
1345-
}
13461362

1347-
impl<'g, A, F> TxDescendants<'g, A, F> {
1363+
/// Traverse all descendants that are not filtered out by the provided closure.
1364+
pub fn run_until_finished(self) {
1365+
self.for_each(|_| {})
1366+
}
1367+
13481368
fn populate_queue(&mut self, depth: usize, txid: Txid) {
13491369
let spend_paths = self
13501370
.graph
@@ -1356,7 +1376,7 @@ impl<'g, A, F> TxDescendants<'g, A, F> {
13561376
}
13571377
}
13581378

1359-
impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F>
1379+
impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F, O>
13601380
where
13611381
F: FnMut(usize, Txid) -> Option<O>,
13621382
{

0 commit comments

Comments
 (0)