11//! Module for structures that store and traverse transactions.
22//!
3- //! [`TxGraph`] is a monotone structure that inserts transactions and indexes the spends. The
4- //! [`ChangeSet`] structure reports changes of [`TxGraph`] but can also be applied to a
5- //! [`TxGraph`] as well. Lastly, [`TxDescendants`] is an [`Iterator`] that traverses descendants of
6- //! a given transaction.
3+ //! [`TxGraph`] contains transactions and indexes them so you can easily traverse the graph of those transactions.
4+ //! `TxGraph` is *monotone* in that you can always insert a transaction -- it doesn't care whether that
5+ //! transaction is in the current best chain or whether it conflicts with any of the
6+ //! existing transactions or what order you insert the transactions. This means that you can always
7+ //! combine two [`TxGraph`]s together, without resulting in inconsistencies.
8+ //! Furthermore, there is currently no way to delete a transaction.
9+ //!
10+ //! Transactions can be either whole or partial (i.e., transactions for which we only
11+ //! know some outputs, which we usually call "floating outputs"; these are usually inserted
12+ //! using the [`insert_txout`] method.).
13+ //!
14+ //! The graph contains transactions in the form of [`TxNode`]s. Each node contains the
15+ //! txid, the transaction (whole or partial), the blocks it's anchored in (see the [`Anchor`]
16+ //! documentation for more details), and the timestamp of the last time we saw
17+ //! the transaction as unconfirmed.
718//!
819//! Conflicting transactions are allowed to coexist within a [`TxGraph`]. This is useful for
9- //! identifying and traversing conflicts and descendants of a given transaction.
20+ //! identifying and traversing conflicts and descendants of a given transaction. Some [`TxGraph`]
21+ //! methods only consider "canonical" (i.e., in the best chain or in mempool) transactions,
22+ //! we decide which transactions are canonical based on anchors `last_seen_unconfirmed`;
23+ //! see the [`try_get_chain_position`] documentation for more details.
24+ //!
25+ //! The [`ChangeSet`] reports changes made to a [`TxGraph`]; it can be used to either save to
26+ //! persistent storage, or to be applied to another [`TxGraph`].
27+ //!
28+ //! Lastly, you can use [`TxAncestors`]/[`TxDescendants`] to traverse ancestors and descendants of
29+ //! a given transaction, respectively.
1030//!
1131//! # Applying changes
1232//!
4969//! let changeset = graph.apply_update(update);
5070//! assert!(changeset.is_empty());
5171//! ```
72+ //! [`try_get_chain_position`]: TxGraph::try_get_chain_position
73+ //! [`insert_txout`]: TxGraph::insert_txout
5274
5375use crate :: {
5476 collections:: * , keychain:: Balance , local_chain:: LocalChain , Anchor , Append , BlockId ,
@@ -91,7 +113,7 @@ impl<A> Default for TxGraph<A> {
91113 }
92114}
93115
94- /// An outward-facing view of a ( transaction) node in the [`TxGraph`].
116+ /// A transaction node in the [`TxGraph`].
95117#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
96118pub struct TxNode < ' a , T , A > {
97119 /// Txid of the transaction.
@@ -128,7 +150,7 @@ impl Default for TxNodeInternal {
128150 }
129151}
130152
131- /// An outwards-facing view of a transaction that is part of the *best chain*'s history .
153+ /// A transaction that is included in the chain, or is still in mempool .
132154#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
133155pub struct CanonicalTx < ' a , T , A > {
134156 /// How the transaction is observed as (confirmed or unconfirmed).
@@ -475,7 +497,7 @@ impl<A: Clone + Ord> TxGraph<A> {
475497 /// Batch insert unconfirmed transactions.
476498 ///
477499 /// Items of `txs` are tuples containing the transaction and a *last seen* timestamp. The
478- /// *last seen* communicates when the transaction is last seen in the mempool which is used for
500+ /// *last seen* communicates when the transaction is last seen in mempool which is used for
479501 /// conflict-resolution (refer to [`TxGraph::insert_seen_at`] for details).
480502 pub fn batch_insert_unconfirmed (
481503 & mut self ,
@@ -708,8 +730,20 @@ impl<A: Anchor> TxGraph<A> {
708730
709731 /// Get the position of the transaction in `chain` with tip `chain_tip`.
710732 ///
711- /// If the given transaction of `txid` does not exist in the chain of `chain_tip`, `None` is
712- /// returned.
733+ /// Chain data is fetched from `chain`, a [`ChainOracle`] implementation.
734+ ///
735+ /// This method returns `Ok(None)` if the transaction is not found in the chain, and no longer
736+ /// belongs in the mempool. The following factors are used to approximate whether an
737+ /// unconfirmed transaction exists in the mempool (not evicted):
738+ ///
739+ /// 1. Unconfirmed transactions that conflict with confirmed transactions are evicted.
740+ /// 2. Unconfirmed transactions that spend from transactions that are evicted, are also
741+ /// evicted.
742+ /// 3. Given two conflicting unconfirmed transactions, the transaction with the lower
743+ /// `last_seen_unconfirmed` parameter is evicted. A transaction's `last_seen_unconfirmed`
744+ /// parameter is the max of all it's descendants' `last_seen_unconfirmed` parameters. If the
745+ /// final `last_seen_unconfirmed`s are the same, the transaction with the lower `txid` (by
746+ /// lexicographical order) is evicted.
713747 ///
714748 /// # Error
715749 ///
@@ -735,7 +769,7 @@ impl<A: Anchor> TxGraph<A> {
735769 }
736770 }
737771
738- // The tx is not anchored to a block which is in the best chain, which means that it
772+ // The tx is not anchored to a block in the best chain, which means that it
739773 // might be in mempool, or it might have been dropped already.
740774 // Let's check conflicts to find out!
741775 let tx = match tx_node {
@@ -945,7 +979,8 @@ impl<A: Anchor> TxGraph<A> {
945979 /// (`OI`) for convenience. If `OI` is not necessary, the caller can use `()`, or
946980 /// [`Iterator::enumerate`] over a list of [`OutPoint`]s.
947981 ///
948- /// Floating outputs are ignored.
982+ /// Floating outputs (i.e., outputs for which we don't have the full transaction in the graph)
983+ /// are ignored.
949984 ///
950985 /// # Error
951986 ///
@@ -1272,7 +1307,7 @@ impl<A> AsRef<TxGraph<A>> for TxGraph<A> {
12721307///
12731308/// The iterator excludes partial transactions.
12741309///
1275- /// This ` struct` is created by the [`walk_ancestors`] method of [`TxGraph`].
1310+ /// This struct is created by the [`walk_ancestors`] method of [`TxGraph`].
12761311///
12771312/// [`walk_ancestors`]: TxGraph::walk_ancestors
12781313pub struct TxAncestors < ' g , A , F > {
0 commit comments