]> Untitled Git - bdk/commitdiff
feat(persist): introduce `CombinedChangeSet`
author志宇 <hello@evanlinjin.me>
Tue, 21 May 2024 13:08:40 +0000 (21:08 +0800)
committerSteve Myers <steve@notmandatory.org>
Thu, 23 May 2024 04:02:54 +0000 (23:02 -0500)
It is a good idea to have common changeset types stored in
`bdk_persist`. This will make it convenient for persistence crates and
`bdk_wallet` interaction.

crates/persist/Cargo.toml
crates/persist/src/changeset.rs [new file with mode: 0644]
crates/persist/src/lib.rs

index d7a8f7083fdfad4110104064ff6edf54372fa1a2..864ccca81fd82a92eb6421c6b69d03c36986efea 100644 (file)
@@ -17,6 +17,6 @@ anyhow = { version = "1", default-features = false }
 bdk_chain = { path = "../chain", version = "0.14.0", default-features = false }
 
 [features]
-default = ["bdk_chain/std"]
-
-
+default = ["bdk_chain/std", "miniscript"]
+serde = ["bdk_chain/serde"]
+miniscript = ["bdk_chain/miniscript"]
diff --git a/crates/persist/src/changeset.rs b/crates/persist/src/changeset.rs
new file mode 100644 (file)
index 0000000..b796b07
--- /dev/null
@@ -0,0 +1,73 @@
+#![cfg(feature = "miniscript")]
+
+use bdk_chain::{bitcoin::Network, indexed_tx_graph, keychain, local_chain, Anchor, Append};
+
+/// Changes from a combination of [`bdk_chain`] structures.
+#[derive(Debug, Clone, PartialEq)]
+#[cfg_attr(
+    feature = "serde",
+    derive(bdk_chain::serde::Deserialize, bdk_chain::serde::Serialize),
+    serde(
+        crate = "bdk_chain::serde",
+        bound(
+            deserialize = "A: Ord + bdk_chain::serde::Deserialize<'de>, K: Ord + bdk_chain::serde::Deserialize<'de>",
+            serialize = "A: Ord + bdk_chain::serde::Serialize, K: Ord + bdk_chain::serde::Serialize",
+        ),
+    )
+)]
+pub struct CombinedChangeSet<K, A> {
+    /// Changes to the [`LocalChain`](local_chain::LocalChain).
+    pub chain: local_chain::ChangeSet,
+    /// Changes to [`IndexedTxGraph`](indexed_tx_graph::IndexedTxGraph).
+    pub indexed_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
+    /// Stores the network type of the transaction data.
+    pub network: Option<Network>,
+}
+
+impl<K, A> Default for CombinedChangeSet<K, A> {
+    fn default() -> Self {
+        Self {
+            chain: Default::default(),
+            indexed_tx_graph: Default::default(),
+            network: None,
+        }
+    }
+}
+
+impl<K: Ord, A: Anchor> Append for CombinedChangeSet<K, A> {
+    fn append(&mut self, other: Self) {
+        Append::append(&mut self.chain, other.chain);
+        Append::append(&mut self.indexed_tx_graph, other.indexed_tx_graph);
+        if other.network.is_some() {
+            debug_assert!(
+                self.network.is_none() || self.network == other.network,
+                "network type must either be just introduced or remain the same"
+            );
+            self.network = other.network;
+        }
+    }
+
+    fn is_empty(&self) -> bool {
+        self.chain.is_empty() && self.indexed_tx_graph.is_empty() && self.network.is_none()
+    }
+}
+
+impl<K, A> From<local_chain::ChangeSet> for CombinedChangeSet<K, A> {
+    fn from(chain: local_chain::ChangeSet) -> Self {
+        Self {
+            chain,
+            ..Default::default()
+        }
+    }
+}
+
+impl<K, A> From<indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>>
+    for CombinedChangeSet<K, A>
+{
+    fn from(indexed_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>) -> Self {
+        Self {
+            indexed_tx_graph,
+            ..Default::default()
+        }
+    }
+}
index e055f2a44e293ac324d63f0e874c0133e0511351..e3824e1adc0e4679954160ab354ca0491abd9d88 100644 (file)
@@ -1,5 +1,8 @@
 #![doc = include_str!("../README.md")]
 #![no_std]
 #![warn(missing_docs)]
+
+mod changeset;
 mod persist;
+pub use changeset::*;
 pub use persist::*;