Struct bdk_chain::indexer::spk_txout::SpkTxOutIndex
source · pub struct SpkTxOutIndex<I> { /* private fields */ }
Expand description
An index storing TxOut
s that have a script pubkey that matches those in a list.
The basic idea is that you insert script pubkeys you care about into the index with
insert_spk
and then when you call Indexer::index_tx
or Indexer::index_txout
, the
index will look at any txouts you pass in and store and index any txouts matching one of its
script pubkeys.
Each script pubkey is associated with an application-defined index script index I
, which must be
Ord
. Usually, this is used to associate the derivation index of the script pubkey or even a
combination of (keychain, derivation_index)
.
Note there is no harm in scanning transactions that disappear from the blockchain or were never
in there in the first place. SpkTxOutIndex
is intentionally monotone – you cannot delete or
modify txouts that have been indexed. To find out which txouts from the index are actually in the
chain or unspent, you must use other sources of information like a TxGraph
.
Implementations§
source§impl<I: Clone + Ord + Debug> SpkTxOutIndex<I>
impl<I: Clone + Ord + Debug> SpkTxOutIndex<I>
sourcepub fn scan(&mut self, tx: &Transaction) -> BTreeSet<I>
pub fn scan(&mut self, tx: &Transaction) -> BTreeSet<I>
Scans a transaction’s outputs for matching script pubkeys.
Typically, this is used in two situations:
- After loading transaction data from the disk, you may scan over all the txouts to restore all your txouts.
- When getting new data from the chain, you usually scan it before incorporating it into your chain state.
sourcepub fn scan_txout(&mut self, op: OutPoint, txout: &TxOut) -> Option<&I>
pub fn scan_txout(&mut self, op: OutPoint, txout: &TxOut) -> Option<&I>
Scan a single TxOut
for a matching script pubkey and returns the index that matches the
script pubkey (if any).
sourcepub fn outpoints(&self) -> &BTreeSet<(I, OutPoint)>
pub fn outpoints(&self) -> &BTreeSet<(I, OutPoint)>
Get a reference to the set of indexed outpoints.
sourcepub fn txouts(
&self
) -> impl DoubleEndedIterator<Item = (&I, OutPoint, &TxOut)> + ExactSizeIterator
pub fn txouts( &self ) -> impl DoubleEndedIterator<Item = (&I, OutPoint, &TxOut)> + ExactSizeIterator
Iterate over all known txouts that spend to tracked script pubkeys.
sourcepub fn txouts_in_tx(
&self,
txid: Txid
) -> impl DoubleEndedIterator<Item = (&I, OutPoint, &TxOut)>
pub fn txouts_in_tx( &self, txid: Txid ) -> impl DoubleEndedIterator<Item = (&I, OutPoint, &TxOut)>
Finds all txouts on a transaction that has previously been scanned and indexed.
sourcepub fn outputs_in_range(
&self,
range: impl RangeBounds<I>
) -> impl DoubleEndedIterator<Item = (&I, OutPoint)>
pub fn outputs_in_range( &self, range: impl RangeBounds<I> ) -> impl DoubleEndedIterator<Item = (&I, OutPoint)>
Iterates over all the outputs with script pubkeys in an index range.
sourcepub fn txout(&self, outpoint: OutPoint) -> Option<(&I, &TxOut)>
pub fn txout(&self, outpoint: OutPoint) -> Option<(&I, &TxOut)>
Returns the txout and script pubkey index of the TxOut
at OutPoint
.
Returns None
if the TxOut
hasn’t been scanned or if nothing matching was found there.
sourcepub fn spk_at_index(&self, index: &I) -> Option<ScriptBuf>
pub fn spk_at_index(&self, index: &I) -> Option<ScriptBuf>
Returns the script that has been inserted at the index
.
If that index hasn’t been inserted yet, it will return None
.
sourcepub fn all_spks(&self) -> &BTreeMap<I, ScriptBuf>
pub fn all_spks(&self) -> &BTreeMap<I, ScriptBuf>
The script pubkeys that are being tracked by the index.
sourcepub fn insert_spk(&mut self, index: I, spk: ScriptBuf) -> bool
pub fn insert_spk(&mut self, index: I, spk: ScriptBuf) -> bool
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 this spk whenever it scans new data.
sourcepub fn unused_spks<R>(
&self,
range: R
) -> impl DoubleEndedIterator<Item = (&I, ScriptBuf)> + Clone + '_where
R: RangeBounds<I>,
pub fn unused_spks<R>(
&self,
range: R
) -> impl DoubleEndedIterator<Item = (&I, ScriptBuf)> + Clone + '_where
R: RangeBounds<I>,
Iterates over all unused script pubkeys in an index range.
Here, “unused” means that after the script pubkey was stored in the index, the index has never scanned a transaction output with it.
§Example
// imagine our spks are indexed like (keychain, derivation_index).
let txout_index = SpkTxOutIndex::<(u32, u32)>::default();
let all_unused_spks = txout_index.unused_spks(..);
let change_index = 1;
let unused_change_spks =
txout_index.unused_spks((change_index, u32::MIN)..(change_index, u32::MAX));
sourcepub fn is_used(&self, index: &I) -> bool
pub fn is_used(&self, index: &I) -> bool
Returns whether the script pubkey at index
has been used or not.
Here, “unused” means that after the script pubkey was stored in the index, the index has never scanned a transaction output with it.
sourcepub fn mark_used(&mut self, index: &I) -> bool
pub fn mark_used(&mut self, index: &I) -> bool
Marks the script pubkey at index
as used even though it hasn’t seen an output spending to it.
This only affects when the index
had already been added to self
and was unused.
Returns whether the index
was initially present as unused
.
This is useful when you want to reserve a script pubkey for something but don’t want to add
the transaction output using it to the index yet. Other callers will consider the index
used
until you call unmark_used
.
sourcepub fn unmark_used(&mut self, index: &I) -> bool
pub fn unmark_used(&mut self, index: &I) -> bool
Undoes the effect of mark_used
. Returns whether the index
is inserted back into
unused
.
Note that if self
has scanned an output with this script pubkey then this will have no
effect.
sourcepub fn index_of_spk(&self, script: ScriptBuf) -> Option<&I>
pub fn index_of_spk(&self, script: ScriptBuf) -> Option<&I>
Returns the index associated with the script pubkey.
sourcepub fn sent_and_received(
&self,
tx: &Transaction,
range: impl RangeBounds<I>
) -> (Amount, Amount)
pub fn sent_and_received( &self, tx: &Transaction, range: impl RangeBounds<I> ) -> (Amount, Amount)
Computes the total value transfer effect tx
has on the script pubkeys in range
. Value is
sent when a script pubkey in the range
is on an input and received when it is on an
output. For sent
to be computed correctly, the output being spent must have already been
scanned by the index. Calculating received just uses the Transaction
outputs directly,
so it will be correct even if it has not been scanned.
sourcepub fn net_value(
&self,
tx: &Transaction,
range: impl RangeBounds<I>
) -> SignedAmount
pub fn net_value( &self, tx: &Transaction, range: impl RangeBounds<I> ) -> SignedAmount
Computes the net value transfer effect of tx
on the script pubkeys in range
. Shorthand
for calling sent_and_received
and subtracting sent from received.
sourcepub fn is_relevant(&self, tx: &Transaction) -> bool
pub fn is_relevant(&self, tx: &Transaction) -> bool
Whether any of the inputs of this transaction spend a txout tracked or whether any output matches one of our script pubkeys.
It is easily possible to misuse this method and get false negatives by calling it before you
have scanned the TxOut
s the transaction is spending. For example, if you want to filter out
all the transactions in a block that are irrelevant, you must first scan all the
transactions in the block and only then use this method.
Trait Implementations§
source§impl<I: Clone> Clone for SpkTxOutIndex<I>
impl<I: Clone> Clone for SpkTxOutIndex<I>
source§fn clone(&self) -> SpkTxOutIndex<I>
fn clone(&self) -> SpkTxOutIndex<I>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<I: Debug> Debug for SpkTxOutIndex<I>
impl<I: Debug> Debug for SpkTxOutIndex<I>
source§impl<I> Default for SpkTxOutIndex<I>
impl<I> Default for SpkTxOutIndex<I>
source§impl<I: Clone + Ord + Debug> Indexer for SpkTxOutIndex<I>
impl<I: Clone + Ord + Debug> Indexer for SpkTxOutIndex<I>
source§fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet
outpoint
and txout
.