Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow checker in initialize #36

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ pub mod pallet {
ensure!(
proof.verify(payload.as_slice(), &relay_account.clone().into()),
Error::<T>::InvalidClaimSignature

);

// We ensure the relay chain id wast not yet associated to avoid multi-claiming
ensure!(
ClaimedRelayChainIds::<T>::get(&relay_account).is_none(),
Expand Down Expand Up @@ -412,12 +412,12 @@ pub mod pallet {
let incoming_rewards: BalanceOf<T> = rewards
.iter()
.fold(0u32.into(), |acc: BalanceOf<T>, (_, _, 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::<T>::BatchBeyondFundPot
);

Expand Down Expand Up @@ -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::<T>::get(native_account) {
Expand All @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::BatchBeyondFundPot
);
});
}