From 8fa899b74619b549e4880c28a0f1d348cb68f755 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E5=BF=97=E5=AE=87?= Date: Thu, 21 Nov 2024 09:43:13 +1100 Subject: [PATCH] fix(chain): Sqlite - allow persisting anchor without tx Previously, we may error when we insert an anchor where the txid being anchored has no corresponding tx. --- crates/chain/src/rusqlite_impl.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/crates/chain/src/rusqlite_impl.rs b/crates/chain/src/rusqlite_impl.rs index 6df4d9f4..873c19a0 100644 --- a/crates/chain/src/rusqlite_impl.rs +++ b/crates/chain/src/rusqlite_impl.rs @@ -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, -- 2.49.0