refactor(chain,wallet)!: move rusqlite things into it's own file

Also fix imports and rename `sqlite` module to `rusqlite_impl`.
This commit is contained in:
志宇
2024-07-19 11:20:34 +00:00
parent 93f9b83e27
commit 2cf07d686b
14 changed files with 587 additions and 602 deletions

View File

@@ -30,15 +30,15 @@ std = ["bitcoin/std", "bitcoin/rand-std", "miniscript/std", "bdk_chain/std"]
compiler = ["miniscript/compiler"]
all-keys = ["keys-bip39"]
keys-bip39 = ["bip39"]
sqlite = ["bdk_chain/sqlite"]
rusqlite = ["bdk_chain/rusqlite"]
file_store = ["bdk_file_store"]
[dev-dependencies]
lazy_static = "1.4"
assert_matches = "1.5.0"
tempfile = "3"
bdk_chain = { path = "../chain", features = ["sqlite"] }
bdk_wallet = { path = ".", features = ["sqlite", "file_store"] }
bdk_chain = { path = "../chain", features = ["rusqlite"] }
bdk_wallet = { path = ".", features = ["rusqlite", "file_store"] }
bdk_file_store = { path = "../file_store" }
anyhow = "1"
rand = "^0.8"

View File

@@ -32,10 +32,10 @@ mod types;
mod wallet;
pub(crate) use bdk_chain::collections;
#[cfg(feature = "sqlite")]
#[cfg(feature = "rusqlite")]
pub use bdk_chain::rusqlite;
#[cfg(feature = "sqlite")]
pub use bdk_chain::sqlite;
#[cfg(feature = "rusqlite")]
pub use bdk_chain::rusqlite_impl;
pub use descriptor::template;
pub use descriptor::HdKeyPaths;
pub use signer;

View File

@@ -64,7 +64,7 @@ impl Merge for ChangeSet {
}
}
#[cfg(feature = "sqlite")]
#[cfg(feature = "rusqlite")]
impl ChangeSet {
/// Schema name for wallet.
pub const WALLET_SCHEMA_NAME: &'static str = "bdk_wallet";
@@ -84,14 +84,14 @@ impl ChangeSet {
) STRICT;",
Self::WALLET_TABLE_NAME,
)];
crate::sqlite::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])
crate::rusqlite_impl::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])
}
/// Recover a [`ChangeSet`] from sqlite database.
pub fn from_sqlite(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<Self> {
Self::init_wallet_sqlite_tables(db_tx)?;
use crate::sqlite::Sql;
use chain::rusqlite::OptionalExtension;
use chain::Impl;
use miniscript::{Descriptor, DescriptorPublicKey};
let mut changeset = Self::default();
@@ -103,13 +103,13 @@ impl ChangeSet {
let row = wallet_statement
.query_row([], |row| {
Ok((
row.get::<_, Sql<Descriptor<DescriptorPublicKey>>>("descriptor")?,
row.get::<_, Sql<Descriptor<DescriptorPublicKey>>>("change_descriptor")?,
row.get::<_, Sql<bitcoin::Network>>("network")?,
row.get::<_, Impl<Descriptor<DescriptorPublicKey>>>("descriptor")?,
row.get::<_, Impl<Descriptor<DescriptorPublicKey>>>("change_descriptor")?,
row.get::<_, Impl<bitcoin::Network>>("network")?,
))
})
.optional()?;
if let Some((Sql(desc), Sql(change_desc), Sql(network))) = row {
if let Some((Impl(desc), Impl(change_desc), Impl(network))) = row {
changeset.descriptor = Some(desc);
changeset.change_descriptor = Some(change_desc);
changeset.network = Some(network);
@@ -129,7 +129,7 @@ impl ChangeSet {
) -> chain::rusqlite::Result<()> {
Self::init_wallet_sqlite_tables(db_tx)?;
use chain::rusqlite::named_params;
use chain::sqlite::Sql;
use chain::Impl;
let mut descriptor_statement = db_tx.prepare_cached(&format!(
"INSERT INTO {}(id, descriptor) VALUES(:id, :descriptor) ON CONFLICT(id) DO UPDATE SET descriptor=:descriptor",
@@ -138,7 +138,7 @@ impl ChangeSet {
if let Some(descriptor) = &self.descriptor {
descriptor_statement.execute(named_params! {
":id": 0,
":descriptor": Sql(descriptor.clone()),
":descriptor": Impl(descriptor.clone()),
})?;
}
@@ -149,7 +149,7 @@ impl ChangeSet {
if let Some(change_descriptor) = &self.change_descriptor {
change_descriptor_statement.execute(named_params! {
":id": 0,
":change_descriptor": Sql(change_descriptor.clone()),
":change_descriptor": Impl(change_descriptor.clone()),
})?;
}
@@ -160,7 +160,7 @@ impl ChangeSet {
if let Some(network) = self.network {
network_statement.execute(named_params! {
":id": 0,
":network": Sql(network),
":network": Impl(network),
})?;
}

View File

@@ -586,7 +586,7 @@ impl Wallet {
///
/// ```rust,no_run
/// # use bdk_wallet::{LoadParams, ChangeSet, KeychainKind};
/// use bdk_chain::sqlite::Connection;
/// use bdk_chain::rusqlite::Connection;
/// let mut conn = Connection::open_in_memory().expect("must open connection");
/// let mut wallet = LoadParams::new()
/// .load_wallet(&mut conn)

View File

@@ -5,8 +5,8 @@ use crate::{descriptor::DescriptorError, Wallet};
/// Represents a persisted wallet.
pub type PersistedWallet = bdk_chain::Persisted<Wallet>;
#[cfg(feature = "sqlite")]
impl<'c> chain::PersistWith<bdk_chain::sqlite::Transaction<'c>> for Wallet {
#[cfg(feature = "rusqlite")]
impl<'c> chain::PersistWith<bdk_chain::rusqlite::Transaction<'c>> for Wallet {
type CreateParams = crate::CreateParams;
type LoadParams = crate::LoadParams;
@@ -15,7 +15,7 @@ impl<'c> chain::PersistWith<bdk_chain::sqlite::Transaction<'c>> for Wallet {
type PersistError = bdk_chain::rusqlite::Error;
fn create(
db: &mut bdk_chain::sqlite::Transaction<'c>,
db: &mut bdk_chain::rusqlite::Transaction<'c>,
params: Self::CreateParams,
) -> Result<Self, Self::CreateError> {
let mut wallet =
@@ -29,7 +29,7 @@ impl<'c> chain::PersistWith<bdk_chain::sqlite::Transaction<'c>> for Wallet {
}
fn load(
conn: &mut bdk_chain::sqlite::Transaction<'c>,
conn: &mut bdk_chain::rusqlite::Transaction<'c>,
params: Self::LoadParams,
) -> Result<Option<Self>, Self::LoadError> {
let changeset =
@@ -41,15 +41,15 @@ impl<'c> chain::PersistWith<bdk_chain::sqlite::Transaction<'c>> for Wallet {
}
fn persist(
db: &mut bdk_chain::sqlite::Transaction<'c>,
db: &mut bdk_chain::rusqlite::Transaction<'c>,
changeset: &<Self as chain::Staged>::ChangeSet,
) -> Result<(), Self::PersistError> {
changeset.persist_to_sqlite(db)
}
}
#[cfg(feature = "sqlite")]
impl chain::PersistWith<bdk_chain::sqlite::Connection> for Wallet {
#[cfg(feature = "rusqlite")]
impl chain::PersistWith<bdk_chain::rusqlite::Connection> for Wallet {
type CreateParams = crate::CreateParams;
type LoadParams = crate::LoadParams;
@@ -58,7 +58,7 @@ impl chain::PersistWith<bdk_chain::sqlite::Connection> for Wallet {
type PersistError = bdk_chain::rusqlite::Error;
fn create(
db: &mut bdk_chain::sqlite::Connection,
db: &mut bdk_chain::rusqlite::Connection,
params: Self::CreateParams,
) -> Result<Self, Self::CreateError> {
let mut db_tx = db.transaction().map_err(CreateWithPersistError::Persist)?;
@@ -68,7 +68,7 @@ impl chain::PersistWith<bdk_chain::sqlite::Connection> for Wallet {
}
fn load(
db: &mut bdk_chain::sqlite::Connection,
db: &mut bdk_chain::rusqlite::Connection,
params: Self::LoadParams,
) -> Result<Option<Self>, Self::LoadError> {
let mut db_tx = db.transaction().map_err(LoadWithPersistError::Persist)?;
@@ -78,7 +78,7 @@ impl chain::PersistWith<bdk_chain::sqlite::Connection> for Wallet {
}
fn persist(
db: &mut bdk_chain::sqlite::Connection,
db: &mut bdk_chain::rusqlite::Connection,
changeset: &<Self as chain::Staged>::ChangeSet,
) -> Result<(), Self::PersistError> {
let db_tx = db.transaction()?;

View File

@@ -169,10 +169,10 @@ fn wallet_is_persisted() -> anyhow::Result<()> {
|path| Ok(bdk_file_store::Store::create_new(DB_MAGIC, path)?),
|path| Ok(bdk_file_store::Store::open(DB_MAGIC, path)?),
)?;
run::<bdk_chain::sqlite::Connection, _, _>(
run::<bdk_chain::rusqlite::Connection, _, _>(
"store.sqlite",
|path| Ok(bdk_chain::sqlite::Connection::open(path)?),
|path| Ok(bdk_chain::sqlite::Connection::open(path)?),
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
)?;
Ok(())
@@ -258,8 +258,8 @@ fn wallet_load_checks() -> anyhow::Result<()> {
)?;
run(
"store.sqlite",
|path| Ok(bdk_chain::sqlite::Connection::open(path)?),
|path| Ok(bdk_chain::sqlite::Connection::open(path)?),
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
|path| Ok(bdk_chain::rusqlite::Connection::open(path)?),
)?;
Ok(())