Skip to content

Commit

Permalink
Use try_fold to build the ChangedBalances object.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed May 20, 2024
1 parent 57dfe29 commit 852d366
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 39 deletions.
78 changes: 40 additions & 38 deletions crates/namada/src/ledger/native_vp/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,50 +269,52 @@ where
)));
}

let mut result = ChangedBalances::default();
// Get the changed balance keys
let counterparts_balances: Vec<_> = keys_changed
.iter()
.filter_map(is_any_shielded_action_balance_key)
.collect();

for (token, counterpart) in counterparts_balances {
let denom = read_denom(&self.ctx.pre(), token)?.ok_or_err_msg(
"No denomination found in storage for the given token",
)?;
unepoched_tokens(token, denom, &mut result.tokens)?;
let counterpart_balance_key = counterpart.to_balance_key(token);
let mut pre_balance: Amount = self
.ctx
.read_pre(&counterpart_balance_key)?
.unwrap_or_default();
let mut post_balance: Amount = self
.ctx
.read_post(&counterpart_balance_key)?
.unwrap_or_default();
if let ShieldedActionOwner::Minted = counterpart {
// When receiving ibc transfers we mint and also shield so we
// have two credits/debits, we need to mock the mint balance as
// the opposite change
std::mem::swap(&mut pre_balance, &mut post_balance);
}
// Public keys must be the hash of the sources/targets
let address_hash = TransparentAddress(<[u8; 20]>::from(
ripemd::Ripemd160::digest(sha2::Sha256::digest(
&counterpart.to_address_ref().serialize_to_vec(),
)),
));

result
.decoder
.insert(address_hash, counterpart.to_address_ref().clone());
*result.pre.entry(address_hash).or_insert(ValueSum::zero()) +=
ValueSum::from_pair(token.clone(), pre_balance);
*result.post.entry(address_hash).or_insert(ValueSum::zero()) +=
ValueSum::from_pair(token.clone(), post_balance);
}

Ok(result)
counterparts_balances.iter().try_fold(
ChangedBalances::default(),
|mut result, (token, counterpart)| {
let denom = read_denom(&self.ctx.pre(), token)?.ok_or_err_msg(
"No denomination found in storage for the given token",
)?;
unepoched_tokens(token, denom, &mut result.tokens)?;
let counterpart_balance_key = counterpart.to_balance_key(token);
let mut pre_balance: Amount = self
.ctx
.read_pre(&counterpart_balance_key)?
.unwrap_or_default();
let mut post_balance: Amount = self
.ctx
.read_post(&counterpart_balance_key)?
.unwrap_or_default();
if let ShieldedActionOwner::Minted = counterpart {
// When receiving ibc transfers we mint and also shield so
// we have two credits/debits, we need
// to mock the mint balance as
// the opposite change
std::mem::swap(&mut pre_balance, &mut post_balance);
}
// Public keys must be the hash of the sources/targets
let address_hash = TransparentAddress(<[u8; 20]>::from(
ripemd::Ripemd160::digest(sha2::Sha256::digest(
&counterpart.to_address_ref().serialize_to_vec(),
)),
));

result
.decoder
.insert(address_hash, counterpart.to_address_ref().clone());
*result.pre.entry(address_hash).or_insert(ValueSum::zero()) +=
ValueSum::from_pair((*token).clone(), pre_balance);
*result.post.entry(address_hash).or_insert(ValueSum::zero()) +=
ValueSum::from_pair((*token).clone(), post_balance);
Ok(result)
},
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/shielded_token/src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ where
use masp_primitives::transaction::components::I128Sum as MaspAmount;
use namada_core::masp::encode_asset_type;
use namada_core::storage::Epoch;
use namada_storage::{Error, ResultExt};
use namada_storage::conversion_state::ConversionLeaf;
use namada_storage::{Error, ResultExt};
use namada_trans_token::storage_key::balance_key;
use namada_trans_token::{MaspDigitPos, NATIVE_MAX_DECIMAL_PLACES};
use rayon::iter::{
Expand Down

0 comments on commit 852d366

Please sign in to comment.