Skip to content

Commit e5fb1ec

Browse files
committed
Merge #1069: Implement Anchor for BlockId
18e8da3 docs: Add doctest for Anchor implementation on BlockId (Vladimir Fomene) 480c273 feat: Implement Anchor for BlockId (Vladimir Fomene) Pull request description: ### Description This PR fixes #1062. It implements the `Anchor` trait on `BlockId`. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: evanlinjin: ACK 18e8da3 Tree-SHA512: af9da8b4e9c7f2a110340efa4e4c86d9b96ea1275f0a66f08e26a47fcacd89e318a32c845e2a4302f3332eeb4c6a976c3771b1d9881adf4feda90d6ba1a2dd9d
2 parents 8f978f8 + 18e8da3 commit e5fb1ec

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

crates/chain/src/chain_data.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ impl From<ChainPosition<ConfirmationTimeAnchor>> for ConfirmationTime {
8787
}
8888

8989
/// A reference to a block in the canonical chain.
90+
///
91+
/// `BlockId` implements [`Anchor`]. When a transaction is anchored to `BlockId`, the confirmation
92+
/// block and anchor block are the same block.
9093
#[derive(Debug, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
9194
#[cfg_attr(
9295
feature = "serde",
@@ -100,6 +103,12 @@ pub struct BlockId {
100103
pub hash: BlockHash,
101104
}
102105

106+
impl Anchor for BlockId {
107+
fn anchor_block(&self) -> Self {
108+
*self
109+
}
110+
}
111+
103112
impl Default for BlockId {
104113
fn default() -> Self {
105114
Self {
@@ -131,6 +140,8 @@ impl From<(&u32, &BlockHash)> for BlockId {
131140
}
132141

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

160171
/// An [`Anchor`] implementation that also records the exact confirmation time and height of the
161172
/// transaction.
173+
///
174+
/// Refer to [`Anchor`] for more details.
162175
#[derive(Debug, Default, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
163176
#[cfg_attr(
164177
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)