-
Notifications
You must be signed in to change notification settings - Fork 467
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,91 @@ | ||||||||||||
| #![cfg(feature = "rusqlite")] | ||||||||||||
| use anyhow::anyhow; | ||||||||||||
| use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime}; | ||||||||||||
| use bdk_testenv::persist_test_utils::{ | ||||||||||||
| assert_persist_changesets, keychain_txout_changesets, local_chain_changesets, | ||||||||||||
| tx_graph_changesets, | ||||||||||||
| }; | ||||||||||||
|
Comment on lines
+4
to
+7
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
|
||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| fn txgraph_is_persisted() -> anyhow::Result<()> { | ||||||||||||
| let temp_dir = tempfile::tempdir().unwrap(); | ||||||||||||
| let changesets = tx_graph_changesets(); | ||||||||||||
| assert_persist_changesets( | ||||||||||||
| || { | ||||||||||||
| let mut db = rusqlite::Connection::open(temp_dir.path().join("wallet.sqlite"))?; | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?; | ||||||||||||
| db_tx.commit()?; | ||||||||||||
| Ok(db) | ||||||||||||
| }, | ||||||||||||
| |db| { | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?; | ||||||||||||
| Ok(changeset) | ||||||||||||
| }, | ||||||||||||
| |db, changeset| { | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| changeset.persist_to_sqlite(&db_tx)?; | ||||||||||||
| db_tx.commit()?; | ||||||||||||
| Ok(()) | ||||||||||||
| }, | ||||||||||||
| &changesets, | ||||||||||||
| ) | ||||||||||||
| .map_err(|err| anyhow!(err)) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
|
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
|
||||||||||||
| fn indexer_is_persisted() -> anyhow::Result<()> { | ||||||||||||
| let temp_dir = tempfile::tempdir().unwrap(); | ||||||||||||
| let changesets = keychain_txout_changesets(); | ||||||||||||
| assert_persist_changesets( | ||||||||||||
| || { | ||||||||||||
| let mut db = rusqlite::Connection::open(temp_dir.path().join("wallet.sqlite"))?; | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?; | ||||||||||||
| db_tx.commit()?; | ||||||||||||
| Ok(db) | ||||||||||||
| }, | ||||||||||||
| |db| { | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?; | ||||||||||||
| Ok(changeset) | ||||||||||||
| }, | ||||||||||||
| |db, changeset| { | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| changeset.persist_to_sqlite(&db_tx)?; | ||||||||||||
| db_tx.commit()?; | ||||||||||||
| Ok(()) | ||||||||||||
| }, | ||||||||||||
| &changesets, | ||||||||||||
| ) | ||||||||||||
| .map_err(|err| anyhow!(err)) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #[test] | ||||||||||||
| fn local_chain_is_persisted() -> anyhow::Result<()> { | ||||||||||||
| let temp_dir = tempfile::tempdir().unwrap(); | ||||||||||||
| let changesets = local_chain_changesets(); | ||||||||||||
| assert_persist_changesets( | ||||||||||||
| || { | ||||||||||||
| let mut db = rusqlite::Connection::open(temp_dir.path().join("wallet.sqlite"))?; | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| local_chain::ChangeSet::init_sqlite_tables(&db_tx)?; | ||||||||||||
| db_tx.commit()?; | ||||||||||||
| Ok(db) | ||||||||||||
| }, | ||||||||||||
| |db| { | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?; | ||||||||||||
| Ok(changeset) | ||||||||||||
| }, | ||||||||||||
| |db, changeset| { | ||||||||||||
| let db_tx = db.transaction()?; | ||||||||||||
| changeset.persist_to_sqlite(&db_tx)?; | ||||||||||||
| db_tx.commit()?; | ||||||||||||
| Ok(()) | ||||||||||||
| }, | ||||||||||||
| &changesets, | ||||||||||||
| ) | ||||||||||||
| .map_err(|err| anyhow!(err)) | ||||||||||||
| } | ||||||||||||
| 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", default-features = false} | ||||||||
| bdk_testenv = {path = "../testenv"} | ||||||||
| bdk_chain = { path = "../chain", version = "0.23.1", default-features = false, features = ["serde"]} | ||||||||
|
Comment on lines
+25
to
+26
Contributor
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
I think we don't need bdk_chain here |
||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -291,10 +291,17 @@ mod test { | |||||||||||||
| io::{Seek, Write}, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| use anyhow::anyhow; | ||||||||||||||
|
|
||||||||||||||
| const TEST_MAGIC_BYTES_LEN: usize = 12; | ||||||||||||||
| const TEST_MAGIC_BYTES: [u8; TEST_MAGIC_BYTES_LEN] = | ||||||||||||||
| [98, 100, 107, 102, 115, 49, 49, 49, 49, 49, 49, 49]; | ||||||||||||||
|
|
||||||||||||||
| use bdk_testenv::persist_test_utils::{ | ||||||||||||||
| assert_persist_changesets, keychain_txout_changesets, local_chain_changesets, | ||||||||||||||
| tx_graph_changesets, | ||||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
|
Comment on lines
+300
to
+304
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
|
||||||||||||||
| type TestChangeSet = BTreeSet<String>; | ||||||||||||||
|
|
||||||||||||||
| /// Check behavior of [`Store::create`] and [`Store::load`]. | ||||||||||||||
|
|
@@ -599,4 +606,43 @@ 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(); | ||||||||||||||
| let changesets = tx_graph_changesets(); | ||||||||||||||
| assert_persist_changesets( | ||||||||||||||
| || Ok(Store::load_or_create(&TEST_MAGIC_BYTES, temp_dir.path().join("store.db"))?.0), | ||||||||||||||
| |db| Ok(db.dump().map(Option::unwrap_or_default)?), | ||||||||||||||
| |db, changeset| Ok(db.append(changeset)?), | ||||||||||||||
| &changesets, | ||||||||||||||
| ) | ||||||||||||||
| .map_err(|err| anyhow!(err)) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[test] | ||||||||||||||
|
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
Contributor
Author
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. On probing more I realised we don't have a |
||||||||||||||
| fn indexer_is_persisted() -> anyhow::Result<()> { | ||||||||||||||
| let temp_dir = tempfile::tempdir().unwrap(); | ||||||||||||||
| let changesets = keychain_txout_changesets(); | ||||||||||||||
| assert_persist_changesets( | ||||||||||||||
| || Ok(Store::load_or_create(&TEST_MAGIC_BYTES, temp_dir.path().join("store.db"))?.0), | ||||||||||||||
| |db| Ok(db.dump().map(Option::unwrap_or_default)?), | ||||||||||||||
| |db, changeset| Ok(db.append(changeset)?), | ||||||||||||||
| &changesets, | ||||||||||||||
| ) | ||||||||||||||
| .map_err(|err| anyhow!(err)) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| #[test] | ||||||||||||||
| fn local_chain_is_persisted() -> anyhow::Result<()> { | ||||||||||||||
| let temp_dir = tempfile::tempdir().unwrap(); | ||||||||||||||
| let changesets = local_chain_changesets(); | ||||||||||||||
| assert_persist_changesets( | ||||||||||||||
| || Ok(Store::load_or_create(&TEST_MAGIC_BYTES, temp_dir.path().join("store.db"))?.0), | ||||||||||||||
| |db| Ok(db.dump().map(Option::unwrap_or_default)?), | ||||||||||||||
| |db, changeset| Ok(db.append(changeset)?), | ||||||||||||||
| &changesets, | ||||||||||||||
| ) | ||||||||||||||
| .map_err(|err| anyhow!(err)) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
ValuedMammal marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.