Skip to content

Commit bfb9a32

Browse files
authored
avoid reiterate all the pools (#305)
* avoid reiterate all the pools * fix test
1 parent f278c76 commit bfb9a32

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

rewards/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ decl_module! {
8585
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
8686

8787
fn on_initialize(now: T::BlockNumber) -> Weight {
88+
let mut count = 0;
8889
T::Handler::accumulate_reward(now, | pool, reward_to_accumulate | {
8990
if !reward_to_accumulate.is_zero() {
91+
count += 1;
9092
Pools::<T>::mutate(pool, | pool_info | pool_info.total_rewards = pool_info.total_rewards.saturating_add(reward_to_accumulate));
9193
}
9294
});
93-
T::WeightInfo::on_initialize(Pools::<T>::iter().count() as u32)
95+
T::WeightInfo::on_initialize(count)
9496
}
9597
}
9698
}

rewards/src/mock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl RewardHandler<AccountId, BlockNumber> for Handler {
7979

8080
fn accumulate_reward(
8181
now: BlockNumber,
82-
callback: impl Fn(Self::PoolId, Self::Balance),
82+
mut callback: impl FnMut(Self::PoolId, Self::Balance),
8383
) -> Vec<(Self::CurrencyId, Self::Balance)> {
8484
if now % 2 == 0 {
8585
let mut total_accumulated_rewards = 0;

traits/src/rewards.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub trait RewardHandler<AccountId, BlockNumber> {
1919
/// Accumulate rewards
2020
fn accumulate_reward(
2121
now: BlockNumber,
22-
callback: impl Fn(Self::PoolId, Self::Balance),
22+
callback: impl FnMut(Self::PoolId, Self::Balance),
2323
) -> Vec<(Self::CurrencyId, Self::Balance)>;
2424

2525
/// Payout the reward to `who`

0 commit comments

Comments
 (0)