Skip to content

Commit

Permalink
Merge pull request #645 from ElrondNetwork/clear-unused-entries
Browse files Browse the repository at this point in the history
Clear unused entries
  • Loading branch information
dorin-iancu authored Dec 8, 2022
2 parents dc56329 + 778846c commit 8b7ea4d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
28 changes: 28 additions & 0 deletions dex/farm/tests/farm_multi_user_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use farm_boosted_yields::boosted_yields_factors::{BoostedYieldsConfig, BoostedYi
use farm_setup::multi_user_farm_setup::*;
use permissions_module::{Permissions, PermissionsModule};
use week_timekeeping::WeekTimekeepingModule;
use weekly_rewards_splitting::global_info::WeeklyRewardsGlobalInfo;

#[test]
fn farm_with_no_boost_test() {
Expand Down Expand Up @@ -703,6 +704,33 @@ fn farm_multiple_claim_weeks_with_collect_undistributed_rewards_test() {
farm_setup.check_remaining_boosted_rewards_to_distribute(1, 0);
farm_setup.check_remaining_boosted_rewards_to_distribute(2, 0);
farm_setup.check_remaining_boosted_rewards_to_distribute(3, 0);

// check entries are not empty
farm_setup
.b_mock
.execute_query(&farm_setup.farm_wrapper, |sc| {
assert!(!sc.total_rewards_for_week(1).is_empty());
assert!(!sc.total_energy_for_week(1).is_empty());

assert!(!sc.total_rewards_for_week(3).is_empty());
assert!(!sc.total_energy_for_week(3).is_empty());
})
.assert_ok();

farm_setup.claim_rewards(&second_user, 11, second_farm_token_amount);

// check 3rd entry was cleared automatically
// 1st entry was not cleared, as we paused for too long
farm_setup
.b_mock
.execute_query(&farm_setup.farm_wrapper, |sc| {
assert!(!sc.total_rewards_for_week(1).is_empty());
assert!(!sc.total_energy_for_week(1).is_empty());

assert!(sc.total_rewards_for_week(3).is_empty());
assert!(sc.total_energy_for_week(3).is_empty());
})
.assert_ok();
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use common_types::Week;
use energy_query::Energy;
use week_timekeeping::EPOCHS_IN_WEEK;

use crate::USER_MAX_CLAIM_WEEKS;

#[elrond_wasm::module]
pub trait WeeklyRewardsGlobalInfo:
crate::events::WeeklyRewardsSplittingEventsModule
Expand Down Expand Up @@ -75,6 +77,15 @@ pub trait WeeklyRewardsGlobalInfo:
self.total_locked_tokens_for_week(current_week)
.set(&total_tokens);
last_week_tokens_mapper.clear();

// clear entries that are not accessible anymore
// users can claim only for weeks of
// (current_week - 1), (current_week - 2), ... (current_week - USER_MAX_CLAIM_WEEKS)
if current_week > USER_MAX_CLAIM_WEEKS + 1 {
let inaccessible_week = current_week - USER_MAX_CLAIM_WEEKS - 1;
self.total_rewards_for_week(inaccessible_week).clear();
self.total_energy_for_week(inaccessible_week).clear();
}
}

fn update_and_get_total_tokens_amounts_after_user_energy_update(
Expand Down

0 comments on commit 8b7ea4d

Please sign in to comment.