• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitcoindevkit / bdk / 9519613010

14 Jun 2024 04:52PM CUT coverage: 83.312% (+0.2%) from 83.064%
9519613010

Pull #1473

github

web-flow
Merge feb27df18 into bc420923c
Pull Request #1473: Remove `persist` submodule

58 of 64 new or added lines in 5 files covered. (90.63%)

1 existing line in 1 file now uncovered.

11128 of 13357 relevant lines covered (83.31%)

17626.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.54
/crates/chain/src/tx_data_traits.rs
1
use crate::collections::BTreeMap;
2
use crate::collections::BTreeSet;
3
use crate::BlockId;
4
use alloc::vec::Vec;
5

6
/// Trait that "anchors" blockchain data to a specific block of height and hash.
7
///
8
/// If transaction A is anchored in block B, and block B is in the best chain, we can
9
/// assume that transaction A is also confirmed in the best chain. This does not necessarily mean
10
/// that transaction A is confirmed in block B. It could also mean transaction A is confirmed in a
11
/// parent block of B.
12
///
13
/// Every [`Anchor`] implementation must contain a [`BlockId`] parameter, and must implement
14
/// [`Ord`]. When implementing [`Ord`], the anchors' [`BlockId`]s should take precedence
15
/// over other elements inside the [`Anchor`]s for comparison purposes, i.e., you should first
16
/// compare the anchors' [`BlockId`]s and then care about the rest.
17
///
18
/// The example shows different types of anchors:
19
/// ```
20
/// # use bdk_chain::local_chain::LocalChain;
21
/// # use bdk_chain::tx_graph::TxGraph;
22
/// # use bdk_chain::BlockId;
23
/// # use bdk_chain::ConfirmationHeightAnchor;
24
/// # use bdk_chain::ConfirmationTimeHeightAnchor;
25
/// # use bdk_chain::example_utils::*;
26
/// # use bitcoin::hashes::Hash;
27
/// // Initialize the local chain with two blocks.
28
/// let chain = LocalChain::from_blocks(
29
///     [
30
///         (1, Hash::hash("first".as_bytes())),
31
///         (2, Hash::hash("second".as_bytes())),
32
///     ]
33
///     .into_iter()
34
///     .collect(),
35
/// );
36
///
37
/// // Transaction to be inserted into `TxGraph`s with different anchor types.
38
/// let tx = tx_from_hex(RAW_TX_1);
39
///
40
/// // Insert `tx` into a `TxGraph` that uses `BlockId` as the anchor type.
41
/// // When a transaction is anchored with `BlockId`, the anchor block and the confirmation block of
42
/// // the transaction is the same block.
43
/// let mut graph_a = TxGraph::<BlockId>::default();
44
/// let _ = graph_a.insert_tx(tx.clone());
45
/// graph_a.insert_anchor(
46
///     tx.compute_txid(),
47
///     BlockId {
48
///         height: 1,
49
///         hash: Hash::hash("first".as_bytes()),
50
///     },
51
/// );
52
///
53
/// // Insert `tx` into a `TxGraph` that uses `ConfirmationHeightAnchor` as the anchor type.
54
/// // This anchor records the anchor block and the confirmation height of the transaction.
55
/// // When a transaction is anchored with `ConfirmationHeightAnchor`, the anchor block and
56
/// // confirmation block can be different. However, the confirmation block cannot be higher than
57
/// // the anchor block and both blocks must be in the same chain for the anchor to be valid.
58
/// let mut graph_b = TxGraph::<ConfirmationHeightAnchor>::default();
59
/// let _ = graph_b.insert_tx(tx.clone());
60
/// graph_b.insert_anchor(
61
///     tx.compute_txid(),
62
///     ConfirmationHeightAnchor {
63
///         anchor_block: BlockId {
64
///             height: 2,
65
///             hash: Hash::hash("second".as_bytes()),
66
///         },
67
///         confirmation_height: 1,
68
///     },
69
/// );
70
///
71
/// // Insert `tx` into a `TxGraph` that uses `ConfirmationTimeHeightAnchor` as the anchor type.
72
/// // This anchor records the anchor block, the confirmation height and time of the transaction.
73
/// // When a transaction is anchored with `ConfirmationTimeHeightAnchor`, the anchor block and
74
/// // confirmation block can be different. However, the confirmation block cannot be higher than
75
/// // the anchor block and both blocks must be in the same chain for the anchor to be valid.
76
/// let mut graph_c = TxGraph::<ConfirmationTimeHeightAnchor>::default();
77
/// let _ = graph_c.insert_tx(tx.clone());
78
/// graph_c.insert_anchor(
79
///     tx.compute_txid(),
80
///     ConfirmationTimeHeightAnchor {
81
///         anchor_block: BlockId {
82
///             height: 2,
83
///             hash: Hash::hash("third".as_bytes()),
84
///         },
85
///         confirmation_height: 1,
86
///         confirmation_time: 123,
87
///     },
88
/// );
89
/// ```
90
pub trait Anchor: core::fmt::Debug + Clone + Eq + PartialOrd + Ord + core::hash::Hash {
91
    /// Returns the [`BlockId`] that the associated blockchain data is "anchored" in.
92
    fn anchor_block(&self) -> BlockId;
93

94
    /// Get the upper bound of the chain data's confirmation height.
95
    ///
96
    /// The default definition gives a pessimistic answer. This can be overridden by the `Anchor`
97
    /// implementation for a more accurate value.
98
    fn confirmation_height_upper_bound(&self) -> u32 {
71✔
99
        self.anchor_block().height
71✔
100
    }
71✔
101
}
102

103
impl<'a, A: Anchor> Anchor for &'a A {
104
    fn anchor_block(&self) -> BlockId {
×
105
        <A as Anchor>::anchor_block(self)
×
106
    }
×
107
}
108

109
/// An [`Anchor`] that can be constructed from a given block, block height and transaction position
110
/// within the block.
111
pub trait AnchorFromBlockPosition: Anchor {
112
    /// Construct the anchor from a given `block`, block height and `tx_pos` within the block.
113
    fn from_block_position(block: &bitcoin::Block, block_id: BlockId, tx_pos: usize) -> Self;
114
}
115

116
/// Trait that makes an object appendable.
117
pub trait Append: Default {
118
    /// Append another object of the same type onto `self`.
119
    fn append(&mut self, other: Self);
120

121
    /// Returns whether the structure is considered empty.
122
    fn is_empty(&self) -> bool;
123

124
    /// Take the value, replacing it with the default value.
125
    fn take(&mut self) -> Option<Self> {
32✔
126
        if self.is_empty() {
32✔
NEW
127
            None
×
128
        } else {
129
            Some(core::mem::take(self))
32✔
130
        }
131
    }
32✔
132
}
133

134
impl<K: Ord, V> Append for BTreeMap<K, V> {
135
    fn append(&mut self, other: Self) {
11,161✔
136
        // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
11,161✔
137
        // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
11,161✔
138
        BTreeMap::extend(self, other)
11,161✔
139
    }
11,161✔
140

141
    fn is_empty(&self) -> bool {
×
142
        BTreeMap::is_empty(self)
×
143
    }
×
144
}
145

146
impl<T: Ord> Append for BTreeSet<T> {
147
    fn append(&mut self, other: Self) {
450✔
148
        // We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
450✔
149
        // Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
450✔
150
        BTreeSet::extend(self, other)
450✔
151
    }
450✔
152

153
    fn is_empty(&self) -> bool {
437✔
154
        BTreeSet::is_empty(self)
437✔
155
    }
437✔
156
}
157

158
impl<T> Append for Vec<T> {
159
    fn append(&mut self, mut other: Self) {
×
160
        Vec::append(self, &mut other)
×
161
    }
×
162

163
    fn is_empty(&self) -> bool {
×
164
        Vec::is_empty(self)
×
165
    }
×
166
}
167

168
macro_rules! impl_append_for_tuple {
169
    ($($a:ident $b:tt)*) => {
170
        impl<$($a),*> Append for ($($a,)*) where $($a: Append),* {
171

172
            fn append(&mut self, _other: Self) {
6,798✔
173
                $(Append::append(&mut self.$b, _other.$b) );*
6,798✔
174
            }
6,798✔
175

176
            fn is_empty(&self) -> bool {
×
177
                $(Append::is_empty(&self.$b) && )* true
×
178
            }
2,244✔
179
        }
180
    }
181
}
182

183
impl_append_for_tuple!();
184
impl_append_for_tuple!(T0 0);
185
impl_append_for_tuple!(T0 0 T1 1);
186
impl_append_for_tuple!(T0 0 T1 1 T2 2);
187
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3);
188
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
189
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
190
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
191
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
192
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
193
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
194
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10);
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc