-
Notifications
You must be signed in to change notification settings - Fork 17
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
aa9ad0f
f02e547
ea23021
bb6f43b
605fab4
e2eb006
726550b
01d141e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
); | ||
|
||
|
@@ -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 | ||
current_initialized_rewards += *reward - initial_payment; | ||
total_contributors += 1; | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
There was a problem hiding this comment.
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 :)