From: 志宇 Date: Tue, 26 Nov 2024 01:36:59 +0000 (+1100) Subject: feat(chain)!: Add `run_until_finished` methods X-Git-Tag: v1.0.0-beta.6~2^2~14 X-Git-Url: http://internal-gitweb-vhost/script/%22https:/enum.EncodeError.html?a=commitdiff_plain;h=f6192a615a7aea2ca9bdcca199356ea4d0386ce1;p=bdk 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. --- diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 444328f1..f00ab9a8 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -435,7 +435,7 @@ impl TxGraph { /// /// The supplied closure returns an `Option`, allowing the caller to map each `Transaction` /// it visits and decide whether to visit ancestors. - pub fn walk_ancestors<'g, T, F, O>(&'g self, tx: T, walk_map: F) -> TxAncestors<'g, A, F> + pub fn walk_ancestors<'g, T, F, O>(&'g self, tx: T, walk_map: F) -> TxAncestors<'g, A, F, O> where T: Into>, F: FnMut(usize, Arc) -> Option + 'g, @@ -453,7 +453,7 @@ impl TxGraph { /// /// The supplied closure returns an `Option`, allowing the caller to map each node it visits /// and decide whether to visit descendants. - pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants + pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants where F: FnMut(usize, Txid) -> Option + 'g, { @@ -470,7 +470,7 @@ impl TxGraph { &'g self, tx: &'g Transaction, walk_map: F, - ) -> TxDescendants + ) -> TxDescendants where F: FnMut(usize, Txid) -> Option + 'g, { @@ -1329,14 +1329,20 @@ impl AsRef> for TxGraph { /// Returned by the [`walk_ancestors`] method of [`TxGraph`]. /// /// [`walk_ancestors`]: TxGraph::walk_ancestors -pub struct TxAncestors<'g, A, F> { +pub struct TxAncestors<'g, A, F, O> +where + F: FnMut(usize, Arc) -> Option, +{ graph: &'g TxGraph, visited: HashSet, queue: VecDeque<(usize, Arc)>, filter_map: F, } -impl<'g, A, F> TxAncestors<'g, A, F> { +impl<'g, A, F, O> TxAncestors<'g, A, F, O> +where + F: FnMut(usize, Arc) -> Option, +{ /// Creates a `TxAncestors` that includes the starting `Transaction` when iterating. pub(crate) fn new_include_root( graph: &'g TxGraph, @@ -1411,6 +1417,11 @@ impl<'g, A, F> TxAncestors<'g, A, F> { ancestors } + /// Traverse all ancestors that are not filtered out by the provided closure. + pub fn run_until_finished(self) { + self.for_each(|_| {}) + } + fn populate_queue(&mut self, depth: usize, tx: Arc) { let ancestors = tx .input @@ -1423,7 +1434,7 @@ impl<'g, A, F> TxAncestors<'g, A, F> { } } -impl<'g, A, F, O> Iterator for TxAncestors<'g, A, F> +impl<'g, A, F, O> Iterator for TxAncestors<'g, A, F, O> where F: FnMut(usize, Arc) -> Option, { @@ -1449,14 +1460,20 @@ where /// Returned by the [`walk_descendants`] method of [`TxGraph`]. /// /// [`walk_descendants`]: TxGraph::walk_descendants -pub struct TxDescendants<'g, A, F> { +pub struct TxDescendants<'g, A, F, O> +where + F: FnMut(usize, Txid) -> Option, +{ graph: &'g TxGraph, visited: HashSet, queue: VecDeque<(usize, Txid)>, filter_map: F, } -impl<'g, A, F> TxDescendants<'g, A, F> { +impl<'g, A, F, O> TxDescendants<'g, A, F, O> +where + F: FnMut(usize, Txid) -> Option, +{ /// Creates a `TxDescendants` that includes the starting `txid` when iterating. #[allow(unused)] pub(crate) fn new_include_root(graph: &'g TxGraph, txid: Txid, filter_map: F) -> Self { @@ -1520,9 +1537,12 @@ impl<'g, A, F> TxDescendants<'g, A, F> { } descendants } -} -impl<'g, A, F> TxDescendants<'g, A, F> { + /// Traverse all descendants that are not filtered out by the provided closure. + pub fn run_until_finished(self) { + self.for_each(|_| {}) + } + fn populate_queue(&mut self, depth: usize, txid: Txid) { let spend_paths = self .graph @@ -1534,7 +1554,7 @@ impl<'g, A, F> TxDescendants<'g, A, F> { } } -impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F> +impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F, O> where F: FnMut(usize, Txid) -> Option, {