Skip to content

Commit

Permalink
feat: remove spawn blocking calls from wallet db (wallet storage)(#4591)
Browse files Browse the repository at this point in the history
Description
---
Removed spawn blocking calls for db operations from the wallet in the wallet storage. (This is another PR in a couple of PRs required to implement this fully throughout the wallet code.)

Motivation and Context
---
As per #3982 and #4555

How Has This Been Tested?
---
Unit tests
Cucumber tests
  • Loading branch information
hansieodendaal committed Aug 31, 2022
1 parent 095196b commit 77bb10d
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 390 deletions.
14 changes: 4 additions & 10 deletions applications/tari_console_wallet/src/automation/commands.rs
Expand Up @@ -704,23 +704,17 @@ pub async fn command_runner(
set_base_node_peer(wallet.clone(), args.public_key.into(), args.address).await?;
wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(), public_key.to_string())
.await?;
.set_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(), public_key.to_string())?;
wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(), net_address.to_string())
.await?;
.set_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(), net_address.to_string())?;
println!("Custom base node peer saved in wallet database.");
},
ClearCustomBaseNode => {
wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())
.await?;
wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())
.await?;
.clear_client_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())?;
wallet.db.clear_client_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())?;
println!("Custom base node peer cleared from wallet database.");
},
InitShaAtomicSwap(args) => {
Expand Down
30 changes: 14 additions & 16 deletions applications/tari_console_wallet/src/init/mod.rs
Expand Up @@ -158,7 +158,7 @@ pub async fn get_base_node_peer_config(
Some(ref custom) => SeedPeer::from_str(custom)
.map(|node| Some(Peer::from(node)))
.map_err(|err| ExitError::new(ExitCode::ConfigError, &format!("Malformed custom base node: {}", err)))?,
None => get_custom_base_node_peer_from_db(wallet).await,
None => get_custom_base_node_peer_from_db(wallet),
};

// If the user has not explicitly set a base node in the config, we try detect one
Expand All @@ -181,7 +181,7 @@ pub async fn get_base_node_peer_config(
let address = detected_node.addresses.first().ok_or_else(|| {
ExitError::new(ExitCode::ConfigError, "No address found for detected base node")
})?;
set_custom_base_node_peer_in_db(wallet, &detected_node.public_key, address).await?;
set_custom_base_node_peer_in_db(wallet, &detected_node.public_key, address)?;
selected_base_node = Some(detected_node.into());
}
},
Expand Down Expand Up @@ -293,13 +293,13 @@ pub async fn init_wallet(

let node_address = match config.wallet.p2p.public_address.clone() {
Some(addr) => addr,
None => match wallet_db.get_node_address().await? {
None => match wallet_db.get_node_address()? {
Some(addr) => addr,
None => Multiaddr::empty(),
},
};

let master_seed = read_or_create_master_seed(recovery_seed.clone(), &wallet_db).await?;
let master_seed = read_or_create_master_seed(recovery_seed.clone(), &wallet_db)?;

let node_identity = match config.wallet.identity_file.as_ref() {
Some(identity_file) => {
Expand All @@ -315,12 +315,12 @@ pub async fn init_wallet(
PeerFeatures::COMMUNICATION_CLIENT,
)?
},
None => setup_identity_from_db(&wallet_db, &master_seed, node_address.clone()).await?,
None => setup_identity_from_db(&wallet_db, &master_seed, node_address.clone())?,
};

let mut wallet_config = config.wallet.clone();
if let TransportType::Tor = config.wallet.p2p.transport.transport_type {
wallet_config.p2p.transport.tor.identity = wallet_db.get_tor_id().await?;
wallet_config.p2p.transport.tor.identity = wallet_db.get_tor_id()?;
}

let factories = CryptoFactories::default();
Expand Down Expand Up @@ -352,7 +352,6 @@ pub async fn init_wallet(
wallet
.db
.set_tor_identity(hs.tor_identity().clone())
.await
.map_err(|e| ExitError::new(ExitCode::WalletError, format!("Problem writing tor identity. {}", e)))?;
}

Expand Down Expand Up @@ -381,7 +380,7 @@ pub async fn init_wallet(
debug!(target: LOG_TARGET, "Wallet encrypted.");

if interactive && recovery_seed.is_none() {
match confirm_seed_words(&mut wallet).await {
match confirm_seed_words(&mut wallet) {
Ok(()) => {
print!("\x1Bc"); // Clear the screen
},
Expand All @@ -392,7 +391,7 @@ pub async fn init_wallet(
}
}
if let Some(file_name) = seed_words_file_name {
let seed_words = wallet.get_seed_words(&MnemonicLanguage::English).await?.join(" ");
let seed_words = wallet.get_seed_words(&MnemonicLanguage::English)?.join(" ");
let _result = fs::write(file_name, seed_words).map_err(|e| {
ExitError::new(
ExitCode::WalletError,
Expand All @@ -416,17 +415,16 @@ async fn detect_local_base_node() -> Option<SeedPeer> {
Some(SeedPeer::new(public_key, vec![address]))
}

async fn setup_identity_from_db<D: WalletBackend + 'static>(
fn setup_identity_from_db<D: WalletBackend + 'static>(
wallet_db: &WalletDatabase<D>,
master_seed: &CipherSeed,
node_address: Multiaddr,
) -> Result<Arc<NodeIdentity>, ExitError> {
let node_features = wallet_db
.get_node_features()
.await?
.get_node_features()?
.unwrap_or(PeerFeatures::COMMUNICATION_CLIENT);

let identity_sig = wallet_db.get_comms_identity_signature().await?;
let identity_sig = wallet_db.get_comms_identity_signature()?;

let comms_secret_key = derive_comms_secret_key(master_seed)?;

Expand All @@ -452,7 +450,7 @@ async fn setup_identity_from_db<D: WalletBackend + 'static>(
.as_ref()
.expect("unreachable panic")
.clone();
wallet_db.set_comms_identity_signature(sig).await?;
wallet_db.set_comms_identity_signature(sig)?;
}

Ok(node_identity)
Expand Down Expand Up @@ -514,8 +512,8 @@ async fn validate_txos(wallet: &mut WalletSqlite) -> Result<(), ExitError> {
Ok(())
}

async fn confirm_seed_words(wallet: &mut WalletSqlite) -> Result<(), ExitError> {
let seed_words = wallet.get_seed_words(&MnemonicLanguage::English).await?;
fn confirm_seed_words(wallet: &mut WalletSqlite) -> Result<(), ExitError> {
let seed_words = wallet.get_seed_words(&MnemonicLanguage::English)?;

println!();
println!("=========================");
Expand Down
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/main.rs
Expand Up @@ -167,7 +167,7 @@ fn main_inner() -> Result<(), ExitError> {
))?;

// Check if there is an in progress recovery in the wallet's database
if runtime.block_on(wallet.is_recovery_in_progress())? {
if wallet.is_recovery_in_progress()? {
println!("A Wallet Recovery was found to be in progress, continuing.");
boot_mode = WalletBoot::Recovery;
}
Expand Down
20 changes: 7 additions & 13 deletions applications/tari_console_wallet/src/ui/state/app_state.rs
Expand Up @@ -892,15 +892,11 @@ impl AppStateInner {
// persist the custom node in wallet db
self.wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(), peer.public_key.to_string())
.await?;
self.wallet
.db
.set_client_key_value(
CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(),
peer.addresses.first().ok_or(UiError::NoAddress)?.to_string(),
)
.await?;
.set_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(), peer.public_key.to_string())?;
self.wallet.db.set_client_key_value(
CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(),
peer.addresses.first().ok_or(UiError::NoAddress)?.to_string(),
)?;
info!(
target: LOG_TARGET,
"Setting custom base node peer for wallet: {}::{}",
Expand Down Expand Up @@ -931,12 +927,10 @@ impl AppStateInner {
// clear from wallet db
self.wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())
.await?;
.clear_client_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())?;
self.wallet
.db
.clear_client_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())
.await?;
.clear_client_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())?;
Ok(())
}

Expand Down
25 changes: 8 additions & 17 deletions applications/tari_console_wallet/src/utils/db.rs
Expand Up @@ -36,23 +36,18 @@ pub const CUSTOM_BASE_NODE_ADDRESS_KEY: &str = "console_wallet_custom_base_node_

/// This helper function will attempt to read a stored base node public key and address from the wallet database.
/// If both are found they are used to construct and return a Peer.
pub async fn get_custom_base_node_peer_from_db(wallet: &mut WalletSqlite) -> Option<Peer> {
pub fn get_custom_base_node_peer_from_db(wallet: &mut WalletSqlite) -> Option<Peer> {
let custom_base_node_peer_pubkey = match wallet
.db
.get_client_key_value(CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string())
.await
{
Ok(val) => val,
Err(e) => {
warn!(target: LOG_TARGET, "Problem reading from wallet database: {}", e);
return None;
},
};
let custom_base_node_peer_address = match wallet
.db
.get_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string())
.await
{
let custom_base_node_peer_address = match wallet.db.get_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string()) {
Ok(val) => val,
Err(e) => {
warn!(target: LOG_TARGET, "Problem reading from wallet database: {}", e);
Expand Down Expand Up @@ -91,23 +86,19 @@ pub async fn get_custom_base_node_peer_from_db(wallet: &mut WalletSqlite) -> Opt
}

/// Sets the base node peer in the database
pub async fn set_custom_base_node_peer_in_db(
pub fn set_custom_base_node_peer_in_db(
wallet: &mut WalletSqlite,
base_node_public_key: &CommsPublicKey,
base_node_address: &Multiaddr,
) -> Result<(), WalletStorageError> {
wallet
.db
.set_client_key_value(
CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(),
base_node_public_key.to_hex(),
)
.await?;
wallet.db.set_client_key_value(
CUSTOM_BASE_NODE_PUBLIC_KEY_KEY.to_string(),
base_node_public_key.to_hex(),
)?;

wallet
.db
.set_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(), base_node_address.to_string())
.await?;
.set_client_key_value(CUSTOM_BASE_NODE_ADDRESS_KEY.to_string(), base_node_address.to_string())?;

Ok(())
}
2 changes: 1 addition & 1 deletion applications/tari_console_wallet/src/wallet_modes.rs
Expand Up @@ -282,7 +282,7 @@ pub fn tui_mode(
let base_node_selected;
if let Some(peer) = base_node_config.base_node_custom.clone() {
base_node_selected = peer;
} else if let Some(peer) = handle.block_on(get_custom_base_node_peer_from_db(&mut wallet)) {
} else if let Some(peer) = get_custom_base_node_peer_from_db(&mut wallet) {
base_node_selected = peer;
} else if let Some(peer) = handle.block_on(wallet.get_base_node_peer()) {
base_node_selected = peer;
Expand Down
2 changes: 1 addition & 1 deletion base_layer/wallet/src/base_node_service/monitor.rs
Expand Up @@ -163,7 +163,7 @@ where
timer.elapsed().as_millis()
);

self.db.set_chain_metadata(chain_metadata.clone()).await?;
self.db.set_chain_metadata(chain_metadata.clone())?;

let is_synced = tip_info.is_synced;
let height_of_longest_chain = chain_metadata.height_of_longest_chain();
Expand Down
4 changes: 2 additions & 2 deletions base_layer/wallet/src/base_node_service/service.rs
Expand Up @@ -150,11 +150,11 @@ where T: WalletBackend + 'static
"Handling Wallet Base Node Service Request: {:?}", request
);
match request {
BaseNodeServiceRequest::GetChainMetadata => match self.get_state().await.chain_metadata.clone() {
BaseNodeServiceRequest::GetChainMetadata => match self.get_state().await.chain_metadata {
Some(metadata) => Ok(BaseNodeServiceResponse::ChainMetadata(Some(metadata))),
None => {
// if we don't have live state, check if we've previously stored state in the wallet db
let metadata = self.db.get_chain_metadata().await?;
let metadata = self.db.get_chain_metadata()?;
Ok(BaseNodeServiceResponse::ChainMetadata(metadata))
},
},
Expand Down

0 comments on commit 77bb10d

Please sign in to comment.