Skip to content

Commit 60d722c

Browse files
committed
Cbf: change registered scripts vec to hashmap
1 parent 556545b commit 60d722c

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/chain/cbf.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use std::collections::HashMap;
8+
use std::collections::{HashMap, HashSet};
99
use std::net::SocketAddr;
1010
use std::sync::atomic::{AtomicU32, Ordering};
1111
use std::sync::{Arc, Mutex, RwLock};
@@ -93,7 +93,7 @@ pub(super) struct CbfChainSource {
9393
/// Serializes concurrent filter scans (on-chain and lightning).
9494
scan_lock: tokio::sync::Mutex<()>,
9595
/// Scripts registered by LDK's Filter trait for lightning channel monitoring.
96-
registered_scripts: Mutex<Vec<ScriptBuf>>,
96+
registered_scripts: Mutex<HashSet<ScriptBuf>>,
9797
/// Deduplicates concurrent on-chain wallet sync requests.
9898
onchain_wallet_sync_status: Mutex<WalletSyncStatus>,
9999
/// Deduplicates concurrent lightning wallet sync requests.
@@ -151,7 +151,7 @@ impl CbfChainSource {
151151
let matched_block_hashes = Arc::new(Mutex::new(Vec::new()));
152152
let sync_completion_tx = Arc::new(Mutex::new(None));
153153
let filter_skip_height = Arc::new(AtomicU32::new(0));
154-
let registered_scripts = Mutex::new(Vec::new());
154+
let registered_scripts = Mutex::new(HashSet::new());
155155
let scan_lock = tokio::sync::Mutex::new(());
156156
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
157157
let lightning_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);
@@ -510,12 +510,12 @@ impl CbfChainSource {
510510

511511
/// Register a transaction script for Lightning channel monitoring.
512512
pub(crate) fn register_tx(&self, _txid: &Txid, script_pubkey: &Script) {
513-
self.registered_scripts.lock().expect("lock").push(script_pubkey.to_owned());
513+
self.registered_scripts.lock().expect("lock").insert(script_pubkey.to_owned());
514514
}
515515

516516
/// Register a watched output script for Lightning channel monitoring.
517517
pub(crate) fn register_output(&self, output: WatchedOutput) {
518-
self.registered_scripts.lock().expect("lock").push(output.script_pubkey.clone());
518+
self.registered_scripts.lock().expect("lock").insert(output.script_pubkey.clone());
519519
}
520520

521521
/// Run a CBF filter scan: set watched scripts, trigger a rescan, wait for
@@ -738,7 +738,8 @@ impl CbfChainSource {
738738
let requester = self.requester()?;
739739
let now = Instant::now();
740740

741-
let scripts: Vec<ScriptBuf> = self.registered_scripts.lock().expect("lock").clone();
741+
let scripts: Vec<ScriptBuf> =
742+
self.registered_scripts.lock().expect("lock").iter().cloned().collect();
742743
if scripts.is_empty() {
743744
log_debug!(self.logger, "No registered scripts for CBF lightning sync.");
744745
} else {

0 commit comments

Comments
 (0)