Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write IndexedDbStorage initialization logs #44

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mutiny-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ pub struct MutinyWalletBuilder<S: MutinyStorage> {
skip_hodl_invoices: bool,
skip_device_lock: bool,
safe_mode: bool,
logs: Vec<String>,
}

impl<S: MutinyStorage> MutinyWalletBuilder<S> {
Expand All @@ -700,6 +701,7 @@ impl<S: MutinyStorage> MutinyWalletBuilder<S> {
skip_device_lock: false,
safe_mode: false,
skip_hodl_invoices: true,
logs: Default::default(),
}
}

Expand All @@ -721,6 +723,10 @@ impl<S: MutinyStorage> MutinyWalletBuilder<S> {
self.session_id = Some(session_id);
}

pub fn with_logs(&mut self, logs: Vec<String>) {
self.logs = logs;
}

pub fn with_network(&mut self, network: Network) {
self.network = Some(network);
}
Expand Down Expand Up @@ -788,6 +794,7 @@ impl<S: MutinyStorage> MutinyWalletBuilder<S> {
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
Expand Down
19 changes: 16 additions & 3 deletions mutiny-core/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ pub struct MutinyLogger {
}

impl MutinyLogger {
pub fn with_writer<S: MutinyStorage>(logging_db: S, session_id: Option<String>) -> Self {
let memory_logs = Arc::new(Mutex::new(vec![]));
pub fn with_writer<S: MutinyStorage>(
logging_db: S,
session_id: Option<String>,
logs: Vec<String>,
) -> Self {
let memory_logs = Arc::new(Mutex::new(logs));

let stop_handle = utils::spawn_with_handle({
let memory_logs = memory_logs.clone();
Expand Down Expand Up @@ -74,6 +78,15 @@ impl MutinyLogger {
}
}

pub fn get_memory_logs(&self) -> Result<Vec<String>, 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<S: MutinyStorage>(
&self,
storage: &S,
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 13 additions & 3 deletions mutiny-wasm/src/indexed_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +58,7 @@ impl IndexedDbStorage {
vss: Option<Arc<MutinyVssClient>>,
logger: Arc<MutinyLogger>,
) -> Result<IndexedDbStorage, MutinyError> {
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());
Expand All @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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));
Expand All @@ -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())
}
Expand Down
1 change: 1 addition & 0 deletions mutiny-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading