Skip to content

Commit c2ac570

Browse files
committed
refactor(persist): update file_store, sqlite, wallet to use bdk_chain::persist
Also update examples and remove bdk_persist crate.
1 parent 68a3760 commit c2ac570

File tree

34 files changed

+555
-803
lines changed

34 files changed

+555
-803
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ members = [
99
"crates/esplora",
1010
"crates/bitcoind_rpc",
1111
"crates/hwi",
12-
"crates/persist",
1312
"crates/testenv",
1413
"example-crates/example_cli",
1514
"example-crates/example_electrum",

crates/chain/src/keychain/txout_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
308308

309309
/// Return a reference to the internal [`SpkTxOutIndex`].
310310
///
311-
/// **WARNING:** The internal index will contain lookahead spks. Refer to
311+
/// **WARNING**: The internal index will contain lookahead spks. Refer to
312312
/// [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
313313
pub fn inner(&self) -> &SpkTxOutIndex<(DescriptorId, u32)> {
314314
&self.inner

crates/chain/src/persist.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
//! This module is home to the [`Persist`] trait which defines the behavior of a data store
1+
//! This module is home to the [`PersistBackend`] trait which defines the behavior of a data store
22
//! required to persist changes made to BDK data structures.
33
//!
4-
//! The [`StagedPersist`] type provides a convenient wrapper around implementations of [`Persist`] that
4+
//! The [`StagedPersistBackend`] type provides a convenient wrapper around implementations of [`PersistBackend`] that
55
//! allows changes to be staged before committing them.
66
//!
77
//! The [`CombinedChangeSet`] type encapsulates a combination of [`crate`] structures that are
@@ -92,7 +92,7 @@ impl<K, A> From<indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>>
9292
///
9393
/// `C` represents the changeset; a datatype that records changes made to in-memory data structures
9494
/// that are to be persisted, or retrieved from persistence.
95-
pub trait Persist<C> {
95+
pub trait PersistBackend<C> {
9696
/// The error the backend returns when it fails to write.
9797
type WriteError: Debug + Display;
9898

@@ -113,7 +113,7 @@ pub trait Persist<C> {
113113
fn load_changes(&mut self) -> Result<Option<C>, Self::LoadError>;
114114
}
115115

116-
impl<C> Persist<C> for () {
116+
impl<C> PersistBackend<C> for () {
117117
type WriteError = Infallible;
118118
type LoadError = Infallible;
119119

@@ -132,7 +132,7 @@ impl<C> Persist<C> for () {
132132
/// `C` represents the changeset; a datatype that records changes made to in-memory data structures
133133
/// that are to be persisted, or retrieved from persistence.
134134
#[async_trait]
135-
pub trait PersistAsync<C> {
135+
pub trait PersistBackendAsync<C> {
136136
/// The error the backend returns when it fails to write.
137137
type WriteError: Debug + Display;
138138

@@ -155,7 +155,7 @@ pub trait PersistAsync<C> {
155155

156156
#[cfg(feature = "async")]
157157
#[async_trait]
158-
impl<C> PersistAsync<C> for () {
158+
impl<C> PersistBackendAsync<C> for () {
159159
type WriteError = Infallible;
160160
type LoadError = Infallible;
161161

@@ -168,17 +168,17 @@ impl<C> PersistAsync<C> for () {
168168
}
169169
}
170170

171-
/// `StagedPersist` adds a convenient staging area for changesets before they are persisted.
171+
/// `StagedPersistBackend` adds a convenient staging area for changesets before they are persisted.
172172
///
173173
/// Not all changes to the in-memory representation needs to be written to disk right away, so
174-
/// [`crate::persist::StagedPersist::stage`] can be used to *stage* changes first and then
175-
/// [`crate::persist::StagedPersist::commit`] can be used to write changes to disk.
176-
pub struct StagedPersist<C, P: Persist<C>> {
174+
/// [`crate::persist::StagedPersistBackend::stage`] can be used to *stage* changes first and then
175+
/// [`crate::persist::StagedPersistBackend::commit`] can be used to write changes to disk.
176+
pub struct StagedPersistBackend<C, P: PersistBackend<C>> {
177177
inner: P,
178178
stage: C,
179179
}
180180

181-
impl<C, P: Persist<C>> Persist<C> for StagedPersist<C, P> {
181+
impl<C, P: PersistBackend<C>> PersistBackend<C> for StagedPersistBackend<C, P> {
182182
type WriteError = P::WriteError;
183183
type LoadError = P::LoadError;
184184

@@ -191,16 +191,16 @@ impl<C, P: Persist<C>> Persist<C> for StagedPersist<C, P> {
191191
}
192192
}
193193

194-
impl<C, P> StagedPersist<C, P>
194+
impl<C, P> StagedPersistBackend<C, P>
195195
where
196196
C: Default + Append,
197-
P: Persist<C>,
197+
P: PersistBackend<C>,
198198
{
199-
/// Create a new [`StagedPersist`] adding staging to an inner data store that implements
200-
/// [`Persist`].
201-
pub fn new(persist: P) -> Self {
199+
/// Create a new [`StagedPersistBackend`] adding staging to an inner data store that implements
200+
/// [`PersistBackend`].
201+
pub fn new(persist_backend: P) -> Self {
202202
Self {
203-
inner: persist,
203+
inner: persist_backend,
204204
stage: Default::default(),
205205
}
206206
}
@@ -258,16 +258,18 @@ where
258258
/// `StagedPersistAsync` adds a convenient async staging area for changesets before they are persisted.
259259
///
260260
/// Not all changes to the in-memory representation needs to be written to disk right away, so
261-
/// [`StagedPersistAsync::stage`] can be used to *stage* changes first and then
262-
/// [`StagedPersistAsync::commit`] can be used to write changes to disk.
263-
pub struct StagedPersistAsync<C, P: PersistAsync<C>> {
261+
/// [`StagedPersistBackendAsync::stage`] can be used to *stage* changes first and then
262+
/// [`StagedPersistBackendAsync::commit`] can be used to write changes to disk.
263+
pub struct StagedPersistBackendAsync<C, P: PersistBackendAsync<C>> {
264264
inner: P,
265265
staged: C,
266266
}
267267

268268
#[cfg(feature = "async")]
269269
#[async_trait]
270-
impl<C: Send + Sync, P: PersistAsync<C> + Send> PersistAsync<C> for StagedPersistAsync<C, P> {
270+
impl<C: Send + Sync, P: PersistBackendAsync<C> + Send> PersistBackendAsync<C>
271+
for StagedPersistBackendAsync<C, P>
272+
{
271273
type WriteError = P::WriteError;
272274
type LoadError = P::LoadError;
273275

@@ -281,16 +283,16 @@ impl<C: Send + Sync, P: PersistAsync<C> + Send> PersistAsync<C> for StagedPersis
281283
}
282284

283285
#[cfg(feature = "async")]
284-
impl<C, P> StagedPersistAsync<C, P>
286+
impl<C, P> StagedPersistBackendAsync<C, P>
285287
where
286288
C: Default + Append + Send + Sync,
287-
P: PersistAsync<C> + Send,
289+
P: PersistBackendAsync<C> + Send,
288290
{
289-
/// Create a new [`StagedPersistAsync`] adding staging to an inner data store that implements
290-
/// [`PersistAsync`].
291-
pub fn new(persist: P) -> Self {
291+
/// Create a new [`StagedPersistBackendAsync`] adding staging to an inner data store that implements
292+
/// [`PersistBackendAsync`].
293+
pub fn new(persist_backend: P) -> Self {
292294
Self {
293-
inner: persist,
295+
inner: persist_backend,
294296
staged: Default::default(),
295297
}
296298
}
@@ -349,7 +351,7 @@ where
349351
mod test {
350352
extern crate core;
351353

352-
use crate::persist::{Persist, StagedPersist};
354+
use crate::persist::{PersistBackend, StagedPersistBackend};
353355
use crate::Append;
354356
use std::error::Error;
355357
use std::fmt::{self, Display, Formatter};
@@ -395,7 +397,7 @@ mod test {
395397
}
396398
}
397399

398-
impl<C> Persist<C> for TestBackend<C>
400+
impl<C> PersistBackend<C> for TestBackend<C>
399401
where
400402
C: Default + Append + Clone + ToString,
401403
{
@@ -426,7 +428,7 @@ mod test {
426428
changeset: TestChangeSet(None),
427429
};
428430

429-
let mut staged_backend = StagedPersist::new(backend);
431+
let mut staged_backend = StagedPersistBackend::new(backend);
430432
staged_backend.stage(TestChangeSet(Some("ONE".to_string())));
431433
staged_backend.stage(TestChangeSet(None));
432434
staged_backend.stage(TestChangeSet(Some("TWO".to_string())));
@@ -446,7 +448,7 @@ mod test {
446448
let backend = TestBackend {
447449
changeset: TestChangeSet(None),
448450
};
449-
let mut staged_backend = StagedPersist::new(backend);
451+
let mut staged_backend = StagedPersistBackend::new(backend);
450452
staged_backend.stage(TestChangeSet(Some("ERROR".to_string())));
451453
let result = staged_backend.commit();
452454
assert!(matches!(result, Err(e) if e == FailedWrite));

crates/file_store/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ authors = ["Bitcoin Dev Kit Developers"]
1111
readme = "README.md"
1212

1313
[dependencies]
14-
anyhow = { version = "1", default-features = false }
1514
bdk_chain = { path = "../chain", version = "0.15.0", features = [ "serde", "miniscript" ] }
16-
bdk_persist = { path = "../persist", version = "0.3.0"}
1715
bincode = { version = "1" }
1816
serde = { version = "1", features = ["derive"] }
1917

crates/file_store/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# BDK File Store
22

3-
This is a simple append-only flat file implementation of [`PersistBackend`](bdk_persist::PersistBackend).
3+
This is a simple append-only flat file implementation of [`Persist`](bdk_chain::persist::Persist).
44

55
The main structure is [`Store`] which works with any [`bdk_chain`] based changesets to persist data into a flat file.
66

crates/file_store/src/store.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::{bincode_options, EntryIter, FileError, IterError};
2-
use anyhow::anyhow;
2+
use bdk_chain::persist::PersistBackend;
33
use bdk_chain::Append;
4-
use bdk_persist::PersistBackend;
54
use bincode::Options;
65
use std::{
76
fmt::{self, Debug},
@@ -25,19 +24,21 @@ where
2524
impl<C> PersistBackend<C> for Store<C>
2625
where
2726
C: Append
27+
+ Debug
2828
+ serde::Serialize
2929
+ serde::de::DeserializeOwned
3030
+ core::marker::Send
3131
+ core::marker::Sync,
3232
{
33-
fn write_changes(&mut self, changeset: &C) -> anyhow::Result<()> {
33+
type WriteError = io::Error;
34+
type LoadError = AggregateChangesetsError<C>;
35+
36+
fn write_changes(&mut self, changeset: &C) -> Result<(), Self::WriteError> {
3437
self.append_changeset(changeset)
35-
.map_err(|e| anyhow!(e).context("failed to write changes to persistence backend"))
3638
}
3739

38-
fn load_from_persistence(&mut self) -> anyhow::Result<Option<C>> {
40+
fn load_changes(&mut self) -> Result<Option<C>, Self::LoadError> {
3941
self.aggregate_changesets()
40-
.map_err(|e| anyhow!(e.iter_error).context("error loading from persistence backend"))
4142
}
4243
}
4344

crates/hwi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! let first_device = devices.remove(0)?;
1919
//! let custom_signer = HWISigner::from_device(&first_device, Network::Testnet.into())?;
2020
//!
21-
//! # let mut wallet = Wallet::new_no_persist(
21+
//! # let mut wallet = Wallet::new(
2222
//! # "",
2323
//! # "",
2424
//! # Network::Testnet,

crates/persist/Cargo.toml

Lines changed: 0 additions & 22 deletions
This file was deleted.

crates/persist/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

crates/persist/src/changeset.rs

Lines changed: 0 additions & 73 deletions
This file was deleted.

0 commit comments

Comments
 (0)