diff --git a/crates/chain/src/indexed_tx_graph.rs b/crates/chain/src/indexed_tx_graph.rs index 574afc1b..0cee62ed 100644 --- a/crates/chain/src/indexed_tx_graph.rs +++ b/crates/chain/src/indexed_tx_graph.rs @@ -5,55 +5,9 @@ use bitcoin::{OutPoint, Script, Transaction, TxOut}; use crate::{ keychain::Balance, tx_graph::{Additions, TxGraph, TxNode}, - Append, BlockAnchor, BlockId, ChainOracle, FullTxOut, ObservedAs, TxIndex, + Append, BlockAnchor, BlockId, ChainOracle, FullTxOut, ObservedAs, }; -/// 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> { - /// Where the transaction is observed (in a block or in mempool). - pub observed_as: ObservedAs<&'a A>, - /// The transaction with anchors and last seen timestamp. - pub tx: TxNode<'a, T, A>, -} - -/// A structure that represents changes to an [`IndexedTxGraph`]. -#[derive(Clone, Debug, PartialEq)] -#[cfg_attr( - feature = "serde", - derive(serde::Deserialize, serde::Serialize), - serde( - crate = "serde_crate", - bound( - deserialize = "A: Ord + serde::Deserialize<'de>, IA: serde::Deserialize<'de>", - serialize = "A: Ord + serde::Serialize, IA: serde::Serialize" - ) - ) -)] -#[must_use] -pub struct IndexedAdditions { - /// [`TxGraph`] additions. - pub graph_additions: Additions, - /// [`TxIndex`] additions. - pub index_additions: IA, -} - -impl Default for IndexedAdditions { - fn default() -> Self { - Self { - graph_additions: Default::default(), - index_additions: Default::default(), - } - } -} - -impl Append for IndexedAdditions { - fn append(&mut self, other: Self) { - self.graph_additions.append(other.graph_additions); - self.index_additions.append(other.index_additions); - } -} - pub struct IndexedTxGraph { /// Transaction index. pub index: I, @@ -367,3 +321,72 @@ impl IndexedTxGraph { .expect("error is infallible") } } + +/// A structure that represents changes to an [`IndexedTxGraph`]. +#[derive(Clone, Debug, PartialEq)] +#[cfg_attr( + feature = "serde", + derive(serde::Deserialize, serde::Serialize), + serde( + crate = "serde_crate", + bound( + deserialize = "A: Ord + serde::Deserialize<'de>, IA: serde::Deserialize<'de>", + serialize = "A: Ord + serde::Serialize, IA: serde::Serialize" + ) + ) +)] +#[must_use] +pub struct IndexedAdditions { + /// [`TxGraph`] additions. + pub graph_additions: Additions, + /// [`TxIndex`] additions. + pub index_additions: IA, +} + +impl Default for IndexedAdditions { + fn default() -> Self { + Self { + graph_additions: Default::default(), + index_additions: Default::default(), + } + } +} + +impl Append for IndexedAdditions { + fn append(&mut self, other: Self) { + self.graph_additions.append(other.graph_additions); + self.index_additions.append(other.index_additions); + } +} + +/// 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> { + /// Where the transaction is observed (in a block or in mempool). + pub observed_as: ObservedAs<&'a A>, + /// The transaction with anchors and last seen timestamp. + pub tx: TxNode<'a, T, A>, +} + +/// Represents an index of transaction data. +pub trait TxIndex { + /// The resultant "additions" when new transaction data is indexed. + type Additions; + + /// Scan and index the given `outpoint` and `txout`. + fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::Additions; + + /// Scan and index the given transaction. + fn index_tx(&mut self, tx: &Transaction) -> Self::Additions; + + /// Apply additions to itself. + fn apply_additions(&mut self, additions: Self::Additions); + + /// Returns whether the txout is marked as relevant in the index. + fn is_txout_relevant(&self, outpoint: OutPoint, txout: &TxOut) -> bool; + + /// Returns whether the transaction is marked as relevant in the index. + fn is_tx_relevant(&self, tx: &Transaction) -> bool; +} + +pub trait SpkIndex: TxIndex {} diff --git a/crates/chain/src/keychain/txout_index.rs b/crates/chain/src/keychain/txout_index.rs index 7dd570a6..43623a3e 100644 --- a/crates/chain/src/keychain/txout_index.rs +++ b/crates/chain/src/keychain/txout_index.rs @@ -1,7 +1,8 @@ use crate::{ collections::*, + indexed_tx_graph::TxIndex, miniscript::{Descriptor, DescriptorPublicKey}, - ForEachTxOut, SpkTxOutIndex, TxIndex, + ForEachTxOut, SpkTxOutIndex, }; use alloc::{borrow::Cow, vec::Vec}; use bitcoin::{secp256k1::Secp256k1, OutPoint, Script, TxOut}; diff --git a/crates/chain/src/spk_txout_index.rs b/crates/chain/src/spk_txout_index.rs index 20be073a..03b7b6a7 100644 --- a/crates/chain/src/spk_txout_index.rs +++ b/crates/chain/src/spk_txout_index.rs @@ -2,7 +2,8 @@ use core::ops::RangeBounds; use crate::{ collections::{hash_map::Entry, BTreeMap, BTreeSet, HashMap}, - ForEachTxOut, TxIndex, + indexed_tx_graph::TxIndex, + ForEachTxOut, }; use bitcoin::{self, OutPoint, Script, Transaction, TxOut, Txid}; diff --git a/crates/chain/src/tx_data_traits.rs b/crates/chain/src/tx_data_traits.rs index 1399ebeb..401b00f9 100644 --- a/crates/chain/src/tx_data_traits.rs +++ b/crates/chain/src/tx_data_traits.rs @@ -68,24 +68,3 @@ pub trait Append { impl Append for () { fn append(&mut self, _other: Self) {} } - -/// Represents an index of transaction data. -pub trait TxIndex { - /// The resultant "additions" when new transaction data is indexed. - type Additions; - - /// Scan and index the given `outpoint` and `txout`. - fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::Additions; - - /// Scan and index the given transaction. - fn index_tx(&mut self, tx: &Transaction) -> Self::Additions; - - /// Apply additions to itself. - fn apply_additions(&mut self, additions: Self::Additions); - - /// Returns whether the txout is marked as relevant in the index. - fn is_txout_relevant(&self, outpoint: OutPoint, txout: &TxOut) -> bool; - - /// Returns whether the transaction is marked as relevant in the index. - fn is_tx_relevant(&self, tx: &Transaction) -> bool; -}