|
| 1 | +#![cfg(feature = "rusqlite")] |
| 2 | +use bdk_chain::{keychain_txout, local_chain, tx_graph, ConfirmationBlockTime}; |
| 3 | +use bdk_testenv::persist_test_utils::{ |
| 4 | + persist_anchors, persist_first_seen, persist_indexer_changeset, persist_last_evicted, |
| 5 | + persist_last_revealed, persist_last_seen, persist_local_chain_changeset, persist_spk_cache, |
| 6 | + persist_txgraph_changeset, persist_txouts, persist_txs, |
| 7 | +}; |
| 8 | + |
| 9 | +fn tx_graph_changeset_init( |
| 10 | + db: &mut rusqlite::Connection, |
| 11 | +) -> rusqlite::Result<tx_graph::ChangeSet<ConfirmationBlockTime>> { |
| 12 | + let db_tx = db.transaction()?; |
| 13 | + tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(&db_tx)?; |
| 14 | + let changeset = tx_graph::ChangeSet::<ConfirmationBlockTime>::from_sqlite(&db_tx)?; |
| 15 | + db_tx.commit()?; |
| 16 | + Ok(changeset) |
| 17 | +} |
| 18 | + |
| 19 | +fn tx_graph_changeset_persist( |
| 20 | + db: &mut rusqlite::Connection, |
| 21 | + changeset: &tx_graph::ChangeSet<ConfirmationBlockTime>, |
| 22 | +) -> rusqlite::Result<()> { |
| 23 | + let db_tx = db.transaction()?; |
| 24 | + changeset.persist_to_sqlite(&db_tx)?; |
| 25 | + db_tx.commit() |
| 26 | +} |
| 27 | + |
| 28 | +fn keychain_txout_changeset_init( |
| 29 | + db: &mut rusqlite::Connection, |
| 30 | +) -> rusqlite::Result<keychain_txout::ChangeSet> { |
| 31 | + let db_tx = db.transaction()?; |
| 32 | + keychain_txout::ChangeSet::init_sqlite_tables(&db_tx)?; |
| 33 | + let changeset = keychain_txout::ChangeSet::from_sqlite(&db_tx)?; |
| 34 | + db_tx.commit()?; |
| 35 | + Ok(changeset) |
| 36 | +} |
| 37 | + |
| 38 | +fn keychain_txout_changeset_persist( |
| 39 | + db: &mut rusqlite::Connection, |
| 40 | + changeset: &keychain_txout::ChangeSet, |
| 41 | +) -> rusqlite::Result<()> { |
| 42 | + let db_tx = db.transaction()?; |
| 43 | + changeset.persist_to_sqlite(&db_tx)?; |
| 44 | + db_tx.commit() |
| 45 | +} |
| 46 | + |
| 47 | +#[test] |
| 48 | +fn txgraph_is_persisted() { |
| 49 | + persist_txgraph_changeset::<rusqlite::Connection, _, _, _>( |
| 50 | + "wallet.sqlite", |
| 51 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 52 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 53 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +#[test] |
| 58 | +fn indexer_is_persisted() { |
| 59 | + persist_indexer_changeset::<rusqlite::Connection, _, _, _>( |
| 60 | + "wallet.sqlite", |
| 61 | + |path| Ok(rusqlite::Connection::open(path)?), |
| 62 | + |db| Ok(keychain_txout_changeset_init(db)?), |
| 63 | + |db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?), |
| 64 | + ); |
| 65 | +} |
| 66 | + |
| 67 | +#[test] |
| 68 | +fn local_chain_is_persisted() { |
| 69 | + persist_local_chain_changeset::<rusqlite::Connection, _, _, _>( |
| 70 | + "wallet.sqlite", |
| 71 | + |path| Ok(rusqlite::Connection::open(path)?), |
| 72 | + |db| { |
| 73 | + let db_tx = db.transaction()?; |
| 74 | + local_chain::ChangeSet::init_sqlite_tables(&db_tx)?; |
| 75 | + let changeset = local_chain::ChangeSet::from_sqlite(&db_tx)?; |
| 76 | + db_tx.commit()?; |
| 77 | + Ok(changeset) |
| 78 | + }, |
| 79 | + |db, changeset| { |
| 80 | + let db_tx = db.transaction()?; |
| 81 | + changeset.persist_to_sqlite(&db_tx)?; |
| 82 | + Ok(db_tx.commit()?) |
| 83 | + }, |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +#[test] |
| 88 | +fn txouts_are_persisted() { |
| 89 | + persist_txouts::<rusqlite::Connection, _, _, _>( |
| 90 | + "wallet.sqlite", |
| 91 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 92 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 93 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn txs_are_persisted() { |
| 99 | + persist_txs::<rusqlite::Connection, _, _, _>( |
| 100 | + "wallet.sqlite", |
| 101 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 102 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 103 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +#[test] |
| 108 | +fn anchors_are_persisted() { |
| 109 | + persist_anchors::<rusqlite::Connection, _, _, _>( |
| 110 | + "wallet.sqlite", |
| 111 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 112 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 113 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +#[test] |
| 118 | +fn last_seen_is_persisted() { |
| 119 | + persist_last_seen::<rusqlite::Connection, _, _, _>( |
| 120 | + "wallet.sqlite", |
| 121 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 122 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 123 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 124 | + ); |
| 125 | +} |
| 126 | + |
| 127 | +#[test] |
| 128 | +fn last_evicted_is_persisted() { |
| 129 | + persist_last_evicted::<rusqlite::Connection, _, _, _>( |
| 130 | + "wallet.sqlite", |
| 131 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 132 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 133 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 134 | + ); |
| 135 | +} |
| 136 | + |
| 137 | +#[test] |
| 138 | +fn first_seen_is_persisted() { |
| 139 | + persist_first_seen::<rusqlite::Connection, _, _, _>( |
| 140 | + "wallet.sqlite", |
| 141 | + |path| Ok(bdk_chain::rusqlite::Connection::open(path)?), |
| 142 | + |db| Ok(tx_graph_changeset_init(db)?), |
| 143 | + |db, changeset| Ok(tx_graph_changeset_persist(db, changeset)?), |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +#[test] |
| 148 | +fn last_revealed_is_persisted() { |
| 149 | + persist_last_revealed::<rusqlite::Connection, _, _, _>( |
| 150 | + "wallet.sqlite", |
| 151 | + |path| Ok(rusqlite::Connection::open(path)?), |
| 152 | + |db| Ok(keychain_txout_changeset_init(db)?), |
| 153 | + |db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?), |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +#[test] |
| 158 | +fn spk_cache_is_persisted() { |
| 159 | + persist_spk_cache::<rusqlite::Connection, _, _, _>( |
| 160 | + "wallet.sqlite", |
| 161 | + |path| Ok(rusqlite::Connection::open(path)?), |
| 162 | + |db| Ok(keychain_txout_changeset_init(db)?), |
| 163 | + |db, changeset| Ok(keychain_txout_changeset_persist(db, changeset)?), |
| 164 | + ); |
| 165 | +} |
0 commit comments