]> Untitled Git - bdk/commitdiff
refactor(chain)!: Replace trait `AnchorFromBlockPosition` with new struct
authorJiri Jakes <jiri@jirijakes.com>
Sun, 8 Sep 2024 08:05:30 +0000 (16:05 +0800)
committerJiri Jakes <jiri@jirijakes.com>
Sun, 29 Sep 2024 04:25:20 +0000 (12:25 +0800)
This change replaces a way of creating new generic anchor from block,
its height and transaction position. Previous way was using conversion
trait, newly it is a struct and `From`.

crates/chain/src/indexed_tx_graph.rs
crates/chain/src/tx_data_traits.rs

index ed2a1f0ce722b6744ff05f7af09fa5d5780677b2..828dc51999755b7fd38659680e0a839bade8a4e6 100644 (file)
@@ -7,7 +7,7 @@ use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid};
 
 use crate::{
     tx_graph::{self, TxGraph},
-    Anchor, AnchorFromBlockPosition, BlockId, Indexer, Merge,
+    Anchor, BlockId, Indexer, Merge, TxPosInBlock,
 };
 
 /// The [`IndexedTxGraph`] combines a [`TxGraph`] and an [`Indexer`] implementation.
@@ -252,17 +252,17 @@ where
     }
 }
 
-/// Methods are available if the anchor (`A`) implements [`AnchorFromBlockPosition`].
-impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
+/// Methods are available if the anchor (`A`) can be created from [`TxPosInBlock`].
+impl<A, I> IndexedTxGraph<A, I>
 where
     I::ChangeSet: Default + Merge,
-    A: AnchorFromBlockPosition,
+    for<'b> A: Anchor + From<TxPosInBlock<'b>>,
+    I: Indexer,
 {
     /// Batch insert all transactions of the given `block` of `height`, filtering out those that are
     /// irrelevant.
     ///
-    /// Each inserted transaction's anchor will be constructed from
-    /// [`AnchorFromBlockPosition::from_block_position`].
+    /// Each inserted transaction's anchor will be constructed using [`TxPosInBlock`].
     ///
     /// Relevancy is determined by the internal [`Indexer::is_tx_relevant`] implementation of `I`.
     /// Irrelevant transactions in `txs` will be ignored.
@@ -280,7 +280,12 @@ where
             changeset.indexer.merge(self.index.index_tx(tx));
             if self.index.is_tx_relevant(tx) {
                 let txid = tx.compute_txid();
-                let anchor = A::from_block_position(block, block_id, tx_pos);
+                let anchor = TxPosInBlock {
+                    block,
+                    block_id,
+                    tx_pos,
+                }
+                .into();
                 changeset.tx_graph.merge(self.graph.insert_tx(tx.clone()));
                 changeset
                     .tx_graph
@@ -292,8 +297,7 @@ where
 
     /// Batch insert all transactions of the given `block` of `height`.
     ///
-    /// Each inserted transaction's anchor will be constructed from
-    /// [`AnchorFromBlockPosition::from_block_position`].
+    /// Each inserted transaction's anchor will be constructed using [`TxPosInBlock`].
     ///
     /// To only insert relevant transactions, use [`apply_block_relevant`] instead.
     ///
@@ -305,7 +309,12 @@ where
         };
         let mut graph = tx_graph::ChangeSet::default();
         for (tx_pos, tx) in block.txdata.iter().enumerate() {
-            let anchor = A::from_block_position(&block, block_id, tx_pos);
+            let anchor = TxPosInBlock {
+                block: &block,
+                block_id,
+                tx_pos,
+            }
+            .into();
             graph.merge(self.graph.insert_anchor(tx.compute_txid(), anchor));
             graph.merge(self.graph.insert_tx(tx.clone()));
         }
index d3d562bf30c72ba70af6290598097c26b3d61969..ebf1e080ad2a76f5af989f2d1edf457be82d4494 100644 (file)
@@ -100,24 +100,31 @@ impl Anchor for ConfirmationBlockTime {
     }
 }
 
-/// An [`Anchor`] that can be constructed from a given block, block height and transaction position
-/// within the block.
-pub trait AnchorFromBlockPosition: Anchor {
-    /// Construct the anchor from a given `block`, block height and `tx_pos` within the block.
-    fn from_block_position(block: &bitcoin::Block, block_id: BlockId, tx_pos: usize) -> Self;
+/// Set of parameters sufficient to construct an [`Anchor`].
+///
+/// Typically used as an additional constraint on anchor:
+/// `for<'b> A: Anchor + From<TxPosInBlock<'b>>`.
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub struct TxPosInBlock<'b> {
+    /// Block in which the transaction appeared.
+    pub block: &'b bitcoin::Block,
+    /// Block's [`BlockId`].
+    pub block_id: BlockId,
+    /// Position in the block on which the transaction appeared.
+    pub tx_pos: usize,
 }
 
-impl AnchorFromBlockPosition for BlockId {
-    fn from_block_position(_block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
-        block_id
+impl<'b> From<TxPosInBlock<'b>> for BlockId {
+    fn from(pos: TxPosInBlock) -> Self {
+        pos.block_id
     }
 }
 
-impl AnchorFromBlockPosition for ConfirmationBlockTime {
-    fn from_block_position(block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
+impl<'b> From<TxPosInBlock<'b>> for ConfirmationBlockTime {
+    fn from(pos: TxPosInBlock) -> Self {
         Self {
-            block_id,
-            confirmation_time: block.header.time as _,
+            block_id: pos.block_id,
+            confirmation_time: pos.block.header.time as _,
         }
     }
 }