Skip to content

Commit 6720fcd

Browse files
authored
update saturating calculation (#1016)
1 parent 8285019 commit 6720fcd

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

rewards/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<T: Config> Pallet<T> {
171171
.saturating_mul(total_reward.to_owned().saturated_into::<u128>().into())
172172
.checked_div(initial_total_shares.to_owned().saturated_into::<u128>().into())
173173
.unwrap_or_default()
174-
.as_u128()
174+
.saturated_into::<u128>()
175175
.saturated_into()
176176
};
177177
*total_reward = total_reward.saturating_add(reward_inflation);
@@ -239,7 +239,7 @@ impl<T: Config> Pallet<T> {
239239
.saturating_mul(withdrawn_reward.to_owned().saturated_into::<u128>().into())
240240
.checked_div(old_share.saturated_into::<u128>().into())
241241
.unwrap_or_default()
242-
.as_u128()
242+
.saturated_into::<u128>()
243243
.saturated_into();
244244

245245
if let Some((total_reward, total_withdrawn_reward)) =

rewards/src/mock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use std::collections::HashMap;
1212
use crate as rewards;
1313

1414
pub type AccountId = u128;
15-
pub type Balance = u64;
16-
pub type Share = u64;
15+
pub type Balance = u128;
16+
pub type Share = u128;
1717
pub type PoolId = u32;
1818
pub type CurrencyId = u32;
1919

rewards/src/tests.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,18 @@ fn add_share_should_work() {
9191
);
9292

9393
// overflow occurs when saturating calculation
94-
assert_ok!(RewardsModule::add_share(&ALICE, &DOT_POOL, u64::MAX));
94+
assert_ok!(RewardsModule::add_share(&ALICE, &DOT_POOL, u128::MAX));
9595

9696
assert_eq!(
9797
RewardsModule::pool_infos(DOT_POOL),
9898
PoolInfo {
99-
total_shares: u64::MAX,
100-
rewards: vec![(NATIVE_COIN, (u64::MAX, u64::MAX))].into_iter().collect()
99+
total_shares: u128::MAX,
100+
rewards: vec![(NATIVE_COIN, (u128::MAX, u128::MAX))].into_iter().collect()
101101
}
102102
);
103103
assert_eq!(
104104
RewardsModule::shares_and_withdrawn_rewards(DOT_POOL, ALICE),
105-
(u64::MAX, vec![(NATIVE_COIN, u64::MAX)].into_iter().collect())
105+
(u128::MAX, vec![(NATIVE_COIN, u128::MAX)].into_iter().collect())
106106
);
107107
});
108108
}
@@ -663,3 +663,29 @@ fn minimal_share_should_be_enforced() {
663663
assert_ok!(RewardsModule::transfer_share_and_rewards(&ALICE, &DOT_POOL, 5, &BOB));
664664
});
665665
}
666+
667+
#[test]
668+
fn overflow_should_work() {
669+
ExtBuilder::default().build().execute_with(|| {
670+
assert_ok!(RewardsModule::add_share(&ALICE, &DOT_POOL, 10));
671+
672+
// The DOT pool accumulate 21 rewards in the NATIVE COIN
673+
assert_ok!(RewardsModule::accumulate_reward(&DOT_POOL, NATIVE_COIN, 21));
674+
assert_eq!(
675+
RewardsModule::pool_infos(DOT_POOL),
676+
PoolInfo {
677+
total_shares: 10,
678+
rewards: vec![(NATIVE_COIN, (21, 0))].into_iter().collect()
679+
}
680+
);
681+
682+
assert_ok!(RewardsModule::add_share(&ALICE, &DOT_POOL, u128::MAX));
683+
assert_eq!(
684+
RewardsModule::pool_infos(DOT_POOL),
685+
PoolInfo {
686+
total_shares: u128::MAX,
687+
rewards: vec![(NATIVE_COIN, (u128::MAX, u128::MAX))].into_iter().collect()
688+
}
689+
);
690+
});
691+
}

0 commit comments

Comments
 (0)