Skip to content

Commit

Permalink
test(sqlite): test persisting anchors and txs independently
Browse files Browse the repository at this point in the history
  • Loading branch information
ValuedMammal authored and evanlinjin committed Nov 25, 2024
1 parent 297ff34 commit 18f5f3f
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions crates/chain/src/rusqlite_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,3 +536,70 @@ impl keychain_txout::ChangeSet {
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;

use bdk_testenv::{anyhow, hash};
use bitcoin::{absolute, transaction, TxIn, TxOut};

#[test]
fn can_persist_anchors_and_txs_independently() -> anyhow::Result<()> {
type ChangeSet = tx_graph::ChangeSet<BlockId>;
let mut conn = rusqlite::Connection::open_in_memory()?;

// init tables
{
let db_tx = conn.transaction()?;
ChangeSet::init_sqlite_tables(&db_tx)?;
db_tx.commit()?;
}

let tx = bitcoin::Transaction {
version: transaction::Version::TWO,
lock_time: absolute::LockTime::ZERO,
input: vec![TxIn::default()],
output: vec![TxOut::NULL],
};
let tx = Arc::new(tx);
let txid = tx.compute_txid();
let anchor = BlockId {
height: 21,
hash: hash!("anchor"),
};

// First persist the anchor
{
let changeset = ChangeSet {
anchors: [(anchor, txid)].into(),
..Default::default()
};
let db_tx = conn.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
db_tx.commit()?;
}

// Now persist the tx
{
let changeset = ChangeSet {
txs: [tx.clone()].into(),
..Default::default()
};
let db_tx = conn.transaction()?;
changeset.persist_to_sqlite(&db_tx)?;
db_tx.commit()?;
}

// Loading changeset from sqlite should succeed
{
let db_tx = conn.transaction()?;
let changeset = ChangeSet::from_sqlite(&db_tx)?;
db_tx.commit()?;
assert!(changeset.txs.contains(&tx));
assert!(changeset.anchors.contains(&(anchor, txid)));
}

Ok(())
}
}

0 comments on commit 18f5f3f

Please sign in to comment.