From 0958ff56b20c1325a7da5d5f21e7ac836c601f1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Tue, 21 May 2024 21:08:40 +0800 Subject: [PATCH] feat(persist): introduce `CombinedChangeSet` 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 | 6 +-- crates/persist/src/changeset.rs | 73 +++++++++++++++++++++++++++++++++ crates/persist/src/lib.rs | 3 ++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 crates/persist/src/changeset.rs diff --git a/crates/persist/Cargo.toml b/crates/persist/Cargo.toml index d7a8f708..864ccca8 100644 --- a/crates/persist/Cargo.toml +++ b/crates/persist/Cargo.toml @@ -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 index 00000000..b796b07f --- /dev/null +++ b/crates/persist/src/changeset.rs @@ -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 { + /// 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>, + /// Stores the network type of the transaction data. + pub network: Option, +} + +impl Default for CombinedChangeSet { + fn default() -> Self { + Self { + chain: Default::default(), + indexed_tx_graph: Default::default(), + network: None, + } + } +} + +impl Append for CombinedChangeSet { + 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 From for CombinedChangeSet { + fn from(chain: local_chain::ChangeSet) -> Self { + Self { + chain, + ..Default::default() + } + } +} + +impl From>> + for CombinedChangeSet +{ + fn from(indexed_tx_graph: indexed_tx_graph::ChangeSet>) -> Self { + Self { + indexed_tx_graph, + ..Default::default() + } + } +} diff --git a/crates/persist/src/lib.rs b/crates/persist/src/lib.rs index e055f2a4..e3824e1a 100644 --- a/crates/persist/src/lib.rs +++ b/crates/persist/src/lib.rs @@ -1,5 +1,8 @@ #![doc = include_str!("../README.md")] #![no_std] #![warn(missing_docs)] + +mod changeset; mod persist; +pub use changeset::*; pub use persist::*;