Skip to content

Commit

Permalink
feat: add bdk_sqlite_store crate implementing PersistBackend backed b…
Browse files Browse the repository at this point in the history
…y a SQLite database
  • Loading branch information
notmandatory committed Sep 19, 2023
1 parent f95506b commit 6603c60
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"crates/bdk",
"crates/chain",
"crates/file_store",
"crates/sqlite_store",
"crates/electrum",
"crates/esplora",
"example-crates/example_cli",
Expand Down
21 changes: 21 additions & 0 deletions crates/sqlite_store/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "bdk_sqlite_store"
version = "0.2.0"
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 SQLite based implementation of Persist for Bitcoin Dev Kit."
keywords = ["bitcoin", "persist", "persistence", "bdk", "sqlite"]
authors = ["Bitcoin Dev Kit Developers"]
readme = "README.md"

[dependencies]
bdk_chain = { path = "../chain", version = "0.5.0", features = [ "serde", "miniscript" ] }
rusqlite = { version = "0.29.0", features = ["bundled"]}
serde = { version = "1", features = ["derive"] }
log = { version = "0.4", features = [] }
serde_json = "1.0.107"

[dev-dependencies]
tempfile = "3"
11 changes: 11 additions & 0 deletions crates/sqlite_store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# BDK SQLite Store

This is a simple append-only [SQLite] database backed implementation of
[`Persist`](`bdk_chain::Persist`).

The main structure is [`Store`](`crate::Store`), which can be used with [`bdk`]'s
`Wallet` to persist wallet data into a SQLite database file.

[`bdk`]: https://docs.rs/bdk/latest
[`bdk_chain`]: https://docs.rs/bdk_chain/latest
[SQLite]: https://www.sqlite.org/index.html
59 changes: 59 additions & 0 deletions crates/sqlite_store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![doc = include_str!("../README.md")]

mod store;

pub use store::*;

/// Error that occurs while appending new change sets to the DB.
#[derive(Debug)]
pub enum AppendError {
/// JSON encoding error.
Json(serde_json::Error),
/// SQLite error.
Sqlite(rusqlite::Error),
}

impl<'a> core::fmt::Display for AppendError {

Check failure on line 16 in crates/sqlite_store/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this lifetime isn't used in the impl

error: this lifetime isn't used in the impl --> crates/sqlite_store/src/lib.rs:16:6 | 16 | impl<'a> core::fmt::Display for AppendError { | ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Json(e) => write!(f, "json error trying to append change set: {}", e),
Self::Sqlite(e) => write!(f, "sqlite error trying to append change set: {}", e),
}
}
}

impl<'a> From<serde_json::Error> for AppendError {

Check failure on line 25 in crates/sqlite_store/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this lifetime isn't used in the impl

error: this lifetime isn't used in the impl --> crates/sqlite_store/src/lib.rs:25:6 | 25 | impl<'a> From<serde_json::Error> for AppendError { | ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes
fn from(error: serde_json::Error) -> Self {
Self::Json(error)
}
}

impl<'a> std::error::Error for AppendError {}

Check failure on line 31 in crates/sqlite_store/src/lib.rs

View workflow job for this annotation

GitHub Actions / clippy

this lifetime isn't used in the impl

error: this lifetime isn't used in the impl --> crates/sqlite_store/src/lib.rs:31:6 | 31 | impl<'a> std::error::Error for AppendError {} | ^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes

/// Error the occurs while iterating stored change sets from the DB.
#[derive(Debug)]
pub enum IterError {
/// Json decoding
Json {
rowid: usize,
err: serde_json::Error,
},
/// Sqlite error
Sqlite(rusqlite::Error),
/// FromSql error
FromSql(rusqlite::types::FromSqlError),
}

impl core::fmt::Display for IterError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
IterError::Json { rowid, err } => write!(
f,
"json error trying to decode change set {}, {}",
rowid, err
),
IterError::Sqlite(err) => write!(f, "Sqlite error {}", err),
IterError::FromSql(err) => write!(f, "FromSql error {}", err),
}
}
}
Loading

0 comments on commit 6603c60

Please sign in to comment.