Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ rand = "0.8"
proptest = "1.2.0"
bdk_testenv = { path = "../testenv" }
criterion = { version = "0.7" }
tempfile = "3"
anyhow = "1.0.102"

[features]
default = ["std", "miniscript"]
Expand Down
109 changes: 109 additions & 0 deletions crates/chain/tests/test_rusqlite_impl.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#![cfg(feature = "rusqlite")]
use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
use bdk_testenv::persist_test_utils::{
persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset,
};

fn tx_graph_changeset_init(
db: &mut rusqlite::Connection,
) -> rusqlite::Result<tx_graph::ChangeSet<ConfirmationBlockTime>> {
let db_tx = db.transaction()?;
tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?;
let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
}

fn tx_graph_changeset_persist(
db: &mut rusqlite::Connection,
changeset: &tx_graph::ChangeSet<ConfirmationBlockTime>,
) -> rusqlite::Result<()> {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
db_tx.commit()
}

fn keychain_txout_changeset_init(
db: &mut rusqlite::Connection,
) -> rusqlite::Result<keychain_txout::ChangeSet> {
let db_tx = db.transaction()?;
keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
}

fn keychain_txout_changeset_persist(
db: &mut rusqlite::Connection,
changeset: &keychain_txout::ChangeSet,
) -> rusqlite::Result<()> {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
db_tx.commit()
}

fn local_chain_changeset_init(
db: &mut rusqlite::Connection,
) -> rusqlite::Result<local_chain::ChangeSet> {
let db_tx = db.transaction()?;
local_chain::ChangeSet::init_sqlite_tables(&db_tx)?;
let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
Ok(changeset)
}

fn local_chain_changeset_persist(
db: &mut rusqlite::Connection,
changeset: &local_chain::ChangeSet,
) -> rusqlite::Result<()> {
let db_tx = db.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
db_tx.commit()
}

#[test]
fn txgraph_is_persisted() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
Ok(persist_txgraph_changeset::<rusqlite::Connection, _, _, _>(
|| {
Ok(rusqlite::Connection::open(
temp_dir.path().join("wallet.sqlite"),
)?)
},
|db| Ok(tx_graph_changeset_init(db)?),
|db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?),
)?)
}

#[test]
fn indexer_is_persisted() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
Ok(persist_indexer_changeset::<rusqlite::Connection, _, _, _>(
|| {
Ok(rusqlite::Connection::open(
temp_dir.path().join("wallet.sqlite"),
)?)
},
|db| Ok(keychain_txout_changeset_init(db)?),
|db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?),
)?)
}

#[test]
fn local_chain_is_persisted() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
Ok(persist_local_chain_changeset::<
rusqlite::Connection,
_,
_,
_,
>(
|| {
Ok(rusqlite::Connection::open(
temp_dir.path().join("wallet.sqlite"),
)?)
},
|db| Ok(local_chain_changeset_init(db)?),
|db, changeset| Ok(local_chain_changeset_persist(db, changeset)?),
)?)
}
3 changes: 3 additions & 0 deletions crates/file_store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ serde = { version = "1", features = ["derive"] }

[dev-dependencies]
tempfile = "3"
anyhow = { version = "1.0.102", default-features = false}
bdk_testenv = {path = "../testenv"}
bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]}
65 changes: 65 additions & 0 deletions crates/file_store/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ mod test {
const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] =
[98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49];

use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime};
use bdk_testenv::persist_test_utils::{
persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset,
};

type TestChangeSet = BTreeSet<String>;

/// Check behavior of [`Store::create`] and [`Store::load`].
Expand Down Expand Up @@ -599,4 +604,64 @@ mod test {
// current position matches EOF
assert_eq!(current_pointer, expected_pointer);
}

#[test]
fn txgraph_is_persisted() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
Ok(persist_txgraph_changeset::<
Store<tx_graph::ChangeSet<ConfirmationBlockTime>>,
_,
_,
_,
>(
|| {
Ok(Store::create(
&TEST_MAGIC_BYTES,
temp_dir.path().join("store.db"),
)?)
},
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
)?)
}

#[test]
fn indexer_is_persisted() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
Ok(persist_indexer_changeset::<
Store<keychain_txout::ChangeSet>,
_,
_,
_,
>(
|| {
Ok(Store::create(
&TEST_MAGIC_BYTES,
temp_dir.path().join("store.db"),
)?)
},
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
)?)
}

#[test]
fn local_chain_is_persisted() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir().unwrap();
Ok(persist_local_chain_changeset::<
Store<local_chain::ChangeSet>,
_,
_,
_,
>(
|| {
Ok(Store::create(
&TEST_MAGIC_BYTES,
temp_dir.path().join("store.db"),
)?)
},
|db| Ok(db.dump().map(Option::unwrap_or_default)?),
|db, changeset| Ok(db.append(changeset)?),
)?)
}
}
4 changes: 2 additions & 2 deletions crates/testenv/Cargo.toml
Comment thread
ValuedMammal marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ bitcoin = { version = "0.32.0", default-features = false }
bdk_testenv = { path = "." }

[features]
default = ["std", "download"]
default = ["std", "download", "miniscript"]
download = ["electrsd/bitcoind_download", "electrsd/bitcoind_28_2", "electrsd/esplora_a33e97e1"]
std = ["bdk_chain/std", "bitcoin/rand-std"]
serde = ["bdk_chain/serde"]

miniscript = ["bdk_chain/miniscript"]
[package.metadata.docs.rs]
no-default-features = true
3 changes: 3 additions & 0 deletions crates/testenv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

pub mod persist_test_utils;
pub mod utils;

use anyhow::Context;
Expand All @@ -14,6 +15,8 @@ use core::time::Duration;
use electrsd::bitcoind::mtype::GetBlockTemplate;
use electrsd::bitcoind::{TemplateRequest, TemplateRules};

extern crate alloc;

pub use electrsd;
pub use electrsd::bitcoind;
pub use electrsd::bitcoind::anyhow;
Expand Down
Loading
Loading