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>
};
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,
/// 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)
}
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.
///
}
/// 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
}
}
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;
}
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))],
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,
.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))