2023-03-01 11:09:08 +01:00
|
|
|
//! Module for structures that store and traverse transactions.
|
|
|
|
//!
|
2023-03-10 23:23:29 +05:30
|
|
|
//! [`TxGraph`] is a monotone structure that inserts transactions and indexes the spends. The
|
|
|
|
//! [`Additions`] structure reports changes of [`TxGraph`] but can also be applied to a
|
|
|
|
//! [`TxGraph`] as well. Lastly, [`TxDescendants`] is an [`Iterator`] that traverses descendants of
|
2023-03-01 11:09:08 +01:00
|
|
|
//! a given transaction.
|
|
|
|
//!
|
|
|
|
//! Conflicting transactions are allowed to coexist within a [`TxGraph`]. This is useful for
|
|
|
|
//! identifying and traversing conflicts and descendants of a given transaction.
|
|
|
|
//!
|
|
|
|
//! # Previewing and applying changes
|
|
|
|
//!
|
|
|
|
//! Methods that either preview or apply changes to [`TxGraph`] will return [`Additions`].
|
2023-03-10 23:23:29 +05:30
|
|
|
//! [`Additions`] can be applied back to a [`TxGraph`] or be used to inform persistent storage
|
2023-03-01 11:09:08 +01:00
|
|
|
//! of the changes to [`TxGraph`].
|
|
|
|
//!
|
|
|
|
//! ```
|
2023-03-24 09:23:36 +08:00
|
|
|
//! # use bdk_chain::BlockId;
|
2023-03-01 11:09:08 +01:00
|
|
|
//! # use bdk_chain::tx_graph::TxGraph;
|
|
|
|
//! # use bdk_chain::example_utils::*;
|
|
|
|
//! # use bitcoin::Transaction;
|
|
|
|
//! # let tx_a = tx_from_hex(RAW_TX_1);
|
|
|
|
//! # let tx_b = tx_from_hex(RAW_TX_2);
|
2023-04-05 19:13:42 +08:00
|
|
|
//! let mut graph: TxGraph = TxGraph::default();
|
2023-03-01 11:09:08 +01:00
|
|
|
//!
|
|
|
|
//! // preview a transaction insertion (not actually inserted)
|
|
|
|
//! let additions = graph.insert_tx_preview(tx_a);
|
|
|
|
//! // apply the insertion
|
|
|
|
//! graph.apply_additions(additions);
|
|
|
|
//!
|
|
|
|
//! // you can also insert a transaction directly
|
|
|
|
//! let already_applied_additions = graph.insert_tx(tx_b);
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! A [`TxGraph`] can also be updated with another [`TxGraph`].
|
|
|
|
//!
|
|
|
|
//! ```
|
2023-03-24 09:23:36 +08:00
|
|
|
//! # use bdk_chain::BlockId;
|
2023-03-01 11:09:08 +01:00
|
|
|
//! # use bdk_chain::tx_graph::TxGraph;
|
|
|
|
//! # use bdk_chain::example_utils::*;
|
|
|
|
//! # use bitcoin::Transaction;
|
|
|
|
//! # let tx_a = tx_from_hex(RAW_TX_1);
|
|
|
|
//! # let tx_b = tx_from_hex(RAW_TX_2);
|
2023-04-05 18:17:08 +08:00
|
|
|
//! let mut graph: TxGraph = TxGraph::default();
|
2023-03-08 11:39:25 +13:00
|
|
|
//! let update = TxGraph::new(vec![tx_a, tx_b]);
|
2023-03-01 11:09:08 +01:00
|
|
|
//!
|
2023-03-10 23:23:29 +05:30
|
|
|
//! // preview additions as the result of the update
|
2023-03-01 11:09:08 +01:00
|
|
|
//! let additions = graph.determine_additions(&update);
|
|
|
|
//! // apply the additions
|
|
|
|
//! graph.apply_additions(additions);
|
|
|
|
//!
|
|
|
|
//! // we can also apply the update graph directly
|
|
|
|
//! // the additions will be empty as we have already applied the same update above
|
|
|
|
//! let additions = graph.apply_update(update);
|
|
|
|
//! assert!(additions.is_empty());
|
|
|
|
//! ```
|
2023-03-24 09:23:36 +08:00
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
use crate::{collections::*, Anchor, BlockId, ChainOracle, ForEachTxOut, FullTxOut, ObservedAs};
|
2023-03-01 11:09:08 +01:00
|
|
|
use alloc::vec::Vec;
|
|
|
|
use bitcoin::{OutPoint, Transaction, TxOut, Txid};
|
2023-03-27 13:59:51 +08:00
|
|
|
use core::{
|
|
|
|
convert::Infallible,
|
|
|
|
ops::{Deref, RangeInclusive},
|
|
|
|
};
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
/// A graph of transactions and spends.
|
|
|
|
///
|
|
|
|
/// See the [module-level documentation] for more.
|
|
|
|
///
|
|
|
|
/// [module-level documentation]: crate::tx_graph
|
2023-03-24 09:23:36 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2023-03-30 18:14:44 +08:00
|
|
|
pub struct TxGraph<A = ()> {
|
2023-03-24 09:23:36 +08:00
|
|
|
// all transactions that the graph is aware of in format: `(tx_node, tx_anchors, tx_last_seen)`
|
2023-03-30 18:33:53 +08:00
|
|
|
txs: HashMap<Txid, (TxNodeInternal, BTreeSet<A>, u64)>,
|
2023-03-01 11:09:08 +01:00
|
|
|
spends: BTreeMap<OutPoint, HashSet<Txid>>,
|
2023-03-24 09:23:36 +08:00
|
|
|
anchors: BTreeSet<(A, Txid)>,
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
// This atrocity exists so that `TxGraph::outspends()` can return a reference.
|
|
|
|
// FIXME: This can be removed once `HashSet::new` is a const fn.
|
|
|
|
empty_outspends: HashSet<Txid>,
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<A> Default for TxGraph<A> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
txs: Default::default(),
|
|
|
|
spends: Default::default(),
|
|
|
|
anchors: Default::default(),
|
|
|
|
empty_outspends: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// An outward-facing view of a (transaction) node in the [`TxGraph`].
|
2023-03-26 11:24:30 +08:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
2023-03-30 18:33:53 +08:00
|
|
|
pub struct TxNode<'a, T, A> {
|
2023-03-24 09:23:36 +08:00
|
|
|
/// Txid of the transaction.
|
|
|
|
pub txid: Txid,
|
|
|
|
/// A partial or full representation of the transaction.
|
|
|
|
pub tx: &'a T,
|
|
|
|
/// The blocks that the transaction is "anchored" in.
|
|
|
|
pub anchors: &'a BTreeSet<A>,
|
2023-03-31 14:15:34 +08:00
|
|
|
/// The last-seen unix timestamp of the transaction as unconfirmed.
|
|
|
|
pub last_seen_unconfirmed: u64,
|
2023-03-24 09:23:36 +08:00
|
|
|
}
|
|
|
|
|
2023-03-30 18:33:53 +08:00
|
|
|
impl<'a, T, A> Deref for TxNode<'a, T, A> {
|
2023-03-24 09:23:36 +08:00
|
|
|
type Target = T;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
self.tx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:33:53 +08:00
|
|
|
impl<'a, A> TxNode<'a, Transaction, A> {
|
2023-03-24 09:23:36 +08:00
|
|
|
pub fn from_tx(tx: &'a Transaction, anchors: &'a BTreeSet<A>) -> Self {
|
|
|
|
Self {
|
|
|
|
txid: tx.txid(),
|
|
|
|
tx,
|
|
|
|
anchors,
|
2023-03-31 14:15:34 +08:00
|
|
|
last_seen_unconfirmed: 0,
|
2023-03-24 09:23:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal representation of a transaction node of a [`TxGraph`].
|
|
|
|
///
|
|
|
|
/// This can either be a whole transaction, or a partial transaction (where we only have select
|
|
|
|
/// outputs).
|
2023-03-01 11:09:08 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2023-03-30 18:33:53 +08:00
|
|
|
enum TxNodeInternal {
|
2023-03-08 11:39:25 +13:00
|
|
|
Whole(Transaction),
|
2023-03-01 11:09:08 +01:00
|
|
|
Partial(BTreeMap<u32, TxOut>),
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:33:53 +08:00
|
|
|
impl Default for TxNodeInternal {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
Self::Partial(BTreeMap::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// An outwards-facing view of a transaction that is part of the *best chain*'s history.
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct CanonicalTx<'a, T, A> {
|
|
|
|
/// How the transaction is observed as (confirmed or unconfirmed).
|
|
|
|
pub observed_as: ObservedAs<&'a A>,
|
|
|
|
/// The transaction node (as part of the graph).
|
|
|
|
pub node: TxNode<'a, T, A>,
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<A> TxGraph<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Iterate over all tx outputs known by [`TxGraph`].
|
2023-04-17 23:25:57 +08:00
|
|
|
///
|
|
|
|
/// This includes txouts of both full transactions as well as floating transactions.
|
2023-03-01 11:09:08 +01:00
|
|
|
pub fn all_txouts(&self) -> impl Iterator<Item = (OutPoint, &TxOut)> {
|
2023-03-24 09:23:36 +08:00
|
|
|
self.txs.iter().flat_map(|(txid, (tx, _, _))| match tx {
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Whole(tx) => tx
|
2023-03-01 11:09:08 +01:00
|
|
|
.output
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(vout, txout)| (OutPoint::new(*txid, vout as _), txout))
|
|
|
|
.collect::<Vec<_>>(),
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Partial(txouts) => txouts
|
2023-03-01 11:09:08 +01:00
|
|
|
.iter()
|
|
|
|
.map(|(vout, txout)| (OutPoint::new(*txid, *vout as _), txout))
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// Iterate over floating txouts known by [`TxGraph`].
|
|
|
|
///
|
|
|
|
/// Floating txouts are txouts that do not have the residing full transaction contained in the
|
|
|
|
/// graph.
|
|
|
|
pub fn floating_txouts(&self) -> impl Iterator<Item = (OutPoint, &TxOut)> {
|
|
|
|
self.txs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(txid, (tx_node, _, _))| match tx_node {
|
|
|
|
TxNodeInternal::Whole(_) => None,
|
|
|
|
TxNodeInternal::Partial(txouts) => Some(
|
|
|
|
txouts
|
|
|
|
.iter()
|
|
|
|
.map(|(&vout, txout)| (OutPoint::new(*txid, vout), txout)),
|
|
|
|
),
|
|
|
|
})
|
|
|
|
.flatten()
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Iterate over all full transactions in the graph.
|
2023-04-17 23:25:57 +08:00
|
|
|
pub fn full_txs(&self) -> impl Iterator<Item = TxNode<'_, Transaction, A>> {
|
2023-03-24 09:23:36 +08:00
|
|
|
self.txs
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(&txid, (tx, anchors, last_seen))| match tx {
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Whole(tx) => Some(TxNode {
|
2023-03-24 09:23:36 +08:00
|
|
|
txid,
|
|
|
|
tx,
|
|
|
|
anchors,
|
2023-03-31 14:15:34 +08:00
|
|
|
last_seen_unconfirmed: *last_seen,
|
2023-03-24 09:23:36 +08:00
|
|
|
}),
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Partial(_) => None,
|
2023-03-24 09:23:36 +08:00
|
|
|
})
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a transaction by txid. This only returns `Some` for full transactions.
|
|
|
|
///
|
|
|
|
/// Refer to [`get_txout`] for getting a specific [`TxOut`].
|
|
|
|
///
|
|
|
|
/// [`get_txout`]: Self::get_txout
|
2023-03-30 18:33:53 +08:00
|
|
|
pub fn get_tx(&self, txid: Txid) -> Option<&Transaction> {
|
|
|
|
self.get_tx_node(txid).map(|n| n.tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a transaction node by txid. This only returns `Some` for full transactions.
|
|
|
|
pub fn get_tx_node(&self, txid: Txid) -> Option<TxNode<'_, Transaction, A>> {
|
2023-03-24 09:23:36 +08:00
|
|
|
match &self.txs.get(&txid)? {
|
2023-03-30 18:33:53 +08:00
|
|
|
(TxNodeInternal::Whole(tx), anchors, last_seen) => Some(TxNode {
|
2023-03-24 09:23:36 +08:00
|
|
|
txid,
|
|
|
|
tx,
|
|
|
|
anchors,
|
2023-03-31 14:15:34 +08:00
|
|
|
last_seen_unconfirmed: *last_seen,
|
2023-03-24 09:23:36 +08:00
|
|
|
}),
|
|
|
|
_ => None,
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:23:29 +05:30
|
|
|
/// Obtains a single tx output (if any) at the specified outpoint.
|
2023-03-01 11:09:08 +01:00
|
|
|
pub fn get_txout(&self, outpoint: OutPoint) -> Option<&TxOut> {
|
2023-03-24 09:23:36 +08:00
|
|
|
match &self.txs.get(&outpoint.txid)?.0 {
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Whole(tx) => tx.output.get(outpoint.vout as usize),
|
|
|
|
TxNodeInternal::Partial(txouts) => txouts.get(&outpoint.vout),
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// Returns known outputs of a given `txid`.
|
|
|
|
///
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Returns a [`BTreeMap`] of vout to output of the provided `txid`.
|
2023-04-17 23:25:57 +08:00
|
|
|
pub fn tx_outputs(&self, txid: Txid) -> Option<BTreeMap<u32, &TxOut>> {
|
2023-03-24 09:23:36 +08:00
|
|
|
Some(match &self.txs.get(&txid)?.0 {
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Whole(tx) => tx
|
2023-03-01 11:09:08 +01:00
|
|
|
.output
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(vout, txout)| (vout as u32, txout))
|
|
|
|
.collect::<BTreeMap<_, _>>(),
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Partial(txouts) => txouts
|
2023-03-01 11:09:08 +01:00
|
|
|
.iter()
|
|
|
|
.map(|(vout, txout)| (*vout, txout))
|
|
|
|
.collect::<BTreeMap<_, _>>(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the fee of a given transaction. Returns 0 if `tx` is a coinbase transaction.
|
|
|
|
/// Returns `Some(_)` if we have all the `TxOut`s being spent by `tx` in the graph (either as
|
2023-03-10 23:23:29 +05:30
|
|
|
/// the full transactions or individual txouts). If the returned value is negative, then the
|
2023-03-01 11:09:08 +01:00
|
|
|
/// transaction is invalid according to the graph.
|
|
|
|
///
|
|
|
|
/// Returns `None` if we're missing an input for the tx in the graph.
|
|
|
|
///
|
|
|
|
/// Note `tx` does not have to be in the graph for this to work.
|
|
|
|
pub fn calculate_fee(&self, tx: &Transaction) -> Option<i64> {
|
|
|
|
if tx.is_coin_base() {
|
|
|
|
return Some(0);
|
|
|
|
}
|
|
|
|
let inputs_sum = tx
|
|
|
|
.input
|
|
|
|
.iter()
|
|
|
|
.map(|txin| {
|
|
|
|
self.get_txout(txin.previous_output)
|
|
|
|
.map(|txout| txout.value as i64)
|
|
|
|
})
|
|
|
|
.sum::<Option<i64>>()?;
|
|
|
|
|
|
|
|
let outputs_sum = tx
|
|
|
|
.output
|
|
|
|
.iter()
|
|
|
|
.map(|txout| txout.value as i64)
|
|
|
|
.sum::<i64>();
|
|
|
|
|
|
|
|
Some(inputs_sum - outputs_sum)
|
|
|
|
}
|
2023-03-30 18:14:44 +08:00
|
|
|
|
|
|
|
/// 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.
|
2023-04-21 12:33:03 +08:00
|
|
|
pub fn outspends(&self, outpoint: OutPoint) -> &HashSet<Txid> {
|
2023-03-30 18:14:44 +08:00
|
|
|
self.spends.get(&outpoint).unwrap_or(&self.empty_outspends)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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`.
|
2023-04-17 23:25:57 +08:00
|
|
|
pub fn tx_spends(
|
2023-03-30 18:14:44 +08:00
|
|
|
&self,
|
|
|
|
txid: Txid,
|
|
|
|
) -> impl DoubleEndedIterator<Item = (u32, &HashSet<Txid>)> + '_ {
|
|
|
|
let start = OutPoint { txid, vout: 0 };
|
|
|
|
let end = OutPoint {
|
|
|
|
txid,
|
|
|
|
vout: u32::MAX,
|
|
|
|
};
|
|
|
|
self.spends
|
|
|
|
.range(start..=end)
|
|
|
|
.map(|(outpoint, spends)| (outpoint.vout, spends))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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`; the `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 vists
|
|
|
|
/// and decide whether to visit descendants.
|
|
|
|
pub fn walk_descendants<'g, F, O>(&'g self, txid: Txid, walk_map: F) -> TxDescendants<A, F>
|
|
|
|
where
|
|
|
|
F: FnMut(usize, Txid) -> Option<O> + 'g,
|
|
|
|
{
|
|
|
|
TxDescendants::new_exclude_root(self, txid, walk_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
pub fn walk_conflicts<'g, F, O>(
|
|
|
|
&'g self,
|
|
|
|
tx: &'g Transaction,
|
|
|
|
walk_map: F,
|
|
|
|
) -> TxDescendants<A, F>
|
|
|
|
where
|
|
|
|
F: FnMut(usize, Txid) -> Option<O> + 'g,
|
|
|
|
{
|
|
|
|
let txids = self.direct_conflicts_of_tx(tx).map(|(_, txid)| txid);
|
|
|
|
TxDescendants::from_multiple_include_root(self, txids, walk_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 does not include descendants of
|
|
|
|
/// those txids (which are technically also conflicting).
|
|
|
|
pub fn direct_conflicts_of_tx<'g>(
|
|
|
|
&'g self,
|
|
|
|
tx: &'g Transaction,
|
|
|
|
) -> impl Iterator<Item = (usize, Txid)> + '_ {
|
|
|
|
let txid = tx.txid();
|
|
|
|
tx.input
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.filter_map(move |(vin, txin)| self.spends.get(&txin.previous_output).zip(Some(vin)))
|
|
|
|
.flat_map(|(spends, vin)| core::iter::repeat(vin).zip(spends.iter().cloned()))
|
|
|
|
.filter(move |(_, conflicting_txid)| *conflicting_txid != txid)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether the graph has any transactions or outputs in it.
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.txs.is_empty()
|
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-30 18:14:44 +08:00
|
|
|
impl<A: Clone + Ord> TxGraph<A> {
|
2023-03-10 23:23:29 +05:30
|
|
|
/// Construct a new [`TxGraph`] from a list of transactions.
|
2023-03-08 11:39:25 +13:00
|
|
|
pub fn new(txs: impl IntoIterator<Item = Transaction>) -> Self {
|
2023-03-01 11:09:08 +01:00
|
|
|
let mut new = Self::default();
|
|
|
|
for tx in txs.into_iter() {
|
|
|
|
let _ = new.insert_tx(tx);
|
|
|
|
}
|
|
|
|
new
|
|
|
|
}
|
2023-03-24 09:23:36 +08:00
|
|
|
|
2023-03-30 18:14:44 +08:00
|
|
|
/// Returns the resultant [`Additions`] if the given `txout` is inserted at `outpoint`. Does not
|
|
|
|
/// mutate `self`.
|
|
|
|
///
|
2023-04-17 23:25:57 +08:00
|
|
|
/// Inserting floating txouts are useful for determining fee/feerate of transactions we care
|
|
|
|
/// about.
|
|
|
|
///
|
2023-03-30 18:14:44 +08:00
|
|
|
/// The [`Additions`] result will be empty if the `outpoint` (or a full transaction containing
|
|
|
|
/// the `outpoint`) already existed in `self`.
|
|
|
|
pub fn insert_txout_preview(&self, outpoint: OutPoint, txout: TxOut) -> Additions<A> {
|
|
|
|
let mut update = Self::default();
|
|
|
|
update.txs.insert(
|
|
|
|
outpoint.txid,
|
|
|
|
(
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Partial([(outpoint.vout, txout)].into()),
|
2023-03-30 18:14:44 +08:00
|
|
|
BTreeSet::new(),
|
|
|
|
0,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
self.determine_additions(&update)
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Inserts the given [`TxOut`] at [`OutPoint`].
|
|
|
|
///
|
2023-04-17 23:25:57 +08:00
|
|
|
/// This is equivalent to calling [`insert_txout_preview`] and [`apply_additions`] in sequence.
|
|
|
|
///
|
|
|
|
/// [`insert_txout_preview`]: Self::insert_txout_preview
|
|
|
|
/// [`apply_additions`]: Self::apply_additions
|
2023-03-24 09:23:36 +08:00
|
|
|
pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) -> Additions<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
let additions = self.insert_txout_preview(outpoint, txout);
|
|
|
|
self.apply_additions(additions.clone());
|
|
|
|
additions
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:14:44 +08:00
|
|
|
/// Returns the resultant [`Additions`] if the given transaction is inserted. Does not actually
|
|
|
|
/// mutate [`Self`].
|
|
|
|
///
|
|
|
|
/// The [`Additions`] result will be empty if `tx` already exists in `self`.
|
|
|
|
pub fn insert_tx_preview(&self, tx: Transaction) -> Additions<A> {
|
|
|
|
let mut update = Self::default();
|
|
|
|
update
|
|
|
|
.txs
|
2023-03-30 18:33:53 +08:00
|
|
|
.insert(tx.txid(), (TxNodeInternal::Whole(tx), BTreeSet::new(), 0));
|
2023-03-30 18:14:44 +08:00
|
|
|
self.determine_additions(&update)
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Inserts the given transaction into [`TxGraph`].
|
|
|
|
///
|
|
|
|
/// The [`Additions`] returned will be empty if `tx` already exists.
|
2023-03-24 09:23:36 +08:00
|
|
|
pub fn insert_tx(&mut self, tx: Transaction) -> Additions<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
let additions = self.insert_tx_preview(tx);
|
|
|
|
self.apply_additions(additions.clone());
|
|
|
|
additions
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:14:44 +08:00
|
|
|
/// Returns the resultant [`Additions`] if the `txid` is set in `anchor`.
|
|
|
|
pub fn insert_anchor_preview(&self, txid: Txid, anchor: A) -> Additions<A> {
|
|
|
|
let mut update = Self::default();
|
|
|
|
update.anchors.insert((anchor, txid));
|
|
|
|
self.determine_additions(&update)
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
/// Inserts the given `anchor` into [`TxGraph`].
|
|
|
|
///
|
|
|
|
/// This is equivalent to calling [`insert_anchor_preview`] and [`apply_additions`] in sequence.
|
|
|
|
/// The [`Additions`] returned will be empty if graph already knows that `txid` exists in
|
|
|
|
/// `anchor`.
|
|
|
|
///
|
|
|
|
/// [`insert_anchor_preview`]: Self::insert_anchor_preview
|
|
|
|
/// [`apply_additions`]: Self::apply_additions
|
|
|
|
pub fn insert_anchor(&mut self, txid: Txid, anchor: A) -> Additions<A> {
|
|
|
|
let additions = self.insert_anchor_preview(txid, anchor);
|
|
|
|
self.apply_additions(additions.clone());
|
|
|
|
additions
|
|
|
|
}
|
|
|
|
|
2023-03-30 18:14:44 +08:00
|
|
|
/// Returns the resultant [`Additions`] if the `txid` is set to `seen_at`.
|
|
|
|
///
|
|
|
|
/// Note that [`TxGraph`] only keeps track of the lastest `seen_at`.
|
|
|
|
pub fn insert_seen_at_preview(&self, txid: Txid, seen_at: u64) -> Additions<A> {
|
|
|
|
let mut update = Self::default();
|
|
|
|
let (_, _, update_last_seen) = update.txs.entry(txid).or_default();
|
|
|
|
*update_last_seen = seen_at;
|
|
|
|
self.determine_additions(&update)
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
/// Inserts the given `seen_at` into [`TxGraph`].
|
|
|
|
///
|
|
|
|
/// This is equivalent to calling [`insert_seen_at_preview`] and [`apply_additions`] in
|
|
|
|
/// sequence.
|
|
|
|
///
|
|
|
|
/// [`insert_seen_at_preview`]: Self::insert_seen_at_preview
|
|
|
|
/// [`apply_additions`]: Self::apply_additions
|
|
|
|
pub fn insert_seen_at(&mut self, txid: Txid, seen_at: u64) -> Additions<A> {
|
|
|
|
let additions = self.insert_seen_at_preview(txid, seen_at);
|
|
|
|
self.apply_additions(additions.clone());
|
|
|
|
additions
|
|
|
|
}
|
|
|
|
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Extends this graph with another so that `self` becomes the union of the two sets of
|
|
|
|
/// transactions.
|
|
|
|
///
|
2023-03-10 23:23:29 +05:30
|
|
|
/// The returned [`Additions`] is the set difference between `update` and `self` (transactions that
|
2023-03-01 11:09:08 +01:00
|
|
|
/// exist in `update` but not in `self`).
|
2023-03-24 09:23:36 +08:00
|
|
|
pub fn apply_update(&mut self, update: TxGraph<A>) -> Additions<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
let additions = self.determine_additions(&update);
|
|
|
|
self.apply_additions(additions.clone());
|
|
|
|
additions
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Applies [`Additions`] to [`TxGraph`].
|
2023-03-24 09:23:36 +08:00
|
|
|
pub fn apply_additions(&mut self, additions: Additions<A>) {
|
2023-03-01 11:09:08 +01:00
|
|
|
for tx in additions.tx {
|
2023-03-08 11:39:25 +13:00
|
|
|
let txid = tx.txid();
|
2023-03-01 11:09:08 +01:00
|
|
|
|
2023-03-08 11:39:25 +13:00
|
|
|
tx.input
|
2023-03-01 11:09:08 +01:00
|
|
|
.iter()
|
|
|
|
.map(|txin| txin.previous_output)
|
|
|
|
// coinbase spends are not to be counted
|
|
|
|
.filter(|outpoint| !outpoint.is_null())
|
|
|
|
// record spend as this tx has spent this outpoint
|
|
|
|
.for_each(|outpoint| {
|
|
|
|
self.spends.entry(outpoint).or_default().insert(txid);
|
|
|
|
});
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
match self.txs.get_mut(&txid) {
|
2023-03-30 18:33:53 +08:00
|
|
|
Some((tx_node @ TxNodeInternal::Partial(_), _, _)) => {
|
|
|
|
*tx_node = TxNodeInternal::Whole(tx);
|
2023-03-24 09:23:36 +08:00
|
|
|
}
|
2023-03-30 18:33:53 +08:00
|
|
|
Some((TxNodeInternal::Whole(tx), _, _)) => {
|
2023-03-24 09:23:36 +08:00
|
|
|
debug_assert_eq!(
|
|
|
|
tx.txid(),
|
|
|
|
txid,
|
|
|
|
"tx should produce txid that is same as key"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.txs
|
2023-03-30 18:33:53 +08:00
|
|
|
.insert(txid, (TxNodeInternal::Whole(tx), BTreeSet::new(), 0));
|
2023-03-24 09:23:36 +08:00
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (outpoint, txout) in additions.txout {
|
|
|
|
let tx_entry = self
|
|
|
|
.txs
|
|
|
|
.entry(outpoint.txid)
|
2023-03-24 09:23:36 +08:00
|
|
|
.or_insert_with(Default::default);
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
match tx_entry {
|
2023-03-30 18:33:53 +08:00
|
|
|
(TxNodeInternal::Whole(_), _, _) => { /* do nothing since we already have full tx */
|
|
|
|
}
|
|
|
|
(TxNodeInternal::Partial(txouts), _, _) => {
|
2023-03-01 11:09:08 +01:00
|
|
|
txouts.insert(outpoint.vout, txout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 09:23:36 +08:00
|
|
|
|
|
|
|
for (anchor, txid) in additions.anchors {
|
|
|
|
if self.anchors.insert((anchor.clone(), txid)) {
|
|
|
|
let (_, anchors, _) = self.txs.entry(txid).or_insert_with(Default::default);
|
|
|
|
anchors.insert(anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (txid, new_last_seen) in additions.last_seen {
|
|
|
|
let (_, _, last_seen) = self.txs.entry(txid).or_insert_with(Default::default);
|
|
|
|
if new_last_seen > *last_seen {
|
|
|
|
*last_seen = new_last_seen;
|
|
|
|
}
|
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Previews the resultant [`Additions`] when [`Self`] is updated against the `update` graph.
|
|
|
|
///
|
2023-03-10 23:23:29 +05:30
|
|
|
/// The [`Additions`] would be the set difference between `update` and `self` (transactions that
|
2023-03-01 11:09:08 +01:00
|
|
|
/// exist in `update` but not in `self`).
|
2023-03-24 09:23:36 +08:00
|
|
|
pub fn determine_additions(&self, update: &TxGraph<A>) -> Additions<A> {
|
2023-03-08 11:39:25 +13:00
|
|
|
let mut additions = Additions::default();
|
2023-03-01 11:09:08 +01:00
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
for (&txid, (update_tx_node, _, update_last_seen)) in &update.txs {
|
|
|
|
let prev_last_seen: u64 = match (self.txs.get(&txid), update_tx_node) {
|
2023-03-30 18:33:53 +08:00
|
|
|
(None, TxNodeInternal::Whole(update_tx)) => {
|
2023-03-24 09:23:36 +08:00
|
|
|
additions.tx.insert(update_tx.clone());
|
|
|
|
0
|
|
|
|
}
|
2023-03-30 18:33:53 +08:00
|
|
|
(None, TxNodeInternal::Partial(update_txos)) => {
|
2023-03-24 09:23:36 +08:00
|
|
|
additions.txout.extend(
|
|
|
|
update_txos
|
|
|
|
.iter()
|
|
|
|
.map(|(&vout, txo)| (OutPoint::new(txid, vout), txo.clone())),
|
|
|
|
);
|
|
|
|
0
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
2023-03-30 18:33:53 +08:00
|
|
|
(Some((TxNodeInternal::Whole(_), _, last_seen)), _) => *last_seen,
|
|
|
|
(
|
|
|
|
Some((TxNodeInternal::Partial(_), _, last_seen)),
|
|
|
|
TxNodeInternal::Whole(update_tx),
|
|
|
|
) => {
|
2023-03-24 09:23:36 +08:00
|
|
|
additions.tx.insert(update_tx.clone());
|
|
|
|
*last_seen
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
2023-03-30 18:33:53 +08:00
|
|
|
(
|
|
|
|
Some((TxNodeInternal::Partial(txos), _, last_seen)),
|
|
|
|
TxNodeInternal::Partial(update_txos),
|
|
|
|
) => {
|
2023-03-24 09:23:36 +08:00
|
|
|
additions.txout.extend(
|
|
|
|
update_txos
|
|
|
|
.iter()
|
|
|
|
.filter(|(vout, _)| !txos.contains_key(*vout))
|
|
|
|
.map(|(&vout, txo)| (OutPoint::new(txid, vout), txo.clone())),
|
|
|
|
);
|
|
|
|
*last_seen
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if *update_last_seen > prev_last_seen {
|
|
|
|
additions.last_seen.insert(txid, *update_last_seen);
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
additions.anchors = update.anchors.difference(&self.anchors).cloned().collect();
|
|
|
|
|
2023-03-01 11:09:08 +01:00
|
|
|
additions
|
|
|
|
}
|
2023-03-30 18:14:44 +08:00
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
impl<A: Anchor> TxGraph<A> {
|
2023-03-26 11:24:30 +08:00
|
|
|
/// Get all heights that are relevant to the graph.
|
2023-04-19 16:14:52 +08:00
|
|
|
pub fn relevant_heights(&self) -> impl DoubleEndedIterator<Item = u32> + '_ {
|
2023-04-20 15:56:28 +08:00
|
|
|
let mut last_height = Option::<u32>::None;
|
2023-03-26 11:24:30 +08:00
|
|
|
self.anchors
|
|
|
|
.iter()
|
|
|
|
.map(|(a, _)| a.anchor_block().height)
|
2023-04-20 15:56:28 +08:00
|
|
|
.filter(move |&height| {
|
|
|
|
let is_unique = Some(height) != last_height;
|
|
|
|
if is_unique {
|
|
|
|
last_height = Some(height);
|
|
|
|
}
|
|
|
|
is_unique
|
|
|
|
})
|
2023-03-26 11:24:30 +08:00
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// Get the position of the transaction in `chain` with tip `chain_tip`.
|
|
|
|
///
|
|
|
|
/// If the given transaction of `txid` does not exist in the chain of `chain_tip`, `None` is
|
|
|
|
/// returned.
|
2023-03-24 15:47:39 +08:00
|
|
|
///
|
2023-04-17 23:25:57 +08:00
|
|
|
/// # Error
|
|
|
|
///
|
|
|
|
/// An error will occur if the [`ChainOracle`] implementation (`chain`) fails. If the
|
|
|
|
/// [`ChainOracle`] is infallible, [`get_chain_position`] can be used instead.
|
|
|
|
///
|
|
|
|
/// [`get_chain_position`]: Self::get_chain_position
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn try_get_chain_position<C: ChainOracle>(
|
2023-03-26 11:24:30 +08:00
|
|
|
&self,
|
2023-04-10 13:03:51 +08:00
|
|
|
chain: &C,
|
2023-04-17 23:25:57 +08:00
|
|
|
chain_tip: BlockId,
|
2023-03-26 11:24:30 +08:00
|
|
|
txid: Txid,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> Result<Option<ObservedAs<&A>>, C::Error> {
|
2023-03-24 15:47:39 +08:00
|
|
|
let (tx_node, anchors, &last_seen) = match self.txs.get(&txid) {
|
|
|
|
Some((tx, anchors, last_seen)) if !(anchors.is_empty() && *last_seen == 0) => {
|
|
|
|
(tx, anchors, last_seen)
|
|
|
|
}
|
2023-03-26 11:24:30 +08:00
|
|
|
_ => return Ok(None),
|
2023-03-24 15:47:39 +08:00
|
|
|
};
|
|
|
|
|
2023-03-26 11:24:30 +08:00
|
|
|
for anchor in anchors {
|
2023-04-17 23:25:57 +08:00
|
|
|
match chain.is_block_in_chain(anchor.anchor_block(), chain_tip)? {
|
2023-04-10 13:03:51 +08:00
|
|
|
Some(true) => return Ok(Some(ObservedAs::Confirmed(anchor))),
|
2023-04-17 23:25:57 +08:00
|
|
|
_ => continue,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 15:47:39 +08:00
|
|
|
// The tx is not anchored to a block which is in the best chain, let's check whether we can
|
|
|
|
// ignore it by checking conflicts!
|
|
|
|
let tx = match tx_node {
|
2023-03-30 18:33:53 +08:00
|
|
|
TxNodeInternal::Whole(tx) => tx,
|
|
|
|
TxNodeInternal::Partial(_) => {
|
2023-04-10 13:03:51 +08:00
|
|
|
// Partial transactions (outputs only) cannot have conflicts.
|
2023-03-26 11:24:30 +08:00
|
|
|
return Ok(None);
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-31 14:15:34 +08:00
|
|
|
// If a conflicting tx is in the best chain, or has `last_seen` higher than this tx, then
|
|
|
|
// this tx cannot exist in the best chain
|
2023-03-30 18:33:53 +08:00
|
|
|
for conflicting_tx in self.walk_conflicts(tx, |_, txid| self.get_tx_node(txid)) {
|
2023-04-10 13:03:51 +08:00
|
|
|
for block in conflicting_tx.anchors.iter().map(A::anchor_block) {
|
2023-04-17 23:25:57 +08:00
|
|
|
if chain.is_block_in_chain(block, chain_tip)? == Some(true) {
|
2023-03-24 15:47:39 +08:00
|
|
|
// conflicting tx is in best chain, so the current tx cannot be in best chain!
|
2023-03-26 11:24:30 +08:00
|
|
|
return Ok(None);
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-31 14:15:34 +08:00
|
|
|
if conflicting_tx.last_seen_unconfirmed > last_seen {
|
|
|
|
return Ok(None);
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
|
|
|
}
|
2023-03-31 14:15:34 +08:00
|
|
|
|
2023-04-05 16:39:54 +08:00
|
|
|
Ok(Some(ObservedAs::Unconfirmed(last_seen)))
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// Get the position of the transaction in `chain` with tip `chain_tip`.
|
|
|
|
///
|
|
|
|
/// This is the infallible version of [`try_get_chain_position`].
|
|
|
|
///
|
|
|
|
/// [`try_get_chain_position`]: Self::try_get_chain_position
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn get_chain_position<C: ChainOracle<Error = Infallible>>(
|
2023-04-10 13:03:51 +08:00
|
|
|
&self,
|
|
|
|
chain: &C,
|
2023-04-17 23:25:57 +08:00
|
|
|
chain_tip: BlockId,
|
2023-04-10 13:03:51 +08:00
|
|
|
txid: Txid,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> Option<ObservedAs<&A>> {
|
2023-04-17 23:25:57 +08:00
|
|
|
self.try_get_chain_position(chain, chain_tip, txid)
|
2023-03-27 13:59:51 +08:00
|
|
|
.expect("error is infallible")
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// 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_chain_spend`]: Self::get_chain_spend
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn try_get_chain_spend<C: ChainOracle>(
|
2023-03-26 11:24:30 +08:00
|
|
|
&self,
|
2023-04-10 13:03:51 +08:00
|
|
|
chain: &C,
|
2023-04-17 23:25:57 +08:00
|
|
|
chain_tip: BlockId,
|
2023-03-26 11:24:30 +08:00
|
|
|
outpoint: OutPoint,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> Result<Option<(ObservedAs<&A>, Txid)>, C::Error> {
|
2023-03-27 13:59:51 +08:00
|
|
|
if self
|
2023-04-17 23:25:57 +08:00
|
|
|
.try_get_chain_position(chain, chain_tip, outpoint.txid)?
|
2023-03-27 13:59:51 +08:00
|
|
|
.is_none()
|
|
|
|
{
|
2023-03-26 11:24:30 +08:00
|
|
|
return Ok(None);
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
|
|
|
if let Some(spends) = self.spends.get(&outpoint) {
|
|
|
|
for &txid in spends {
|
2023-04-17 23:25:57 +08:00
|
|
|
if let Some(observed_at) = self.try_get_chain_position(chain, chain_tip, txid)? {
|
2023-03-26 11:24:30 +08:00
|
|
|
return Ok(Some((observed_at, txid)));
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-26 11:24:30 +08:00
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:25:57 +08:00
|
|
|
/// 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`]
|
|
|
|
///
|
|
|
|
/// [`try_get_chain_spend`]: Self::try_get_chain_spend
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn get_chain_spend<C: ChainOracle<Error = Infallible>>(
|
2023-04-10 13:03:51 +08:00
|
|
|
&self,
|
|
|
|
chain: &C,
|
|
|
|
static_block: BlockId,
|
|
|
|
outpoint: OutPoint,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> Option<(ObservedAs<&A>, Txid)> {
|
2023-04-17 23:25:57 +08:00
|
|
|
self.try_get_chain_spend(chain, static_block, outpoint)
|
2023-03-27 13:59:51 +08:00
|
|
|
.expect("error is infallible")
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|
2023-04-17 23:25:57 +08:00
|
|
|
|
|
|
|
/// 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_chain_txs`]: Self::list_chain_txs
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn try_list_chain_txs<'a, C: ChainOracle + 'a>(
|
2023-04-17 23:25:57 +08:00
|
|
|
&'a self,
|
|
|
|
chain: &'a C,
|
|
|
|
chain_tip: BlockId,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> impl Iterator<Item = Result<CanonicalTx<'a, Transaction, A>, C::Error>> {
|
2023-04-17 23:25:57 +08:00
|
|
|
self.full_txs().filter_map(move |tx| {
|
|
|
|
self.try_get_chain_position(chain, chain_tip, tx.txid)
|
|
|
|
.map(|v| {
|
|
|
|
v.map(|observed_in| CanonicalTx {
|
|
|
|
observed_as: observed_in,
|
|
|
|
node: tx,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.transpose()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List graph transactions that are in `chain` with `chain_tip`.
|
|
|
|
///
|
|
|
|
/// This is the infallible version of [`try_list_chain_txs`].
|
|
|
|
///
|
|
|
|
/// [`try_list_chain_txs`]: Self::try_list_chain_txs
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn list_chain_txs<'a, C: ChainOracle + 'a>(
|
2023-04-17 23:25:57 +08:00
|
|
|
&'a self,
|
|
|
|
chain: &'a C,
|
|
|
|
chain_tip: BlockId,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> impl Iterator<Item = CanonicalTx<'a, Transaction, A>> {
|
2023-04-17 23:25:57 +08:00
|
|
|
self.try_list_chain_txs(chain, chain_tip)
|
|
|
|
.map(|r| r.expect("oracle is infallible"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List outputs that are in `chain` with `chain_tip`.
|
|
|
|
///
|
|
|
|
/// Floating ouputs are not iterated over.
|
|
|
|
///
|
|
|
|
/// The `filter_predicate` should return true for outputs that we wish to iterate over.
|
|
|
|
///
|
|
|
|
/// # Error
|
|
|
|
///
|
|
|
|
/// A returned item can error if the [`ChainOracle`] implementation (`chain`) fails.
|
|
|
|
///
|
|
|
|
/// If the [`ChainOracle`] is infallible, [`list_chain_txouts`] can be used instead.
|
|
|
|
///
|
|
|
|
/// [`list_chain_txouts`]: Self::list_chain_txouts
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn try_list_chain_txouts<'a, C: ChainOracle + 'a>(
|
2023-04-17 23:25:57 +08:00
|
|
|
&'a self,
|
|
|
|
chain: &'a C,
|
|
|
|
chain_tip: BlockId,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> impl Iterator<Item = Result<FullTxOut<ObservedAs<A>>, C::Error>> + 'a {
|
2023-04-17 23:25:57 +08:00
|
|
|
self.try_list_chain_txs(chain, chain_tip)
|
|
|
|
.flat_map(move |tx_res| match tx_res {
|
|
|
|
Ok(canonical_tx) => canonical_tx
|
|
|
|
.node
|
|
|
|
.output
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2023-04-20 18:07:26 +08:00
|
|
|
.map(|(vout, txout)| {
|
2023-04-17 23:25:57 +08:00
|
|
|
let outpoint = OutPoint::new(canonical_tx.node.txid, vout as _);
|
2023-04-20 18:07:26 +08:00
|
|
|
Ok((outpoint, txout.clone(), canonical_tx.clone()))
|
2023-04-17 23:25:57 +08:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
Err(err) => vec![Err(err)],
|
|
|
|
})
|
|
|
|
.map(move |res| -> Result<_, C::Error> {
|
|
|
|
let (
|
|
|
|
outpoint,
|
|
|
|
txout,
|
|
|
|
CanonicalTx {
|
|
|
|
observed_as,
|
|
|
|
node: tx_node,
|
|
|
|
},
|
|
|
|
) = res?;
|
|
|
|
let chain_position = observed_as.cloned();
|
|
|
|
let spent_by = self
|
|
|
|
.try_get_chain_spend(chain, chain_tip, outpoint)?
|
|
|
|
.map(|(obs_as, txid)| (obs_as.cloned(), txid));
|
|
|
|
let is_on_coinbase = tx_node.tx.is_coin_base();
|
|
|
|
Ok(FullTxOut {
|
|
|
|
outpoint,
|
|
|
|
txout,
|
|
|
|
chain_position,
|
|
|
|
spent_by,
|
|
|
|
is_on_coinbase,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List outputs that are in `chain` with `chain_tip`.
|
|
|
|
///
|
|
|
|
/// This is the infallible version of [`try_list_chain_txouts`].
|
|
|
|
///
|
|
|
|
/// [`try_list_chain_txouts`]: Self::try_list_chain_txouts
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn list_chain_txouts<'a, C: ChainOracle<Error = Infallible> + 'a>(
|
2023-04-17 23:25:57 +08:00
|
|
|
&'a self,
|
|
|
|
chain: &'a C,
|
|
|
|
chain_tip: BlockId,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> impl Iterator<Item = FullTxOut<ObservedAs<A>>> + 'a {
|
|
|
|
self.try_list_chain_txouts(chain, chain_tip)
|
2023-04-17 23:25:57 +08:00
|
|
|
.map(|r| r.expect("error in infallible"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List unspent outputs (UTXOs) that are in `chain` with `chain_tip`.
|
|
|
|
///
|
|
|
|
/// Floating outputs are not iterated over.
|
|
|
|
///
|
|
|
|
/// # Error
|
|
|
|
///
|
|
|
|
/// An item can be an error if the [`ChainOracle`] implementation fails. If the oracle is
|
|
|
|
/// infallible, [`list_chain_unspents`] can be used instead.
|
|
|
|
///
|
|
|
|
/// [`list_chain_unspents`]: Self::list_chain_unspents
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn try_list_chain_unspents<'a, C: ChainOracle + 'a>(
|
2023-04-17 23:25:57 +08:00
|
|
|
&'a self,
|
|
|
|
chain: &'a C,
|
|
|
|
chain_tip: BlockId,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> impl Iterator<Item = Result<FullTxOut<ObservedAs<A>>, C::Error>> + 'a {
|
|
|
|
self.try_list_chain_txouts(chain, chain_tip)
|
2023-04-17 23:25:57 +08:00
|
|
|
.filter(|r| !matches!(r, Ok(txo) if txo.spent_by.is_none()))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// List unspent outputs (UTXOs) that are in `chain` with `chain_tip`.
|
|
|
|
///
|
|
|
|
/// This is the infallible version of [`try_list_chain_unspents`].
|
|
|
|
///
|
|
|
|
/// [`try_list_chain_unspents`]: Self::try_list_chain_unspents
|
2023-04-20 18:07:26 +08:00
|
|
|
pub fn list_chain_unspents<'a, C: ChainOracle<Error = Infallible> + 'a>(
|
2023-04-17 23:25:57 +08:00
|
|
|
&'a self,
|
|
|
|
chain: &'a C,
|
|
|
|
static_block: BlockId,
|
2023-04-20 18:07:26 +08:00
|
|
|
) -> impl Iterator<Item = FullTxOut<ObservedAs<A>>> + 'a {
|
|
|
|
self.try_list_chain_unspents(chain, static_block)
|
2023-04-17 23:25:57 +08:00
|
|
|
.map(|r| r.expect("error is infallible"))
|
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A structure that represents changes to a [`TxGraph`].
|
|
|
|
///
|
2023-03-10 23:23:29 +05:30
|
|
|
/// It is named "additions" because [`TxGraph`] is monotone, so transactions can only be added and
|
2023-03-01 11:09:08 +01:00
|
|
|
/// not removed.
|
|
|
|
///
|
|
|
|
/// Refer to [module-level documentation] for more.
|
|
|
|
///
|
|
|
|
/// [module-level documentation]: crate::tx_graph
|
2023-03-24 09:23:36 +08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2023-03-01 11:09:08 +01:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "serde",
|
|
|
|
derive(serde::Deserialize, serde::Serialize),
|
2023-03-24 09:23:36 +08:00
|
|
|
serde(
|
|
|
|
crate = "serde_crate",
|
|
|
|
bound(
|
|
|
|
deserialize = "A: Ord + serde::Deserialize<'de>",
|
|
|
|
serialize = "A: Ord + serde::Serialize",
|
|
|
|
)
|
|
|
|
)
|
2023-03-01 11:09:08 +01:00
|
|
|
)]
|
|
|
|
#[must_use]
|
2023-04-21 13:29:44 +08:00
|
|
|
pub struct Additions<A = ()> {
|
2023-03-08 11:39:25 +13:00
|
|
|
pub tx: BTreeSet<Transaction>,
|
2023-03-01 11:09:08 +01:00
|
|
|
pub txout: BTreeMap<OutPoint, TxOut>,
|
2023-03-24 09:23:36 +08:00
|
|
|
pub anchors: BTreeSet<(A, Txid)>,
|
|
|
|
pub last_seen: BTreeMap<Txid, u64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A> Default for Additions<A> {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
tx: Default::default(),
|
|
|
|
txout: Default::default(),
|
|
|
|
anchors: Default::default(),
|
|
|
|
last_seen: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<A> Additions<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Returns true if the [`Additions`] is empty (no transactions or txouts).
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.tx.is_empty() && self.txout.is_empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterates over all outpoints contained within [`Additions`].
|
2023-03-08 11:39:25 +13:00
|
|
|
pub fn txouts(&self) -> impl Iterator<Item = (OutPoint, &TxOut)> {
|
2023-03-01 11:09:08 +01:00
|
|
|
self.tx
|
|
|
|
.iter()
|
|
|
|
.flat_map(|tx| {
|
2023-03-08 11:39:25 +13:00
|
|
|
tx.output
|
2023-03-01 11:09:08 +01:00
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2023-03-08 11:39:25 +13:00
|
|
|
.map(move |(vout, txout)| (OutPoint::new(tx.txid(), vout as _), txout))
|
2023-03-01 11:09:08 +01:00
|
|
|
})
|
|
|
|
.chain(self.txout.iter().map(|(op, txout)| (*op, txout)))
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:23:29 +05:30
|
|
|
/// Appends the changes in `other` into self such that applying `self` afterward has the same
|
2023-03-01 11:09:08 +01:00
|
|
|
/// effect as sequentially applying the original `self` and `other`.
|
2023-04-26 01:09:19 +08:00
|
|
|
pub fn append(&mut self, mut other: Additions<A>)
|
|
|
|
where
|
|
|
|
A: Ord,
|
|
|
|
{
|
2023-03-01 11:09:08 +01:00
|
|
|
self.tx.append(&mut other.tx);
|
|
|
|
self.txout.append(&mut other.txout);
|
2023-04-26 01:09:19 +08:00
|
|
|
self.anchors.append(&mut other.anchors);
|
|
|
|
self.last_seen.append(&mut other.last_seen);
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<A> AsRef<TxGraph<A>> for TxGraph<A> {
|
|
|
|
fn as_ref(&self) -> &TxGraph<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<A> ForEachTxOut for Additions<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) {
|
|
|
|
self.txouts().for_each(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<A> ForEachTxOut for TxGraph<A> {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) {
|
|
|
|
self.all_txouts().for_each(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// An iterator that traverses transaction descendants.
|
|
|
|
///
|
|
|
|
/// This `struct` is created by the [`walk_descendants`] method of [`TxGraph`].
|
|
|
|
///
|
|
|
|
/// [`walk_descendants`]: TxGraph::walk_descendants
|
2023-03-24 09:23:36 +08:00
|
|
|
pub struct TxDescendants<'g, A, F> {
|
|
|
|
graph: &'g TxGraph<A>,
|
2023-03-01 11:09:08 +01:00
|
|
|
visited: HashSet<Txid>,
|
|
|
|
stack: Vec<(usize, Txid)>,
|
|
|
|
filter_map: F,
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<'g, A, F> TxDescendants<'g, A, F> {
|
2023-03-01 11:09:08 +01:00
|
|
|
/// Creates a `TxDescendants` that includes the starting `txid` when iterating.
|
|
|
|
#[allow(unused)]
|
2023-03-24 09:23:36 +08:00
|
|
|
pub(crate) fn new_include_root(graph: &'g TxGraph<A>, txid: Txid, filter_map: F) -> Self {
|
2023-03-01 11:09:08 +01:00
|
|
|
Self {
|
|
|
|
graph,
|
|
|
|
visited: Default::default(),
|
|
|
|
stack: [(0, txid)].into(),
|
|
|
|
filter_map,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a `TxDescendants` that excludes the starting `txid` when iterating.
|
2023-03-24 09:23:36 +08:00
|
|
|
pub(crate) fn new_exclude_root(graph: &'g TxGraph<A>, txid: Txid, filter_map: F) -> Self {
|
2023-03-01 11:09:08 +01:00
|
|
|
let mut descendants = Self {
|
|
|
|
graph,
|
|
|
|
visited: Default::default(),
|
|
|
|
stack: Default::default(),
|
|
|
|
filter_map,
|
|
|
|
};
|
|
|
|
descendants.populate_stack(1, txid);
|
|
|
|
descendants
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:23:29 +05:30
|
|
|
/// Creates a `TxDescendants` from multiple starting transactions that include the starting
|
2023-03-01 11:09:08 +01:00
|
|
|
/// `txid`s when iterating.
|
2023-03-24 09:23:36 +08:00
|
|
|
pub(crate) fn from_multiple_include_root<I>(
|
|
|
|
graph: &'g TxGraph<A>,
|
|
|
|
txids: I,
|
|
|
|
filter_map: F,
|
|
|
|
) -> Self
|
2023-03-01 11:09:08 +01:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Txid>,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
graph,
|
|
|
|
visited: Default::default(),
|
|
|
|
stack: txids.into_iter().map(|txid| (0, txid)).collect(),
|
|
|
|
filter_map,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a `TxDescendants` from multiple starting transactions that excludes the starting
|
|
|
|
/// `txid`s when iterating.
|
|
|
|
#[allow(unused)]
|
2023-03-24 09:23:36 +08:00
|
|
|
pub(crate) fn from_multiple_exclude_root<I>(
|
|
|
|
graph: &'g TxGraph<A>,
|
|
|
|
txids: I,
|
|
|
|
filter_map: F,
|
|
|
|
) -> Self
|
2023-03-01 11:09:08 +01:00
|
|
|
where
|
|
|
|
I: IntoIterator<Item = Txid>,
|
|
|
|
{
|
|
|
|
let mut descendants = Self {
|
|
|
|
graph,
|
|
|
|
visited: Default::default(),
|
|
|
|
stack: Default::default(),
|
|
|
|
filter_map,
|
|
|
|
};
|
|
|
|
for txid in txids {
|
|
|
|
descendants.populate_stack(1, txid);
|
|
|
|
}
|
|
|
|
descendants
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<'g, A, F> TxDescendants<'g, A, F> {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn populate_stack(&mut self, depth: usize, txid: Txid) {
|
|
|
|
let spend_paths = self
|
|
|
|
.graph
|
|
|
|
.spends
|
|
|
|
.range(tx_outpoint_range(txid))
|
|
|
|
.flat_map(|(_, spends)| spends)
|
|
|
|
.map(|&txid| (depth, txid));
|
|
|
|
self.stack.extend(spend_paths);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl<'g, A, F, O> Iterator for TxDescendants<'g, A, F>
|
2023-03-01 11:09:08 +01:00
|
|
|
where
|
|
|
|
F: FnMut(usize, Txid) -> Option<O>,
|
|
|
|
{
|
|
|
|
type Item = O;
|
|
|
|
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
let (op_spends, txid, item) = loop {
|
|
|
|
// we have exhausted all paths when stack is empty
|
|
|
|
let (op_spends, txid) = self.stack.pop()?;
|
|
|
|
// we do not want to visit the same transaction twice
|
|
|
|
if self.visited.insert(txid) {
|
|
|
|
// ignore paths when user filters them out
|
|
|
|
if let Some(item) = (self.filter_map)(op_spends, txid) {
|
|
|
|
break (op_spends, txid, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.populate_stack(op_spends + 1, txid);
|
2023-03-02 19:08:33 +01:00
|
|
|
Some(item)
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn tx_outpoint_range(txid: Txid) -> RangeInclusive<OutPoint> {
|
|
|
|
OutPoint::new(txid, u32::MIN)..=OutPoint::new(txid, u32::MAX)
|
|
|
|
}
|