-
Notifications
You must be signed in to change notification settings - Fork 455
Add functions to test persistence #2012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, _, _, _>( | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: The explicit turbofishing is not really needed for any of these functions.
Suggested change
|
||||||
| || { | ||||||
| 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)?), | ||||||
| )?) | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,3 +21,6 @@ serde = { version = "1", features = ["derive"] } | |||||
|
|
||||||
| [dev-dependencies] | ||||||
| tempfile = "3" | ||||||
| anyhow = { version = "1.0.102", default-features = false} | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
No need to explicitly define the minor and patch versions. |
||||||
| bdk_testenv = {path = "../testenv"} | ||||||
| bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]} | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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`]. | ||||||||||||||||
|
|
@@ -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>>, | ||||||||||||||||
| _, | ||||||||||||||||
| _, | ||||||||||||||||
| _, | ||||||||||||||||
| >( | ||||||||||||||||
|
Comment on lines
+611
to
+616
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Turbofishing not necessary. |
||||||||||||||||
| || { | ||||||||||||||||
| 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)?), | ||||||||||||||||
| )?) | ||||||||||||||||
| } | ||||||||||||||||
| } | ||||||||||||||||
|
ValuedMammal marked this conversation as resolved.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a whole bunch of functions in this file that is only called once. Let's just inline it - this will improve readability and reduce lines of code.