Skip to content

Commit 06a9d6c

Browse files
committed
feat(chain,wallet)!: publicize .init_sqlite_tables changeset methods
Changeset methods `.persist_to_sqlite` and `from_sqlite` no longer internally call `.init_sqlite_tables`. Instead, it is up to the caller to call `.init_sqlite_tables` beforehand. This allows us to utilize `WalletPersister::initialize`, instead of calling `.init_sqlite_tables` every time we persist/load.
1 parent 039622f commit 06a9d6c

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

crates/chain/src/rusqlite_impl.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ where
225225
pub const ANCHORS_TABLE_NAME: &'static str = "bdk_anchors";
226226

227227
/// Initialize sqlite tables.
228-
fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
228+
pub fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
229229
let schema_v0: &[&str] = &[
230230
// full transactions
231231
&format!(
@@ -264,9 +264,9 @@ where
264264
}
265265

266266
/// Construct a [`TxGraph`] from an sqlite database.
267+
///
268+
/// Remember to call [`Self::init_sqlite_tables`] beforehand.
267269
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
268-
Self::init_sqlite_tables(db_tx)?;
269-
270270
let mut changeset = Self::default();
271271

272272
let mut statement = db_tx.prepare(&format!(
@@ -332,9 +332,9 @@ where
332332
}
333333

334334
/// Persist `changeset` to the sqlite database.
335+
///
336+
/// Remember to call [`Self::init_sqlite_tables`] beforehand.
335337
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
336-
Self::init_sqlite_tables(db_tx)?;
337-
338338
let mut statement = db_tx.prepare_cached(&format!(
339339
"INSERT INTO {}(txid, raw_tx) VALUES(:txid, :raw_tx) ON CONFLICT(txid) DO UPDATE SET raw_tx=:raw_tx",
340340
Self::TXS_TABLE_NAME,
@@ -396,7 +396,7 @@ impl local_chain::ChangeSet {
396396
pub const BLOCKS_TABLE_NAME: &'static str = "bdk_blocks";
397397

398398
/// Initialize sqlite tables for persisting [`local_chain::LocalChain`].
399-
fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
399+
pub fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
400400
let schema_v0: &[&str] = &[
401401
// blocks
402402
&format!(
@@ -411,9 +411,9 @@ impl local_chain::ChangeSet {
411411
}
412412

413413
/// Construct a [`LocalChain`](local_chain::LocalChain) from sqlite database.
414+
///
415+
/// Remember to call [`Self::init_sqlite_tables`] beforehand.
414416
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
415-
Self::init_sqlite_tables(db_tx)?;
416-
417417
let mut changeset = Self::default();
418418

419419
let mut statement = db_tx.prepare(&format!(
@@ -435,9 +435,9 @@ impl local_chain::ChangeSet {
435435
}
436436

437437
/// Persist `changeset` to the sqlite database.
438+
///
439+
/// Remember to call [`Self::init_sqlite_tables`] beforehand.
438440
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
439-
Self::init_sqlite_tables(db_tx)?;
440-
441441
let mut replace_statement = db_tx.prepare_cached(&format!(
442442
"REPLACE INTO {}(block_height, block_hash) VALUES(:block_height, :block_hash)",
443443
Self::BLOCKS_TABLE_NAME,
@@ -471,7 +471,7 @@ impl keychain_txout::ChangeSet {
471471

472472
/// Initialize sqlite tables for persisting
473473
/// [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex).
474-
fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
474+
pub fn init_sqlite_tables(db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
475475
let schema_v0: &[&str] = &[
476476
// last revealed
477477
&format!(
@@ -487,9 +487,9 @@ impl keychain_txout::ChangeSet {
487487

488488
/// Construct [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex) from sqlite database
489489
/// and given parameters.
490+
///
491+
/// Remember to call [`Self::init_sqlite_tables`] beforehand.
490492
pub fn from_sqlite(db_tx: &rusqlite::Transaction) -> rusqlite::Result<Self> {
491-
Self::init_sqlite_tables(db_tx)?;
492-
493493
let mut changeset = Self::default();
494494

495495
let mut statement = db_tx.prepare(&format!(
@@ -511,9 +511,9 @@ impl keychain_txout::ChangeSet {
511511
}
512512

513513
/// Persist `changeset` to the sqlite database.
514+
///
515+
/// Remember to call [`Self::init_sqlite_tables`] beforehand.
514516
pub fn persist_to_sqlite(&self, db_tx: &rusqlite::Transaction) -> rusqlite::Result<()> {
515-
Self::init_sqlite_tables(db_tx)?;
516-
517517
let mut statement = db_tx.prepare_cached(&format!(
518518
"REPLACE INTO {}(descriptor_id, last_revealed) VALUES(:descriptor_id, :last_revealed)",
519519
Self::LAST_REVEALED_TABLE_NAME,

crates/wallet/src/wallet/changeset.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ impl ChangeSet {
7272
/// Name of table to store wallet descriptors and network.
7373
pub const WALLET_TABLE_NAME: &'static str = "bdk_wallet";
7474

75-
/// Initialize sqlite tables for wallet schema & table.
76-
fn init_wallet_sqlite_tables(
77-
db_tx: &chain::rusqlite::Transaction,
78-
) -> chain::rusqlite::Result<()> {
75+
/// Initialize sqlite tables for wallet tables.
76+
pub fn init_sqlite_tables(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<()> {
7977
let schema_v0: &[&str] = &[&format!(
8078
"CREATE TABLE {} ( \
8179
id INTEGER PRIMARY KEY NOT NULL CHECK (id = 0), \
@@ -85,12 +83,17 @@ impl ChangeSet {
8583
) STRICT;",
8684
Self::WALLET_TABLE_NAME,
8785
)];
88-
crate::rusqlite_impl::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])
86+
crate::rusqlite_impl::migrate_schema(db_tx, Self::WALLET_SCHEMA_NAME, &[schema_v0])?;
87+
88+
bdk_chain::local_chain::ChangeSet::init_sqlite_tables(db_tx)?;
89+
bdk_chain::tx_graph::ChangeSet::<ConfirmationBlockTime>::init_sqlite_tables(db_tx)?;
90+
bdk_chain::keychain_txout::ChangeSet::init_sqlite_tables(db_tx)?;
91+
92+
Ok(())
8993
}
9094

9195
/// Recover a [`ChangeSet`] from sqlite database.
9296
pub fn from_sqlite(db_tx: &chain::rusqlite::Transaction) -> chain::rusqlite::Result<Self> {
93-
Self::init_wallet_sqlite_tables(db_tx)?;
9497
use chain::rusqlite::OptionalExtension;
9598
use chain::Impl;
9699

@@ -129,7 +132,6 @@ impl ChangeSet {
129132
&self,
130133
db_tx: &chain::rusqlite::Transaction,
131134
) -> chain::rusqlite::Result<()> {
132-
Self::init_wallet_sqlite_tables(db_tx)?;
133135
use chain::rusqlite::named_params;
134136
use chain::Impl;
135137

crates/wallet/src/wallet/persisted.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ impl<'c> WalletPersister for bdk_chain::rusqlite::Transaction<'c> {
237237
type Error = bdk_chain::rusqlite::Error;
238238

239239
fn initialize(persister: &mut Self) -> Result<ChangeSet, Self::Error> {
240+
ChangeSet::init_sqlite_tables(&*persister)?;
240241
ChangeSet::from_sqlite(persister)
241242
}
242243

@@ -251,6 +252,7 @@ impl WalletPersister for bdk_chain::rusqlite::Connection {
251252

252253
fn initialize(persister: &mut Self) -> Result<ChangeSet, Self::Error> {
253254
let db_tx = persister.transaction()?;
255+
ChangeSet::init_sqlite_tables(&db_tx)?;
254256
let changeset = ChangeSet::from_sqlite(&db_tx)?;
255257
db_tx.commit()?;
256258
Ok(changeset)
@@ -299,7 +301,9 @@ impl WalletPersister for bdk_file_store::Store<ChangeSet> {
299301
}
300302

301303
fn persist(persister: &mut Self, changeset: &ChangeSet) -> Result<(), Self::Error> {
302-
persister.append_changeset(changeset).map_err(FileStoreError::Write)
304+
persister
305+
.append_changeset(changeset)
306+
.map_err(FileStoreError::Write)
303307
}
304308
}
305309

crates/wallet/tests/wallet.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,11 @@ fn wallet_load_checks() -> anyhow::Result<()> {
249249

250250
run(
251251
"store.db",
252-
|path| Ok(bdk_file_store::Store::<ChangeSet>::create_new(DB_MAGIC, path)?),
252+
|path| {
253+
Ok(bdk_file_store::Store::<ChangeSet>::create_new(
254+
DB_MAGIC, path,
255+
)?)
256+
},
253257
|path| Ok(bdk_file_store::Store::<ChangeSet>::open(DB_MAGIC, path)?),
254258
)?;
255259
run(

0 commit comments

Comments
 (0)