|
| 1 | +#![cfg(feature = "miniscript")] |
| 2 | + |
| 3 | +use bdk_chain::{bitcoin::Network, indexed_tx_graph, keychain, local_chain, Anchor, Append}; |
| 4 | + |
| 5 | +/// Changes from a combination of [`bdk_chain`] structures. |
| 6 | +#[derive(Debug, Clone, PartialEq)] |
| 7 | +#[cfg_attr( |
| 8 | + feature = "serde", |
| 9 | + derive(bdk_chain::serde::Deserialize, bdk_chain::serde::Serialize), |
| 10 | + serde( |
| 11 | + crate = "bdk_chain::serde", |
| 12 | + bound( |
| 13 | + deserialize = "A: Ord + bdk_chain::serde::Deserialize<'de>, K: Ord + bdk_chain::serde::Deserialize<'de>", |
| 14 | + serialize = "A: Ord + bdk_chain::serde::Serialize, K: Ord + bdk_chain::serde::Serialize", |
| 15 | + ), |
| 16 | + ) |
| 17 | +)] |
| 18 | +pub struct CombinedChangeSet<K, A> { |
| 19 | + /// Changes to the [`LocalChain`](local_chain::LocalChain). |
| 20 | + pub chain: local_chain::ChangeSet, |
| 21 | + /// Changes to [`IndexedTxGraph`](indexed_tx_graph::IndexedTxGraph). |
| 22 | + pub indexed_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>, |
| 23 | + /// Stores the network type of the transaction data. |
| 24 | + pub network: Option<Network>, |
| 25 | +} |
| 26 | + |
| 27 | +impl<K, A> Default for CombinedChangeSet<K, A> { |
| 28 | + fn default() -> Self { |
| 29 | + Self { |
| 30 | + chain: Default::default(), |
| 31 | + indexed_tx_graph: Default::default(), |
| 32 | + network: None, |
| 33 | + } |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +impl<K: Ord, A: Anchor> Append for CombinedChangeSet<K, A> { |
| 38 | + fn append(&mut self, other: Self) { |
| 39 | + Append::append(&mut self.chain, other.chain); |
| 40 | + Append::append(&mut self.indexed_tx_graph, other.indexed_tx_graph); |
| 41 | + if other.network.is_some() { |
| 42 | + debug_assert!( |
| 43 | + self.network.is_none() || self.network == other.network, |
| 44 | + "network type must either be just introduced or remain the same" |
| 45 | + ); |
| 46 | + self.network = other.network; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + fn is_empty(&self) -> bool { |
| 51 | + self.chain.is_empty() && self.indexed_tx_graph.is_empty() && self.network.is_none() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<K, A> From<local_chain::ChangeSet> for CombinedChangeSet<K, A> { |
| 56 | + fn from(chain: local_chain::ChangeSet) -> Self { |
| 57 | + Self { |
| 58 | + chain, |
| 59 | + ..Default::default() |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +impl<K, A> From<indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>> |
| 65 | + for CombinedChangeSet<K, A> |
| 66 | +{ |
| 67 | + fn from(indexed_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>) -> Self { |
| 68 | + Self { |
| 69 | + indexed_tx_graph, |
| 70 | + ..Default::default() |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments