Skip to content

Commit 963d2ff

Browse files
committed
Optimize the reward update process by requiring the AllowedConversions be precomputed.
1 parent 676d5d3 commit 963d2ff

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

crates/shielded_token/src/conversion.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,20 @@ use namada_core::masp::MaspEpoch;
2727
use namada_core::token::MaspDigitPos;
2828
use namada_core::token::{Amount, DenominatedAmount, Denomination};
2929
use namada_core::uint::Uint;
30+
#[cfg(any(feature = "multicore", test))]
3031
use namada_state::iter_prefix_with_filter_map;
3132
use namada_systems::{parameters, trans_token};
3233

34+
#[cfg(any(feature = "multicore", test))]
3335
use crate::storage_key::{
34-
is_masp_conversion_key, masp_conversion_key_prefix, masp_kd_gain_key,
35-
masp_kp_gain_key, masp_last_inflation_key, masp_last_locked_amount_key,
36-
masp_locked_amount_target_key, masp_max_reward_rate_key,
37-
masp_reward_precision_key,
36+
is_masp_conversion_key, masp_assets_hash_key, masp_conversion_key_prefix,
37+
masp_token_map_key,
38+
};
39+
use crate::storage_key::{
40+
masp_kd_gain_key, masp_kp_gain_key, masp_last_inflation_key,
41+
masp_last_locked_amount_key, masp_locked_amount_target_key,
42+
masp_max_reward_rate_key, masp_reward_precision_key,
3843
};
39-
#[cfg(any(feature = "multicore", test))]
40-
use crate::storage_key::{masp_assets_hash_key, masp_token_map_key};
4144
#[cfg(any(feature = "multicore", test))]
4245
use crate::{ConversionLeaf, Error, OptionExt, ResultExt};
4346
use crate::{Result, StorageRead, StorageWrite, WithConversionState};
@@ -525,8 +528,6 @@ fn apply_stored_conversion_updates<S>(storage: &mut S) -> Result<()>
525528
where
526529
S: StorageWrite + StorageRead + WithConversionState,
527530
{
528-
use masp_primitives::transaction::components::I128Sum;
529-
530531
let conversion_key_prefix = masp_conversion_key_prefix();
531532
let mut conversion_updates = BTreeMap::new();
532533
// Read conversion updates from storage and store them in a map
@@ -555,8 +556,7 @@ where
555556
);
556557
continue;
557558
};
558-
// This operation will be expensive for large conversions
559-
leaf.conversion = From::<I128Sum>::from(conv);
559+
leaf.conversion = conv;
560560
}
561561
// Delete the updates now that they have been applied
562562
storage.delete_prefix(&conversion_key_prefix)?;

crates/tests/src/integration/masp.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::str::FromStr;
44

55
use color_eyre::eyre::Result;
66
use color_eyre::owo_colors::OwoColorize;
7+
use masp_primitives::convert::AllowedConversion;
78
use masp_primitives::transaction::components::I128Sum;
89
use namada_apps_lib::wallet::defaults::{
910
get_unencrypted_keypair, is_use_device,
@@ -1767,7 +1768,10 @@ fn reset_conversions() -> Result<()> {
17671768
precision_btcs
17681769
.entry((epoch, digit))
17691770
.or_insert_with(|| {
1770-
I128Sum::from_pair(asset_type(epoch, digit), PRECISION)
1771+
AllowedConversion::from(I128Sum::from_pair(
1772+
asset_type(epoch, digit),
1773+
PRECISION,
1774+
))
17711775
})
17721776
.clone()
17731777
};
@@ -1784,7 +1788,7 @@ fn reset_conversions() -> Result<()> {
17841788
// Write the new BTC conversions to memory
17851789
for digit in token::MaspDigitPos::iter() {
17861790
// -PRECISION BTC[ep, digit] + PRECISION BTC[current_ep, digit]
1787-
let mut reward = I128Sum::zero();
1791+
let mut reward: AllowedConversion = I128Sum::zero().into();
17881792
for epoch in MaspEpoch::iter_bounds_inclusive(
17891793
MaspEpoch::zero(),
17901794
node.current_masp_epoch().prev().unwrap(),

0 commit comments

Comments
 (0)