Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions crates/chain/src/indexer/keychain_txout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down
11 changes: 7 additions & 4 deletions crates/chain/src/indexer/spk_txout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/chain/tests/test_tx_graph_conflicts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))],
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/example_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading