Skip to content

Commit a2a64ff

Browse files
committed
fix(electrum)!: Remove seen_at param from into_tx_graph
and `into_confirmation_time_tx_graph`, since now it makes sense to use `TxGraph::update_last_seen_unconfirmed`. Also, use `update_last_seen_unconfirmed` in examples for electrum/esplora. We show how to update the last seen time for transactions by calling `update_last_seen_unconfirmed` on the graph update returned from a blockchain source, passing in the current time, before applying it to another `TxGraph`.
1 parent 37fca35 commit a2a64ff

File tree

7 files changed

+27
-17
lines changed

7 files changed

+27
-17
lines changed

crates/electrum/src/electrum_ext.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ impl RelevantTxids {
4040
pub fn into_tx_graph(
4141
self,
4242
client: &Client,
43-
seen_at: Option<u64>,
4443
missing: Vec<Txid>,
4544
) -> Result<TxGraph<ConfirmationHeightAnchor>, Error> {
4645
let new_txs = client.batch_transaction_get(&missing)?;
4746
let mut graph = TxGraph::<ConfirmationHeightAnchor>::new(new_txs);
4847
for (txid, anchors) in self.0 {
49-
if let Some(seen_at) = seen_at {
50-
let _ = graph.insert_seen_at(txid, seen_at);
51-
}
5248
for anchor in anchors {
5349
let _ = graph.insert_anchor(txid, anchor);
5450
}
@@ -67,10 +63,9 @@ impl RelevantTxids {
6763
pub fn into_confirmation_time_tx_graph(
6864
self,
6965
client: &Client,
70-
seen_at: Option<u64>,
7166
missing: Vec<Txid>,
7267
) -> Result<TxGraph<ConfirmationTimeHeightAnchor>, Error> {
73-
let graph = self.into_tx_graph(client, seen_at, missing)?;
68+
let graph = self.into_tx_graph(client, missing)?;
7469

7570
let relevant_heights = {
7671
let mut visited_heights = HashSet::new();

crates/electrum/tests/test_electrum.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn scan_detects_confirmed_tx() -> Result<()> {
6868
} = client.sync(recv_chain.tip(), [spk_to_track], None, None, 5)?;
6969

7070
let missing = relevant_txids.missing_full_txs(recv_graph.graph());
71-
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, None, missing)?;
71+
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
7272
let _ = recv_chain
7373
.apply_update(chain_update)
7474
.map_err(|err| anyhow::anyhow!("LocalChain update error: {:?}", err))?;
@@ -134,7 +134,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> Result<()> {
134134
} = client.sync(recv_chain.tip(), [spk_to_track.clone()], None, None, 5)?;
135135

136136
let missing = relevant_txids.missing_full_txs(recv_graph.graph());
137-
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, None, missing)?;
137+
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
138138
let _ = recv_chain
139139
.apply_update(chain_update)
140140
.map_err(|err| anyhow::anyhow!("LocalChain update error: {:?}", err))?;
@@ -164,8 +164,7 @@ fn tx_can_become_unconfirmed_after_reorg() -> Result<()> {
164164
} = client.sync(recv_chain.tip(), [spk_to_track.clone()], None, None, 5)?;
165165

166166
let missing = relevant_txids.missing_full_txs(recv_graph.graph());
167-
let graph_update =
168-
relevant_txids.into_confirmation_time_tx_graph(&client, None, missing)?;
167+
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
169168
let _ = recv_chain
170169
.apply_update(chain_update)
171170
.map_err(|err| anyhow::anyhow!("LocalChain update error: {:?}", err))?;

example-crates/example_electrum/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ fn main() -> anyhow::Result<()> {
299299
relevant_txids.missing_full_txs(graph.graph())
300300
};
301301

302+
let mut graph_update = relevant_txids.into_tx_graph(&client, missing_txids)?;
302303
let now = std::time::UNIX_EPOCH
303304
.elapsed()
304305
.expect("must get time")
305306
.as_secs();
306-
307-
let graph_update = relevant_txids.into_tx_graph(&client, Some(now), missing_txids)?;
307+
let _ = graph_update.update_last_seen_unconfirmed(now);
308308

309309
let db_changeset = {
310310
let mut chain = chain.lock().unwrap();

example-crates/example_esplora/src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,14 @@ fn main() -> anyhow::Result<()> {
189189
// is reached. It returns a `TxGraph` update (`graph_update`) and a structure that
190190
// represents the last active spk derivation indices of keychains
191191
// (`keychain_indices_update`).
192-
let (graph_update, last_active_indices) = client
192+
let (mut graph_update, last_active_indices) = client
193193
.full_scan(keychain_spks, *stop_gap, scan_options.parallel_requests)
194194
.context("scanning for transactions")?;
195195

196+
// We want to keep track of the latest time a transaction was seen unconfirmed.
197+
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
198+
let _ = graph_update.update_last_seen_unconfirmed(now);
199+
196200
let mut graph = graph.lock().expect("mutex must not be poisoned");
197201
// Because we did a stop gap based scan we are likely to have some updates to our
198202
// deriviation indices. Usually before a scan you are on a fresh wallet with no
@@ -307,9 +311,13 @@ fn main() -> anyhow::Result<()> {
307311
}
308312
}
309313

310-
let graph_update =
314+
let mut graph_update =
311315
client.sync(spks, txids, outpoints, scan_options.parallel_requests)?;
312316

317+
// Update last seen unconfirmed
318+
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
319+
let _ = graph_update.update_last_seen_unconfirmed(now);
320+
313321
graph.lock().unwrap().apply_update(graph_update)
314322
}
315323
};

example-crates/wallet_electrum/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ fn main() -> Result<(), anyhow::Error> {
6666
println!();
6767

6868
let missing = relevant_txids.missing_full_txs(wallet.as_ref());
69-
let graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, None, missing)?;
69+
let mut graph_update = relevant_txids.into_confirmation_time_tx_graph(&client, missing)?;
70+
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
71+
let _ = graph_update.update_last_seen_unconfirmed(now);
7072

7173
let wallet_update = Update {
7274
last_active_indices: keychain_update,

example-crates/wallet_esplora_async/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ async fn main() -> Result<(), anyhow::Error> {
5353
(k, k_spks)
5454
})
5555
.collect();
56-
let (update_graph, last_active_indices) = client
56+
let (mut update_graph, last_active_indices) = client
5757
.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)
5858
.await?;
59+
60+
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
61+
let _ = update_graph.update_last_seen_unconfirmed(now);
5962
let missing_heights = update_graph.missing_heights(wallet.local_chain());
6063
let chain_update = client.update_local_chain(prev_tip, missing_heights).await?;
6164
let update = Update {

example-crates/wallet_esplora_blocking/src/main.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ fn main() -> Result<(), anyhow::Error> {
5353
})
5454
.collect();
5555

56-
let (update_graph, last_active_indices) =
56+
let (mut update_graph, last_active_indices) =
5757
client.full_scan(keychain_spks, STOP_GAP, PARALLEL_REQUESTS)?;
58+
59+
let now = std::time::UNIX_EPOCH.elapsed().unwrap().as_secs();
60+
let _ = update_graph.update_last_seen_unconfirmed(now);
5861
let missing_heights = update_graph.missing_heights(wallet.local_chain());
5962
let chain_update = client.update_local_chain(prev_tip, missing_heights)?;
6063
let update = Update {

0 commit comments

Comments
 (0)