Skip to content

Commit 139d971

Browse files
committed
Merge #1594: Replace trait AnchorFromBlockPosition with new struct
ab8068b refactor(chain)!: Replace trait `AnchorFromBlockPosition` with new struct (Jiri Jakes) Pull request description: ### Description This change replaces a way of creating new generic anchor from block and its height. Previous way was using conversion trait, newly it is a struct and `From`. Closes #1266. ### Notes to the reviewers - Removed `tx_pos`: I did not see its relevance (`Anchor` does not give access to it). Was it a relic from the past or something I overlooked? - I put `BlockPosition` into `tx_data_traits.rs` because I believe it should be next to `Anchor`. But then it's not a module just for traits. What would be better place? ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: LagginTimes: ACK ab8068b LLFourn: ACK ab8068b Tree-SHA512: 9546ff177247796a36aeec6e015e564fb093cae0fe3b2962520843e8371bf3060f6d62f676a3989a49a6c25da6b7adbcde47c5fffe2f772f871980eb77cd1aea
2 parents e4ee629 + ab8068b commit 139d971

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

crates/chain/src/indexed_tx_graph.rs

+19-10
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

+19-12
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,31 @@ impl Anchor for ConfirmationBlockTime {
9898
}
9999
}
100100

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

108-
impl AnchorFromBlockPosition for BlockId {
109-
fn from_block_position(_block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
110-
block_id
115+
impl<'b> From<TxPosInBlock<'b>> for BlockId {
116+
fn from(pos: TxPosInBlock) -> Self {
117+
pos.block_id
111118
}
112119
}
113120

114-
impl AnchorFromBlockPosition for ConfirmationBlockTime {
115-
fn from_block_position(block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
121+
impl<'b> From<TxPosInBlock<'b>> for ConfirmationBlockTime {
122+
fn from(pos: TxPosInBlock) -> Self {
116123
Self {
117-
block_id,
118-
confirmation_time: block.header.time as _,
124+
block_id: pos.block_id,
125+
confirmation_time: pos.block.header.time as _,
119126
}
120127
}
121128
}

0 commit comments

Comments
 (0)