Skip to content

Commit

Permalink
fix: unsafe use of arithmetic ops
Browse files Browse the repository at this point in the history
  • Loading branch information
clostao committed Nov 7, 2023
1 parent c08207d commit 3666c21
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
6 changes: 3 additions & 3 deletions pallets/custom-balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ pub mod pallet {

fn split(self, amount: T) -> (Self, Self) {
let first = self.0.min(amount);
let second = self.0 - first;
let second = self.0.saturating_sub(first);
(Self::new(first), Self::new(second))
}

fn merge(self, other: Self) -> Self {
Self::new(self.0 + other.0)
Self::new(self.0.saturating_add(other.0))
}

fn subsume(&mut self, other: Self) {
self.0 += other.0;
self.0 = self.0.saturating_add(other.0);
}

fn offset(self, _other: Self::Opposite) -> SameOrOther<Self, Self> {
Expand Down
6 changes: 5 additions & 1 deletion pallets/dnt-fee-controller/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ pub mod pallet {
.saturating_mul(validator_share.into())
.div_mod(U256::from(100))
.0;
let dapp_fee = fee_in_user_token - validator_fee;

let dapp_fee = match fee_in_user_token.checked_sub(validator_fee) {
Some(v) => v,
None => return Err(Error::<T>::FeeVaultOverflow),
};

pallet_fee_rewards_vault::Pallet::<T>::add_claimable_reward(
validator,
Expand Down
15 changes: 13 additions & 2 deletions pallets/sponsored-transactions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,19 @@ pub mod pallet {

let (transaction_fee_token, _) = Self::get_fee_token_info(&from);

let max_gas_used = match gas_price.checked_mul(transaction_data.gas_limit) {
Some(v) => v,
None => {
return Err(TransactionValidityError::Invalid(
InvalidTransaction::Custom(1),
))
}
};

Self::ensure_sponsor_balance(
meta_trx_sponsor.clone(),
transaction_fee_token,
gas_price * transaction_data.gas_limit,
max_gas_used,
)
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;

Expand Down Expand Up @@ -117,7 +126,9 @@ pub mod pallet {
meta_trx_sponsor: H160,
meta_trx_sponsor_signature: Vec<u8>,
) -> DispatchResult {
SponsorNonce::<T>::mutate(meta_trx_sponsor.clone(), |nonce| *nonce += 1);
SponsorNonce::<T>::mutate(meta_trx_sponsor.clone(), |nonce| {
*nonce = nonce.saturating_add(1)
});

let from = Self::ensure_transaction_signature(transaction.clone())
.map_err(|_| DispatchError::Other("Invalid transaction signature"))?;
Expand Down
12 changes: 8 additions & 4 deletions pallets/validator-set/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use frame_support::{
};
use log;
pub use pallet::*;
use sp_runtime::traits::{Convert, Zero};
use sp_runtime::traits::{Convert, Saturating, Zero};
use sp_std::{collections::btree_set::BTreeSet, prelude::*};

pub const LOG_TARGET: &'static str = "runtime::validator-set";
Expand Down Expand Up @@ -546,7 +546,10 @@ impl<T: Config> Pallet<T> {

// Provides the new set of validators to the session module when session is
// being rotated.
impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> {
impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T>
where
T::BlockNumber: Saturating,
{
// Plan a new session and provide new validator set.

fn new_session(_new_index: u32) -> Option<Vec<T::AccountId>> {
Expand All @@ -568,7 +571,8 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> {

// Get current block number
let session_start_block = T::SessionBlockManager::session_start_block(end_index);
let session_end_block = T::SessionBlockManager::session_start_block(end_index + 1);
let session_end_block =
T::SessionBlockManager::session_start_block(end_index.saturating_add(1));

let mut epoch_block_authors = BTreeSet::<T::AccountId>::new();

Expand All @@ -579,7 +583,7 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> {
if let Some(author) = block_author {
epoch_block_authors.insert(author);
}
i += 1u32.into();
i.saturating_inc();
}

for validator in validators {
Expand Down

0 comments on commit 3666c21

Please sign in to comment.