|
| 1 | +//! This module is home to the [`PersistBackend`] trait which defines the behavior of a data store |
| 2 | +//! required to persist changes made to BDK data structures. |
| 3 | +//! |
| 4 | +//! The [`CombinedChangeSet`] type encapsulates a combination of [`crate`] structures that are |
| 5 | +//! typically persisted together. |
| 6 | +
|
| 7 | +/// A changeset containing [`crate`] structures typically persisted together. |
| 8 | +#[cfg(feature = "miniscript")] |
| 9 | +#[derive(Debug, Clone, PartialEq)] |
| 10 | +#[cfg_attr( |
| 11 | + feature = "serde", |
| 12 | + derive(crate::serde::Deserialize, crate::serde::Serialize), |
| 13 | + serde( |
| 14 | + crate = "crate::serde", |
| 15 | + bound( |
| 16 | + deserialize = "A: Ord + crate::serde::Deserialize<'de>, K: Ord + crate::serde::Deserialize<'de>", |
| 17 | + serialize = "A: Ord + crate::serde::Serialize, K: Ord + crate::serde::Serialize", |
| 18 | + ), |
| 19 | + ) |
| 20 | +)] |
| 21 | +pub struct CombinedChangeSet<K, A> { |
| 22 | + /// Changes to the [`LocalChain`](crate::local_chain::LocalChain). |
| 23 | + pub chain: crate::local_chain::ChangeSet, |
| 24 | + /// Changes to [`IndexedTxGraph`](crate::indexed_tx_graph::IndexedTxGraph). |
| 25 | + pub indexed_tx_graph: crate::indexed_tx_graph::ChangeSet<A, crate::keychain::ChangeSet<K>>, |
| 26 | + /// Stores the network type of the transaction data. |
| 27 | + pub network: Option<bitcoin::Network>, |
| 28 | +} |
| 29 | + |
| 30 | +#[cfg(feature = "miniscript")] |
| 31 | +impl<K, A> core::default::Default for CombinedChangeSet<K, A> { |
| 32 | + fn default() -> Self { |
| 33 | + Self { |
| 34 | + chain: core::default::Default::default(), |
| 35 | + indexed_tx_graph: core::default::Default::default(), |
| 36 | + network: None, |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[cfg(feature = "miniscript")] |
| 42 | +impl<K: Ord, A: crate::Anchor> crate::Append for CombinedChangeSet<K, A> { |
| 43 | + fn append(&mut self, other: Self) { |
| 44 | + crate::Append::append(&mut self.chain, other.chain); |
| 45 | + crate::Append::append(&mut self.indexed_tx_graph, other.indexed_tx_graph); |
| 46 | + if other.network.is_some() { |
| 47 | + debug_assert!( |
| 48 | + self.network.is_none() || self.network == other.network, |
| 49 | + "network type must either be just introduced or remain the same" |
| 50 | + ); |
| 51 | + self.network = other.network; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + fn is_empty(&self) -> bool { |
| 56 | + self.chain.is_empty() && self.indexed_tx_graph.is_empty() && self.network.is_none() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +#[cfg(feature = "miniscript")] |
| 61 | +impl<K, A> From<crate::local_chain::ChangeSet> for CombinedChangeSet<K, A> { |
| 62 | + fn from(chain: crate::local_chain::ChangeSet) -> Self { |
| 63 | + Self { |
| 64 | + chain, |
| 65 | + ..Default::default() |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[cfg(feature = "miniscript")] |
| 71 | +impl<K, A> From<crate::indexed_tx_graph::ChangeSet<A, crate::keychain::ChangeSet<K>>> |
| 72 | + for CombinedChangeSet<K, A> |
| 73 | +{ |
| 74 | + fn from( |
| 75 | + indexed_tx_graph: crate::indexed_tx_graph::ChangeSet<A, crate::keychain::ChangeSet<K>>, |
| 76 | + ) -> Self { |
| 77 | + Self { |
| 78 | + indexed_tx_graph, |
| 79 | + ..Default::default() |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[cfg(feature = "miniscript")] |
| 85 | +impl<K, A> From<crate::keychain::ChangeSet<K>> for CombinedChangeSet<K, A> { |
| 86 | + fn from(indexer: crate::keychain::ChangeSet<K>) -> Self { |
| 87 | + Self { |
| 88 | + indexed_tx_graph: crate::indexed_tx_graph::ChangeSet { |
| 89 | + indexer, |
| 90 | + ..Default::default() |
| 91 | + }, |
| 92 | + ..Default::default() |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments