-
Notifications
You must be signed in to change notification settings - Fork 471
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
Open
110CodingP
wants to merge
6
commits into
bitcoindevkit:master
Choose a base branch
from
110CodingP:add_persist_test_utils
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d1a9e15
feat: add utilities for testing persistence
110CodingP f2a3f87
fix
110CodingP de27ded
fix: modify persistence helper
110CodingP c9022fb
refactor: use wildcard imports, add feature gate
110CodingP 9558bb5
docs: update assert_persist_changesets doc
110CodingP cb1cec0
refactor(testenv): use Version instead of u32
110CodingP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #![cfg(feature = "rusqlite")] | ||
| use anyhow::anyhow; | ||
| use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime}; | ||
| use bdk_testenv::persist_test_utils::*; | ||
|
|
||
| #[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] | ||
|
110CodingP marked this conversation as resolved.
|
||
| #[cfg(feature = "miniscript")] | ||
| 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)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -291,10 +291,14 @@ 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::*; | ||||||||
|
|
||||||||
| type TestChangeSet = BTreeSet<String>; | ||||||||
|
|
||||||||
| /// Check behavior of [`Store::create`] and [`Store::load`]. | ||||||||
|
|
@@ -599,4 +603,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.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.