Skip to content

Commit a7d01dc

Browse files
committed
feat(chain)!: make IndexedTxGraph::apply_block_relevant more efficient
Previously, `apply_block_relevant` used `batch_insert_relevant` which allows inserting non-topologically-ordered transactions. However, transactions from blocks are always ordered, so we can avoid looping through block transactions twice (as done in `batch_insert_relevant`). Additionally, `apply_block_relevant` now takes in a reference to a `Block` instead of consuming the `Block`. This makes sense as typically very few of the transactions in the block are inserted.
1 parent e0512ac commit a7d01dc

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,7 +2385,7 @@ impl<D> Wallet<D> {
23852385
/// with `prev_blockhash` and `height-1` as the `connected_to` parameter.
23862386
///
23872387
/// [`apply_block_connected_to`]: Self::apply_block_connected_to
2388-
pub fn apply_block(&mut self, block: Block, height: u32) -> Result<(), CannotConnectError>
2388+
pub fn apply_block(&mut self, block: &Block, height: u32) -> Result<(), CannotConnectError>
23892389
where
23902390
D: PersistBackend<ChangeSet>,
23912391
{
@@ -2416,7 +2416,7 @@ impl<D> Wallet<D> {
24162416
/// internal [`TxGraph`].
24172417
pub fn apply_block_connected_to(
24182418
&mut self,
2419-
block: Block,
2419+
block: &Block,
24202420
height: u32,
24212421
connected_to: BlockId,
24222422
) -> Result<(), ApplyHeaderError>

crates/bitcoind_rpc/tests/test_emitter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
298298
tip: emission.checkpoint,
299299
introduce_older_blocks: false,
300300
})?;
301-
let indexed_additions = indexed_tx_graph.apply_block_relevant(emission.block, height);
301+
let indexed_additions = indexed_tx_graph.apply_block_relevant(&emission.block, height);
302302
assert!(indexed_additions.is_empty());
303303
}
304304

@@ -362,7 +362,7 @@ fn test_into_tx_graph() -> anyhow::Result<()> {
362362
tip: emission.checkpoint,
363363
introduce_older_blocks: false,
364364
})?;
365-
let indexed_additions = indexed_tx_graph.apply_block_relevant(emission.block, height);
365+
let indexed_additions = indexed_tx_graph.apply_block_relevant(&emission.block, height);
366366
assert!(indexed_additions.graph.txs.is_empty());
367367
assert!(indexed_additions.graph.txouts.is_empty());
368368
assert_eq!(indexed_additions.graph.anchors, exp_anchors);

crates/chain/src/indexed_tx_graph.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -224,20 +224,26 @@ where
224224
/// Irrelevant transactions in `txs` will be ignored.
225225
pub fn apply_block_relevant(
226226
&mut self,
227-
block: Block,
227+
block: &Block,
228228
height: u32,
229229
) -> ChangeSet<A, I::ChangeSet> {
230230
let block_id = BlockId {
231231
hash: block.block_hash(),
232232
height,
233233
};
234-
let txs = block.txdata.iter().enumerate().map(|(tx_pos, tx)| {
235-
(
236-
tx,
237-
core::iter::once(A::from_block_position(&block, block_id, tx_pos)),
238-
)
239-
});
240-
self.batch_insert_relevant(txs)
234+
let mut changeset = ChangeSet::<A, I::ChangeSet>::default();
235+
for (tx_pos, tx) in block.txdata.iter().enumerate() {
236+
changeset.indexer.append(self.index.index_tx(tx));
237+
if self.index.is_tx_relevant(tx) {
238+
let txid = tx.txid();
239+
let anchor = A::from_block_position(block, block_id, tx_pos);
240+
changeset.graph.append(self.graph.insert_tx(tx.clone()));
241+
changeset
242+
.graph
243+
.append(self.graph.insert_anchor(txid, anchor));
244+
}
245+
}
246+
changeset
241247
}
242248

243249
/// Batch insert all transactions of the given `block` of `height`.

0 commit comments

Comments
 (0)