Struct bdk_chain::tx_graph::TxGraph

source ·
pub struct TxGraph<A = ()> { /* private fields */ }
Expand description

A graph of transactions and spends.

See the module-level documentation for more.

Implementations§

Iterate over all tx outputs known by TxGraph.

This includes txouts of both full transactions as well as floating transactions.

Iterate over floating txouts known by TxGraph.

Floating txouts are txouts that do not have the residing full transaction contained in the graph.

Iterate over all full transactions in the graph.

Get a transaction by txid. This only returns Some for full transactions.

Refer to get_txout for getting a specific [TxOut].

Get a transaction node by txid. This only returns Some for full transactions.

Obtains a single tx output (if any) at the specified outpoint.

Returns known outputs of a given txid.

Returns a BTreeMap of vout to output of the provided txid.

Calculates the fee of a given transaction. Returns 0 if tx is a coinbase transaction. Returns OK(_) if we have all the [TxOut]s being spent by tx in the graph (either as the full transactions or individual txouts).

To calculate the fee for a [Transaction] that depends on foreign [TxOut] values you must first manually insert the foreign TxOuts into the tx graph using the insert_txout function. Only insert TxOuts you trust the values for!

Note tx does not have to be in the graph for this to work.

The transactions spending from this output.

TxGraph allows conflicting transactions within the graph. Obviously the transactions in the returned set will never be in the same active-chain.

Iterates over the transactions spending from txid.

The iterator item is a union of (vout, txid-set) where:

  • vout is the provided txid’s outpoint that is being spent
  • txid-set is the set of txids spending the vout.

Creates an iterator that filters and maps ancestor transactions.

The iterator starts with the ancestors of the supplied tx (ancestor transactions of tx are transactions spent by tx). The supplied transaction is excluded from the iterator.

The supplied closure takes in two inputs (depth, ancestor_tx):

  • depth is the distance between the starting Transaction and the ancestor_tx. I.e., if the Transaction is spending an output of the ancestor_tx then depth will be 1.
  • ancestor_tx is the Transaction’s ancestor which we are considering to walk.

The supplied closure returns an Option<T>, allowing the caller to map each Transaction it visits and decide whether to visit ancestors.

Creates an iterator that filters and maps descendants from the starting txid.

The supplied closure takes in two inputs (depth, descendant_txid):

  • depth is the distance between the starting txid and the descendant_txid. I.e., if the descendant is spending an output of the starting txid then depth will be 1.
  • descendant_txid is the descendant’s txid which we are considering to walk.

The supplied closure returns an Option<T>, allowing the caller to map each node it visits and decide whether to visit descendants.

Creates an iterator that both filters and maps conflicting transactions (this includes descendants of directly-conflicting transactions, which are also considered conflicts).

Refer to Self::walk_descendants for walk_map usage.

Given a transaction, return an iterator of txids that directly conflict with the given transaction’s inputs (spends). The conflicting txids are returned with the given transaction’s vin (in which it conflicts).

Note that this only returns directly conflicting txids and won’t include:

  • descendants of conflicting transactions (which are technically also conflicting)
  • transactions conflicting with the given transaction’s ancestors

Get all transaction anchors known by TxGraph.

Whether the graph has any transactions or outputs in it.

Transform the TxGraph to have Anchors of another type.

This takes in a closure of signature FnMut(A) -> A2 which is called for each Anchor to transform it.

Construct a new TxGraph from a list of transactions.

Inserts the given [TxOut] at [OutPoint].

Inserting floating txouts are useful for determining fee/feerate of transactions we care about.

The ChangeSet result will be empty if the outpoint (or a full transaction containing the outpoint) already existed in self.

Inserts the given transaction into TxGraph.

The ChangeSet returned will be empty if tx already exists.

Batch insert unconfirmed transactions.

Items of txs are tuples containing the transaction and a last seen timestamp. The last seen communicates when the transaction is last seen in mempool which is used for conflict-resolution (refer to TxGraph::insert_seen_at for details).

Inserts the given anchor into TxGraph.

The ChangeSet returned will be empty if graph already knows that txid exists in anchor.

Inserts the given seen_at for txid into TxGraph.

Note that TxGraph only keeps track of the latest seen_at. To batch update all unconfirmed transactions with the latest seen_at, see update_last_seen_unconfirmed.

Update the last seen time for all unconfirmed transactions.

This method updates the last seen unconfirmed time for this TxGraph by inserting the given seen_at for every transaction not yet anchored to a confirmed block, and returns the ChangeSet after applying all updates to self.

This is useful for keeping track of the latest time a transaction was seen unconfirmed, which is important for evaluating transaction conflicts in the same TxGraph. For details of how TxGraph resolves conflicts, see the docs for try_get_chain_position.

A normal use of this method is to call it with the current system time. Although block headers contain a timestamp, using the header time would be less effective at tracking mempool transactions, because it can drift from actual clock time, plus we may want to update a transaction’s last seen time repeatedly between blocks.

Example
let now = std::time::SystemTime::now()
    .duration_since(UNIX_EPOCH)
    .expect("valid duration")
    .as_secs();
let changeset = tx_graph.update_last_seen_unconfirmed(now);
assert!(!changeset.last_seen.is_empty());

Note that TxGraph only keeps track of the latest seen_at, so the given time must by strictly greater than what is currently stored for a transaction to have an effect. To insert a last seen time for a single txid, see insert_seen_at.

Extends this graph with another so that self becomes the union of the two sets of transactions.

The returned ChangeSet is the set difference between update and self (transactions that exist in update but not in self).

Determines the ChangeSet between self and an empty TxGraph.

Applies ChangeSet to TxGraph.

Get the position of the transaction in chain with tip chain_tip.

Chain data is fetched from chain, a ChainOracle implementation.

This method returns Ok(None) if the transaction is not found in the chain, and no longer belongs in the mempool. The following factors are used to approximate whether an unconfirmed transaction exists in the mempool (not evicted):

  1. Unconfirmed transactions that conflict with confirmed transactions are evicted.
  2. Unconfirmed transactions that spend from transactions that are evicted, are also evicted.
  3. Given two conflicting unconfirmed transactions, the transaction with the lower last_seen_unconfirmed parameter is evicted. A transaction’s last_seen_unconfirmed parameter is the max of all it’s descendants’ last_seen_unconfirmed parameters. If the final last_seen_unconfirmeds are the same, the transaction with the lower txid (by lexicographical order) is evicted.
Error

An error will occur if the ChainOracle implementation (chain) fails. If the ChainOracle is infallible, get_chain_position can be used instead.

Get the position of the transaction in chain with tip chain_tip.

This is the infallible version of try_get_chain_position.

Get the txid of the spending transaction and where the spending transaction is observed in the chain of chain_tip.

If no in-chain transaction spends outpoint, None will be returned.

Error

An error will occur only if the ChainOracle implementation (chain) fails.

If the ChainOracle is infallible, get_chain_spend can be used instead.

Get the txid of the spending transaction and where the spending transaction is observed in the chain of chain_tip.

This is the infallible version of try_get_chain_spend

List graph transactions that are in chain with chain_tip.

Each transaction is represented as a CanonicalTx that contains where the transaction is observed in-chain, and the TxNode.

Error

If the ChainOracle implementation (chain) fails, an error will be returned with the returned item.

If the ChainOracle is infallible, list_chain_txs can be used instead.

List graph transactions that are in chain with chain_tip.

This is the infallible version of try_list_chain_txs.

Get a filtered list of outputs from the given outpoints that are in chain with chain_tip.

outpoints is a list of outpoints we are interested in, coupled with an outpoint identifier (OI) for convenience. If OI is not necessary, the caller can use (), or Iterator::enumerate over a list of [OutPoint]s.

Floating outputs (i.e., outputs for which we don’t have the full transaction in the graph) are ignored.

Error

An Iterator::Item can be an Err if the ChainOracle implementation (chain) fails.

If the ChainOracle implementation is infallible, filter_chain_txouts can be used instead.

Get a filtered list of outputs from the given outpoints that are in chain with chain_tip.

This is the infallible version of try_filter_chain_txouts.

Get a filtered list of unspent outputs (UTXOs) from the given outpoints that are in chain with chain_tip.

outpoints is a list of outpoints we are interested in, coupled with an outpoint identifier (OI) for convenience. If OI is not necessary, the caller can use (), or Iterator::enumerate over a list of [OutPoint]s.

Floating outputs are ignored.

Error

An Iterator::Item can be an Err if the ChainOracle implementation (chain) fails.

If the ChainOracle implementation is infallible, filter_chain_unspents can be used instead.

Get a filtered list of unspent outputs (UTXOs) from the given outpoints that are in chain with chain_tip.

This is the infallible version of try_filter_chain_unspents.

Get the total balance of outpoints that are in chain of chain_tip.

The output of trust_predicate should return true for scripts that we trust.

outpoints is a list of outpoints we are interested in, coupled with an outpoint identifier (OI) for convenience. If OI is not necessary, the caller can use (), or Iterator::enumerate over a list of [OutPoint]s.

If the provided ChainOracle implementation (chain) is infallible, balance can be used instead.

Get the total balance of outpoints that are in chain of chain_tip.

This is the infallible version of try_balance.

Trait Implementations§

Converts this type into a shared reference of the (usually inferred) input type.
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
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

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.