]> Untitled Git - bdk/commitdiff
refactor(chain): replace `ScriptBuf` with `AsRef<Script>` in SPK index methods
authorYuki Kishimoto <yukikishimoto@protonmail.com>
Mon, 29 Sep 2025 11:58:10 +0000 (08:58 -0300)
committerYuki Kishimoto <yukikishimoto@protonmail.com>
Tue, 30 Sep 2025 11:29:57 +0000 (08:29 -0300)
Replace `ScriptBuf` with `AsRef<Script>` in `SpkTxOutIndex::index_of_spk` and `KeychainTxOutIndex::index_of_spk` methods, to avoid the need of cloning the `ScriptBuf` for a SPK index lookup.

Signed-off-by: Yuki Kishimoto <yukikishimoto@protonmail.com>
crates/chain/src/indexer/keychain_txout.rs
crates/chain/src/indexer/spk_txout.rs
crates/chain/tests/test_tx_graph_conflicts.rs
examples/example_cli/src/lib.rs

index c4668cbe9f6c46b6fb9bf08298badfccde83b982..b99a6a224241e663234620b4f7790a01086ed75e 100644 (file)
@@ -12,7 +12,7 @@ use crate::{
 };
 use alloc::{borrow::ToOwned, vec::Vec};
 use bitcoin::{
-    key::Secp256k1, Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxOut, Txid,
+    key::Secp256k1, Amount, OutPoint, Script, ScriptBuf, SignedAmount, Transaction, TxOut, Txid,
 };
 use core::{
     fmt::Debug,
@@ -354,7 +354,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
     /// Returns the keychain and keychain index associated with the spk.
     ///
     /// This calls [`SpkTxOutIndex::index_of_spk`] internally.
-    pub fn index_of_spk(&self, script: ScriptBuf) -> Option<&(K, u32)> {
+    pub fn index_of_spk<T>(&self, script: T) -> Option<&(K, u32)>
+    where
+        T: AsRef<Script>,
+    {
         self.inner.index_of_spk(script)
     }
 
index 6fb55d57747610674364b7be5a43ff5a89d05364..32ad6f0d9ec17e404fd30a9409ce8b90cc22c7bb 100644 (file)
@@ -7,7 +7,7 @@ use crate::{
     collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap},
     Indexer,
 };
-use bitcoin::{Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxOut, Txid};
+use bitcoin::{Amount, OutPoint, Script, ScriptBuf, SignedAmount, Transaction, TxOut, Txid};
 
 /// An index storing [`TxOut`]s that have a script pubkey that matches those in a list.
 ///
@@ -280,8 +280,11 @@ impl<I: Clone + Ord + core::fmt::Debug> SpkTxOutIndex<I> {
     }
 
     /// Returns the index associated with the script pubkey.
-    pub fn index_of_spk(&self, script: ScriptBuf) -> Option<&I> {
-        self.spk_indices.get(script.as_script())
+    pub fn index_of_spk<T>(&self, script: T) -> Option<&I>
+    where
+        T: AsRef<Script>,
+    {
+        self.spk_indices.get(script.as_ref())
     }
 
     /// Computes the total value transfer effect `tx` has on the script pubkeys in `range`. Value is
@@ -305,7 +308,7 @@ impl<I: Clone + Ord + core::fmt::Debug> SpkTxOutIndex<I> {
             }
         }
         for txout in &tx.output {
-            if let Some(index) = self.index_of_spk(txout.script_pubkey.clone()) {
+            if let Some(index) = self.index_of_spk(txout.script_pubkey.as_script()) {
                 if range.contains(index) {
                     received += txout.value;
                 }
index 70dc01884b7a916e6f354c7dff47683c1c046a9a..38f21365c3cc739d1dee3030b8bc46be1ca7b9df 100644 (file)
@@ -64,7 +64,7 @@ fn test_tx_conflict_handling() {
                 TxTemplate {
                     tx_name: "unconfirmed_conflict",
                     inputs: &[
-                        TxInTemplate::PrevTx("confirmed_genesis", 0), 
+                        TxInTemplate::PrevTx("confirmed_genesis", 0),
                         TxInTemplate::PrevTx("unconfirmed_coinbase", 0)
                     ],
                     outputs: &[TxOutTemplate::new(20000, Some(2))],
@@ -1034,7 +1034,7 @@ fn test_tx_conflict_handling() {
                 env.indexer.outpoints().iter().cloned(),
                 |_, txout| {
                     env.indexer
-                        .index_of_spk(txout.txout.script_pubkey.clone())
+                        .index_of_spk(txout.txout.script_pubkey.as_script())
                         .is_some()
                 },
                 0,
index f130c1adf1b5eac51baaa411d8ae450365ebc762..baa17e6d7b2fd3dc34df2ba844f62b36c6a4b093 100644 (file)
@@ -756,7 +756,7 @@ pub fn handle_commands<CS: clap::Subcommand, S: clap::Args>(
                                 .last()
                                 .expect("must have a keychain");
                             let change_index = tx.output.iter().find_map(|txout| {
-                                let spk = txout.script_pubkey.clone();
+                                let spk = txout.script_pubkey.as_script();
                                 match graph.index.index_of_spk(spk) {
                                     Some(&(keychain, index)) if keychain == change_keychain => {
                                         Some((keychain, index))