|
| 1 | +#![cfg(feature = "rusqlite")] |
| 2 | +use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime}; |
| 3 | +use bdk_testenv::persist_test_utils::{ |
| 4 | + persist_indexer_changeset, persist_local_chain_changeset, persist_txgraph_changeset, |
| 5 | +}; |
| 6 | + |
| 7 | +fn tx_graph_changeset_init( |
| 8 | + db: &mut rusqlite::Connection, |
| 9 | +) -> rusqlite::Result<tx_graph::ChangeSet<ConfirmationBlockTime>> { |
| 10 | + let db_tx = db.transaction()?; |
| 11 | + tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?; |
| 12 | + let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?; |
| 13 | + db_tx.commit()?; |
| 14 | + Ok(changeset) |
| 15 | +} |
| 16 | + |
| 17 | +fn tx_graph_changeset_persist( |
| 18 | + db: &mut rusqlite::Connection, |
| 19 | + changeset: &tx_graph::ChangeSet<ConfirmationBlockTime>, |
| 20 | +) -> rusqlite::Result<()> { |
| 21 | + let db_tx = db.transaction()?; |
| 22 | + changeset.persist_to_sqlite(&db_tx)?; |
| 23 | + db_tx.commit() |
| 24 | +} |
| 25 | + |
| 26 | +fn keychain_txout_changeset_init( |
| 27 | + db: &mut rusqlite::Connection, |
| 28 | +) -> rusqlite::Result<keychain_txout::ChangeSet> { |
| 29 | + let db_tx = db.transaction()?; |
| 30 | + keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?; |
| 31 | + let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?; |
| 32 | + db_tx.commit()?; |
| 33 | + Ok(changeset) |
| 34 | +} |
| 35 | + |
| 36 | +fn keychain_txout_changeset_persist( |
| 37 | + db: &mut rusqlite::Connection, |
| 38 | + changeset: &keychain_txout::ChangeSet, |
| 39 | +) -> rusqlite::Result<()> { |
| 40 | + let db_tx = db.transaction()?; |
| 41 | + changeset.persist_to_sqlite(&db_tx)?; |
| 42 | + db_tx.commit() |
| 43 | +} |
| 44 | + |
| 45 | +fn local_chain_changeset_init( |
| 46 | + db: &mut rusqlite::Connection, |
| 47 | +) -> rusqlite::Result<local_chain::ChangeSet> { |
| 48 | + let db_tx = db.transaction()?; |
| 49 | + local_chain::ChangeSet::init_sqlite_tables(&db_tx)?; |
| 50 | + let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?; |
| 51 | + db_tx.commit()?; |
| 52 | + Ok(changeset) |
| 53 | +} |
| 54 | + |
| 55 | +fn local_chain_changeset_persist( |
| 56 | + db: &mut rusqlite::Connection, |
| 57 | + changeset: &local_chain::ChangeSet, |
| 58 | +) -> rusqlite::Result<()> { |
| 59 | + let db_tx = db.transaction()?; |
| 60 | + changeset.persist_to_sqlite(&db_tx)?; |
| 61 | + db_tx.commit() |
| 62 | +} |
| 63 | + |
| 64 | +#[test] |
| 65 | +fn txgraph_is_persisted() -> anyhow::Result<()> { |
| 66 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 67 | + Ok(persist_txgraph_changeset::<rusqlite::Connection, _, _, _>( |
| 68 | + || { |
| 69 | + Ok(rusqlite::Connection::open( |
| 70 | + temp_dir.path().join("wallet.sqlite"), |
| 71 | + )?) |
| 72 | + }, |
| 73 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 74 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 75 | + )?) |
| 76 | +} |
| 77 | + |
| 78 | +#[test] |
| 79 | +fn indexer_is_persisted() -> anyhow::Result<()> { |
| 80 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 81 | + Ok(persist_indexer_changeset::<rusqlite::Connection, _, _, _>( |
| 82 | + || { |
| 83 | + Ok(rusqlite::Connection::open( |
| 84 | + temp_dir.path().join("wallet.sqlite"), |
| 85 | + )?) |
| 86 | + }, |
| 87 | + |db| Ok(keychain_txout_changeset_init(db)?), |
| 88 | + |db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?), |
| 89 | + )?) |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn local_chain_is_persisted() -> anyhow::Result<()> { |
| 94 | + let temp_dir = tempfile::tempdir().unwrap(); |
| 95 | + Ok(persist_local_chain_changeset::< |
| 96 | + rusqlite::Connection, |
| 97 | + _, |
| 98 | + _, |
| 99 | + _, |
| 100 | + >( |
| 101 | + || { |
| 102 | + Ok(rusqlite::Connection::open( |
| 103 | + temp_dir.path().join("wallet.sqlite"), |
| 104 | + )?) |
| 105 | + }, |
| 106 | + |db| Ok(local_chain_changeset_init(db)?), |
| 107 | + |db, changeset| Ok(local_chain_changeset_persist(db, changeset)?), |
| 108 | + )?) |
| 109 | +} |
0 commit comments