Skip to content

Commit 37eb136

Browse files
committed
chore(chain,example)!: Remove ChainOracle and fix docs
1 parent 031de40 commit 37eb136

File tree

10 files changed

+43
-97
lines changed

10 files changed

+43
-97
lines changed

crates/chain/src/canonical.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,8 @@ impl<A: Anchor> CanonicalView<A> {
488488
}
489489

490490
impl<A: Anchor> CanonicalTxs<A> {
491-
/// Creates a [`CanonicalViewTask`] that resolves [`CanonicalReason`]s into [`ChainPosition`]s.
491+
/// Creates a [`CanonicalViewTask`] that resolves [`CanonicalReason`](crate::CanonicalReason)s
492+
/// into [`ChainPosition`]s.
492493
///
493494
/// This is the second phase of the canonicalization pipeline. The resulting task
494495
/// queries the chain to verify anchors for transitively anchored transactions and

crates/chain/src/canonical_task.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ pub struct CanonicalParams {
5353
///
5454
/// This task implements the first phase of canonicalization: it walks the transaction
5555
/// graph and determines which transactions are canonical (non-conflicting) and why
56-
/// (via [`CanonicalReason`]). The output is a [`CanonicalTxs`] which can then be
57-
/// further processed by [`CanonicalViewTask`] to resolve reasons into
58-
/// [`ChainPosition`]s.
56+
/// (via [`CanonicalReason`](crate::CanonicalReason)). The output is a [`CanonicalTxs`] which can
57+
/// then be further processed by [`CanonicalViewTask`](crate::CanonicalViewTask) to resolve reasons
58+
/// into [`ChainPosition`](crate::ChainPosition)s.
5959
pub struct CanonicalTask<'g, A> {
6060
tx_graph: &'g TxGraph<A>,
6161
chain_tip: BlockId,

crates/chain/src/chain_oracle.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

crates/chain/src/indexed_tx_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,8 @@ impl<A, X> IndexedTxGraph<A, X>
432432
where
433433
A: Anchor,
434434
{
435-
/// Creates a [`CanonicalTask`] to determine the [`CanonicalView`] of transactions.
435+
/// Creates a [`CanonicalTask`] to determine the [`CanonicalView`](crate::CanonicalView) of
436+
/// transactions.
436437
///
437438
/// This method delegates to the underlying [`TxGraph`] to create a [`CanonicalTask`]
438439
/// that can be used to determine which transactions are canonical based on the provided

crates/chain/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ mod tx_data_traits;
4242
pub use tx_data_traits::*;
4343
pub mod tx_graph;
4444
pub use tx_graph::TxGraph;
45-
mod chain_oracle;
46-
pub use chain_oracle::*;
4745
mod canonical_task;
4846
pub use canonical_task::*;
4947
mod canonical;

crates/chain/src/local_chain.rs

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

76
use crate::collections::BTreeMap;
8-
use crate::{Anchor, BlockId, CanonicalParams, CanonicalView, ChainOracle, Merge, TxGraph};
7+
use crate::{Anchor, BlockId, CanonicalParams, CanonicalView, Merge, TxGraph};
98
use bdk_core::{ChainQuery, ToBlockHash};
109
pub use bdk_core::{CheckPoint, CheckPointIter};
1110
use bitcoin::block::Header;
@@ -57,7 +56,7 @@ where
5756
Ok(init_cp)
5857
}
5958

60-
/// This is a local implementation of [`ChainOracle`].
59+
/// A local chain of checkpoints.
6160
#[derive(Debug, Clone)]
6261
pub struct LocalChain<D = BlockHash> {
6362
tip: CheckPoint<D>,
@@ -69,62 +68,37 @@ impl<D> PartialEq for LocalChain<D> {
6968
}
7069
}
7170

72-
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> {
71+
// Methods for `LocalChain<BlockHash>`
72+
impl LocalChain<BlockHash> {
73+
/// Check if a block is in the chain.
74+
///
75+
/// # Arguments
76+
/// * `block` - The block to check
77+
/// * `chain_tip` - The chain tip to check against
78+
///
79+
/// # Returns
80+
/// * `Some(true)` if the block is in the chain
81+
/// * `Some(false)` if the block is not in the chain
82+
/// * `None` if it cannot be determined
83+
pub fn is_block_in_chain(&self, block: BlockId, chain_tip: BlockId) -> Option<bool> {
8084
let chain_tip_cp = match self.tip.get(chain_tip.height) {
8185
// we can only determine whether `block` is in chain of `chain_tip` if `chain_tip` can
8286
// be identified in chain
8387
Some(cp) if cp.hash() == chain_tip.hash => cp,
84-
_ => return Ok(None),
88+
_ => return None,
8589
};
86-
match chain_tip_cp.get(block.height) {
87-
Some(cp) => Ok(Some(cp.hash() == block.hash)),
88-
None => Ok(None),
89-
}
90+
chain_tip_cp
91+
.get(block.height)
92+
.map(|cp| cp.hash() == block.hash)
9093
}
9194

92-
fn get_chain_tip(&self) -> Result<BlockId, Self::Error> {
93-
Ok(self.tip.block_id())
95+
/// Get the chain tip.
96+
///
97+
/// # Returns
98+
/// The [`BlockId`] of the chain tip.
99+
pub fn chain_tip(&self) -> BlockId {
100+
self.tip.block_id()
94101
}
95-
}
96-
97-
// Methods for `LocalChain<BlockHash>`
98-
impl LocalChain<BlockHash> {
99-
// /// Check if a block is in the chain.
100-
// ///
101-
// /// # Arguments
102-
// /// * `block` - The block to check
103-
// /// * `chain_tip` - The chain tip to check against
104-
// ///
105-
// /// # Returns
106-
// /// * `Some(true)` if the block is in the chain
107-
// /// * `Some(false)` if the block is not in the chain
108-
// /// * `None` if it cannot be determined
109-
// pub fn is_block_in_chain(&self, block: BlockId, chain_tip: BlockId) -> Option<bool> {
110-
// let chain_tip_cp = match self.tip.get(chain_tip.height) {
111-
// // we can only determine whether `block` is in chain of `chain_tip` if `chain_tip`
112-
// can // be identified in chain
113-
// Some(cp) if cp.hash() == chain_tip.hash => cp,
114-
// _ => return None,
115-
// };
116-
// chain_tip_cp
117-
// .get(block.height)
118-
// .map(|cp| cp.hash() == block.hash)
119-
// }
120-
121-
// /// Get the chain tip.
122-
// ///
123-
// /// # Returns
124-
// /// The [`BlockId`] of the chain tip.
125-
// pub fn chain_tip(&self) -> BlockId {
126-
// self.tip.block_id()
127-
// }
128102

129103
/// Canonicalize a transaction graph using this chain.
130104
///
@@ -151,11 +125,7 @@ impl LocalChain<BlockHash> {
151125
while let Some(request) = task.next_query() {
152126
let mut best_block_id = None;
153127
for block_id in &request {
154-
if self
155-
.is_block_in_chain(*block_id, chain_tip)
156-
.expect("infallible")
157-
== Some(true)
158-
{
128+
if self.is_block_in_chain(*block_id, chain_tip) == Some(true) {
159129
best_block_id = Some(*block_id);
160130
break;
161131
}

crates/chain/src/tx_graph.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
//! assert!(changeset.is_empty());
125125
//! ```
126126
//! [`insert_txout`]: TxGraph::insert_txout
127+
//! [`CanonicalView`]: crate::CanonicalView
127128
128129
use crate::collections::*;
129130
use crate::CanonicalParams;
@@ -978,6 +979,8 @@ impl<A: Anchor> TxGraph<A> {
978979
/// that can be used to determine which transactions are canonical based on the provided
979980
/// parameters. The task handles the stateless canonicalization logic and can be polled
980981
/// for anchor verification requests.
982+
///
983+
/// [`CanonicalView`]: crate::CanonicalView
981984
pub fn canonical_task(
982985
&'_ self,
983986
chain_tip: BlockId,

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ fn insert_relevant_txs() {
305305
}
306306

307307
/// Ensure consistency IndexedTxGraph list_* and balance methods. These methods lists
308-
/// relevant txouts and utxos from the information fetched from a ChainOracle (here a LocalChain).
308+
/// relevant txouts and utxos from the information fetched from a LocalChain.
309309
///
310310
/// Test Setup:
311311
///

crates/chain/tests/test_tx_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bdk_chain::{
77
local_chain::LocalChain,
88
tx_graph::{self, CalculateFeeError},
99
tx_graph::{ChangeSet, TxGraph},
10-
Anchor, ChainOracle, ChainPosition, Merge,
10+
Anchor, ChainPosition, Merge,
1111
};
1212
use bdk_testenv::{block_id, hash, utils::new_tx};
1313
use bitcoin::hex::FromHex;
@@ -758,7 +758,7 @@ fn test_walk_ancestors() {
758758
let tx_node = graph.get_tx_node(tx.compute_txid())?;
759759
for block in tx_node.anchors {
760760
match local_chain.is_block_in_chain(block.anchor_block(), tip.block_id()) {
761-
Ok(Some(true)) => return None,
761+
Some(true) => return None,
762762
_ => continue,
763763
}
764764
}

examples/example_cli/src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use bdk_chain::ConfirmationBlockTime;
2424
use bdk_chain::{
2525
indexer::keychain_txout::{self, KeychainTxOutIndex},
2626
local_chain::{self, LocalChain},
27-
tx_graph, CanonicalTxOut, ChainOracle, ChainPosition, DescriptorExt, IndexedTxGraph, Merge,
27+
tx_graph, CanonicalTxOut, ChainPosition, DescriptorExt, IndexedTxGraph, Merge,
2828
};
2929
use bdk_coin_select::{
3030
metrics::LowestFee, Candidate, ChangePolicy, CoinSelector, DrainWeights, FeeRate, Target,
@@ -390,9 +390,7 @@ pub fn create_tx(
390390
version: transaction::Version::TWO,
391391
lock_time: assets
392392
.absolute_timelock
393-
.unwrap_or(absolute::LockTime::from_height(
394-
chain.get_chain_tip()?.height,
395-
)?),
393+
.unwrap_or(absolute::LockTime::from_height(chain.chain_tip().height)?),
396394
input: selected
397395
.iter()
398396
.map(|(plan, utxo)| TxIn {
@@ -554,7 +552,7 @@ pub fn handle_commands<CS: clap::Subcommand, S: clap::Args>(
554552
Commands::TxOut { txout_cmd } => {
555553
let graph = &*graph.lock().unwrap();
556554
let chain = &*chain.lock().unwrap();
557-
let chain_tip = chain.get_chain_tip()?;
555+
let chain_tip = chain.chain_tip();
558556
let outpoints = graph.index.outpoints();
559557

560558
match txout_cmd {

0 commit comments

Comments
 (0)