Skip to content

Commit

Permalink
Merge pull request #1145 from MutinyWallet/mv-device-lock
Browse files Browse the repository at this point in the history
Move device lock out of NodeManager
  • Loading branch information
TonyGiorgio authored Apr 17, 2024
2 parents 3615aac + d65f815 commit 9c1c4ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
32 changes: 32 additions & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -836,6 +837,37 @@ impl<S: MutinyStorage> MutinyWalletBuilder<S> {
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())
Expand Down
32 changes: 0 additions & 32 deletions mutiny-core/src/nodemanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -329,36 +327,6 @@ impl<S: MutinyStorage> NodeManagerBuilder<S> {
.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");

Expand Down
3 changes: 2 additions & 1 deletion mutiny-core/src/storage.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down

0 comments on commit 9c1c4ac

Please sign in to comment.