Skip to content

Commit 6d4b33e

Browse files
committed
chain: split IndexedTxGraph::insert_tx into 3 methods
Instead of inserting anchors and seen_at timestamp in the same method, we have three separate methods. This makes the API easier to understand and makes `IndexedTxGraph` more consistent with the `TxGraph` API.
1 parent 4f5695d commit 6d4b33e

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,16 @@ impl<D> Wallet<D> {
738738
ConfirmationTime::Unconfirmed { last_seen } => (None, Some(last_seen)),
739739
};
740740

741-
let changeset: ChangeSet = self.indexed_graph.insert_tx(&tx, anchor, last_seen).into();
741+
let mut changeset = ChangeSet::default();
742+
let txid = tx.txid();
743+
changeset.append(self.indexed_graph.insert_tx(tx).into());
744+
if let Some(anchor) = anchor {
745+
changeset.append(self.indexed_graph.insert_anchor(txid, anchor).into());
746+
}
747+
if let Some(last_seen) = last_seen {
748+
changeset.append(self.indexed_graph.insert_seen_at(txid, last_seen).into());
749+
}
750+
742751
let changed = !changeset.is_empty();
743752
self.persist.stage(changeset);
744753
Ok(changed)

crates/chain/src/indexed_tx_graph.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This is essentially a [`TxGraph`] combined with an indexer.
44
55
use alloc::vec::Vec;
6-
use bitcoin::{Block, OutPoint, Transaction, TxOut};
6+
use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid};
77

88
use crate::{
99
keychain,
@@ -103,32 +103,25 @@ where
103103
}
104104

105105
/// Insert and index a transaction into the graph.
106-
///
107-
/// `anchors` can be provided to anchor the transaction to various blocks. `seen_at` is a
108-
/// unix timestamp of when the transaction is last seen.
109-
pub fn insert_tx(
110-
&mut self,
111-
tx: &Transaction,
112-
anchors: impl IntoIterator<Item = A>,
113-
seen_at: Option<u64>,
114-
) -> ChangeSet<A, I::ChangeSet> {
115-
let txid = tx.txid();
116-
117-
let mut graph = tx_graph::ChangeSet::default();
118-
if self.graph.get_tx(txid).is_none() {
119-
graph.append(self.graph.insert_tx(tx.clone()));
120-
}
121-
for anchor in anchors.into_iter() {
122-
graph.append(self.graph.insert_anchor(txid, anchor));
123-
}
124-
if let Some(seen_at) = seen_at {
125-
graph.append(self.graph.insert_seen_at(txid, seen_at));
126-
}
127-
106+
pub fn insert_tx(&mut self, tx: Transaction) -> ChangeSet<A, I::ChangeSet> {
107+
let graph = self.graph.insert_tx(tx);
128108
let indexer = self.index_tx_graph_changeset(&graph);
129109
ChangeSet { graph, indexer }
130110
}
131111

112+
/// Insert an `anchor` for a given transaction.
113+
pub fn insert_anchor(&mut self, txid: Txid, anchor: A) -> ChangeSet<A, I::ChangeSet> {
114+
self.graph.insert_anchor(txid, anchor).into()
115+
}
116+
117+
/// Insert a unix timestamp of when a transaction is seen in the mempool.
118+
///
119+
/// This is used for transaction conflict resolution in [`TxGraph`] where the transaction with
120+
/// the later last-seen is prioritized.
121+
pub fn insert_seen_at(&mut self, txid: Txid, seen_at: u64) -> ChangeSet<A, I::ChangeSet> {
122+
self.graph.insert_seen_at(txid, seen_at).into()
123+
}
124+
132125
/// Batch insert transactions, filtering out those that are irrelevant.
133126
///
134127
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant

example-crates/example_cli/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,7 @@ where
624624
Ok(_) => {
625625
println!("Broadcasted Tx : {}", transaction.txid());
626626

627-
let keychain_changeset =
628-
graph.lock().unwrap().insert_tx(&transaction, None, None);
627+
let keychain_changeset = graph.lock().unwrap().insert_tx(transaction);
629628

630629
// We know the tx is at least unconfirmed now. Note if persisting here fails,
631630
// it's not a big deal since we can always find it again form

0 commit comments

Comments
 (0)