Skip to content

Commit 0958ff5

Browse files
evanlinjinnotmandatory
authored andcommitted
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.
1 parent 54942a9 commit 0958ff5

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

crates/persist/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ anyhow = { version = "1", default-features = false }
1717
bdk_chain = { path = "../chain", version = "0.14.0", default-features = false }
1818

1919
[features]
20-
default = ["bdk_chain/std"]
21-
22-
20+
default = ["bdk_chain/std", "miniscript"]
21+
serde = ["bdk_chain/serde"]
22+
miniscript = ["bdk_chain/miniscript"]

crates/persist/src/changeset.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

crates/persist/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![doc = include_str!("../README.md")]
22
#![no_std]
33
#![warn(missing_docs)]
4+
5+
mod changeset;
46
mod persist;
7+
pub use changeset::*;
58
pub use persist::*;

0 commit comments

Comments
 (0)