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 6 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
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
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 @@ -470,6 +470,8 @@ pub mod pallet {
claimed_reward: initial_payment,
};

// It should be safe not to use saturating_add here
// as we already checked before that these rewards do not overflow existing ones
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could consider using an assert here to express this. That may or may not be a good idea :)

current_initialized_rewards += *reward - initial_payment;
total_contributors += 1;

Expand All @@ -478,6 +480,8 @@ pub mod pallet {
// the native account has already some rewards in, we add the new ones
AccountsPayable::<T>::insert(
native_account,
// It should be safe not to use saturating_add here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to still use the saturating_add in those cases no? Is there a cost to it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small one in terms of a couple of checks (the value being greater than the maximum). But I guess they are minimal, so I can also add it here

// as we already checked before that these rewards do not overflow existing ones
RewardInfo {
total_reward: inserted_reward_info.total_reward
+ reward_info.total_reward,
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
);
});
}