Skip to content

Commit 9808712

Browse files
evanlinjinruben
authored andcommitted
chore(chain): Address CanonicalIter nitpicks
1 parent 819daca commit 9808712

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

crates/chain/src/canonical_iter.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ pub struct CanonicalIter<'g, A, C> {
1313
chain: &'g C,
1414
chain_tip: BlockId,
1515

16-
pending_anchored: Box<dyn Iterator<Item = (Txid, Arc<Transaction>, &'g BTreeSet<A>)> + 'g>,
17-
pending_last_seen: Box<dyn Iterator<Item = (Txid, Arc<Transaction>, u64)> + 'g>,
18-
pending_remaining: VecDeque<(Txid, Arc<Transaction>, u32)>,
16+
unprocessed_txs_with_anchors:
17+
Box<dyn Iterator<Item = (Txid, Arc<Transaction>, &'g BTreeSet<A>)> + 'g>,
18+
unprocessed_txs_with_last_seens: Box<dyn Iterator<Item = (Txid, Arc<Transaction>, u64)> + 'g>,
19+
unprocessed_txs_left_over: VecDeque<(Txid, Arc<Transaction>, u32)>,
1920

2021
canonical: HashMap<Txid, (Arc<Transaction>, CanonicalReason<A>)>,
2122
not_canonical: HashSet<Txid>,
@@ -41,9 +42,9 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
4142
tx_graph,
4243
chain,
4344
chain_tip,
44-
pending_anchored,
45-
pending_last_seen,
46-
pending_remaining: VecDeque::new(),
45+
unprocessed_txs_with_anchors: pending_anchored,
46+
unprocessed_txs_with_last_seens: pending_last_seen,
47+
unprocessed_txs_left_over: VecDeque::new(),
4748
canonical: HashMap::new(),
4849
not_canonical: HashSet::new(),
4950
queue: VecDeque::new(),
@@ -67,27 +68,29 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
6768
.chain
6869
.is_block_in_chain(anchor.anchor_block(), self.chain_tip)?;
6970
if in_chain_opt == Some(true) {
70-
self.mark_canonical(tx, CanonicalReason::from_anchor(anchor.clone()));
71+
self.mark_canonical(txid, tx, CanonicalReason::from_anchor(anchor.clone()));
7172
return Ok(());
7273
}
7374
}
7475
// cannot determine
75-
self.pending_remaining.push_back((
76+
self.unprocessed_txs_left_over.push_back((
7677
txid,
7778
tx,
7879
anchors
7980
.iter()
8081
.last()
81-
.unwrap()
82+
.expect(
83+
"tx taken from `unprocessed_txs_with_anchors` so it must atleast have an anchor",
84+
)
8285
.confirmation_height_upper_bound(),
8386
));
8487
Ok(())
8588
}
8689

8790
/// Marks a transaction and it's ancestors as canoncial. Mark all conflicts of these as
8891
/// `not_canonical`.
89-
fn mark_canonical(&mut self, tx: Arc<Transaction>, reason: CanonicalReason<A>) {
90-
let starting_txid = tx.compute_txid();
92+
fn mark_canonical(&mut self, txid: Txid, tx: Arc<Transaction>, reason: CanonicalReason<A>) {
93+
let starting_txid = txid;
9194
let mut is_root = true;
9295
TxAncestors::new_include_root(
9396
self.tx_graph,
@@ -126,11 +129,11 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
126129
Some(())
127130
},
128131
)
129-
.for_each(|_| {})
132+
.run_until_finished()
130133
}
131134
}
132135

133-
impl<'g, A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'g, A, C> {
136+
impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
134137
type Item = Result<(Txid, Arc<Transaction>, CanonicalReason<A>), C::Error>;
135138

136139
fn next(&mut self) -> Option<Self::Item> {
@@ -144,7 +147,7 @@ impl<'g, A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'g, A, C> {
144147
return Some(Ok((txid, tx, reason)));
145148
}
146149

147-
if let Some((txid, tx, anchors)) = self.pending_anchored.next() {
150+
if let Some((txid, tx, anchors)) = self.unprocessed_txs_with_anchors.next() {
148151
if !self.is_canonicalized(txid) {
149152
if let Err(err) = self.scan_anchors(txid, tx, anchors) {
150153
return Some(Err(err));
@@ -153,18 +156,18 @@ impl<'g, A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'g, A, C> {
153156
continue;
154157
}
155158

156-
if let Some((txid, tx, last_seen)) = self.pending_last_seen.next() {
159+
if let Some((txid, tx, last_seen)) = self.unprocessed_txs_with_last_seens.next() {
157160
if !self.is_canonicalized(txid) {
158161
let observed_in = ObservedIn::Mempool(last_seen);
159-
self.mark_canonical(tx, CanonicalReason::from_observed_in(observed_in));
162+
self.mark_canonical(txid, tx, CanonicalReason::from_observed_in(observed_in));
160163
}
161164
continue;
162165
}
163166

164-
if let Some((txid, tx, height)) = self.pending_remaining.pop_front() {
167+
if let Some((txid, tx, height)) = self.unprocessed_txs_left_over.pop_front() {
165168
if !self.is_canonicalized(txid) {
166169
let observed_in = ObservedIn::Block(height);
167-
self.mark_canonical(tx, CanonicalReason::from_observed_in(observed_in));
170+
self.mark_canonical(txid, tx, CanonicalReason::from_observed_in(observed_in));
168171
}
169172
continue;
170173
}
@@ -212,7 +215,7 @@ impl<A: Clone> CanonicalReason<A> {
212215
}
213216
}
214217

215-
/// Constructs a [`CanonicalReason`] from a `last_seen` value.
218+
/// Constructs a [`CanonicalReason`] from an `observed_in` value.
216219
pub fn from_observed_in(observed_in: ObservedIn) -> Self {
217220
Self::ObservedIn {
218221
observed_in,
@@ -230,11 +233,8 @@ impl<A: Clone> CanonicalReason<A> {
230233
anchor: anchor.clone(),
231234
descendant: Some(descendant),
232235
},
233-
CanonicalReason::ObservedIn {
234-
observed_in: last_seen,
235-
..
236-
} => Self::ObservedIn {
237-
observed_in: *last_seen,
236+
CanonicalReason::ObservedIn { observed_in, .. } => Self::ObservedIn {
237+
observed_in: *observed_in,
238238
descendant: Some(descendant),
239239
},
240240
}

0 commit comments

Comments
 (0)