Skip to content

Commit

Permalink
refactor(persist): update file_store, sqlite, wallet to use bdk_chain…
Browse files Browse the repository at this point in the history
…::persist

Also update examples and remove bdk_persist crate.
  • Loading branch information
notmandatory committed Jun 1, 2024
1 parent 9cbf0de commit e5c73ec
Show file tree
Hide file tree
Showing 27 changed files with 488 additions and 738 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ members = [
"crates/esplora",
"crates/bitcoind_rpc",
"crates/hwi",
"crates/persist",
"crates/testenv",
"example-crates/example_cli",
"example-crates/example_electrum",
Expand Down
11 changes: 7 additions & 4 deletions crates/chain/src/persist.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
//! This module is home to the [`Persist`] trait which defines the behavior of a data store
//! required to persist changes made to BDK data structures.
//!
//! The [`Stage`] type provides a convenient wrapper around implementations of [`Persist`] that
//! The [`StagedPersist`] type provides a convenient wrapper around implementations of [`Persist`] that
//! allows changes to be staged before committing them.
//!
//! The [`CombinedChangeSet`] type encapsulates a combination of [`crate`] structures that are
//! typically persisted together.

use crate::{indexed_tx_graph, keychain, local_chain, Anchor, Append};
use bitcoin::Network;
use core::convert::Infallible;
use core::default::Default;
use core::fmt::{Debug, Display};
use std::convert::Infallible;
use std::mem;
use core::mem;

/// A changeset containing [`crate`] structures typically persisted together.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -151,7 +151,7 @@ where
P: Persist<C>,
{
/// Create a new [`StagedPersist`] adding staging to an inner data store that implements
/// [`Persit`].
/// [`Persist`].
pub fn new(persist: P) -> Self {
Self {
inner: persist,
Expand Down Expand Up @@ -214,6 +214,7 @@ mod test {

use crate::persist::{Persist, StagedPersist};
use crate::Append;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
use std::prelude::rust_2015::{String, ToString};
use TestError::FailedWrite;
Expand All @@ -234,6 +235,8 @@ mod test {
}
}

impl Error for TestError {}

#[derive(Clone, Default)]
struct TestChangeSet(Option<String>);

Expand Down
2 changes: 0 additions & 2 deletions crates/file_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ authors = ["Bitcoin Dev Kit Developers"]
readme = "README.md"

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

Expand Down
2 changes: 1 addition & 1 deletion crates/file_store/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BDK File Store

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

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

Expand Down
15 changes: 8 additions & 7 deletions crates/file_store/src/store.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::{bincode_options, EntryIter, FileError, IterError};
use anyhow::anyhow;
use bdk_chain::persist::Persist;
use bdk_chain::Append;
use bdk_persist::PersistBackend;
use bincode::Options;
use std::{
fmt::{self, Debug},
Expand All @@ -22,22 +21,24 @@ where
marker: PhantomData<C>,
}

impl<C> PersistBackend<C> for Store<C>
impl<C> Persist<C> for Store<C>
where
C: Append
+ Debug
+ serde::Serialize
+ serde::de::DeserializeOwned
+ core::marker::Send
+ core::marker::Sync,
{
fn write_changes(&mut self, changeset: &C) -> anyhow::Result<()> {
type WriteError = io::Error;
type LoadError = AggregateChangesetsError<C>;

fn write_changes(&mut self, changeset: &C) -> Result<(), Self::WriteError> {
self.append_changeset(changeset)
.map_err(|e| anyhow!(e).context("failed to write changes to persistence backend"))
}

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

Expand Down
22 changes: 0 additions & 22 deletions crates/persist/Cargo.toml

This file was deleted.

5 changes: 0 additions & 5 deletions crates/persist/README.md

This file was deleted.

73 changes: 0 additions & 73 deletions crates/persist/src/changeset.rs

This file was deleted.

8 changes: 0 additions & 8 deletions crates/persist/src/lib.rs

This file was deleted.

106 changes: 0 additions & 106 deletions crates/persist/src/persist.rs

This file was deleted.

2 changes: 0 additions & 2 deletions crates/sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ authors = ["Bitcoin Dev Kit Developers"]
readme = "README.md"

[dependencies]
anyhow = { version = "1", default-features = false }
bdk_chain = { path = "../chain", version = "0.15.0", features = ["serde", "miniscript"] }
bdk_persist = { path = "../persist", version = "0.3.0", features = ["serde"] }
rusqlite = { version = "0.31.0", features = ["bundled"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
6 changes: 3 additions & 3 deletions crates/sqlite/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# BDK SQLite

This is a simple [SQLite] relational database schema backed implementation of [`PersistBackend`](bdk_persist::PersistBackend).
This is a simple [SQLite] relational database schema backed implementation of `Persist`.

The main structure is `Store` which persists [`bdk_persist`] `CombinedChangeSet` data into a SQLite database file.
The main structure is `Store` which persists `CombinedChangeSet` data into a SQLite database file.

[`bdk_persist`]:https://docs.rs/bdk_persist/latest/bdk_persist/
<!-- [`Persist`]: bdk_chain::persist::PersistBackend -->
[SQLite]: https://www.sqlite.org/index.html
Loading

0 comments on commit e5c73ec

Please sign in to comment.