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.
This commit is contained in:
志宇 2024-05-21 21:08:40 +08:00 committed by Steve Myers
parent 54942a902d
commit 0958ff56b2
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
3 changed files with 79 additions and 3 deletions

View 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"]

View File

@ -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()
}
}
}

View 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::*;