|
10 | 10 | //! |
11 | 11 | //! [`SpkTxOutIndex`]: crate::SpkTxOutIndex |
12 | 12 |
|
13 | | -use crate::{collections::BTreeMap, Append}; |
14 | | - |
15 | 13 | #[cfg(feature = "miniscript")] |
16 | 14 | mod txout_index; |
17 | 15 | #[cfg(feature = "miniscript")] |
18 | 16 | pub use txout_index::*; |
19 | 17 |
|
20 | | -/// Represents updates to the derivation index of a [`KeychainTxOutIndex`]. |
21 | | -/// It maps each keychain `K` to its last revealed index. |
22 | | -/// |
23 | | -/// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`]. [`ChangeSet] are |
24 | | -/// monotone in that they will never decrease the revealed derivation index. |
25 | | -/// |
26 | | -/// [`KeychainTxOutIndex`]: crate::keychain::KeychainTxOutIndex |
27 | | -/// [`apply_changeset`]: crate::keychain::KeychainTxOutIndex::apply_changeset |
28 | | -#[derive(Clone, Debug, PartialEq)] |
29 | | -#[cfg_attr( |
30 | | - feature = "serde", |
31 | | - derive(serde::Deserialize, serde::Serialize), |
32 | | - serde( |
33 | | - crate = "serde_crate", |
34 | | - bound( |
35 | | - deserialize = "K: Ord + serde::Deserialize<'de>", |
36 | | - serialize = "K: Ord + serde::Serialize" |
37 | | - ) |
38 | | - ) |
39 | | -)] |
40 | | -#[must_use] |
41 | | -pub struct ChangeSet<K>(pub BTreeMap<K, u32>); |
42 | | - |
43 | | -impl<K> ChangeSet<K> { |
44 | | - /// Get the inner map of the keychain to its new derivation index. |
45 | | - pub fn as_inner(&self) -> &BTreeMap<K, u32> { |
46 | | - &self.0 |
47 | | - } |
48 | | -} |
49 | | - |
50 | | -impl<K: Ord> Append for ChangeSet<K> { |
51 | | - /// Append another [`ChangeSet`] into self. |
52 | | - /// |
53 | | - /// If the keychain already exists, increase the index when the other's index > self's index. |
54 | | - /// If the keychain did not exist, append the new keychain. |
55 | | - fn append(&mut self, mut other: Self) { |
56 | | - self.0.iter_mut().for_each(|(key, index)| { |
57 | | - if let Some(other_index) = other.0.remove(key) { |
58 | | - *index = other_index.max(*index); |
59 | | - } |
60 | | - }); |
61 | | - |
62 | | - self.0.append(&mut other.0); |
63 | | - } |
64 | | - |
65 | | - /// Returns whether the changeset are empty. |
66 | | - fn is_empty(&self) -> bool { |
67 | | - self.0.is_empty() |
68 | | - } |
69 | | -} |
70 | | - |
71 | | -impl<K> Default for ChangeSet<K> { |
72 | | - fn default() -> Self { |
73 | | - Self(Default::default()) |
74 | | - } |
75 | | -} |
76 | | - |
77 | | -impl<K> AsRef<BTreeMap<K, u32>> for ChangeSet<K> { |
78 | | - fn as_ref(&self) -> &BTreeMap<K, u32> { |
79 | | - &self.0 |
80 | | - } |
81 | | -} |
82 | | - |
83 | 18 | /// Balance, differentiated into various categories. |
84 | 19 | #[derive(Debug, PartialEq, Eq, Clone, Default)] |
85 | 20 | #[cfg_attr( |
@@ -135,40 +70,3 @@ impl core::ops::Add for Balance { |
135 | 70 | } |
136 | 71 | } |
137 | 72 | } |
138 | | - |
139 | | -#[cfg(test)] |
140 | | -mod test { |
141 | | - use super::*; |
142 | | - |
143 | | - #[test] |
144 | | - fn append_keychain_derivation_indices() { |
145 | | - #[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)] |
146 | | - enum Keychain { |
147 | | - One, |
148 | | - Two, |
149 | | - Three, |
150 | | - Four, |
151 | | - } |
152 | | - let mut lhs_di = BTreeMap::<Keychain, u32>::default(); |
153 | | - let mut rhs_di = BTreeMap::<Keychain, u32>::default(); |
154 | | - lhs_di.insert(Keychain::One, 7); |
155 | | - lhs_di.insert(Keychain::Two, 0); |
156 | | - rhs_di.insert(Keychain::One, 3); |
157 | | - rhs_di.insert(Keychain::Two, 5); |
158 | | - lhs_di.insert(Keychain::Three, 3); |
159 | | - rhs_di.insert(Keychain::Four, 4); |
160 | | - |
161 | | - let mut lhs = ChangeSet(lhs_di); |
162 | | - let rhs = ChangeSet(rhs_di); |
163 | | - lhs.append(rhs); |
164 | | - |
165 | | - // Exiting index doesn't update if the new index in `other` is lower than `self`. |
166 | | - assert_eq!(lhs.0.get(&Keychain::One), Some(&7)); |
167 | | - // Existing index updates if the new index in `other` is higher than `self`. |
168 | | - assert_eq!(lhs.0.get(&Keychain::Two), Some(&5)); |
169 | | - // Existing index is unchanged if keychain doesn't exist in `other`. |
170 | | - assert_eq!(lhs.0.get(&Keychain::Three), Some(&3)); |
171 | | - // New keychain gets added if the keychain is in `other` but not in `self`. |
172 | | - assert_eq!(lhs.0.get(&Keychain::Four), Some(&4)); |
173 | | - } |
174 | | -} |
0 commit comments