]> Untitled Git - bdk/commitdiff
[bdk_chain_redesign] MOVE `TxIndex` into `indexed_chain_graph.rs`
author志宇 <hello@evanlinjin.me>
Wed, 12 Apr 2023 03:24:05 +0000 (11:24 +0800)
committer志宇 <hello@evanlinjin.me>
Wed, 12 Apr 2023 03:24:05 +0000 (11:24 +0800)
`tx_graph.rs` is rearranged as well.

crates/chain/src/indexed_tx_graph.rs
crates/chain/src/keychain/txout_index.rs
crates/chain/src/spk_txout_index.rs
crates/chain/src/tx_data_traits.rs

index 574afc1b8e605ec90a51198b18b6f10413a5b522..0cee62edc8f39663276923efd9d20d5761c631d8 100644 (file)
@@ -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<A, IA> {
-    /// [`TxGraph`] additions.
-    pub graph_additions: Additions<A>,
-    /// [`TxIndex`] additions.
-    pub index_additions: IA,
-}
-
-impl<A, IA: Default> Default for IndexedAdditions<A, IA> {
-    fn default() -> Self {
-        Self {
-            graph_additions: Default::default(),
-            index_additions: Default::default(),
-        }
-    }
-}
-
-impl<A: BlockAnchor, IA: Append> Append for IndexedAdditions<A, IA> {
-    fn append(&mut self, other: Self) {
-        self.graph_additions.append(other.graph_additions);
-        self.index_additions.append(other.index_additions);
-    }
-}
-
 pub struct IndexedTxGraph<A, I> {
     /// Transaction index.
     pub index: I,
@@ -367,3 +321,72 @@ impl<A: BlockAnchor, I: TxIndex> IndexedTxGraph<A, I> {
             .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<A, IA> {
+    /// [`TxGraph`] additions.
+    pub graph_additions: Additions<A>,
+    /// [`TxIndex`] additions.
+    pub index_additions: IA,
+}
+
+impl<A, IA: Default> Default for IndexedAdditions<A, IA> {
+    fn default() -> Self {
+        Self {
+            graph_additions: Default::default(),
+            index_additions: Default::default(),
+        }
+    }
+}
+
+impl<A: BlockAnchor, IA: Append> Append for IndexedAdditions<A, IA> {
+    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 {}
index 7dd570a6360171f80750ba55ccb052768b47de57..43623a3eff4b580326b3348c93ea0a5da345355c 100644 (file)
@@ -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};
index 20be073ae5ffc456667e61b0823e281da835092d..03b7b6a7fdefb6820b0774ca7688aa26438427be 100644 (file)
@@ -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};
 
index 1399ebeb1b4b29de98f5bc094450193e6e1ced3c..401b00f920e2a778a551af8251db33968f5253d3 100644 (file)
@@ -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;
-}