Skip to content

Commit 66a8c03

Browse files
committed
Deduplicate registered chain txids
Track registered transaction IDs in a set so repeated filter registrations do not grow the collection or slow block-connected checks. This keeps the wallet's registered-transaction lookup bounded by unique transaction IDs. This commit was created with assistance from OpenAI Codex.
1 parent c4d1e49 commit 66a8c03

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

src/chain/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub(crate) mod bitcoind;
99
mod electrum;
1010
mod esplora;
1111

12-
use std::collections::HashMap;
12+
use std::collections::{HashMap, HashSet};
1313
use std::sync::{Arc, Mutex};
1414
use std::time::Duration;
1515

@@ -84,7 +84,7 @@ impl WalletSyncStatus {
8484

8585
pub(crate) struct ChainSource {
8686
kind: ChainSourceKind,
87-
registered_txids: Mutex<Vec<Txid>>,
87+
registered_txids: Mutex<HashSet<Txid>>,
8888
tx_broadcaster: Arc<Broadcaster>,
8989
logger: Arc<Logger>,
9090
}
@@ -113,7 +113,7 @@ impl ChainSource {
113113
node_metrics,
114114
)?;
115115
let kind = ChainSourceKind::Esplora(esplora_chain_source);
116-
let registered_txids = Mutex::new(Vec::new());
116+
let registered_txids = Mutex::new(HashSet::new());
117117
Ok((Self { kind, registered_txids, tx_broadcaster, logger }, None))
118118
}
119119

@@ -133,7 +133,7 @@ impl ChainSource {
133133
node_metrics,
134134
);
135135
let kind = ChainSourceKind::Electrum(electrum_chain_source);
136-
let registered_txids = Mutex::new(Vec::new());
136+
let registered_txids = Mutex::new(HashSet::new());
137137
(Self { kind, registered_txids, tx_broadcaster, logger }, None)
138138
}
139139

@@ -156,7 +156,7 @@ impl ChainSource {
156156
);
157157
let best_block = bitcoind_chain_source.poll_best_block().await.ok();
158158
let kind = ChainSourceKind::Bitcoind(bitcoind_chain_source);
159-
let registered_txids = Mutex::new(Vec::new());
159+
let registered_txids = Mutex::new(HashSet::new());
160160
(Self { kind, registered_txids, tx_broadcaster, logger }, best_block)
161161
}
162162

@@ -180,7 +180,7 @@ impl ChainSource {
180180
);
181181
let best_block = bitcoind_chain_source.poll_best_block().await.ok();
182182
let kind = ChainSourceKind::Bitcoind(bitcoind_chain_source);
183-
let registered_txids = Mutex::new(Vec::new());
183+
let registered_txids = Mutex::new(HashSet::new());
184184
(Self { kind, registered_txids, tx_broadcaster, logger }, best_block)
185185
}
186186

@@ -214,7 +214,7 @@ impl ChainSource {
214214
}
215215
}
216216

217-
pub(crate) fn registered_txids(&self) -> Vec<Txid> {
217+
pub(crate) fn registered_txids(&self) -> HashSet<Txid> {
218218
self.registered_txids.lock().expect("lock").clone()
219219
}
220220

@@ -472,7 +472,7 @@ impl ChainSource {
472472

473473
impl Filter for ChainSource {
474474
fn register_tx(&self, txid: &Txid, script_pubkey: &Script) {
475-
self.registered_txids.lock().expect("lock").push(*txid);
475+
self.registered_txids.lock().expect("lock").insert(*txid);
476476
match &self.kind {
477477
ChainSourceKind::Esplora(esplora_chain_source) => {
478478
esplora_chain_source.register_tx(txid, script_pubkey)

0 commit comments

Comments
 (0)