Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #118 from rajarshimaitra/add-spk-fix
Browse files Browse the repository at this point in the history
Fix add_spk API
  • Loading branch information
LLFourn authored Jan 10, 2023
2 parents f207acf + 46524ad commit e9ac33e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
5 changes: 3 additions & 2 deletions bdk_chain/src/keychain/keychain_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
.derived_descriptor(&secp)
.expect("the descritpor cannot need hardened derivation")
.script_pubkey();
self.inner.add_spk((keychain.clone(), index), spk);
self.inner
.insert_script_pubkey((keychain.clone(), index), spk);
}

true
Expand Down Expand Up @@ -231,7 +232,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
.script_pubkey();

let index = (keychain.clone(), next_derivation_index);
self.inner.add_spk(index.clone(), new_spk);
self.inner.insert_script_pubkey(index.clone(), new_spk);
let new_spk = self
.inner
.script_pubkeys()
Expand Down
3 changes: 3 additions & 0 deletions bdk_chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ pub mod collections {
#![allow(dead_code)]
pub type HashSet<K> = alloc::collections::BTreeSet<K>;
pub type HashMap<K, V> = alloc::collections::BTreeMap<K, V>;
pub use alloc::collections::btree_map as hash_map;
pub use alloc::collections::*;
}

// When we have std use `std`'s all collections
#[cfg(all(feature = "std", not(feature = "hashbrown")))]
#[doc(hidden)]
pub mod collections {
pub use std::collections::hash_map;
pub use std::collections::*;
}

Expand All @@ -60,4 +62,5 @@ pub mod collections {
pub type HashSet<K> = hashbrown::HashSet<K>;
pub type HashMap<K, V> = hashbrown::HashMap<K, V>;
pub use alloc::collections::*;
pub use hashbrown::hash_map;
}
20 changes: 13 additions & 7 deletions bdk_chain/src/spk_txout_index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::ops::RangeBounds;

use crate::{
collections::{BTreeMap, BTreeSet, HashMap},
collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap},
ForEachTxout,
};
use bitcoin::{self, OutPoint, Script, Transaction, TxOut, Txid};
Expand All @@ -22,7 +22,7 @@ use bitcoin::{self, OutPoint, Script, Transaction, TxOut, Txid};
/// chain or unspent etc you must use other sources of information like a [`SparseChain`].
///
/// [`TxOut`]: bitcoin::TxOut
/// [`add_spk`]: Self::add_spk
/// [`add_spk`]: Self::insert_script_pubkey
/// [`Ord`]: core::cmp::Ord
/// [`scan`]: Self::scan
/// [`SparseChain`]: crate::sparse_chain::SparseChain
Expand Down Expand Up @@ -149,13 +149,19 @@ impl<I: Clone + Ord> SpkTxOutIndex<I> {
&self.script_pubkeys
}

/// Adds a script pubkey to scan for.
/// Adds a script pubkey to scan for. Returns `false` and does nothing if spk already exists in the map
///
/// the index will look for outputs spending to whenever it scans new data.
pub fn add_spk(&mut self, index: I, spk: Script) {
self.spk_indexes.insert(spk.clone(), index.clone());
self.script_pubkeys.insert(index.clone(), spk);
self.unused.insert(index);
pub fn insert_script_pubkey(&mut self, index: I, spk: Script) -> bool {
match self.spk_indexes.entry(spk.clone()) {
Entry::Vacant(value) => {
value.insert(index.clone());
self.script_pubkeys.insert(index.clone(), spk);
self.unused.insert(index);
true
}
Entry::Occupied(_) => false,
}
}

/// Iterates over a unused script pubkeys in a index range.
Expand Down
4 changes: 2 additions & 2 deletions bdk_chain/tests/test_spk_txout_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ fn spk_txout_sent_and_received() {
let spk2 = Script::from_hex("00142b57404ae14f08c3a0c903feb2af7830605eb00f").unwrap();

let mut index = SpkTxOutIndex::default();
index.add_spk(0, spk1.clone());
index.add_spk(1, spk2.clone());
index.insert_script_pubkey(0, spk1.clone());
index.insert_script_pubkey(1, spk2.clone());

let tx1 = Transaction {
version: 0x02,
Expand Down

0 comments on commit e9ac33e

Please sign in to comment.