From bd2d8767378a3efd985541b60988dd29d9dc1b5c Mon Sep 17 00:00:00 2001 From: jjy Date: Wed, 27 Nov 2024 01:21:15 +0800 Subject: [PATCH] Write IndexedDbStorage initialization logs --- mutiny-core/src/lib.rs | 7 +++++++ mutiny-core/src/logging.rs | 19 ++++++++++++++++--- mutiny-wasm/src/indexed_db.rs | 16 +++++++++++++--- mutiny-wasm/src/lib.rs | 1 + 4 files changed, 37 insertions(+), 6 deletions(-) diff --git a/mutiny-core/src/lib.rs b/mutiny-core/src/lib.rs index 693010880..442b0b667 100644 --- a/mutiny-core/src/lib.rs +++ b/mutiny-core/src/lib.rs @@ -681,6 +681,7 @@ pub struct MutinyWalletBuilder { skip_hodl_invoices: bool, skip_device_lock: bool, safe_mode: bool, + logs: Vec, } impl MutinyWalletBuilder { @@ -700,6 +701,7 @@ impl MutinyWalletBuilder { skip_device_lock: false, safe_mode: false, skip_hodl_invoices: true, + logs: Default::default(), } } @@ -721,6 +723,10 @@ impl MutinyWalletBuilder { self.session_id = Some(session_id); } + pub fn with_logs(&mut self, logs: Vec) { + self.logs = logs; + } + pub fn with_network(&mut self, network: Network) { self.network = Some(network); } @@ -788,6 +794,7 @@ impl MutinyWalletBuilder { let logger = Arc::new(MutinyLogger::with_writer( self.storage.clone(), self.session_id, + self.logs, )); // Need to prevent other devices from running at the same time diff --git a/mutiny-core/src/logging.rs b/mutiny-core/src/logging.rs index d0c8a60e4..7b234d268 100644 --- a/mutiny-core/src/logging.rs +++ b/mutiny-core/src/logging.rs @@ -21,8 +21,12 @@ pub struct MutinyLogger { } impl MutinyLogger { - pub fn with_writer(logging_db: S, session_id: Option) -> Self { - let memory_logs = Arc::new(Mutex::new(vec![])); + pub fn with_writer( + logging_db: S, + session_id: Option, + logs: Vec, + ) -> Self { + let memory_logs = Arc::new(Mutex::new(logs)); let stop_handle = utils::spawn_with_handle({ let memory_logs = memory_logs.clone(); @@ -74,6 +78,15 @@ impl MutinyLogger { } } + pub fn get_memory_logs(&self) -> Result, MutinyError> { + let logs = self + .memory_logs + .lock() + .map_err(|_err| MutinyError::Other(anyhow::anyhow!("can't get memory logs lock")))? + .to_vec(); + Ok(logs) + } + pub(crate) fn get_logs( &self, storage: &S, @@ -231,7 +244,7 @@ mod tests { let storage = MemoryStorage::default(); - let logger = MutinyLogger::with_writer(storage.clone(), None); + let logger = MutinyLogger::with_writer(storage.clone(), None, Default::default()); let log_str = "testing logging with storage"; log_debug!(logger, "{}", log_str); diff --git a/mutiny-wasm/src/indexed_db.rs b/mutiny-wasm/src/indexed_db.rs index 009670c1b..d39ed803c 100644 --- a/mutiny-wasm/src/indexed_db.rs +++ b/mutiny-wasm/src/indexed_db.rs @@ -4,7 +4,7 @@ use bip39::Mnemonic; use futures::lock::Mutex; use gloo_utils::format::JsValueSerdeExt; use lightning::util::logger::Logger; -use lightning::{log_debug, log_error, log_info, log_trace}; +use lightning::{log_debug, log_error, log_info}; use log::error; use mutiny_core::event::PaymentInfo; use mutiny_core::logging::MutinyLogger; @@ -58,6 +58,7 @@ impl IndexedDbStorage { vss: Option>, logger: Arc, ) -> Result { + log_debug!(logger, "Initialize indexed DB storage"); let idx = Self::build_indexed_db_database().await?; let indexed_db = Arc::new(RwLock::new(RexieContainer(Some(idx)))); let password = password.filter(|p| !p.is_empty()); @@ -72,6 +73,8 @@ impl IndexedDbStorage { .await?; let memory = Arc::new(RwLock::new(map)); + log_debug!(logger, "Complete initialize indexed DB storage"); + Ok(IndexedDbStorage { password, cipher, @@ -324,6 +327,12 @@ impl IndexedDbStorage { MutinyError::read_err(anyhow!("Failed to get all from store: {e}").into()) })?; + log_info!( + logger, + "Read {} key values from browser storage", + all_json.len() + ); + for (key, value) in all_json { let key = key .as_string() @@ -340,7 +349,7 @@ impl IndexedDbStorage { let json: Value = value.into_serde()?; map.write_raw(vec![(key, json)])?; } - log_trace!( + log_debug!( logger, "Reading browser storage took {}ms", start.elapsed().as_millis() @@ -356,6 +365,7 @@ impl IndexedDbStorage { log_info!(logger, "Reading from vss"); let start = instant::Instant::now(); let keys = vss.list_key_versions(None).await?; + log_info!(logger, "Read {} keys from vss", keys.len()); let mut futs = Vec::with_capacity(keys.len()); for kv in keys { futs.push(Self::handle_vss_key(kv, vss, &map, logger)); @@ -374,7 +384,7 @@ impl IndexedDbStorage { } let final_map = map.memory.read().unwrap(); - log_trace!(logger, "Reading VSS took {}ms", start.elapsed().as_millis()); + log_debug!(logger, "Reading VSS took {}ms", start.elapsed().as_millis()); Ok(final_map.clone()) } diff --git a/mutiny-wasm/src/lib.rs b/mutiny-wasm/src/lib.rs index aa4013f63..49180f1bd 100644 --- a/mutiny-wasm/src/lib.rs +++ b/mutiny-wasm/src/lib.rs @@ -277,6 +277,7 @@ impl MutinyWallet { let mut mw_builder = MutinyWalletBuilder::new(xprivkey, storage).with_config(config); mw_builder.with_session_id(logger.session_id.clone()); + mw_builder.with_logs(logger.get_memory_logs()?); if let Some(cb) = ln_event_callback { mw_builder.with_ln_event_callback(cb); }