Skip to content

Commit 8b19cb3

Browse files
committed
refactor(chain)!: update ChainOracle and TxGraph methods to being infallible
1 parent faf520d commit 8b19cb3

File tree

7 files changed

+96
-268
lines changed

7 files changed

+96
-268
lines changed

crates/chain/src/canonical_iter.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,14 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
9090
txid: Txid,
9191
tx: Arc<Transaction>,
9292
anchors: &BTreeSet<A>,
93-
) -> Result<(), C::Error> {
93+
) {
9494
for anchor in anchors {
9595
let in_chain_opt = self
9696
.chain
97-
.is_block_in_chain(anchor.anchor_block(), self.chain_tip)?;
97+
.is_block_in_chain(anchor.anchor_block(), self.chain_tip);
9898
if in_chain_opt == Some(true) {
9999
self.mark_canonical(txid, tx, CanonicalReason::from_anchor(anchor.clone()));
100-
return Ok(());
100+
return;
101101
}
102102
}
103103
// cannot determine
@@ -112,7 +112,6 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
112112
)
113113
.confirmation_height_upper_bound(),
114114
));
115-
Ok(())
116115
}
117116

118117
/// Marks `tx` and it's ancestors as canonical and mark all conflicts of these as
@@ -201,7 +200,7 @@ impl<'g, A: Anchor, C: ChainOracle> CanonicalIter<'g, A, C> {
201200
}
202201

203202
impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
204-
type Item = Result<(Txid, Arc<Transaction>, CanonicalReason<A>), C::Error>;
203+
type Item = (Txid, Arc<Transaction>, CanonicalReason<A>);
205204

206205
fn next(&mut self) -> Option<Self::Item> {
207206
loop {
@@ -211,7 +210,7 @@ impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
211210
.get(&txid)
212211
.cloned()
213212
.expect("reason must exist");
214-
return Some(Ok((txid, tx, reason)));
213+
return Some((txid, tx, reason));
215214
}
216215

217216
if let Some((txid, tx)) = self.unprocessed_assumed_txs.next() {
@@ -222,9 +221,7 @@ impl<A: Anchor, C: ChainOracle> Iterator for CanonicalIter<'_, A, C> {
222221

223222
if let Some((txid, tx, anchors)) = self.unprocessed_anchored_txs.next() {
224223
if !self.is_canonicalized(txid) {
225-
if let Err(err) = self.scan_anchors(txid, tx, anchors) {
226-
return Some(Err(err));
227-
}
224+
self.scan_anchors(txid, tx, anchors);
228225
}
229226
continue;
230227
}

crates/chain/src/chain_oracle.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,12 @@ use crate::BlockId;
77
///
88
/// [`is_block_in_chain`]: Self::is_block_in_chain
99
pub trait ChainOracle {
10-
/// Error type.
11-
type Error: core::fmt::Debug;
12-
1310
/// Determines whether `block` of [`BlockId`] exists as an ancestor of `chain_tip`.
1411
///
1512
/// If `None` is returned, it means the implementation cannot determine whether `block` exists
1613
/// under `chain_tip`.
17-
fn is_block_in_chain(
18-
&self,
19-
block: BlockId,
20-
chain_tip: BlockId,
21-
) -> Result<Option<bool>, Self::Error>;
14+
fn is_block_in_chain(&self, block: BlockId, chain_tip: BlockId) -> Option<bool>;
2215

2316
/// Get the best chain's chain tip.
24-
fn get_chain_tip(&self) -> Result<BlockId, Self::Error>;
17+
fn get_chain_tip(&self) -> BlockId;
2518
}

crates/chain/src/indexed_tx_graph.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Contains the [`IndexedTxGraph`] and associated types. Refer to the
22
//! [`IndexedTxGraph`] documentation for more.
33
use core::{
4-
convert::Infallible,
54
fmt::{self, Debug},
65
ops::RangeBounds,
76
};
@@ -439,46 +438,21 @@ where
439438
///
440439
/// The spk index range can be contrained with `range`.
441440
///
442-
/// # Error
443-
///
444-
/// If the [`ChainOracle`] implementation (`chain`) fails, an error will be returned with the
445-
/// returned item.
446-
///
447-
/// If the [`ChainOracle`] is infallible,
448-
/// [`list_expected_spk_txids`](Self::list_expected_spk_txids) can be used instead.
449-
pub fn try_list_expected_spk_txids<'a, C, I>(
441+
pub fn list_expected_spk_txids<'a, C, I>(
450442
&'a self,
451443
chain: &'a C,
452444
chain_tip: BlockId,
453445
spk_index_range: impl RangeBounds<I> + 'a,
454-
) -> impl Iterator<Item = Result<(ScriptBuf, Txid), C::Error>> + 'a
446+
) -> impl Iterator<Item = (ScriptBuf, Txid)> + 'a
455447
where
456448
C: ChainOracle,
457449
X: AsRef<SpkTxOutIndex<I>> + 'a,
458450
I: fmt::Debug + Clone + Ord + 'a,
459451
{
460452
self.graph
461-
.try_list_expected_spk_txids(chain, chain_tip, &self.index, spk_index_range)
453+
.list_expected_spk_txids(chain, chain_tip, &self.index, spk_index_range)
462454
}
463455

464-
/// List txids that are expected to exist under the given spks.
465-
///
466-
/// This is the infallible version of
467-
/// [`try_list_expected_spk_txids`](Self::try_list_expected_spk_txids).
468-
pub fn list_expected_spk_txids<'a, C, I>(
469-
&'a self,
470-
chain: &'a C,
471-
chain_tip: BlockId,
472-
spk_index_range: impl RangeBounds<I> + 'a,
473-
) -> impl Iterator<Item = (ScriptBuf, Txid)> + 'a
474-
where
475-
C: ChainOracle<Error = Infallible>,
476-
X: AsRef<SpkTxOutIndex<I>> + 'a,
477-
I: fmt::Debug + Clone + Ord + 'a,
478-
{
479-
self.try_list_expected_spk_txids(chain, chain_tip, spk_index_range)
480-
.map(|r| r.expect("infallible"))
481-
}
482456
}
483457

484458
impl<A, I> AsRef<TxGraph<A>> for IndexedTxGraph<A, I> {

crates/chain/src/local_chain.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! The [`LocalChain`] is a local implementation of [`ChainOracle`].
22
3-
use core::convert::Infallible;
43
use core::fmt;
54
use core::ops::RangeBounds;
65

@@ -70,27 +69,21 @@ impl<D> PartialEq for LocalChain<D> {
7069
}
7170

7271
impl<D> ChainOracle for LocalChain<D> {
73-
type Error = Infallible;
74-
75-
fn is_block_in_chain(
76-
&self,
77-
block: BlockId,
78-
chain_tip: BlockId,
79-
) -> Result<Option<bool>, Self::Error> {
72+
fn is_block_in_chain(&self, block: BlockId, chain_tip: BlockId) -> Option<bool> {
8073
let chain_tip_cp = match self.tip.get(chain_tip.height) {
8174
// we can only determine whether `block` is in chain of `chain_tip` if `chain_tip` can
8275
// be identified in chain
8376
Some(cp) if cp.hash() == chain_tip.hash => cp,
84-
_ => return Ok(None),
77+
_ => return None,
8578
};
8679
match chain_tip_cp.get(block.height) {
87-
Some(cp) => Ok(Some(cp.hash() == block.hash)),
88-
None => Ok(None),
80+
Some(cp) => Some(cp.hash() == block.hash),
81+
None => None,
8982
}
9083
}
9184

92-
fn get_chain_tip(&self) -> Result<BlockId, Self::Error> {
93-
Ok(self.tip.block_id())
85+
fn get_chain_tip(&self) -> BlockId {
86+
self.tip.block_id()
9487
}
9588
}
9689

0 commit comments

Comments
 (0)