Skip to content

Commit

Permalink
Atomic version of insert_address
Browse files Browse the repository at this point in the history
  • Loading branch information
karbyshev committed Mar 7, 2024
1 parent 076ac28 commit d285065
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
3 changes: 2 additions & 1 deletion crates/apps/src/lib/cli/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,8 @@ fn transparent_address_add(
let alias = alias.to_lowercase();
let mut wallet = load_wallet(ctx);
if wallet
.insert_address(&alias, address, alias_force)
.insert_address_atomic(&alias, address, alias_force)
.expect("Failed to update the wallet storage.")
.is_none()
{
edisplay_line!(io, "Address not added");
Expand Down
24 changes: 14 additions & 10 deletions crates/apps/src/lib/config/genesis/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ impl Finalized {
) -> Wallet<CliWalletUtils> {
let mut wallet = crate::wallet::load_or_new(base_dir);
for (alias, config) in &self.tokens.token {
wallet.insert_address(
alias.normalize(),
config.address.clone(),
false,
);
wallet
.insert_address_atomic(
alias.normalize(),
config.address.clone(),
false,
)
.expect("Failed to update the wallet storage.");
wallet
.add_vp_type_to_address_atomic(
AddressVpType::Token,
Expand Down Expand Up @@ -170,11 +172,13 @@ impl Finalized {
InternalAddress::Governance,
InternalAddress::Pgf,
] {
wallet.insert_address(
int_add.to_string().to_lowercase(),
Address::Internal(int_add.clone()),
false,
);
wallet
.insert_address_atomic(
int_add.to_string().to_lowercase(),
Address::Internal(int_add.clone()),
false,
)
.expect("Failed to update the wallet storage.");
}

wallet
Expand Down
14 changes: 9 additions & 5 deletions crates/sdk/src/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,11 +536,15 @@ pub async fn save_initialized_accounts<N: Namada>(
None => N::WalletUtils::read_alias(&encoded).into(),
};
let alias = alias.into_owned();
let added = context.wallet_mut().await.insert_address(
alias.clone(),
address.clone(),
args.wallet_alias_force,
);
let added = context
.wallet_mut()
.await
.insert_address_atomic(
alias.clone(),
address.clone(),
args.wallet_alias_force,
)
.expect("Failed to update the wallet storage.");
match added {
Some(new_alias) if new_alias != encoded => {
display_line!(
Expand Down
49 changes: 34 additions & 15 deletions crates/sdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,21 +1224,21 @@ impl<U: WalletIo> Wallet<U> {
}
}

/// Add a new address with the given alias. If the alias is already used,
/// will ask whether the existing alias should be replaced, a different
/// alias is desired, or the alias creation should be cancelled. Return
/// the chosen alias if the address has been added, otherwise return
/// nothing.
pub fn insert_address(
&mut self,
alias: impl AsRef<str>,
address: Address,
force_alias: bool,
) -> Option<String> {
self.store
.insert_address::<U>(alias.into(), address, force_alias)
.map(Into::into)
}
// /// Add a new address with the given alias. If the alias is already used,
// /// will ask whether the existing alias should be replaced, a different
// /// alias is desired, or the alias creation should be cancelled. Return
// /// the chosen alias if the address has been added, otherwise return
// /// nothing.
// pub fn insert_address(
// &mut self,
// alias: impl AsRef<str>,
// address: Address,
// force_alias: bool,
// ) -> Option<String> {
// self.store
// .insert_address::<U>(alias.into(), address, force_alias)
// .map(Into::into)
// }

/// Add a new keypair with the given alias. If the alias is already used,
/// will ask whether the existing alias should be replaced, a different
Expand Down Expand Up @@ -1468,4 +1468,23 @@ impl<U: WalletIo + WalletStorage> Wallet<U> {
},
)
}

/// Add a new address with the given alias. If the alias is already used,
/// will ask whether the existing alias should be replaced, a different
/// alias is desired, or the alias creation should be cancelled. Return
/// the chosen alias if the address has been added, otherwise return
/// nothing.
pub fn insert_address_atomic(
&mut self,
alias: impl AsRef<str>,
address: Address,
force_alias: bool,
) -> Result<Option<String>, LoadStoreError> {
let mut addr_alias: Option<Alias> = Option::default();
self.utils.update_store(|store| {
addr_alias =
store.insert_address::<U>(alias.into(), address, force_alias);
})?;
Ok(addr_alias.map(Into::into))
}
}

0 comments on commit d285065

Please sign in to comment.