Skip to content

Commit f6192a6

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 0aa39f9 commit f6192a6

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

crates/chain/src/tx_graph.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl<A: Clone + Ord> TxGraph<A> {
435435
///
436436
/// The supplied closure returns an `Option<T>`, allowing the caller to map each `Transaction`
437437
/// it visits and decide whether to visit ancestors.
438-
pub fn walk_ancestors<'g, T, F, O>(&'g self, tx: T, walk_map: F) -> TxAncestors<'g, A, F>
438+
pub fn walk_ancestors<'g, T, F, O>(&'g self, tx: T, walk_map: F) -> TxAncestors<'g, A, F, O>
439439
where
440440
T: Into<Arc<Transaction>>,
441441
F: FnMut(usize, Arc<Transaction>) -> Option<O> + 'g,
@@ -453,7 +453,7 @@ impl<A: Clone + Ord> TxGraph<A> {
453453
///
454454
/// The supplied closure returns an `Option<T>`, allowing the caller to map each node it visits
455455
/// and decide whether to visit descendants.
456-
pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants<A, F>
456+
pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants<A, F, O>
457457
where
458458
F: FnMut(usize, Txid) -> Option<O> + 'g,
459459
{
@@ -470,7 +470,7 @@ impl<A> TxGraph<A> {
470470
&'g self,
471471
tx: &'g Transaction,
472472
walk_map: F,
473-
) -> TxDescendants<A, F>
473+
) -> TxDescendants<A, F, O>
474474
where
475475
F: FnMut(usize, Txid) -> Option<O> + 'g,
476476
{
@@ -1329,14 +1329,20 @@ impl<A> AsRef<TxGraph<A>> for TxGraph<A> {
13291329
/// Returned by the [`walk_ancestors`] method of [`TxGraph`].
13301330
///
13311331
/// [`walk_ancestors`]: TxGraph::walk_ancestors
1332-
pub struct TxAncestors<'g, A, F> {
1332+
pub struct TxAncestors<'g, A, F, O>
1333+
where
1334+
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
1335+
{
13331336
graph: &'g TxGraph<A>,
13341337
visited: HashSet<Txid>,
13351338
queue: VecDeque<(usize, Arc<Transaction>)>,
13361339
filter_map: F,
13371340
}
13381341

1339-
impl<'g, A, F> TxAncestors<'g, A, F> {
1342+
impl<'g, A, F, O> TxAncestors<'g, A, F, O>
1343+
where
1344+
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
1345+
{
13401346
/// Creates a `TxAncestors` that includes the starting `Transaction` when iterating.
13411347
pub(crate) fn new_include_root(
13421348
graph: &'g TxGraph<A>,
@@ -1411,6 +1417,11 @@ impl<'g, A, F> TxAncestors<'g, A, F> {
14111417
ancestors
14121418
}
14131419

1420+
/// Traverse all ancestors that are not filtered out by the provided closure.
1421+
pub fn run_until_finished(self) {
1422+
self.for_each(|_| {})
1423+
}
1424+
14141425
fn populate_queue(&mut self, depth: usize, tx: Arc<Transaction>) {
14151426
let ancestors = tx
14161427
.input
@@ -1423,7 +1434,7 @@ impl<'g, A, F> TxAncestors<'g, A, F> {
14231434
}
14241435
}
14251436

1426-
impl<'g, A, F, O> Iterator for TxAncestors<'g, A, F>
1437+
impl<'g, A, F, O> Iterator for TxAncestors<'g, A, F, O>
14271438
where
14281439
F: FnMut(usize, Arc<Transaction>) -> Option<O>,
14291440
{
@@ -1449,14 +1460,20 @@ where
14491460
/// Returned by the [`walk_descendants`] method of [`TxGraph`].
14501461
///
14511462
/// [`walk_descendants`]: TxGraph::walk_descendants
1452-
pub struct TxDescendants<'g, A, F> {
1463+
pub struct TxDescendants<'g, A, F, O>
1464+
where
1465+
F: FnMut(usize, Txid) -> Option<O>,
1466+
{
14531467
graph: &'g TxGraph<A>,
14541468
visited: HashSet<Txid>,
14551469
queue: VecDeque<(usize, Txid)>,
14561470
filter_map: F,
14571471
}
14581472

1459-
impl<'g, A, F> TxDescendants<'g, A, F> {
1473+
impl<'g, A, F, O> TxDescendants<'g, A, F, O>
1474+
where
1475+
F: FnMut(usize, Txid) -> Option<O>,
1476+
{
14601477
/// Creates a `TxDescendants` that includes the starting `txid` when iterating.
14611478
#[allow(unused)]
14621479
pub(crate) fn new_include_root(graph: &'g TxGraph<A>, txid: Txid, filter_map: F) -> Self {
@@ -1520,9 +1537,12 @@ impl<'g, A, F> TxDescendants<'g, A, F> {
15201537
}
15211538
descendants
15221539
}
1523-
}
15241540

1525-
impl<'g, A, F> TxDescendants<'g, A, F> {
1541+
/// Traverse all descendants that are not filtered out by the provided closure.
1542+
pub fn run_until_finished(self) {
1543+
self.for_each(|_| {})
1544+
}
1545+
15261546
fn populate_queue(&mut self, depth: usize, txid: Txid) {
15271547
let spend_paths = self
15281548
.graph
@@ -1534,7 +1554,7 @@ impl<'g, A, F> TxDescendants<'g, A, F> {
15341554
}
15351555
}
15361556

1537-
impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F>
1557+
impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F, O>
15381558
where
15391559
F: FnMut(usize, Txid) -> Option<O>,
15401560
{

0 commit comments

Comments
 (0)