• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitcoindevkit / bdk / 9665693468

25 Jun 2024 03:53PM UTC coverage: 83.026% (+0.002%) from 83.024%
9665693468

Pull #1486

github

web-flow
Merge 2425c019e into 6dab68d35
Pull Request #1486: refactor(chain)!: use hash of spk at index 0 as keychain's unique id

42 of 45 new or added lines in 3 files covered. (93.33%)

184 existing lines in 4 files now uncovered.

11138 of 13415 relevant lines covered (83.03%)

16296.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.66
/crates/sqlite/src/store.rs
1
use bdk_chain::bitcoin::consensus::{deserialize, serialize};
2
use bdk_chain::bitcoin::hashes::Hash;
3
use bdk_chain::bitcoin::{Amount, Network, OutPoint, ScriptBuf, Transaction, TxOut};
4
use bdk_chain::bitcoin::{BlockHash, Txid};
5
use bdk_chain::miniscript::descriptor::{Descriptor, DescriptorPublicKey};
6
use rusqlite::{named_params, Connection};
7
use serde::{Deserialize, Serialize};
8
use std::collections::{BTreeMap, BTreeSet};
9
use std::fmt::Debug;
10
use std::marker::PhantomData;
11
use std::str::FromStr;
12
use std::sync::{Arc, Mutex};
13

14
use crate::Error;
15
use bdk_chain::CombinedChangeSet;
16
use bdk_chain::{
17
    indexed_tx_graph, keychain, local_chain, tx_graph, Anchor, Append, DescriptorExt, KeychainId,
18
};
19

20
/// Persists data in to a relational schema based [SQLite] database file.
21
///
22
/// The changesets loaded or stored represent changes to keychain and blockchain data.
23
///
24
/// [SQLite]: https://www.sqlite.org/index.html
25
pub struct Store<K, A> {
26
    // A rusqlite connection to the SQLite database. Uses a Mutex for thread safety.
27
    conn: Mutex<Connection>,
28
    keychain_marker: PhantomData<K>,
29
    anchor_marker: PhantomData<A>,
30
}
31

32
impl<K, A> Debug for Store<K, A> {
33
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
×
34
        Debug::fmt(&self.conn, f)
×
35
    }
×
36
}
37

38
impl<K, A> Store<K, A>
39
where
40
    K: Ord + for<'de> Deserialize<'de> + Serialize + Send,
41
    A: Anchor + for<'de> Deserialize<'de> + Serialize + Send,
42
{
43
    /// Creates a new store from a [`Connection`].
44
    pub fn new(mut conn: Connection) -> Result<Self, rusqlite::Error> {
11✔
45
        Self::migrate(&mut conn)?;
11✔
46

47
        Ok(Self {
11✔
48
            conn: Mutex::new(conn),
11✔
49
            keychain_marker: Default::default(),
11✔
50
            anchor_marker: Default::default(),
11✔
51
        })
11✔
52
    }
11✔
53

54
    pub(crate) fn db_transaction(&mut self) -> Result<rusqlite::Transaction, Error> {
20✔
55
        let connection = self.conn.get_mut().expect("unlocked connection mutex");
20✔
56
        connection.transaction().map_err(Error::Sqlite)
20✔
57
    }
20✔
58
}
59

60
/// Network table related functions.
61
impl<K, A> Store<K, A> {
62
    /// Insert [`Network`] for which all other tables data is valid.
63
    ///
64
    /// Error if trying to insert different network value.
65
    fn insert_network(
11✔
66
        current_network: &Option<Network>,
11✔
67
        db_transaction: &rusqlite::Transaction,
11✔
68
        network_changeset: &Option<Network>,
11✔
69
    ) -> Result<(), Error> {
11✔
70
        if let Some(network) = network_changeset {
11✔
71
            match current_network {
×
72
                // if no network change do nothing
×
73
                Some(current_network) if current_network == network => Ok(()),
×
74
                // if new network not the same as current, error
75
                Some(current_network) => Err(Error::Network {
×
76
                    expected: *current_network,
×
77
                    given: *network,
×
78
                }),
×
79
                // insert network if none exists
80
                None => {
81
                    let insert_network_stmt = &mut db_transaction
5✔
82
                        .prepare_cached("INSERT INTO network (name) VALUES (:name)")
5✔
83
                        .expect("insert network statement");
5✔
84
                    let name = network.to_string();
5✔
85
                    insert_network_stmt
5✔
86
                        .execute(named_params! {":name": name })
5✔
87
                        .map_err(Error::Sqlite)?;
5✔
88
                    Ok(())
5✔
89
                }
90
            }
91
        } else {
92
            Ok(())
6✔
93
        }
94
    }
11✔
95

96
    /// Select the valid [`Network`] for this database, or `None` if not set.
97
    fn select_network(db_transaction: &rusqlite::Transaction) -> Result<Option<Network>, Error> {
20✔
98
        let mut select_network_stmt = db_transaction
20✔
99
            .prepare_cached("SELECT name FROM network WHERE rowid = 1")
20✔
100
            .expect("select network statement");
20✔
101

20✔
102
        let network = select_network_stmt
20✔
103
            .query_row([], |row| {
20✔
104
                let network = row.get_unwrap::<usize, String>(0);
15✔
105
                let network = Network::from_str(network.as_str()).expect("valid network");
15✔
106
                Ok(network)
15✔
107
            })
20✔
108
            .map_err(Error::Sqlite);
20✔
109
        match network {
5✔
110
            Ok(network) => Ok(Some(network)),
15✔
111
            Err(Error::Sqlite(rusqlite::Error::QueryReturnedNoRows)) => Ok(None),
5✔
112
            Err(e) => Err(e),
×
113
        }
114
    }
20✔
115
}
116

117
/// Block table related functions.
118
impl<K, A> Store<K, A> {
119
    /// Insert or delete local chain blocks.
120
    ///
121
    /// Error if trying to insert existing block hash.
122
    fn insert_or_delete_blocks(
11✔
123
        db_transaction: &rusqlite::Transaction,
11✔
124
        chain_changeset: &local_chain::ChangeSet,
11✔
125
    ) -> Result<(), Error> {
11✔
126
        for (height, hash) in chain_changeset.iter() {
11✔
127
            match hash {
11✔
128
                // add new hash at height
129
                Some(hash) => {
11✔
130
                    let insert_block_stmt = &mut db_transaction
11✔
131
                        .prepare_cached("INSERT INTO block (hash, height) VALUES (:hash, :height)")
11✔
132
                        .expect("insert block statement");
11✔
133
                    let hash = hash.to_string();
11✔
134
                    insert_block_stmt
11✔
135
                        .execute(named_params! {":hash": hash, ":height": height })
11✔
136
                        .map_err(Error::Sqlite)?;
11✔
137
                }
138
                // delete block at height
139
                None => {
140
                    let delete_block_stmt = &mut db_transaction
×
141
                        .prepare_cached("DELETE FROM block WHERE height IS :height")
×
142
                        .expect("delete block statement");
×
143
                    delete_block_stmt
×
144
                        .execute(named_params! {":height": height })
×
145
                        .map_err(Error::Sqlite)?;
×
146
                }
147
            }
148
        }
149

150
        Ok(())
11✔
151
    }
11✔
152

153
    /// Select all blocks.
154
    fn select_blocks(
9✔
155
        db_transaction: &rusqlite::Transaction,
9✔
156
    ) -> Result<BTreeMap<u32, Option<BlockHash>>, Error> {
9✔
157
        let mut select_blocks_stmt = db_transaction
9✔
158
            .prepare_cached("SELECT height, hash FROM block")
9✔
159
            .expect("select blocks statement");
9✔
160

161
        let blocks = select_blocks_stmt
9✔
162
            .query_map([], |row| {
15✔
163
                let height = row.get_unwrap::<usize, u32>(0);
15✔
164
                let hash = row.get_unwrap::<usize, String>(1);
15✔
165
                let hash = Some(BlockHash::from_str(hash.as_str()).expect("block hash"));
15✔
166
                Ok((height, hash))
15✔
167
            })
15✔
168
            .map_err(Error::Sqlite)?;
9✔
169
        blocks
9✔
170
            .into_iter()
9✔
171
            .map(|row| row.map_err(Error::Sqlite))
15✔
172
            .collect()
9✔
173
    }
9✔
174
}
175

176
/// Keychain table related functions.
177
///
178
/// The keychain objects are stored as [`JSONB`] data.
179
/// [`JSONB`]: https://sqlite.org/json1.html#jsonb
180
impl<K, A> Store<K, A>
181
where
182
    K: Ord + for<'de> Deserialize<'de> + Serialize + Send,
183
    A: Anchor + Send,
184
{
185
    /// Insert keychain with descriptor and last active index.
186
    ///
187
    /// If keychain exists only update last active index.
188
    fn insert_keychains(
11✔
189
        db_transaction: &rusqlite::Transaction,
11✔
190
        tx_graph_changeset: &indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
11✔
191
    ) -> Result<(), Error> {
11✔
192
        let keychain_changeset = &tx_graph_changeset.indexer;
11✔
193
        for (keychain, descriptor) in keychain_changeset.keychains_added.iter() {
13✔
194
            let insert_keychain_stmt = &mut db_transaction
10✔
195
                .prepare_cached("INSERT INTO keychain (keychain, descriptor, keychain_id) VALUES (jsonb(:keychain), :descriptor, :keychain_id)")
10✔
196
                .expect("insert keychain statement");
10✔
197
            let keychain_json = serde_json::to_string(keychain).expect("keychain json");
10✔
198
            let keychain_id = descriptor.keychain_id().to_byte_array();
10✔
199
            let descriptor = descriptor.to_string();
10✔
200
            insert_keychain_stmt.execute(named_params! {":keychain": keychain_json, ":descriptor": descriptor, ":keychain_id": keychain_id })
10✔
201
                .map_err(Error::Sqlite)?;
10✔
202
        }
203
        Ok(())
11✔
204
    }
11✔
205

206
    /// Update descriptor last revealed index.
207
    fn update_last_revealed(
11✔
208
        db_transaction: &rusqlite::Transaction,
11✔
209
        tx_graph_changeset: &indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
11✔
210
    ) -> Result<(), Error> {
11✔
211
        let keychain_changeset = &tx_graph_changeset.indexer;
11✔
212
        for (keychain_id, last_revealed) in keychain_changeset.last_revealed.iter() {
11✔
213
            let update_last_revealed_stmt = &mut db_transaction
7✔
214
                .prepare_cached(
7✔
215
                    "UPDATE keychain SET last_revealed = :last_revealed
7✔
216
                              WHERE keychain_id = :keychain_id",
7✔
217
                )
7✔
218
                .expect("update last revealed statement");
7✔
219
            let keychain_id = keychain_id.to_byte_array();
7✔
220
            update_last_revealed_stmt
7✔
221
                .execute(
7✔
222
                    named_params! {":keychain_id": keychain_id, ":last_revealed": * last_revealed },
7✔
223
                )
7✔
224
                .map_err(Error::Sqlite)?;
7✔
225
        }
226
        Ok(())
11✔
227
    }
11✔
228

229
    /// Select keychains added.
230
    fn select_keychains(
9✔
231
        db_transaction: &rusqlite::Transaction,
9✔
232
    ) -> Result<BTreeMap<K, Descriptor<DescriptorPublicKey>>, Error> {
9✔
233
        let mut select_keychains_added_stmt = db_transaction
9✔
234
            .prepare_cached("SELECT json(keychain), descriptor FROM keychain")
9✔
235
            .expect("select keychains statement");
9✔
236

237
        let keychains = select_keychains_added_stmt
9✔
238
            .query_map([], |row| {
18✔
239
                let keychain = row.get_unwrap::<usize, String>(0);
18✔
240
                let keychain = serde_json::from_str::<K>(keychain.as_str()).expect("keychain");
18✔
241
                let descriptor = row.get_unwrap::<usize, String>(1);
18✔
242
                let descriptor = Descriptor::from_str(descriptor.as_str()).expect("descriptor");
18✔
243
                Ok((keychain, descriptor))
18✔
244
            })
18✔
245
            .map_err(Error::Sqlite)?;
9✔
246
        keychains
9✔
247
            .into_iter()
9✔
248
            .map(|row| row.map_err(Error::Sqlite))
18✔
249
            .collect()
9✔
250
    }
9✔
251

252
    /// Select descriptor last revealed indexes.
253
    fn select_last_revealed(
9✔
254
        db_transaction: &rusqlite::Transaction,
9✔
255
    ) -> Result<BTreeMap<KeychainId, u32>, Error> {
9✔
256
        let mut select_last_revealed_stmt = db_transaction
9✔
257
            .prepare_cached(
9✔
258
                "SELECT descriptor, last_revealed FROM keychain WHERE last_revealed IS NOT NULL",
9✔
259
            )
9✔
260
            .expect("select last revealed statement");
9✔
261

262
        let last_revealed = select_last_revealed_stmt
9✔
263
            .query_map([], |row| {
12✔
264
                let descriptor = row.get_unwrap::<usize, String>(0);
7✔
265
                let descriptor = Descriptor::from_str(descriptor.as_str()).expect("descriptor");
7✔
266
                let keychain_id = descriptor.keychain_id();
7✔
267
                let last_revealed = row.get_unwrap::<usize, u32>(1);
7✔
268
                Ok((keychain_id, last_revealed))
7✔
269
            })
12✔
270
            .map_err(Error::Sqlite)?;
9✔
271
        last_revealed
9✔
272
            .into_iter()
9✔
273
            .map(|row| row.map_err(Error::Sqlite))
12✔
274
            .collect()
9✔
275
    }
9✔
276
}
277

278
/// Tx (transaction) and txout (transaction output) table related functions.
279
impl<K, A> Store<K, A> {
280
    /// Insert transactions.
281
    ///
282
    /// Error if trying to insert existing txid.
283
    fn insert_txs(
11✔
284
        db_transaction: &rusqlite::Transaction,
11✔
285
        tx_graph_changeset: &indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
11✔
286
    ) -> Result<(), Error> {
11✔
287
        for tx in tx_graph_changeset.graph.txs.iter() {
11✔
288
            let insert_tx_stmt = &mut db_transaction
9✔
289
                .prepare_cached("INSERT INTO tx (txid, whole_tx) VALUES (:txid, :whole_tx) ON CONFLICT (txid) DO UPDATE SET whole_tx = :whole_tx WHERE txid = :txid")
9✔
290
                .expect("insert or update tx whole_tx statement");
9✔
291
            let txid = tx.compute_txid().to_string();
9✔
292
            let whole_tx = serialize(&tx);
9✔
293
            insert_tx_stmt
9✔
294
                .execute(named_params! {":txid": txid, ":whole_tx": whole_tx })
9✔
295
                .map_err(Error::Sqlite)?;
9✔
296
        }
297
        Ok(())
11✔
298
    }
11✔
299

300
    /// Select all transactions.
301
    fn select_txs(
9✔
302
        db_transaction: &rusqlite::Transaction,
9✔
303
    ) -> Result<BTreeSet<Arc<Transaction>>, Error> {
9✔
304
        let mut select_tx_stmt = db_transaction
9✔
305
            .prepare_cached("SELECT whole_tx FROM tx WHERE whole_tx IS NOT NULL")
9✔
306
            .expect("select tx statement");
9✔
307

308
        let txs = select_tx_stmt
9✔
309
            .query_map([], |row| {
15✔
310
                let whole_tx = row.get_unwrap::<usize, Vec<u8>>(0);
9✔
311
                let whole_tx: Transaction = deserialize(&whole_tx).expect("transaction");
9✔
312
                Ok(Arc::new(whole_tx))
9✔
313
            })
15✔
314
            .map_err(Error::Sqlite)?;
9✔
315

316
        txs.into_iter()
9✔
317
            .map(|row| row.map_err(Error::Sqlite))
15✔
318
            .collect()
9✔
319
    }
9✔
320

321
    /// Select all transactions with last_seen values.
322
    fn select_last_seen(
9✔
323
        db_transaction: &rusqlite::Transaction,
9✔
324
    ) -> Result<BTreeMap<Txid, u64>, Error> {
9✔
325
        // load tx last_seen
9✔
326
        let mut select_last_seen_stmt = db_transaction
9✔
327
            .prepare_cached("SELECT txid, last_seen FROM tx WHERE last_seen IS NOT NULL")
9✔
328
            .expect("select tx last seen statement");
9✔
329

330
        let last_seen = select_last_seen_stmt
9✔
331
            .query_map([], |row| {
15✔
332
                let txid = row.get_unwrap::<usize, String>(0);
9✔
333
                let txid = Txid::from_str(&txid).expect("txid");
9✔
334
                let last_seen = row.get_unwrap::<usize, u64>(1);
9✔
335
                Ok((txid, last_seen))
9✔
336
            })
15✔
337
            .map_err(Error::Sqlite)?;
9✔
338
        last_seen
9✔
339
            .into_iter()
9✔
340
            .map(|row| row.map_err(Error::Sqlite))
15✔
341
            .collect()
9✔
342
    }
9✔
343

344
    /// Insert txouts.
345
    ///
346
    /// Error if trying to insert existing outpoint.
347
    fn insert_txouts(
11✔
348
        db_transaction: &rusqlite::Transaction,
11✔
349
        tx_graph_changeset: &indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
11✔
350
    ) -> Result<(), Error> {
11✔
351
        for txout in tx_graph_changeset.graph.txouts.iter() {
11✔
352
            let insert_txout_stmt = &mut db_transaction
6✔
353
                .prepare_cached("INSERT INTO txout (txid, vout, value, script) VALUES (:txid, :vout, :value, :script)")
6✔
354
                .expect("insert txout statement");
6✔
355
            let txid = txout.0.txid.to_string();
6✔
356
            let vout = txout.0.vout;
6✔
357
            let value = txout.1.value.to_sat();
6✔
358
            let script = txout.1.script_pubkey.as_bytes();
6✔
359
            insert_txout_stmt.execute(named_params! {":txid": txid, ":vout": vout, ":value": value, ":script": script })
6✔
360
                .map_err(Error::Sqlite)?;
6✔
361
        }
362
        Ok(())
11✔
363
    }
11✔
364

365
    /// Select all transaction outputs.
366
    fn select_txouts(
9✔
367
        db_transaction: &rusqlite::Transaction,
9✔
368
    ) -> Result<BTreeMap<OutPoint, TxOut>, Error> {
9✔
369
        // load tx outs
9✔
370
        let mut select_txout_stmt = db_transaction
9✔
371
            .prepare_cached("SELECT txid, vout, value, script FROM txout")
9✔
372
            .expect("select txout statement");
9✔
373

374
        let txouts = select_txout_stmt
9✔
375
            .query_map([], |row| {
12✔
376
                let txid = row.get_unwrap::<usize, String>(0);
6✔
377
                let txid = Txid::from_str(&txid).expect("txid");
6✔
378
                let vout = row.get_unwrap::<usize, u32>(1);
6✔
379
                let outpoint = OutPoint::new(txid, vout);
6✔
380
                let value = row.get_unwrap::<usize, u64>(2);
6✔
381
                let script_pubkey = row.get_unwrap::<usize, Vec<u8>>(3);
6✔
382
                let script_pubkey = ScriptBuf::from_bytes(script_pubkey);
6✔
383
                let txout = TxOut {
6✔
384
                    value: Amount::from_sat(value),
6✔
385
                    script_pubkey,
6✔
386
                };
6✔
387
                Ok((outpoint, txout))
6✔
388
            })
12✔
389
            .map_err(Error::Sqlite)?;
9✔
390
        txouts
9✔
391
            .into_iter()
9✔
392
            .map(|row| row.map_err(Error::Sqlite))
12✔
393
            .collect()
9✔
394
    }
9✔
395

396
    /// Update transaction last seen times.
397
    fn update_last_seen(
11✔
398
        db_transaction: &rusqlite::Transaction,
11✔
399
        tx_graph_changeset: &indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
11✔
400
    ) -> Result<(), Error> {
11✔
401
        for tx_last_seen in tx_graph_changeset.graph.last_seen.iter() {
14✔
402
            let insert_or_update_tx_stmt = &mut db_transaction
12✔
403
                .prepare_cached("INSERT INTO tx (txid, last_seen) VALUES (:txid, :last_seen) ON CONFLICT (txid) DO UPDATE SET last_seen = :last_seen WHERE txid = :txid")
12✔
404
                .expect("insert or update tx last_seen statement");
12✔
405
            let txid = tx_last_seen.0.to_string();
12✔
406
            let last_seen = *tx_last_seen.1;
12✔
407
            insert_or_update_tx_stmt
12✔
408
                .execute(named_params! {":txid": txid, ":last_seen": last_seen })
12✔
409
                .map_err(Error::Sqlite)?;
12✔
410
        }
411
        Ok(())
11✔
412
    }
11✔
413
}
414

415
/// Anchor table related functions.
416
impl<K, A> Store<K, A>
417
where
418
    K: Ord + for<'de> Deserialize<'de> + Serialize + Send,
419
    A: Anchor + for<'de> Deserialize<'de> + Serialize + Send,
420
{
421
    /// Insert anchors.
422
    fn insert_anchors(
11✔
423
        db_transaction: &rusqlite::Transaction,
11✔
424
        tx_graph_changeset: &indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>>,
11✔
425
    ) -> Result<(), Error> {
11✔
426
        // serde_json::to_string
427
        for anchor in tx_graph_changeset.graph.anchors.iter() {
14✔
428
            let insert_anchor_stmt = &mut db_transaction
12✔
429
                .prepare_cached("INSERT INTO anchor_tx (block_hash, anchor, txid) VALUES (:block_hash, jsonb(:anchor), :txid)")
12✔
430
                .expect("insert anchor statement");
12✔
431
            let block_hash = anchor.0.anchor_block().hash.to_string();
12✔
432
            let anchor_json = serde_json::to_string(&anchor.0).expect("anchor json");
12✔
433
            let txid = anchor.1.to_string();
12✔
434
            insert_anchor_stmt.execute(named_params! {":block_hash": block_hash, ":anchor": anchor_json, ":txid": txid })
12✔
435
                .map_err(Error::Sqlite)?;
12✔
436
        }
437
        Ok(())
11✔
438
    }
11✔
439

440
    /// Select all anchors.
441
    fn select_anchors(
9✔
442
        db_transaction: &rusqlite::Transaction,
9✔
443
    ) -> Result<BTreeSet<(A, Txid)>, Error> {
9✔
444
        // serde_json::from_str
9✔
445
        let mut select_anchor_stmt = db_transaction
9✔
446
            .prepare_cached("SELECT block_hash, json(anchor), txid FROM anchor_tx")
9✔
447
            .expect("select anchor statement");
9✔
448
        let anchors = select_anchor_stmt
9✔
449
            .query_map([], |row| {
18✔
450
                let hash = row.get_unwrap::<usize, String>(0);
12✔
451
                let hash = BlockHash::from_str(hash.as_str()).expect("block hash");
12✔
452
                let anchor = row.get_unwrap::<usize, String>(1);
12✔
453
                let anchor: A = serde_json::from_str(anchor.as_str()).expect("anchor");
12✔
454
                // double check anchor blob block hash matches
12✔
455
                assert_eq!(hash, anchor.anchor_block().hash);
12✔
456
                let txid = row.get_unwrap::<usize, String>(2);
12✔
457
                let txid = Txid::from_str(&txid).expect("txid");
12✔
458
                Ok((anchor, txid))
12✔
459
            })
18✔
460
            .map_err(Error::Sqlite)?;
9✔
461
        anchors
9✔
462
            .into_iter()
9✔
463
            .map(|row| row.map_err(Error::Sqlite))
18✔
464
            .collect()
9✔
465
    }
9✔
466
}
467

468
/// Functions to read and write all [`CombinedChangeSet`] data.
469
impl<K, A> Store<K, A>
470
where
471
    K: Ord + for<'de> Deserialize<'de> + Serialize + Send,
472
    A: Anchor + for<'de> Deserialize<'de> + Serialize + Send,
473
{
474
    /// Write the given `changeset` atomically.
475
    pub fn write(&mut self, changeset: &CombinedChangeSet<K, A>) -> Result<(), Error> {
11✔
476
        // no need to write anything if changeset is empty
11✔
477
        if changeset.is_empty() {
11✔
UNCOV
478
            return Ok(());
×
479
        }
11✔
480

481
        let db_transaction = self.db_transaction()?;
11✔
482

483
        let network_changeset = &changeset.network;
11✔
484
        let current_network = Self::select_network(&db_transaction)?;
11✔
485
        Self::insert_network(&current_network, &db_transaction, network_changeset)?;
11✔
486

487
        let chain_changeset = &changeset.chain;
11✔
488
        Self::insert_or_delete_blocks(&db_transaction, chain_changeset)?;
11✔
489

490
        let tx_graph_changeset = &changeset.indexed_tx_graph;
11✔
491
        Self::insert_keychains(&db_transaction, tx_graph_changeset)?;
11✔
492
        Self::update_last_revealed(&db_transaction, tx_graph_changeset)?;
11✔
493
        Self::insert_txs(&db_transaction, tx_graph_changeset)?;
11✔
494
        Self::insert_txouts(&db_transaction, tx_graph_changeset)?;
11✔
495
        Self::insert_anchors(&db_transaction, tx_graph_changeset)?;
11✔
496
        Self::update_last_seen(&db_transaction, tx_graph_changeset)?;
11✔
497
        db_transaction.commit().map_err(Error::Sqlite)
11✔
498
    }
11✔
499

500
    /// Read the entire database and return the aggregate [`CombinedChangeSet`].
501
    pub fn read(&mut self) -> Result<Option<CombinedChangeSet<K, A>>, Error> {
9✔
502
        let db_transaction = self.db_transaction()?;
9✔
503

504
        let network = Self::select_network(&db_transaction)?;
9✔
505
        let chain = Self::select_blocks(&db_transaction)?;
9✔
506
        let keychains_added = Self::select_keychains(&db_transaction)?;
9✔
507
        let last_revealed = Self::select_last_revealed(&db_transaction)?;
9✔
508
        let txs = Self::select_txs(&db_transaction)?;
9✔
509
        let last_seen = Self::select_last_seen(&db_transaction)?;
9✔
510
        let txouts = Self::select_txouts(&db_transaction)?;
9✔
511
        let anchors = Self::select_anchors(&db_transaction)?;
9✔
512

513
        let graph: tx_graph::ChangeSet<A> = tx_graph::ChangeSet {
9✔
514
            txs,
9✔
515
            txouts,
9✔
516
            anchors,
9✔
517
            last_seen,
9✔
518
        };
9✔
519

9✔
520
        let indexer: keychain::ChangeSet<K> = keychain::ChangeSet {
9✔
521
            keychains_added,
9✔
522
            last_revealed,
9✔
523
        };
9✔
524

9✔
525
        let indexed_tx_graph: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<K>> =
9✔
526
            indexed_tx_graph::ChangeSet { graph, indexer };
9✔
527

9✔
528
        if network.is_none() && chain.is_empty() && indexed_tx_graph.is_empty() {
9✔
UNCOV
529
            Ok(None)
×
530
        } else {
531
            Ok(Some(CombinedChangeSet {
9✔
532
                chain,
9✔
533
                indexed_tx_graph,
9✔
534
                network,
9✔
535
            }))
9✔
536
        }
537
    }
9✔
538
}
539

540
#[cfg(test)]
541
mod test {
542
    use super::*;
543
    use crate::store::Append;
544
    use bdk_chain::bitcoin::consensus::encode::deserialize;
545
    use bdk_chain::bitcoin::constants::genesis_block;
546
    use bdk_chain::bitcoin::hashes::hex::FromHex;
547
    use bdk_chain::bitcoin::transaction::Transaction;
548
    use bdk_chain::bitcoin::Network::Testnet;
549
    use bdk_chain::bitcoin::{secp256k1, BlockHash, OutPoint};
550
    use bdk_chain::miniscript::Descriptor;
551
    use bdk_chain::CombinedChangeSet;
552
    use bdk_chain::{
553
        indexed_tx_graph, keychain, tx_graph, BlockId, ConfirmationHeightAnchor,
554
        ConfirmationTimeHeightAnchor, DescriptorExt,
555
    };
556
    use std::str::FromStr;
557
    use std::sync::Arc;
558

559
    #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug, Serialize, Deserialize)]
30✔
560
    enum Keychain {
561
        External { account: u32, name: String },
562
        Internal { account: u32, name: String },
563
    }
564

565
    #[test]
566
    fn insert_and_load_aggregate_changesets_with_confirmation_time_height_anchor() {
1✔
567
        let (test_changesets, agg_test_changesets) =
1✔
568
            create_test_changesets(&|height, time, hash| ConfirmationTimeHeightAnchor {
2✔
569
                confirmation_height: height,
2✔
570
                confirmation_time: time,
2✔
571
                anchor_block: (height, hash).into(),
2✔
572
            });
2✔
573

1✔
574
        let conn = Connection::open_in_memory().expect("in memory connection");
1✔
575
        let mut store = Store::<Keychain, ConfirmationTimeHeightAnchor>::new(conn)
1✔
576
            .expect("create new memory db store");
1✔
577

1✔
578
        test_changesets.iter().for_each(|changeset| {
3✔
579
            store.write(changeset).expect("write changeset");
3✔
580
        });
3✔
581

1✔
582
        let agg_changeset = store.read().expect("aggregated changeset");
1✔
583

1✔
584
        assert_eq!(agg_changeset, Some(agg_test_changesets));
1✔
585
    }
1✔
586

587
    #[test]
588
    fn insert_and_load_aggregate_changesets_with_confirmation_height_anchor() {
1✔
589
        let (test_changesets, agg_test_changesets) =
1✔
590
            create_test_changesets(&|height, _time, hash| ConfirmationHeightAnchor {
2✔
591
                confirmation_height: height,
2✔
592
                anchor_block: (height, hash).into(),
2✔
593
            });
2✔
594

1✔
595
        let conn = Connection::open_in_memory().expect("in memory connection");
1✔
596
        let mut store = Store::<Keychain, ConfirmationHeightAnchor>::new(conn)
1✔
597
            .expect("create new memory db store");
1✔
598

1✔
599
        test_changesets.iter().for_each(|changeset| {
3✔
600
            store.write(changeset).expect("write changeset");
3✔
601
        });
3✔
602

1✔
603
        let agg_changeset = store.read().expect("aggregated changeset");
1✔
604

1✔
605
        assert_eq!(agg_changeset, Some(agg_test_changesets));
1✔
606
    }
1✔
607

608
    #[test]
609
    fn insert_and_load_aggregate_changesets_with_blockid_anchor() {
1✔
610
        let (test_changesets, agg_test_changesets) =
1✔
611
            create_test_changesets(&|height, _time, hash| BlockId { height, hash });
2✔
612

1✔
613
        let conn = Connection::open_in_memory().expect("in memory connection");
1✔
614
        let mut store = Store::<Keychain, BlockId>::new(conn).expect("create new memory db store");
1✔
615

1✔
616
        test_changesets.iter().for_each(|changeset| {
3✔
617
            store.write(changeset).expect("write changeset");
3✔
618
        });
3✔
619

1✔
620
        let agg_changeset = store.read().expect("aggregated changeset");
1✔
621

1✔
622
        assert_eq!(agg_changeset, Some(agg_test_changesets));
1✔
623
    }
1✔
624

625
    fn create_test_changesets<A: Anchor + Copy>(
3✔
626
        anchor_fn: &dyn Fn(u32, u64, BlockHash) -> A,
3✔
627
    ) -> (
3✔
628
        Vec<CombinedChangeSet<Keychain, A>>,
3✔
629
        CombinedChangeSet<Keychain, A>,
3✔
630
    ) {
3✔
631
        let secp = &secp256k1::Secp256k1::signing_only();
3✔
632

3✔
633
        let network_changeset = Some(Testnet);
3✔
634

3✔
635
        let block_hash_0: BlockHash = genesis_block(Testnet).block_hash();
3✔
636
        let block_hash_1 =
3✔
637
            BlockHash::from_str("00000000b873e79784647a6c82962c70d228557d24a747ea4d1b8bbe878e1206")
3✔
638
                .unwrap();
3✔
639
        let block_hash_2 =
3✔
640
            BlockHash::from_str("000000006c02c8ea6e4ff69651f7fcde348fb9d557a06e6957b65552002a7820")
3✔
641
                .unwrap();
3✔
642

3✔
643
        let block_changeset = [
3✔
644
            (0, Some(block_hash_0)),
3✔
645
            (1, Some(block_hash_1)),
3✔
646
            (2, Some(block_hash_2)),
3✔
647
        ]
3✔
648
        .into();
3✔
649

3✔
650
        let ext_keychain = Keychain::External {
3✔
651
            account: 0,
3✔
652
            name: "ext test".to_string(),
3✔
653
        };
3✔
654
        let (ext_desc, _ext_keymap) = Descriptor::parse_descriptor(secp, "wpkh(tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy/0/*)").unwrap();
3✔
655
        let ext_desc_id = ext_desc.keychain_id();
3✔
656
        let int_keychain = Keychain::Internal {
3✔
657
            account: 0,
3✔
658
            name: "int test".to_string(),
3✔
659
        };
3✔
660
        let (int_desc, _int_keymap) = Descriptor::parse_descriptor(secp, "wpkh(tprv8ZgxMBicQKsPcx5nBGsR63Pe8KnRUqmbJNENAfGftF3yuXoMMoVJJcYeUw5eVkm9WBPjWYt6HMWYJNesB5HaNVBaFc1M6dRjWSYnmewUMYy/1/*)").unwrap();
3✔
661
        let int_desc_id = int_desc.keychain_id();
3✔
662

3✔
663
        let tx0_hex = Vec::<u8>::from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000").unwrap();
3✔
664
        let tx0: Arc<Transaction> = Arc::new(deserialize(tx0_hex.as_slice()).unwrap());
3✔
665
        let tx1_hex = Vec::<u8>::from_hex("010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff025151feffffff0200f2052a010000001600149243f727dd5343293eb83174324019ec16c2630f0000000000000000776a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf94c4fecc7daa2490047304402205e423a8754336ca99dbe16509b877ef1bf98d008836c725005b3c787c41ebe46022047246e4467ad7cc7f1ad98662afcaf14c115e0095a227c7b05c5182591c23e7e01000120000000000000000000000000000000000000000000000000000000000000000000000000").unwrap();
3✔
666
        let tx1: Arc<Transaction> = Arc::new(deserialize(tx1_hex.as_slice()).unwrap());
3✔
667
        let tx2_hex = Vec::<u8>::from_hex("01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0e0432e7494d010e062f503253482fffffffff0100f2052a010000002321038a7f6ef1c8ca0c588aa53fa860128077c9e6c11e6830f4d7ee4e763a56b7718fac00000000").unwrap();
3✔
668
        let tx2: Arc<Transaction> = Arc::new(deserialize(tx2_hex.as_slice()).unwrap());
3✔
669

3✔
670
        let outpoint0_0 = OutPoint::new(tx0.compute_txid(), 0);
3✔
671
        let txout0_0 = tx0.output.first().unwrap().clone();
3✔
672
        let outpoint1_0 = OutPoint::new(tx1.compute_txid(), 0);
3✔
673
        let txout1_0 = tx1.output.first().unwrap().clone();
3✔
674

3✔
675
        let anchor1 = anchor_fn(1, 1296667328, block_hash_1);
3✔
676
        let anchor2 = anchor_fn(2, 1296688946, block_hash_2);
3✔
677

3✔
678
        let tx_graph_changeset = tx_graph::ChangeSet::<A> {
3✔
679
            txs: [tx0.clone(), tx1.clone()].into(),
3✔
680
            txouts: [(outpoint0_0, txout0_0), (outpoint1_0, txout1_0)].into(),
3✔
681
            anchors: [(anchor1, tx0.compute_txid()), (anchor1, tx1.compute_txid())].into(),
3✔
682
            last_seen: [
3✔
683
                (tx0.compute_txid(), 1598918400),
3✔
684
                (tx1.compute_txid(), 1598919121),
3✔
685
                (tx2.compute_txid(), 1608919121),
3✔
686
            ]
3✔
687
            .into(),
3✔
688
        };
3✔
689

3✔
690
        let keychain_changeset = keychain::ChangeSet {
3✔
691
            keychains_added: [(ext_keychain, ext_desc), (int_keychain, int_desc)].into(),
3✔
692
            last_revealed: [(ext_desc_id, 124), (int_desc_id, 421)].into(),
3✔
693
        };
3✔
694

3✔
695
        let graph_changeset: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<Keychain>> =
3✔
696
            indexed_tx_graph::ChangeSet {
3✔
697
                graph: tx_graph_changeset,
3✔
698
                indexer: keychain_changeset,
3✔
699
            };
3✔
700

3✔
701
        // test changesets to write to db
3✔
702
        let mut changesets = Vec::new();
3✔
703

3✔
704
        changesets.push(CombinedChangeSet {
3✔
705
            chain: block_changeset,
3✔
706
            indexed_tx_graph: graph_changeset,
3✔
707
            network: network_changeset,
3✔
708
        });
3✔
709

3✔
710
        // create changeset that sets the whole tx2 and updates it's lastseen where before there was only the txid and last_seen
3✔
711
        let tx_graph_changeset2 = tx_graph::ChangeSet::<A> {
3✔
712
            txs: [tx2.clone()].into(),
3✔
713
            txouts: BTreeMap::default(),
3✔
714
            anchors: BTreeSet::default(),
3✔
715
            last_seen: [(tx2.compute_txid(), 1708919121)].into(),
3✔
716
        };
3✔
717

3✔
718
        let graph_changeset2: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<Keychain>> =
3✔
719
            indexed_tx_graph::ChangeSet {
3✔
720
                graph: tx_graph_changeset2,
3✔
721
                indexer: keychain::ChangeSet::default(),
3✔
722
            };
3✔
723

3✔
724
        changesets.push(CombinedChangeSet {
3✔
725
            chain: local_chain::ChangeSet::default(),
3✔
726
            indexed_tx_graph: graph_changeset2,
3✔
727
            network: None,
3✔
728
        });
3✔
729

3✔
730
        // create changeset that adds a new anchor2 for tx0 and tx1
3✔
731
        let tx_graph_changeset3 = tx_graph::ChangeSet::<A> {
3✔
732
            txs: BTreeSet::default(),
3✔
733
            txouts: BTreeMap::default(),
3✔
734
            anchors: [(anchor2, tx0.compute_txid()), (anchor2, tx1.compute_txid())].into(),
3✔
735
            last_seen: BTreeMap::default(),
3✔
736
        };
3✔
737

3✔
738
        let graph_changeset3: indexed_tx_graph::ChangeSet<A, keychain::ChangeSet<Keychain>> =
3✔
739
            indexed_tx_graph::ChangeSet {
3✔
740
                graph: tx_graph_changeset3,
3✔
741
                indexer: keychain::ChangeSet::default(),
3✔
742
            };
3✔
743

3✔
744
        changesets.push(CombinedChangeSet {
3✔
745
            chain: local_chain::ChangeSet::default(),
3✔
746
            indexed_tx_graph: graph_changeset3,
3✔
747
            network: None,
3✔
748
        });
3✔
749

3✔
750
        // aggregated test changesets
3✔
751
        let agg_test_changesets =
3✔
752
            changesets
3✔
753
                .iter()
3✔
754
                .fold(CombinedChangeSet::<Keychain, A>::default(), |mut i, cs| {
9✔
755
                    i.append(cs.clone());
9✔
756
                    i
9✔
757
                });
9✔
758

3✔
759
        (changesets, agg_test_changesets)
3✔
760
    }
3✔
761
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc