Skip to content

Commit 6424562

Browse files
vladimirfomeneevanlinjin
authored andcommitted
refactor: move WalletChangeset to wallet module
1 parent f96fca2 commit 6424562

File tree

4 files changed

+75
-74
lines changed

4 files changed

+75
-74
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ use alloc::{
2222
pub use bdk_chain::keychain::Balance;
2323
use bdk_chain::{
2424
indexed_tx_graph,
25-
keychain::{KeychainTxOutIndex, WalletChangeSet},
25+
keychain::{self, KeychainTxOutIndex},
2626
local_chain::{self, CannotConnectError, CheckPoint, CheckPointIter, LocalChain},
2727
tx_graph::{CanonicalTx, TxGraph},
28-
Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeAnchor, FullTxOut,
28+
Anchor, Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeAnchor, FullTxOut,
2929
IndexedTxGraph, Persist, PersistBackend,
3030
};
3131
use bitcoin::consensus::encode::serialize;
@@ -123,6 +123,62 @@ impl<K, A> WalletUpdate<K, A> {
123123
}
124124
}
125125

126+
/// A structure that records the corresponding changes as result of applying an [`WalletUpdate`].
127+
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
128+
pub struct WalletChangeSet<K, A> {
129+
/// Changes to the [`LocalChain`].
130+
///
131+
/// [`LocalChain`]: local_chain::LocalChain
132+
pub chain: local_chain::ChangeSet,
133+
134+
/// ChangeSet to [`IndexedTxGraph`].
135+
///
136+
/// [`IndexedTxGraph`]: bdk_chain::indexed_tx_graph::IndexedTxGraph
137+
#[serde(bound(
138+
deserialize = "K: Ord + serde::Deserialize<'de>, A: Ord + serde::Deserialize<'de>",
139+
serialize = "K: Ord + serde::Serialize, A: Ord + serde::Serialize",
140+
))]
141+
pub index_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
142+
}
143+
144+
impl<K, A> Default for WalletChangeSet<K, A> {
145+
fn default() -> Self {
146+
Self {
147+
chain: Default::default(),
148+
index_tx_graph: Default::default(),
149+
}
150+
}
151+
}
152+
153+
impl<K: Ord, A: Anchor> Append for WalletChangeSet<K, A> {
154+
fn append(&mut self, other: Self) {
155+
Append::append(&mut self.chain, other.chain);
156+
Append::append(&mut self.index_tx_graph, other.index_tx_graph);
157+
}
158+
159+
fn is_empty(&self) -> bool {
160+
self.chain.is_empty() && self.index_tx_graph.is_empty()
161+
}
162+
}
163+
164+
impl<K, A> From<local_chain::ChangeSet> for WalletChangeSet<K, A> {
165+
fn from(chain: local_chain::ChangeSet) -> Self {
166+
Self {
167+
chain,
168+
..Default::default()
169+
}
170+
}
171+
}
172+
173+
impl<K, A> From<indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>> for WalletChangeSet<K, A> {
174+
fn from(index_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>) -> Self {
175+
Self {
176+
index_tx_graph,
177+
..Default::default()
178+
}
179+
}
180+
}
181+
126182
/// The update to a [`Wallet`] used in [`Wallet::apply_update`]. This is usually returned from blockchain data sources.
127183
pub type Update = WalletUpdate<KeychainKind, ConfirmationTimeAnchor>;
128184

crates/chain/src/keychain.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//!
1111
//! [`SpkTxOutIndex`]: crate::SpkTxOutIndex
1212
13-
use crate::{collections::BTreeMap, indexed_tx_graph, local_chain, Anchor, Append};
13+
use crate::{collections::BTreeMap, Append};
1414

1515
#[cfg(feature = "miniscript")]
1616
mod txout_index;
@@ -80,69 +80,6 @@ impl<K> AsRef<BTreeMap<K, u32>> for ChangeSet<K> {
8080
}
8181
}
8282

83-
/// A structure that records the corresponding changes as result of applying an [`WalletUpdate`].
84-
#[derive(Debug, Clone, PartialEq)]
85-
#[cfg_attr(
86-
feature = "serde",
87-
derive(serde::Deserialize, serde::Serialize),
88-
serde(
89-
crate = "serde_crate",
90-
bound(
91-
deserialize = "K: Ord + serde::Deserialize<'de>, A: Ord + serde::Deserialize<'de>",
92-
serialize = "K: Ord + serde::Serialize, A: Ord + serde::Serialize",
93-
)
94-
)
95-
)]
96-
pub struct WalletChangeSet<K, A> {
97-
/// Changes to the [`LocalChain`].
98-
///
99-
/// [`LocalChain`]: local_chain::LocalChain
100-
pub chain: local_chain::ChangeSet,
101-
102-
/// ChangeSet to [`IndexedTxGraph`].
103-
///
104-
/// [`IndexedTxGraph`]: crate::indexed_tx_graph::IndexedTxGraph
105-
pub index_tx_graph: indexed_tx_graph::ChangeSet<A, ChangeSet<K>>,
106-
}
107-
108-
impl<K, A> Default for WalletChangeSet<K, A> {
109-
fn default() -> Self {
110-
Self {
111-
chain: Default::default(),
112-
index_tx_graph: Default::default(),
113-
}
114-
}
115-
}
116-
117-
impl<K: Ord, A: Anchor> Append for WalletChangeSet<K, A> {
118-
fn append(&mut self, other: Self) {
119-
Append::append(&mut self.chain, other.chain);
120-
Append::append(&mut self.index_tx_graph, other.index_tx_graph);
121-
}
122-
123-
fn is_empty(&self) -> bool {
124-
self.chain.is_empty() && self.index_tx_graph.is_empty()
125-
}
126-
}
127-
128-
impl<K, A> From<local_chain::ChangeSet> for WalletChangeSet<K, A> {
129-
fn from(chain: local_chain::ChangeSet) -> Self {
130-
Self {
131-
chain,
132-
..Default::default()
133-
}
134-
}
135-
}
136-
137-
impl<K, A> From<indexed_tx_graph::ChangeSet<A, ChangeSet<K>>> for WalletChangeSet<K, A> {
138-
fn from(index_tx_graph: indexed_tx_graph::ChangeSet<A, ChangeSet<K>>) -> Self {
139-
Self {
140-
index_tx_graph,
141-
..Default::default()
142-
}
143-
}
144-
}
145-
14683
/// Balance, differentiated into various categories.
14784
#[derive(Debug, PartialEq, Eq, Clone, Default)]
14885
#[cfg_attr(

crates/chain/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,11 @@ pub mod collections {
100100

101101
/// How many confirmations are needed f or a coinbase output to be spent.
102102
pub const COINBASE_MATURITY: u32 = 100;
103+
104+
impl<A, IA> From<indexed_tx_graph::ChangeSet<A, IA>>
105+
for (local_chain::ChangeSet, indexed_tx_graph::ChangeSet<A, IA>)
106+
{
107+
fn from(indexed_changeset: indexed_tx_graph::ChangeSet<A, IA>) -> Self {
108+
(local_chain::ChangeSet::default(), indexed_changeset)
109+
}
110+
}

example-crates/example_electrum/src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
use bdk_chain::{
88
bitcoin::{Address, Network, OutPoint, ScriptBuf, Txid},
99
indexed_tx_graph::{self, IndexedTxGraph},
10-
keychain::WalletChangeSet,
10+
keychain,
1111
local_chain::{self, LocalChain},
1212
Append, ConfirmationHeightAnchor,
1313
};
@@ -60,19 +60,22 @@ pub struct ScanOptions {
6060
pub batch_size: usize,
6161
}
6262

63-
type ChangeSet = WalletChangeSet<Keychain, ConfirmationHeightAnchor>;
63+
type ChangeSet = (
64+
local_chain::ChangeSet,
65+
indexed_tx_graph::ChangeSet<ConfirmationHeightAnchor, keychain::ChangeSet<Keychain>>,
66+
);
6467

6568
fn main() -> anyhow::Result<()> {
6669
let (args, keymap, index, db, init_changeset) =
6770
example_cli::init::<ElectrumCommands, ChangeSet>(DB_MAGIC, DB_PATH)?;
6871

6972
let graph = Mutex::new({
7073
let mut graph = IndexedTxGraph::new(index);
71-
graph.apply_changeset(init_changeset.index_tx_graph);
74+
graph.apply_changeset(init_changeset.1);
7275
graph
7376
});
7477

75-
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.chain));
78+
let chain = Mutex::new(LocalChain::from_changeset(init_changeset.0));
7679

7780
let electrum_url = match args.network {
7881
Network::Bitcoin => "ssl://electrum.blockstream.info:50002",
@@ -293,10 +296,7 @@ fn main() -> anyhow::Result<()> {
293296
changeset
294297
};
295298

296-
ChangeSet {
297-
index_tx_graph,
298-
chain,
299-
}
299+
(chain, index_tx_graph)
300300
};
301301

302302
let mut db = db.lock().unwrap();

0 commit comments

Comments
 (0)