Skip to content

Commit

Permalink
fix: search filters + wallet save() (#4387)
Browse files Browse the repository at this point in the history
  • Loading branch information
aoudiamoncef authored Sep 5, 2023
1 parent de428f9 commit 4e32df3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 41 deletions.
48 changes: 15 additions & 33 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions massa-grpc/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,7 @@ pub(crate) fn search_blocks(
b_ids
};
if let Some(block_ids) = res.as_mut() {
block_ids.extend(b_ids);
block_ids.retain(|id: &BlockId| b_ids.contains(id));
} else {
res = Some(b_ids)
}
Expand All @@ -889,7 +889,7 @@ pub(crate) fn search_blocks(
read_lock.aggregate_blocks_by_slot_range(start_slot..end_slot);

if let Some(block_ids) = res.as_mut() {
block_ids.extend(b_ids);
block_ids.retain(|id: &BlockId| b_ids.contains(id));
} else {
res = Some(b_ids)
}
Expand Down Expand Up @@ -1018,7 +1018,7 @@ pub(crate) fn search_endorsements(
e_ids
};
if let Some(endorsement_ids) = eds_ids.as_mut() {
endorsement_ids.extend(e_ids);
endorsement_ids.retain(|id: &EndorsementId| e_ids.contains(id));
} else {
eds_ids = Some(e_ids)
}
Expand All @@ -1043,7 +1043,7 @@ pub(crate) fn search_endorsements(
}

if let Some(endorsement_ids) = eds_ids.as_mut() {
endorsement_ids.extend(e_ids);
endorsement_ids.retain(|id: &EndorsementId| e_ids.contains(id));
} else {
eds_ids = Some(e_ids)
}
Expand Down Expand Up @@ -1208,7 +1208,7 @@ pub(crate) fn search_operations(
o_ids
};
if let Some(operation_ids) = ops_ids.as_mut() {
operation_ids.extend(o_ids);
operation_ids.retain(|id: &OperationId| o_ids.contains(id));
} else {
ops_ids = Some(o_ids)
}
Expand Down
21 changes: 18 additions & 3 deletions massa-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use massa_models::secure_share::SecureShareContent;
use massa_signature::{KeyPair, PublicKey};
use serde::{Deserialize, Serialize};
use std::collections::hash_map::Entry;
use std::collections::HashSet;
use std::path::PathBuf;
use std::str::FromStr;

Expand Down Expand Up @@ -157,9 +158,17 @@ impl Wallet {

/// Save the wallets in a directory, each wallet in a yaml file.
fn save(&self) -> Result<(), WalletError> {
let mut existing_keys: HashSet<PathBuf> = HashSet::new();
if !self.wallet_path.exists() {
std::fs::create_dir_all(&self.wallet_path)?;
} else {
let read_dir = std::fs::read_dir(&self.wallet_path)?;
for path in read_dir {
existing_keys.insert(path?.path());
}
}
let mut persisted_keys: HashSet<PathBuf> = HashSet::new();
// write the keys in the directory
for (addr, keypair) in &self.keys {
let encrypted_secret = encrypt(&self.password, &keypair.to_bytes())?;
let file_formatted = WalletFileFormat {
Expand All @@ -172,11 +181,17 @@ impl Wallet {
public_key: keypair.get_public_key().to_bytes().to_vec(),
};
let ser_keys = serde_yaml::to_string(&file_formatted)?;
let mut file_path = self.wallet_path.clone();
file_path.push(format!("wallet_{}", addr));
file_path.set_extension("yaml");
let file_path = self.wallet_path.join(format!("wallet_{}.yaml", addr));

std::fs::write(&file_path, ser_keys)?;
persisted_keys.insert(file_path);
}

let to_remove = existing_keys.difference(&persisted_keys);
for path in to_remove {
std::fs::remove_file(path)?;
}

Ok(())
}

Expand Down

0 comments on commit 4e32df3

Please sign in to comment.