From d65f815a29b9203860b54a47e6aee421a33fbd6c Mon Sep 17 00:00:00 2001 From: benthecarman Date: Tue, 16 Apr 2024 19:19:35 -0500 Subject: [PATCH] Move device lock out of NodeManager Another refactor for optional NodeManager. This moves the device lock out of the NodeManager so we can still enforce a single device even for fedimint only wallets. --- mutiny-core/src/lib.rs | 32 ++++++++++++++++++++++++++++++++ mutiny-core/src/nodemanager.rs | 32 -------------------------------- mutiny-core/src/storage.rs | 3 ++- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/mutiny-core/src/lib.rs b/mutiny-core/src/lib.rs index 626f47307..4bed8b0b9 100644 --- a/mutiny-core/src/lib.rs +++ b/mutiny-core/src/lib.rs @@ -132,6 +132,7 @@ use crate::nostr::{NostrKeySource, RELAYS}; #[cfg(test)] use mockall::{automock, predicate::*}; +pub const DEVICE_LOCK_INTERVAL_SECS: u64 = 30; const BITCOIN_PRICE_CACHE_SEC: u64 = 300; const DEFAULT_PAYMENT_TIMEOUT: u64 = 30; const MAX_FEDERATION_INVOICE_AMT: u64 = 200_000; @@ -836,6 +837,37 @@ impl MutinyWalletBuilder { self.session_id, )); + // Need to prevent other devices from running at the same time + if !config.skip_device_lock { + let start = Instant::now(); + log_trace!(logger, "Checking device lock"); + if let Some(lock) = self.storage.get_device_lock()? { + log_info!(logger, "Current device lock: {lock:?}"); + } + self.storage.set_device_lock().await?; + log_trace!( + logger, + "Device lock set: took {}ms", + start.elapsed().as_millis() + ); + } + + // spawn thread to claim device lock + let storage_clone = self.storage.clone(); + let logger_clone = logger.clone(); + let stop_clone = stop.clone(); + spawn(async move { + loop { + if stop_clone.load(Ordering::Relaxed) { + break; + } + sleep((DEVICE_LOCK_INTERVAL_SECS * 1_000) as i32).await; + if let Err(e) = storage_clone.set_device_lock().await { + log_error!(logger_clone, "Error setting device lock: {e}"); + } + } + }); + let start = Instant::now(); let mut nm_builder = NodeManagerBuilder::new(self.xprivkey, self.storage.clone()) diff --git a/mutiny-core/src/nodemanager.rs b/mutiny-core/src/nodemanager.rs index b6977d903..f185e845d 100644 --- a/mutiny-core/src/nodemanager.rs +++ b/mutiny-core/src/nodemanager.rs @@ -63,8 +63,6 @@ use std::{collections::HashMap, ops::Deref, sync::Arc}; #[cfg(target_arch = "wasm32")] use web_time::Instant; -pub const DEVICE_LOCK_INTERVAL_SECS: u64 = 30; - // This is the NodeStorage object saved to the DB #[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)] pub struct NodeStorage { @@ -329,36 +327,6 @@ impl NodeManagerBuilder { .websocket_proxy_addr .unwrap_or_else(|| String::from("wss://p.mutinywallet.com")); - // Need to prevent other devices from running at the same time - if !c.skip_device_lock { - let start = Instant::now(); - log_trace!(logger, "Checking device lock"); - if let Some(lock) = self.storage.get_device_lock()? { - log_info!(logger, "Current device lock: {lock:?}"); - } - self.storage.set_device_lock().await?; - log_trace!( - logger, - "Device lock set: took {}ms", - start.elapsed().as_millis() - ); - } - - let storage_clone = self.storage.clone(); - let logger_clone = logger.clone(); - let stop_clone = stop.clone(); - utils::spawn(async move { - loop { - if stop_clone.load(Ordering::Relaxed) { - break; - } - sleep((DEVICE_LOCK_INTERVAL_SECS * 1_000) as i32).await; - if let Err(e) = storage_clone.set_device_lock().await { - log_error!(logger_clone, "Error setting device lock: {e}"); - } - } - }); - let start = Instant::now(); log_info!(logger, "Building node manager components"); diff --git a/mutiny-core/src/storage.rs b/mutiny-core/src/storage.rs index 68f4d17dd..b3ea94573 100644 --- a/mutiny-core/src/storage.rs +++ b/mutiny-core/src/storage.rs @@ -1,11 +1,12 @@ use crate::event::HTLCStatus; -use crate::nodemanager::{ChannelClosure, NodeStorage, DEVICE_LOCK_INTERVAL_SECS}; +use crate::nodemanager::{ChannelClosure, NodeStorage}; use crate::utils::{now, spawn}; use crate::vss::{MutinyVssClient, VssKeyValueItem}; use crate::{blindauth::TokenStorage, logging::MutinyLogger}; use crate::{ encrypt::{decrypt_with_password, encrypt, encryption_key_from_pass, Cipher}, federation::FederationStorage, + DEVICE_LOCK_INTERVAL_SECS, }; use crate::{ error::{MutinyError, MutinyStorageError},