Skip to content

Commit 00b5f02

Browse files
committed
Merge #2052: chain: replace ScriptBuf with &Script in SPK index methods
c095145 refactor(chain): replace `ScriptBuf` with `AsRef<Script>` in SPK index methods (Yuki Kishimoto) Pull request description: ### Description Replace `ScriptBuf` with `&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. ### Changelog notice Breaking changes: - Change `SpkTxOutIndex::index_of_spk` and `KeychainTxOutIndex::index_of_spk` args from `ScriptBuf` to `&Script`. ### Checklists #### All Submissions: * [X] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) #### New Features: * [ ] I've added tests for the new feature * [ ] I've added docs for the new feature #### Bugfixes: * [X] This pull request breaks the existing API * [ ] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: oleonardolima: cACK c095145 evanlinjin: ACK c095145 Tree-SHA512: f77bb4d9b58a682f7b0371ec1a76b2e6cca2bc0a76181d085bb7905d99a5eb4ca8907c6f2b229d38893673aa186eea0c79431b8736a67932c9956cd97f99f085
2 parents 24654a5 + c095145 commit 00b5f02

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

crates/chain/src/indexer/keychain_txout.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{
1212
};
1313
use alloc::{borrow::ToOwned, vec::Vec};
1414
use bitcoin::{
15-
key::Secp256k1, Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxOut, Txid,
15+
key::Secp256k1, Amount, OutPoint, Script, ScriptBuf, SignedAmount, Transaction, TxOut, Txid,
1616
};
1717
use core::{
1818
fmt::Debug,
@@ -354,7 +354,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
354354
/// Returns the keychain and keychain index associated with the spk.
355355
///
356356
/// This calls [`SpkTxOutIndex::index_of_spk`] internally.
357-
pub fn index_of_spk(&self, script: ScriptBuf) -> Option<&(K, u32)> {
357+
pub fn index_of_spk<T>(&self, script: T) -> Option<&(K, u32)>
358+
where
359+
T: AsRef<Script>,
360+
{
358361
self.inner.index_of_spk(script)
359362
}
360363

crates/chain/src/indexer/spk_txout.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap},
88
Indexer,
99
};
10-
use bitcoin::{Amount, OutPoint, ScriptBuf, SignedAmount, Transaction, TxOut, Txid};
10+
use bitcoin::{Amount, OutPoint, Script, ScriptBuf, SignedAmount, Transaction, TxOut, Txid};
1111

1212
/// An index storing [`TxOut`]s that have a script pubkey that matches those in a list.
1313
///
@@ -280,8 +280,11 @@ impl<I: Clone + Ord + core::fmt::Debug> SpkTxOutIndex<I> {
280280
}
281281

282282
/// Returns the index associated with the script pubkey.
283-
pub fn index_of_spk(&self, script: ScriptBuf) -> Option<&I> {
284-
self.spk_indices.get(script.as_script())
283+
pub fn index_of_spk<T>(&self, script: T) -> Option<&I>
284+
where
285+
T: AsRef<Script>,
286+
{
287+
self.spk_indices.get(script.as_ref())
285288
}
286289

287290
/// 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> {
305308
}
306309
}
307310
for txout in &tx.output {
308-
if let Some(index) = self.index_of_spk(txout.script_pubkey.clone()) {
311+
if let Some(index) = self.index_of_spk(txout.script_pubkey.as_script()) {
309312
if range.contains(index) {
310313
received += txout.value;
311314
}

crates/chain/tests/test_tx_graph_conflicts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn test_tx_conflict_handling() {
6464
TxTemplate {
6565
tx_name: "unconfirmed_conflict",
6666
inputs: &[
67-
TxInTemplate::PrevTx("confirmed_genesis", 0),
67+
TxInTemplate::PrevTx("confirmed_genesis", 0),
6868
TxInTemplate::PrevTx("unconfirmed_coinbase", 0)
6969
],
7070
outputs: &[TxOutTemplate::new(20000, Some(2))],
@@ -1034,7 +1034,7 @@ fn test_tx_conflict_handling() {
10341034
env.indexer.outpoints().iter().cloned(),
10351035
|_, txout| {
10361036
env.indexer
1037-
.index_of_spk(txout.txout.script_pubkey.clone())
1037+
.index_of_spk(txout.txout.script_pubkey.as_script())
10381038
.is_some()
10391039
},
10401040
0,

examples/example_cli/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ pub fn handle_commands<CS: clap::Subcommand, S: clap::Args>(
756756
.last()
757757
.expect("must have a keychain");
758758
let change_index = tx.output.iter().find_map(|txout| {
759-
let spk = txout.script_pubkey.clone();
759+
let spk = txout.script_pubkey.as_script();
760760
match graph.index.index_of_spk(spk) {
761761
Some(&(keychain, index)) if keychain == change_keychain => {
762762
Some((keychain, index))

0 commit comments

Comments
 (0)