Skip to content

Commit

Permalink
rusk-wallet: Gracefully exit on interrupt
Browse files Browse the repository at this point in the history
Ask confirmation before deleting cache
  • Loading branch information
Daksh14 committed Oct 11, 2024
1 parent 531a467 commit 62aff84
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 16 deletions.
2 changes: 2 additions & 0 deletions rusk-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ tracing-subscriber = { workspace = true, features = [
"json",
] }

fastwebsockets = "0.8.0"

rkyv = { workspace = true }

konst = { workspace = true }
Expand Down
9 changes: 9 additions & 0 deletions rusk-wallet/src/bin/io/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,15 @@ pub(crate) fn ask_confirm() -> anyhow::Result<bool> {
Ok(a.as_bool().expect("answer to be a bool"))
}

/// Asks the user for confirmation before deleting cache
pub(crate) fn ask_confirm_erase_cache() -> anyhow::Result<bool> {
let q = requestty::Question::confirm("confirm")
.message("Corrupt cache detected, delete the cache? (Alternatively specify --profile to change the location)")
.build();
let a = requestty::prompt_one(q)?;
Ok(a.as_bool().expect("answer to be a bool"))
}

/// Request a receiver address
pub(crate) fn request_rcvr_addr(addr_for: &str) -> anyhow::Result<Address> {
// let the user input the receiver address
Expand Down
34 changes: 30 additions & 4 deletions rusk-wallet/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) use menu::Menu;
use clap::Parser;
use std::fs::{self, File};
use std::io::Write;
use tracing::{warn, Level};
use tracing::{error, info, warn, Level};

use bip39::{Language, Mnemonic, MnemonicType};

Expand Down Expand Up @@ -85,10 +85,33 @@ where

// check for connection errors
match con {
Err(Error::RocksDB(e)) => panic!{"Please reset the cache! {e}"},
Err(e) => warn!("[OFFLINE MODE]: Unable to connect to Rusk, limited functionality available: {e}"),
Err(Error::RocksDB(_)) => {
wallet.close();

match prompt::ask_confirm_erase_cache() {
Ok(true) => {
if let Some(io_err) = wallet.delete_cache().err() {
error!("Error while deleting the cache: {io_err}");
}

info!("Restart the wallet to create new cache and sync with network");
},
Ok(false) => {

info!("Wallet will now exit, reset the cache manually");
},
Err(e) => {
error!("Error while asking for confirmation error: {e}");
}
}

// Exit because we cannot proceed because of db error
// wallet is already closed
std::process::exit(1);
},
Err(ref e) => warn!("[OFFLINE MODE]: Unable to connect to Rusk, limited functionality available: {e}"),
_ => {}
}
};

wallet
}
Expand Down Expand Up @@ -343,5 +366,8 @@ async fn exec() -> anyhow::Result<()> {
}
}

// Gracefully close the wallet
wallet.close();

Ok(())
}
3 changes: 1 addition & 2 deletions rusk-wallet/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ impl Cache {
Ok(notes)
}

pub fn close(&self) -> Result<(), Error> {
pub fn close(&self) {
self.db.cancel_all_background_work(false);
Ok(())
}
}
4 changes: 1 addition & 3 deletions rusk-wallet/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,7 @@ impl State {
}

pub fn close(&mut self) {
// UNWRAP: its okay to panic here because we're closing the database
// if there's an error we want an exception to happen
self.cache().close().unwrap();
self.cache().close();
let store = &mut self.store;

// if there's sync handle we abort it
Expand Down
28 changes: 21 additions & 7 deletions rusk-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,7 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
_=> {},
}

let cache_dir = {
if let Some(file) = &self.file {
file.path().cache_dir()
} else {
return Err(Error::WalletFileMissing);
}
};
let cache_dir = self.cache_path()?;

// create a state client
self.state = Some(State::new(
Expand Down Expand Up @@ -385,6 +379,19 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
derive_phoenix_vk(seed, index)
}

/// get cache database path
pub(crate) fn cache_path(&self) -> Result<PathBuf, Error> {
let cache_dir = {
if let Some(file) = &self.file {
file.path().cache_dir()
} else {
return Err(Error::WalletFileMissing);
}
};

Ok(cache_dir)
}

/// Returns the Phoenix public-key for a given index.
///
/// # Errors
Expand Down Expand Up @@ -1112,6 +1119,13 @@ impl<F: SecureWalletFile + Debug> Wallet<F> {
Ok(network_last_pos == db_pos)
}

/// Erase the cache directory
pub fn delete_cache(&mut self) -> Result<(), Error> {
let path = self.cache_path()?;

std::fs::remove_dir_all(path).map_err(Error::IO)
}

/// Close the wallet and zeroize the seed
pub fn close(&mut self) {
self.store.inner_mut().zeroize();
Expand Down

0 comments on commit 62aff84

Please sign in to comment.