@@ -44,6 +44,58 @@ impl ForEachTxOut for Transaction {
44
44
/// assume that transaction A is also confirmed in the best chain. This does not necessarily mean
45
45
/// that transaction A is confirmed in block B. It could also mean transaction A is confirmed in a
46
46
/// 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
+ /// ```
47
99
pub trait Anchor : core:: fmt:: Debug + Clone + Eq + PartialOrd + Ord + core:: hash:: Hash {
48
100
/// Returns the [`BlockId`] that the associated blockchain data is "anchored" in.
49
101
fn anchor_block ( & self ) -> BlockId ;
0 commit comments