From bfb9a32d792f2d3343d82ce343c09cfb96b9f27b Mon Sep 17 00:00:00 2001
From: Xiliang Chen <xlchen1291@gmail.com>
Date: Wed, 21 Oct 2020 14:28:07 +1300
Subject: [PATCH] avoid reiterate all the pools (#305)

* avoid reiterate all the pools

* fix test
---
 rewards/src/lib.rs    | 4 +++-
 rewards/src/mock.rs   | 2 +-
 traits/src/rewards.rs | 2 +-
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/rewards/src/lib.rs b/rewards/src/lib.rs
index 44dc305..48053b3 100644
--- a/rewards/src/lib.rs
+++ b/rewards/src/lib.rs
@@ -85,12 +85,14 @@ decl_module! {
 	pub struct Module<T: Trait> for enum Call where origin: T::Origin {
 
 		fn on_initialize(now: T::BlockNumber) -> Weight {
+			let mut count = 0;
 			T::Handler::accumulate_reward(now, | pool, reward_to_accumulate | {
 				if !reward_to_accumulate.is_zero() {
+					count += 1;
 					Pools::<T>::mutate(pool, | pool_info | pool_info.total_rewards = pool_info.total_rewards.saturating_add(reward_to_accumulate));
 				}
 			});
-			T::WeightInfo::on_initialize(Pools::<T>::iter().count() as u32)
+			T::WeightInfo::on_initialize(count)
 		}
 	}
 }
diff --git a/rewards/src/mock.rs b/rewards/src/mock.rs
index 6be5c56..1047321 100644
--- a/rewards/src/mock.rs
+++ b/rewards/src/mock.rs
@@ -79,7 +79,7 @@ impl RewardHandler<AccountId, BlockNumber> for Handler {
 
 	fn accumulate_reward(
 		now: BlockNumber,
-		callback: impl Fn(Self::PoolId, Self::Balance),
+		mut callback: impl FnMut(Self::PoolId, Self::Balance),
 	) -> Vec<(Self::CurrencyId, Self::Balance)> {
 		if now % 2 == 0 {
 			let mut total_accumulated_rewards = 0;
diff --git a/traits/src/rewards.rs b/traits/src/rewards.rs
index 7e545f2..e53b905 100644
--- a/traits/src/rewards.rs
+++ b/traits/src/rewards.rs
@@ -19,7 +19,7 @@ pub trait RewardHandler<AccountId, BlockNumber> {
 	/// Accumulate rewards
 	fn accumulate_reward(
 		now: BlockNumber,
-		callback: impl Fn(Self::PoolId, Self::Balance),
+		callback: impl FnMut(Self::PoolId, Self::Balance),
 	) -> Vec<(Self::CurrencyId, Self::Balance)>;
 
 	/// Payout the reward to `who`
-- 
GitLab