]> Untitled Git - bdk/commitdiff
fix(chain): Sqlite - allow persisting anchor without tx
author志宇 <hello@evanlinjin.me>
Wed, 20 Nov 2024 22:43:13 +0000 (09:43 +1100)
committer志宇 <hello@evanlinjin.me>
Mon, 25 Nov 2024 23:29:46 +0000 (10:29 +1100)
Previously, we may error when we insert an anchor where the txid being
anchored has no corresponding tx.

crates/chain/src/rusqlite_impl.rs

index 6df4d9f45eb837cbbad6d348dd2f463599a9d622..873c19a0e717caf9a1553c6710fcf6c7ab5b9eca 100644 (file)
@@ -376,8 +376,15 @@ where
             "REPLACE INTO {}(txid, block_height, block_hash, anchor) VALUES(:txid, :block_height, :block_hash, jsonb(:anchor))",
             Self::ANCHORS_TABLE_NAME,
         ))?;
+        let mut statement_txid = db_tx.prepare_cached(&format!(
+            "INSERT OR IGNORE INTO {}(txid) VALUES(:txid)",
+            Self::TXS_TABLE_NAME,
+        ))?;
         for (anchor, txid) in &self.anchors {
             let anchor_block = anchor.anchor_block();
+            statement_txid.execute(named_params! {
+                ":txid": Impl(*txid)
+            })?;
             statement.execute(named_params! {
                 ":txid": Impl(*txid),
                 ":block_height": anchor_block.height,