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 12, 2024
1 parent 68a3760 commit c23d2bc
Show file tree
Hide file tree
Showing 34 changed files with 556 additions and 804 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
2 changes: 1 addition & 1 deletion crates/chain/src/keychain/txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {

/// Return a reference to the internal [`SpkTxOutIndex`].
///
/// **WARNING:** The internal index will contain lookahead spks. Refer to
/// **WARNING**: The internal index will contain lookahead spks. Refer to
/// [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
pub fn inner(&self) -> &SpkTxOutIndex<(DescriptorId, u32)> {
&self.inner
Expand Down
64 changes: 33 additions & 31 deletions crates/chain/src/persist.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module is home to the [`Persist`] trait which defines the behavior of a data store
//! This module is home to the [`PersistBackend`] trait which defines the behavior of a data store
//! required to persist changes made to BDK data structures.
//!
//! The [`StagedPersist`] type provides a convenient wrapper around implementations of [`Persist`] that
//! The [`StagedPersistBackend`] type provides a convenient wrapper around implementations of [`PersistBackend`] that
//! allows changes to be staged before committing them.
//!
//! The [`CombinedChangeSet`] type encapsulates a combination of [`crate`] structures that are
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<K, A> From<indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>>
///
/// `C` represents the changeset; a datatype that records changes made to in-memory data structures
/// that are to be persisted, or retrieved from persistence.
pub trait Persist<C> {
pub trait PersistBackend<C> {
/// The error the backend returns when it fails to write.
type WriteError: Debug + Display;

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

impl<C> Persist<C> for () {
impl<C> PersistBackend<C> for () {
type WriteError = Infallible;
type LoadError = Infallible;

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

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

#[cfg(feature = "async")]
#[async_trait]
impl<C> PersistAsync<C> for () {
impl<C> PersistBackendAsync<C> for () {
type WriteError = Infallible;
type LoadError = Infallible;

Expand All @@ -168,17 +168,17 @@ impl<C> PersistAsync<C> for () {
}
}

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

impl<C, P: Persist<C>> Persist<C> for StagedPersist<C, P> {
impl<C, P: PersistBackend<C>> PersistBackend<C> for StagedPersistBackend<C, P> {
type WriteError = P::WriteError;
type LoadError = P::LoadError;

Expand All @@ -191,16 +191,16 @@ impl<C, P: Persist<C>> Persist<C> for StagedPersist<C, P> {
}
}

impl<C, P> StagedPersist<C, P>
impl<C, P> StagedPersistBackend<C, P>
where
C: Default + Append,
P: Persist<C>,
P: PersistBackend<C>,
{
/// Create a new [`StagedPersist`] adding staging to an inner data store that implements
/// [`Persist`].
pub fn new(persist: P) -> Self {
/// Create a new [`StagedPersistBackend`] adding staging to an inner data store that implements
/// [`PersistBackend`].
pub fn new(persist_backend: P) -> Self {
Self {
inner: persist,
inner: persist_backend,
stage: Default::default(),
}
}
Expand Down Expand Up @@ -258,16 +258,18 @@ where
/// `StagedPersistAsync` adds a convenient async staging area for changesets before they are persisted.
///
/// Not all changes to the in-memory representation needs to be written to disk right away, so
/// [`StagedPersistAsync::stage`] can be used to *stage* changes first and then
/// [`StagedPersistAsync::commit`] can be used to write changes to disk.
pub struct StagedPersistAsync<C, P: PersistAsync<C>> {
/// [`StagedPersistBackendAsync::stage`] can be used to *stage* changes first and then
/// [`StagedPersistBackendAsync::commit`] can be used to write changes to disk.
pub struct StagedPersistBackendAsync<C, P: PersistBackendAsync<C>> {
inner: P,
staged: C,
}

#[cfg(feature = "async")]
#[async_trait]
impl<C: Send + Sync, P: PersistAsync<C> + Send> PersistAsync<C> for StagedPersistAsync<C, P> {
impl<C: Send + Sync, P: PersistBackendAsync<C> + Send> PersistBackendAsync<C>
for StagedPersistBackendAsync<C, P>
{
type WriteError = P::WriteError;
type LoadError = P::LoadError;

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

#[cfg(feature = "async")]
impl<C, P> StagedPersistAsync<C, P>
impl<C, P> StagedPersistBackendAsync<C, P>
where
C: Default + Append + Send + Sync,
P: PersistAsync<C> + Send,
P: PersistBackendAsync<C> + Send,
{
/// Create a new [`StagedPersistAsync`] adding staging to an inner data store that implements
/// [`PersistAsync`].
pub fn new(persist: P) -> Self {
/// Create a new [`StagedPersistBackendAsync`] adding staging to an inner data store that implements
/// [`PersistBackendAsync`].
pub fn new(persist_backend: P) -> Self {
Self {
inner: persist,
inner: persist_backend,
staged: Default::default(),
}
}
Expand Down Expand Up @@ -349,7 +351,7 @@ where
mod test {
extern crate core;

use crate::persist::{Persist, StagedPersist};
use crate::persist::{PersistBackend, StagedPersistBackend};
use crate::Append;
use std::error::Error;
use std::fmt::{self, Display, Formatter};
Expand Down Expand Up @@ -395,7 +397,7 @@ mod test {
}
}

impl<C> Persist<C> for TestBackend<C>
impl<C> PersistBackend<C> for TestBackend<C>
where
C: Default + Append + Clone + ToString,
{
Expand Down Expand Up @@ -426,7 +428,7 @@ mod test {
changeset: TestChangeSet(None),
};

let mut staged_backend = StagedPersist::new(backend);
let mut staged_backend = StagedPersistBackend::new(backend);
staged_backend.stage(TestChangeSet(Some("ONE".to_string())));
staged_backend.stage(TestChangeSet(None));
staged_backend.stage(TestChangeSet(Some("TWO".to_string())));
Expand All @@ -446,7 +448,7 @@ mod test {
let backend = TestBackend {
changeset: TestChangeSet(None),
};
let mut staged_backend = StagedPersist::new(backend);
let mut staged_backend = StagedPersistBackend::new(backend);
staged_backend.stage(TestChangeSet(Some("ERROR".to_string())));
let result = staged_backend.commit();
assert!(matches!(result, Err(e) if e == FailedWrite));
Expand Down
4 changes: 1 addition & 3 deletions crates/file_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/bitcoindevkit/bdk"
documentation = "https://docs.rs/bdk_file_store"
description = "A simple append-only flat file implementation of Persist for Bitcoin Dev Kit."
description = "A simple append-only flat file implementation of PersistBackend for Bitcoin Dev Kit."
keywords = ["bitcoin", "persist", "persistence", "bdk", "file"]
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 [`PersistBackend`](bdk_chain::persist::PersistBackend).

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

Expand Down
13 changes: 7 additions & 6 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::PersistBackend;
use bdk_chain::Append;
use bdk_persist::PersistBackend;
use bincode::Options;
use std::{
fmt::{self, Debug},
Expand All @@ -25,19 +24,21 @@ where
impl<C> PersistBackend<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
2 changes: 1 addition & 1 deletion crates/hwi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! let first_device = devices.remove(0)?;
//! let custom_signer = HWISigner::from_device(&first_device, Network::Testnet.into())?;
//!
//! # let mut wallet = Wallet::new_no_persist(
//! # let mut wallet = Wallet::new(
//! # "",
//! # "",
//! # Network::Testnet,
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.

Loading

0 comments on commit c23d2bc

Please sign in to comment.