pub struct SpkTxOutIndex<I> { /* private fields */ }
Expand description

An index storing TxOuts 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§

Scans a transaction’s outputs for matching script pubkeys.

Typically, this is used in two situations:

  1. After loading transaction data from the disk, you may scan over all the txouts to restore all your txouts.
  2. When getting new data from the chain, you usually scan it before incorporating it into your chain state.

Scan a single TxOut for a matching script pubkey and returns the index that matches the script pubkey (if any).

Get a reference to the set of indexed outpoints.

Iterate over all known txouts that spend to tracked script pubkeys.

Finds all txouts on a transaction that has previously been scanned and indexed.

Iterates over all the outputs with script pubkeys in an index range.

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.

Returns the script that has been inserted at the index.

If that index hasn’t been inserted yet, it will return None.

The script pubkeys that are being tracked by the index.

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.

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));

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.

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.

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.

Returns the index associated with the script pubkey.

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.

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.

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 TxOuts 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§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resultant “changeset” when new transaction data is indexed.
Scan and index the given outpoint and txout.
Scans a transaction for relevant outpoints, which are stored and indexed internally.
Determines the ChangeSet between self and an empty Indexer.
Apply changeset to itself.
Determines whether the transaction should be included in the index.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.