diff --git a/src/lib.rs b/src/lib.rs index 9e5b6c2..c59950e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,8 +189,8 @@ pub mod pallet { ensure!( proof.verify(payload.as_slice(), &relay_account.clone().into()), Error::::InvalidClaimSignature + ); - // We ensure the relay chain id wast not yet associated to avoid multi-claiming ensure!( ClaimedRelayChainIds::::get(&relay_account).is_none(), @@ -412,12 +412,12 @@ pub mod pallet { let incoming_rewards: BalanceOf = rewards .iter() .fold(0u32.into(), |acc: BalanceOf, (_, _, reward)| { - acc + *reward + acc.saturating_add(*reward) }); // Ensure we dont go over funds ensure!( - current_initialized_rewards + incoming_rewards <= Self::pot(), + current_initialized_rewards.saturating_add(incoming_rewards) <= Self::pot(), Error::::BatchBeyondFundPot ); @@ -469,9 +469,9 @@ pub mod pallet { total_reward: *reward, claimed_reward: initial_payment, }; - - current_initialized_rewards += *reward - initial_payment; - total_contributors += 1; + + current_initialized_rewards = current_initialized_rewards.saturating_add((*reward).saturating_sub(initial_payment)); + total_contributors = total_contributors.saturating_add(1); if let Some(native_account) = native_account { if let Some(inserted_reward_info) = AccountsPayable::::get(native_account) { @@ -480,9 +480,9 @@ pub mod pallet { native_account, RewardInfo { total_reward: inserted_reward_info.total_reward - + reward_info.total_reward, + .saturating_add(reward_info.total_reward), claimed_reward: inserted_reward_info.claimed_reward - + reward_info.claimed_reward, + .saturating_add(reward_info.claimed_reward), }, ); } else { diff --git a/src/tests.rs b/src/tests.rs index 78bdd02..f5e48dd 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -877,3 +877,26 @@ fn test_initialization_errors() { ); }); } + +#[test] +fn test_assert_we_cannot_overflow_at_init() { + empty().execute_with(|| { + // The init relay block gets inserted + roll_to(2); + assert_ok!(Crowdloan::initialize_reward_vec( + Origin::root(), + vec![([1u8; 32].into(), Some(1), 500u32.into()),] + )); + // This should overflow + assert_noop!( + Crowdloan::initialize_reward_vec( + Origin::root(), + vec![ + ([2u8; 32].into(), Some(2), 1), + ([3u8; 32].into(), Some(3), u128::MAX), + ] + ), + Error::::BatchBeyondFundPot + ); + }); +}