Skip to content

Commit 74b14a7

Browse files
committed
feat(chain,wallet)!: change methods to take in generic instead of &Transaction
* `Wallet::apply_unconfirmed_txs` * `IndexedTxGraph::batch_insert_relevant` * `IndexedTxGraph::batch_insert_relevant_unconfirmed`
1 parent 4c3e0cb commit 74b14a7

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed

crates/chain/src/indexed_tx_graph.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,38 +157,38 @@ where
157157
///
158158
/// Relevancy is determined by the [`Indexer::is_tx_relevant`] implementation of `I`. Irrelevant
159159
/// transactions in `txs` will be ignored. `txs` do not need to be in topological order.
160-
pub fn batch_insert_relevant<'t>(
160+
pub fn batch_insert_relevant<T: Into<Arc<Transaction>>>(
161161
&mut self,
162-
txs: impl IntoIterator<Item = (&'t Transaction, impl IntoIterator<Item = A>)>,
162+
txs: impl IntoIterator<Item = (T, impl IntoIterator<Item = A>)>,
163163
) -> ChangeSet<A, I::ChangeSet> {
164164
// The algorithm below allows for non-topologically ordered transactions by using two loops.
165165
// This is achieved by:
166166
// 1. insert all txs into the index. If they are irrelevant then that's fine it will just
167167
// not store anything about them.
168168
// 2. decide whether to insert them into the graph depending on whether `is_tx_relevant`
169169
// returns true or not. (in a second loop).
170-
let txs = txs.into_iter().collect::<Vec<_>>();
170+
let txs = txs
171+
.into_iter()
172+
.map(|(tx, anchors)| (<T as Into<Arc<Transaction>>>::into(tx), anchors))
173+
.collect::<Vec<_>>();
171174

172175
let mut indexer = I::ChangeSet::default();
173176
for (tx, _) in &txs {
174177
indexer.merge(self.index.index_tx(tx));
175178
}
176179

177-
let mut graph = tx_graph::ChangeSet::default();
180+
let mut tx_graph = tx_graph::ChangeSet::default();
178181
for (tx, anchors) in txs {
179-
if self.index.is_tx_relevant(tx) {
182+
if self.index.is_tx_relevant(&tx) {
180183
let txid = tx.compute_txid();
181-
graph.merge(self.graph.insert_tx(tx.clone()));
184+
tx_graph.merge(self.graph.insert_tx(tx.clone()));
182185
for anchor in anchors {
183-
graph.merge(self.graph.insert_anchor(txid, anchor));
186+
tx_graph.merge(self.graph.insert_anchor(txid, anchor));
184187
}
185188
}
186189
}
187190

188-
ChangeSet {
189-
tx_graph: graph,
190-
indexer,
191-
}
191+
ChangeSet { tx_graph, indexer }
192192
}
193193

194194
/// Batch insert unconfirmed transactions, filtering out those that are irrelevant.
@@ -199,17 +199,20 @@ where
199199
/// Items of `txs` are tuples containing the transaction and a *last seen* timestamp. The
200200
/// *last seen* communicates when the transaction is last seen in the mempool which is used for
201201
/// conflict-resolution in [`TxGraph`] (refer to [`TxGraph::insert_seen_at`] for details).
202-
pub fn batch_insert_relevant_unconfirmed<'t>(
202+
pub fn batch_insert_relevant_unconfirmed<T: Into<Arc<Transaction>>>(
203203
&mut self,
204-
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
204+
unconfirmed_txs: impl IntoIterator<Item = (T, u64)>,
205205
) -> ChangeSet<A, I::ChangeSet> {
206206
// The algorithm below allows for non-topologically ordered transactions by using two loops.
207207
// This is achieved by:
208208
// 1. insert all txs into the index. If they are irrelevant then that's fine it will just
209209
// not store anything about them.
210210
// 2. decide whether to insert them into the graph depending on whether `is_tx_relevant`
211211
// returns true or not. (in a second loop).
212-
let txs = unconfirmed_txs.into_iter().collect::<Vec<_>>();
212+
let txs = unconfirmed_txs
213+
.into_iter()
214+
.map(|(tx, last_seen)| (<T as Into<Arc<Transaction>>>::into(tx), last_seen))
215+
.collect::<Vec<_>>();
213216

214217
let mut indexer = I::ChangeSet::default();
215218
for (tx, _) in &txs {

crates/chain/tests/test_indexed_tx_graph.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn insert_relevant_txs() {
8181
};
8282

8383
assert_eq!(
84-
graph.batch_insert_relevant(txs.iter().map(|tx| (tx, None))),
84+
graph.batch_insert_relevant(txs.iter().cloned().map(|tx| (tx, None))),
8585
changeset,
8686
);
8787

@@ -237,10 +237,10 @@ fn test_list_owned_txouts() {
237237
// Insert unconfirmed txs with a last_seen timestamp
238238

239239
let _ =
240-
graph.batch_insert_relevant([&tx1, &tx2, &tx3, &tx6].iter().enumerate().map(|(i, tx)| {
240+
graph.batch_insert_relevant([&tx1, &tx2, &tx3, &tx6].iter().enumerate().map(|(i, &tx)| {
241241
let height = i as u32;
242242
(
243-
*tx,
243+
tx.clone(),
244244
local_chain
245245
.get(height)
246246
.map(|cp| cp.block_id())
@@ -251,7 +251,8 @@ fn test_list_owned_txouts() {
251251
)
252252
}));
253253

254-
let _ = graph.batch_insert_relevant_unconfirmed([&tx4, &tx5].iter().map(|tx| (*tx, 100)));
254+
let _ =
255+
graph.batch_insert_relevant_unconfirmed([&tx4, &tx5].iter().map(|&tx| (tx.clone(), 100)));
255256

256257
// A helper lambda to extract and filter data from the graph.
257258
let fetch =

crates/wallet/src/wallet/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,9 +2441,9 @@ impl Wallet {
24412441
/// **WARNING**: You must persist the changes resulting from one or more calls to this method
24422442
/// if you need the applied unconfirmed transactions to be reloaded after closing the wallet.
24432443
/// See [`Wallet::reveal_next_address`].
2444-
pub fn apply_unconfirmed_txs<'t>(
2444+
pub fn apply_unconfirmed_txs<T: Into<Arc<Transaction>>>(
24452445
&mut self,
2446-
unconfirmed_txs: impl IntoIterator<Item = (&'t Transaction, u64)>,
2446+
unconfirmed_txs: impl IntoIterator<Item = (T, u64)>,
24472447
) {
24482448
let indexed_graph_changeset = self
24492449
.indexed_graph

example-crates/example_bitcoind_rpc_polling/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,10 @@ fn main() -> anyhow::Result<()> {
201201
}
202202

203203
let mempool_txs = emitter.mempool()?;
204-
let graph_changeset = graph.lock().unwrap().batch_insert_relevant_unconfirmed(
205-
mempool_txs.iter().map(|(tx, time)| (tx, *time)),
206-
);
204+
let graph_changeset = graph
205+
.lock()
206+
.unwrap()
207+
.batch_insert_relevant_unconfirmed(mempool_txs);
207208
{
208209
let db = &mut *db.lock().unwrap();
209210
db_stage.merge(ChangeSet {
@@ -287,7 +288,7 @@ fn main() -> anyhow::Result<()> {
287288
}
288289
Emission::Mempool(mempool_txs) => {
289290
let graph_changeset = graph.batch_insert_relevant_unconfirmed(
290-
mempool_txs.iter().map(|(tx, time)| (tx, *time)),
291+
mempool_txs,
291292
);
292293
(local_chain::ChangeSet::default(), graph_changeset)
293294
}

example-crates/wallet_rpc/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn main() -> anyhow::Result<()> {
157157
}
158158
Emission::Mempool(mempool_emission) => {
159159
let start_apply_mempool = Instant::now();
160-
wallet.apply_unconfirmed_txs(mempool_emission.iter().map(|(tx, time)| (tx, *time)));
160+
wallet.apply_unconfirmed_txs(mempool_emission);
161161
wallet.persist(&mut db)?;
162162
println!(
163163
"Applied unconfirmed transactions in {}s",

0 commit comments

Comments
 (0)