Skip to content

Commit ab8068b

Browse files
committed
refactor(chain)!: Replace trait AnchorFromBlockPosition with new struct
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`.
1 parent 257c5f7 commit ab8068b

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

crates/chain/src/indexed_tx_graph.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid};
77

88
use crate::{
99
tx_graph::{self, TxGraph},
10-
Anchor, AnchorFromBlockPosition, BlockId, Indexer, Merge,
10+
Anchor, BlockId, Indexer, Merge, TxPosInBlock,
1111
};
1212

1313
/// The [`IndexedTxGraph`] combines a [`TxGraph`] and an [`Indexer`] implementation.
@@ -252,17 +252,17 @@ where
252252
}
253253
}
254254

255-
/// Methods are available if the anchor (`A`) implements [`AnchorFromBlockPosition`].
256-
impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
255+
/// Methods are available if the anchor (`A`) can be created from [`TxPosInBlock`].
256+
impl<A, I> IndexedTxGraph<A, I>
257257
where
258258
I::ChangeSet: Default + Merge,
259-
A: AnchorFromBlockPosition,
259+
for<'b> A: Anchor + From<TxPosInBlock<'b>>,
260+
I: Indexer,
260261
{
261262
/// Batch insert all transactions of the given `block` of `height`, filtering out those that are
262263
/// irrelevant.
263264
///
264-
/// Each inserted transaction's anchor will be constructed from
265-
/// [`AnchorFromBlockPosition::from_block_position`].
265+
/// Each inserted transaction's anchor will be constructed using [`TxPosInBlock`].
266266
///
267267
/// Relevancy is determined by the internal [`Indexer::is_tx_relevant`] implementation of `I`.
268268
/// Irrelevant transactions in `txs` will be ignored.
@@ -280,7 +280,12 @@ where
280280
changeset.indexer.merge(self.index.index_tx(tx));
281281
if self.index.is_tx_relevant(tx) {
282282
let txid = tx.compute_txid();
283-
let anchor = A::from_block_position(block, block_id, tx_pos);
283+
let anchor = TxPosInBlock {
284+
block,
285+
block_id,
286+
tx_pos,
287+
}
288+
.into();
284289
changeset.tx_graph.merge(self.graph.insert_tx(tx.clone()));
285290
changeset
286291
.tx_graph
@@ -292,8 +297,7 @@ where
292297

293298
/// Batch insert all transactions of the given `block` of `height`.
294299
///
295-
/// Each inserted transaction's anchor will be constructed from
296-
/// [`AnchorFromBlockPosition::from_block_position`].
300+
/// Each inserted transaction's anchor will be constructed using [`TxPosInBlock`].
297301
///
298302
/// To only insert relevant transactions, use [`apply_block_relevant`] instead.
299303
///
@@ -305,7 +309,12 @@ where
305309
};
306310
let mut graph = tx_graph::ChangeSet::default();
307311
for (tx_pos, tx) in block.txdata.iter().enumerate() {
308-
let anchor = A::from_block_position(&block, block_id, tx_pos);
312+
let anchor = TxPosInBlock {
313+
block: &block,
314+
block_id,
315+
tx_pos,
316+
}
317+
.into();
309318
graph.merge(self.graph.insert_anchor(tx.compute_txid(), anchor));
310319
graph.merge(self.graph.insert_tx(tx.clone()));
311320
}

crates/chain/src/tx_data_traits.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,31 @@ impl Anchor for ConfirmationBlockTime {
100100
}
101101
}
102102

103-
/// An [`Anchor`] that can be constructed from a given block, block height and transaction position
104-
/// within the block.
105-
pub trait AnchorFromBlockPosition: Anchor {
106-
/// Construct the anchor from a given `block`, block height and `tx_pos` within the block.
107-
fn from_block_position(block: &bitcoin::Block, block_id: BlockId, tx_pos: usize) -> Self;
103+
/// Set of parameters sufficient to construct an [`Anchor`].
104+
///
105+
/// Typically used as an additional constraint on anchor:
106+
/// `for<'b> A: Anchor + From<TxPosInBlock<'b>>`.
107+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108+
pub struct TxPosInBlock<'b> {
109+
/// Block in which the transaction appeared.
110+
pub block: &'b bitcoin::Block,
111+
/// Block's [`BlockId`].
112+
pub block_id: BlockId,
113+
/// Position in the block on which the transaction appeared.
114+
pub tx_pos: usize,
108115
}
109116

110-
impl AnchorFromBlockPosition for BlockId {
111-
fn from_block_position(_block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
112-
block_id
117+
impl<'b> From<TxPosInBlock<'b>> for BlockId {
118+
fn from(pos: TxPosInBlock) -> Self {
119+
pos.block_id
113120
}
114121
}
115122

116-
impl AnchorFromBlockPosition for ConfirmationBlockTime {
117-
fn from_block_position(block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
123+
impl<'b> From<TxPosInBlock<'b>> for ConfirmationBlockTime {
124+
fn from(pos: TxPosInBlock) -> Self {
118125
Self {
119-
block_id,
120-
confirmation_time: block.header.time as _,
126+
block_id: pos.block_id,
127+
confirmation_time: pos.block.header.time as _,
121128
}
122129
}
123130
}

0 commit comments

Comments
 (0)