Skip to content

Commit 18e8da3

Browse files
docs: Add doctest for Anchor implementation on BlockId
1 parent 480c273 commit 18e8da3

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

crates/chain/src/chain_data.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ impl From<(&u32, &BlockHash)> for BlockId {
140140
}
141141

142142
/// An [`Anchor`] implementation that also records the exact confirmation height of the transaction.
143+
///
144+
/// Refer to [`Anchor`] for more details.
143145
#[derive(Debug, Default, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
144146
#[cfg_attr(
145147
feature = "serde",
@@ -168,6 +170,8 @@ impl Anchor for ConfirmationHeightAnchor {
168170

169171
/// An [`Anchor`] implementation that also records the exact confirmation time and height of the
170172
/// transaction.
173+
///
174+
/// Refer to [`Anchor`] for more details.
171175
#[derive(Debug, Default, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
172176
#[cfg_attr(
173177
feature = "serde",

crates/chain/src/tx_data_traits.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,58 @@ impl ForEachTxOut for Transaction {
4444
/// assume that transaction A is also confirmed in the best chain. This does not necessarily mean
4545
/// that transaction A is confirmed in block B. It could also mean transaction A is confirmed in a
4646
/// parent block of B.
47+
///
48+
/// ```
49+
/// # use bdk_chain::local_chain::LocalChain;
50+
/// # use bdk_chain::tx_graph::TxGraph;
51+
/// # use bdk_chain::BlockId;
52+
/// # use bdk_chain::ConfirmationHeightAnchor;
53+
/// # use bdk_chain::example_utils::*;
54+
/// # use bitcoin::hashes::Hash;
55+
///
56+
/// // Initialize the local chain with two blocks.
57+
/// let chain = LocalChain::from_blocks(
58+
/// [
59+
/// (1, Hash::hash("first".as_bytes())),
60+
/// (2, Hash::hash("second".as_bytes())),
61+
/// ]
62+
/// .into_iter()
63+
/// .collect(),
64+
/// );
65+
///
66+
/// // Transaction to be inserted into `TxGraph`s with different anchor types.
67+
/// let tx = tx_from_hex(RAW_TX_1);
68+
///
69+
/// // Insert `tx` into a `TxGraph` that uses `BlockId` as the anchor type.
70+
/// // When a transaction is anchored with `BlockId`, the anchor block and the confirmation block of
71+
/// // the transaction is the same block.
72+
/// let mut graph_a = TxGraph::<BlockId>::default();
73+
/// let _ = graph_a.insert_tx(tx.clone());
74+
/// graph_a.insert_anchor(
75+
/// tx.txid(),
76+
/// BlockId {
77+
/// height: 1,
78+
/// hash: Hash::hash("first".as_bytes()),
79+
/// },
80+
/// );
81+
///
82+
/// // Insert `tx` into a `TxGraph` that uses `ConfirmationHeightAnchor` as the anchor type.
83+
/// // When a transaction is anchored with `ConfirmationHeightAnchor`, the anchor block and
84+
/// // confirmation block can be different. However, the confirmation block cannot be higher than
85+
/// // the anchor block and both blocks must be in the same chain for the anchor to be valid.
86+
/// let mut graph_b = TxGraph::<ConfirmationHeightAnchor>::default();
87+
/// let _ = graph_b.insert_tx(tx.clone());
88+
/// graph_b.insert_anchor(
89+
/// tx.txid(),
90+
/// ConfirmationHeightAnchor {
91+
/// anchor_block: BlockId {
92+
/// height: 2,
93+
/// hash: Hash::hash("second".as_bytes()),
94+
/// },
95+
/// confirmation_height: 1,
96+
/// },
97+
/// );
98+
/// ```
4799
pub trait Anchor: core::fmt::Debug + Clone + Eq + PartialOrd + Ord + core::hash::Hash {
48100
/// Returns the [`BlockId`] that the associated blockchain data is "anchored" in.
49101
fn anchor_block(&self) -> BlockId;

0 commit comments

Comments
 (0)