diff --git a/pallets/farming/src/boost.rs b/pallets/farming/src/boost.rs index fed7fa3ec..fe6b5e9ec 100644 --- a/pallets/farming/src/boost.rs +++ b/pallets/farming/src/boost.rs @@ -107,7 +107,7 @@ impl Pallet { let current_block_number: T::BlockNumber = frame_system::Pallet::::block_number(); boost_pool_info.start_round = current_block_number; boost_pool_info.round_length = round_length; - boost_pool_info.end_round = current_block_number + round_length; + boost_pool_info.end_round = current_block_number.saturating_add(round_length); boost_pool_info.total_votes = Zero::zero(); BoostPoolInfos::::set(boost_pool_info); let _ = BoostVotingPools::::clear(u32::max_value(), None); @@ -152,7 +152,8 @@ impl Pallet { .ok(); let current_block_number: T::BlockNumber = frame_system::Pallet::::block_number(); boost_pool_info.start_round = current_block_number; - boost_pool_info.end_round = current_block_number + boost_pool_info.round_length; + boost_pool_info.end_round = + current_block_number.saturating_add(boost_pool_info.round_length); boost_pool_info.total_votes = Zero::zero(); Self::deposit_event(Event::RoundStart { round_length: boost_pool_info.round_length }); BoostPoolInfos::::set(boost_pool_info); diff --git a/pallets/farming/src/gauge.rs b/pallets/farming/src/gauge.rs index 97c8e4d5e..432804b5f 100644 --- a/pallets/farming/src/gauge.rs +++ b/pallets/farming/src/gauge.rs @@ -173,7 +173,12 @@ where ensure!( gauge_pool_info.max_block >= - gauge_info.gauge_stop_block - gauge_info.gauge_start_block + gauge_block, + gauge_info + .gauge_stop_block + .checked_sub(&gauge_info.gauge_start_block) + .ok_or(ArithmeticError::Overflow)? + .checked_add(&gauge_block) + .ok_or(ArithmeticError::Overflow)?, Error::::GaugeMaxBlockOverflow ); @@ -190,17 +195,25 @@ where let time_factor_a = gauge_value .saturated_into::() .checked_mul( - (gauge_info.gauge_stop_block - current_block_number) - .saturated_into::(), + (gauge_info + .gauge_stop_block + .checked_sub(¤t_block_number) + .ok_or(ArithmeticError::Overflow)?) + .saturated_into::(), ) .ok_or(ArithmeticError::Overflow)?; let time_factor_b = gauge_block .saturated_into::() .checked_mul( - (gauge_value + gauge_info.gauge_amount).saturated_into::(), + (gauge_value + .checked_add(&gauge_info.gauge_amount) + .ok_or(ArithmeticError::Overflow)?) + .saturated_into::(), ) .ok_or(ArithmeticError::Overflow)?; - let incease_total_time_factor = time_factor_a + time_factor_b; + let incease_total_time_factor = time_factor_a + .checked_add(time_factor_b) + .ok_or(ArithmeticError::Overflow)?; gauge_info.total_time_factor = gauge_info .total_time_factor .checked_add(incease_total_time_factor) @@ -210,8 +223,10 @@ where .gauge_amount .saturated_into::() .checked_mul( - (current_block_number - gauge_info.gauge_last_block) - .saturated_into::(), + (current_block_number + .checked_sub(&gauge_info.gauge_last_block) + .ok_or(ArithmeticError::Overflow)?) + .saturated_into::(), ) .ok_or(ArithmeticError::Overflow)?; gauge_info.latest_time_factor = gauge_info @@ -226,7 +241,10 @@ where .gauge_amount .checked_add(&gauge_value) .ok_or(ArithmeticError::Overflow)?; - gauge_info.gauge_stop_block = gauge_info.gauge_stop_block + gauge_block; + gauge_info.gauge_stop_block = gauge_info + .gauge_stop_block + .checked_add(&gauge_block) + .ok_or(ArithmeticError::Overflow)?; gauge_pool_info.total_time_factor = gauge_pool_info .total_time_factor @@ -274,11 +292,16 @@ where .gauge_amount .saturated_into::() .checked_mul( - (start_block - gauge_info.gauge_last_block).saturated_into::(), + (start_block + .checked_sub(&gauge_info.gauge_last_block) + .ok_or(ArithmeticError::Overflow)?) + .saturated_into::(), ) .ok_or(ArithmeticError::Overflow)?; let gauge_rate = Perbill::from_rational( - latest_claimed_time_factor - gauge_info.claimed_time_factor, + latest_claimed_time_factor + .checked_sub(gauge_info.claimed_time_factor) + .ok_or(ArithmeticError::Overflow)?, gauge_pool_info.total_time_factor, ); let total_shares = @@ -445,11 +468,16 @@ where .gauge_amount .saturated_into::() .checked_mul( - (start_block - gauge_info.gauge_last_block).saturated_into::(), + (start_block + .checked_sub(&gauge_info.gauge_last_block) + .ok_or(ArithmeticError::Overflow)?) + .saturated_into::(), ) .ok_or(ArithmeticError::Overflow)?; let gauge_rate = Perbill::from_rational( - latest_claimed_time_factor - gauge_info.claimed_time_factor, + latest_claimed_time_factor + .checked_sub(gauge_info.claimed_time_factor) + .ok_or(ArithmeticError::Overflow)?, gauge_pool_info.total_time_factor, ); let total_shares = diff --git a/pallets/farming/src/lib.rs b/pallets/farming/src/lib.rs index 5549aceb8..db5314028 100644 --- a/pallets/farming/src/lib.rs +++ b/pallets/farming/src/lib.rs @@ -18,8 +18,6 @@ // Ensure we're `no_std` when compiling for Wasm. #![cfg_attr(not(feature = "std"), no_std)] -#![allow(deprecated)] // TODO: remove_prefix: Use `clear_prefix` instead - #[cfg(test)] mod mock; @@ -216,6 +214,7 @@ pub mod pallet { NobodyVoting, NotInWhitelist, PercentOverflow, + PoolNotCleared, } #[pallet::storage] @@ -336,8 +335,8 @@ pub mod pallet { { pool_info.block_startup = Some(n); pool_info.state = PoolState::Ongoing; + PoolInfos::::insert(pid, &pool_info); } - PoolInfos::::insert(pid, &pool_info); }, _ => (), }); @@ -573,7 +572,8 @@ pub mod pallet { let share_info = Self::shares_and_withdrawn_rewards(&pid, &exchanger) .ok_or(Error::::ShareInfoNotExists)?; ensure!( - share_info.claim_last_block + pool_info.claim_limit_time <= current_block_number, + share_info.claim_last_block.saturating_add(pool_info.claim_limit_time) <= + current_block_number, Error::::CanNotClaim ); @@ -742,7 +742,8 @@ pub mod pallet { pool_info.state == PoolState::Retired || pool_info.state == PoolState::UnCharged, Error::::InvalidPoolState ); - SharesAndWithdrawnRewards::::remove_prefix(pid, None); + let res = SharesAndWithdrawnRewards::::clear_prefix(pid, u32::max_value(), None); + ensure!(res.maybe_cursor.is_none(), Error::::PoolNotCleared); PoolInfos::::remove(pid); Self::deposit_event(Event::FarmingPoolKilled { pid }); diff --git a/pallets/slp/src/benchmarking.rs b/pallets/slp/src/benchmarking.rs index 71efbfb79..c44867ea0 100644 --- a/pallets/slp/src/benchmarking.rs +++ b/pallets/slp/src/benchmarking.rs @@ -17,7 +17,6 @@ // along with this program. If not, see . // Ensure we're `no_std` when compiling for Wasm. -#![recursion_limit = "256"] #![cfg(feature = "runtime-benchmarks")] use frame_benchmarking::{account, benchmarks, v1::BenchmarkError, whitelisted_caller}; diff --git a/pallets/slp/src/lib.rs b/pallets/slp/src/lib.rs index 53843d5fe..90848d426 100644 --- a/pallets/slp/src/lib.rs +++ b/pallets/slp/src/lib.rs @@ -17,6 +17,7 @@ // along with this program. If not, see . #![cfg_attr(not(feature = "std"), no_std)] +#![recursion_limit = "256"] extern crate core; diff --git a/pallets/system-maker/src/benchmarking.rs b/pallets/system-maker/src/benchmarking.rs index 336911c7f..d048f5d23 100644 --- a/pallets/system-maker/src/benchmarking.rs +++ b/pallets/system-maker/src/benchmarking.rs @@ -20,14 +20,18 @@ #![cfg(feature = "runtime-benchmarks")] -use crate::{BalanceOf, Call, Config, Info, Pallet as SystemMaker, Pallet}; +use crate::{BalanceOf, Call, Config, Info, Pallet as SystemMaker, Pallet, *}; use frame_benchmarking::v1::{account, benchmarks, BenchmarkError}; -use frame_support::{assert_ok, traits::EnsureOrigin}; +use frame_support::{ + assert_ok, + traits::{EnsureOrigin, Hooks}, +}; use frame_system::RawOrigin; use node_primitives::{CurrencyId, TokenSymbol}; use orml_traits::MultiCurrency; use sp_core::Get; use sp_runtime::traits::{AccountIdConversion, UniqueSaturatedFrom}; +// use crate::{Pallet as SystemMaker, *}; benchmarks! { set_config { @@ -66,5 +70,21 @@ benchmarks! { T::MultiCurrency::deposit(CurrencyId::Token(TokenSymbol::DOT), &pallet_account, BalanceOf::::unique_saturated_from(100000000000u128))?; }: _(origin,CurrencyId::Token(TokenSymbol::DOT),BalanceOf::::unique_saturated_from(10000000000u128)) + on_idle { + let origin = T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?; + assert_ok!(SystemMaker::::set_config( + T::ControlOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?, + CurrencyId::Token(TokenSymbol::KSM), + Info { + vcurrency_id: CurrencyId::VToken(TokenSymbol::KSM), + annualization: 600_000u32, + granularity: 1000u32.into(), + minimum_redeem: 20000u32.into() + }, + )); + }: { + SystemMaker::::on_idle(BlockNumberFor::::from(0u32),Weight::from_parts(0, u64::MAX)); + } + impl_benchmark_test_suite!(SystemMaker,crate::mock::ExtBuilder::default().build(),crate::mock::Runtime); } diff --git a/pallets/system-maker/src/lib.rs b/pallets/system-maker/src/lib.rs index a7fa46939..42f1f5c95 100644 --- a/pallets/system-maker/src/lib.rs +++ b/pallets/system-maker/src/lib.rs @@ -142,7 +142,7 @@ pub mod pallet { Self::handle_redeem_by_currency_id(&system_maker, &info, redeem_amount); } } - Weight::zero() + T::WeightInfo::on_idle() } } diff --git a/pallets/system-maker/src/weights.rs b/pallets/system-maker/src/weights.rs index c0b0effac..621853e51 100644 --- a/pallets/system-maker/src/weights.rs +++ b/pallets/system-maker/src/weights.rs @@ -59,6 +59,7 @@ pub trait WeightInfo { fn charge() -> Weight; fn close() -> Weight; fn payout() -> Weight; + fn on_idle() -> Weight; } /// Weights for bifrost_system_maker using the Bifrost node and recommended hardware. @@ -115,6 +116,20 @@ impl WeightInfo for BifrostWeight { .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } + /// Storage: SystemMaker Infos (r:2 w:0) + /// Proof Skipped: SystemMaker Infos (max_values: None, max_size: None, mode: Measured) + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: Tokens Accounts (r:2 w:0) + /// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen) + fn on_idle() -> Weight { + // Proof Size summary in bytes: + // Measured: `709` + // Estimated: `11344` + // Minimum execution time: 591_941 nanoseconds. + Weight::from_parts(594_646_000, 11344) + .saturating_add(T::DbWeight::get().reads(5)) + } } // For backwards compatibility and tests @@ -170,4 +185,18 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) } + /// Storage: SystemMaker Infos (r:2 w:0) + /// Proof Skipped: SystemMaker Infos (max_values: None, max_size: None, mode: Measured) + /// Storage: ParachainInfo ParachainId (r:1 w:0) + /// Proof: ParachainInfo ParachainId (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen) + /// Storage: Tokens Accounts (r:2 w:0) + /// Proof: Tokens Accounts (max_values: None, max_size: Some(118), added: 2593, mode: MaxEncodedLen) + fn on_idle() -> Weight { + // Proof Size summary in bytes: + // Measured: `709` + // Estimated: `11344` + // Minimum execution time: 591_941 nanoseconds. + Weight::from_parts(594_646_000, 11344) + .saturating_add(RocksDbWeight::get().reads(5)) + } } diff --git a/pallets/ve-minting/src/lib.rs b/pallets/ve-minting/src/lib.rs index 0f1849bb3..d33de65a8 100644 --- a/pallets/ve-minting/src/lib.rs +++ b/pallets/ve-minting/src/lib.rs @@ -403,7 +403,7 @@ pub mod pallet { .checked_mul(&T::Week::get()) .ok_or(ArithmeticError::Overflow)?; for _i in 0..255 { - t_i += T::Week::get(); + t_i = t_i.checked_add(&T::Week::get()).ok_or(ArithmeticError::Overflow)?; let mut d_slope = Zero::zero(); if t_i > current_block_number { t_i = current_block_number @@ -438,7 +438,7 @@ pub mod pallet { last_checkpoint = t_i; last_point.block = t_i; - g_epoch += U256::one(); + g_epoch = g_epoch.checked_add(U256::one()).ok_or(ArithmeticError::Overflow)?; // Fill for the current block, if applicable if t_i == current_block_number { @@ -494,7 +494,9 @@ pub mod pallet { } // Now handle user history - let user_epoch = Self::user_point_epoch(addr) + U256::one(); + let user_epoch = Self::user_point_epoch(addr) + .checked_add(U256::one()) + .ok_or(ArithmeticError::Overflow)?; UserPointEpoch::::insert(addr, user_epoch); u_new.block = current_block_number; u_new.amount = Self::locked(addr).amount; @@ -512,10 +514,10 @@ pub mod pallet { let current_block_number: T::BlockNumber = frame_system::Pallet::::block_number(); let mut _locked = locked_balance; let supply_before = Self::supply(); - Supply::::set(supply_before + value); + Supply::::set(supply_before.checked_add(&value).ok_or(ArithmeticError::Overflow)?); let old_locked = _locked.clone(); - _locked.amount += value; + _locked.amount = _locked.amount.checked_add(&value).ok_or(ArithmeticError::Overflow)?; if unlock_time != Zero::zero() { _locked.end = unlock_time } @@ -537,7 +539,10 @@ pub mod pallet { end: _locked.end, now: current_block_number, }); - Self::deposit_event(Event::Supply { supply_before, supply: supply_before + value }); + Self::deposit_event(Event::Supply { + supply_before, + supply: supply_before.checked_add(&value).ok_or(ArithmeticError::Overflow)?, + }); Ok(()) } @@ -597,12 +602,18 @@ pub mod pallet { if _min >= _max { break; } - let _mid = (_min + _max + 1) / 2; + let _mid = (_min + .checked_add(_max) + .ok_or(ArithmeticError::Overflow)? + .checked_add(U256::one()) + .ok_or(ArithmeticError::Overflow)?) + .checked_div(U256::from(2_u128)) + .ok_or(ArithmeticError::Overflow)?; if Self::user_point_history(addr, _mid).block <= block { _min = _mid } else { - _max = _mid - 1 + _max = _mid.checked_sub(U256::one()).ok_or(ArithmeticError::Overflow)? } } diff --git a/pallets/vstoken-conversion/src/lib.rs b/pallets/vstoken-conversion/src/lib.rs index 6be223637..17307b609 100644 --- a/pallets/vstoken-conversion/src/lib.rs +++ b/pallets/vstoken-conversion/src/lib.rs @@ -162,7 +162,7 @@ pub mod pallet { #[pallet::call] impl Pallet { #[pallet::call_index(0)] - #[pallet::weight(10000)] + #[pallet::weight(T::WeightInfo::vsbond_convert_to_vstoken())] pub fn vsbond_convert_to_vstoken( origin: OriginFor, vs_bond_currency_id: CurrencyIdOf, @@ -237,7 +237,7 @@ pub mod pallet { } #[pallet::call_index(1)] - #[pallet::weight(10000)] + #[pallet::weight(T::WeightInfo::vstoken_convert_to_vsbond())] pub fn vstoken_convert_to_vsbond( origin: OriginFor, currency_id: CurrencyIdOf, @@ -317,7 +317,7 @@ pub mod pallet { } #[pallet::call_index(2)] - #[pallet::weight(0)] + #[pallet::weight(T::WeightInfo::set_exchange_fee())] pub fn set_exchange_fee( origin: OriginFor, exchange_fee: VstokenConversionExchangeFee>, @@ -333,7 +333,7 @@ pub mod pallet { } #[pallet::call_index(3)] - #[pallet::weight(0)] + #[pallet::weight(T::WeightInfo::set_exchange_rate())] pub fn set_exchange_rate( origin: OriginFor, lease: i32, @@ -350,7 +350,7 @@ pub mod pallet { } #[pallet::call_index(4)] - #[pallet::weight(0)] + #[pallet::weight(T::WeightInfo::set_relaychain_lease())] pub fn set_relaychain_lease(origin: OriginFor, lease: u32) -> DispatchResult { T::ControlOrigin::ensure_origin(origin)?; diff --git a/pallets/vtoken-minting/src/lib.rs b/pallets/vtoken-minting/src/lib.rs index ccc977aaa..affee5592 100644 --- a/pallets/vtoken-minting/src/lib.rs +++ b/pallets/vtoken-minting/src/lib.rs @@ -786,24 +786,35 @@ pub mod pallet { pub fn add_time_unit(a: TimeUnit, b: TimeUnit) -> Result { let result = match a { TimeUnit::Era(era_a) => match b { - TimeUnit::Era(era_b) => TimeUnit::Era(era_a + era_b), + TimeUnit::Era(era_b) => TimeUnit::Era( + era_a.checked_add(era_b).ok_or(Error::::CalculationOverflow)?, + ), _ => return Err(Error::::Unexpected.into()), }, TimeUnit::Round(round_a) => match b { - TimeUnit::Round(round_b) => TimeUnit::Round(round_a + round_b), + TimeUnit::Round(round_b) => TimeUnit::Round( + round_a.checked_add(round_b).ok_or(Error::::CalculationOverflow)?, + ), _ => return Err(Error::::Unexpected.into()), }, TimeUnit::SlashingSpan(slashing_span_a) => match b { - TimeUnit::SlashingSpan(slashing_span_b) => - TimeUnit::SlashingSpan(slashing_span_a + slashing_span_b), + TimeUnit::SlashingSpan(slashing_span_b) => TimeUnit::SlashingSpan( + slashing_span_a + .checked_add(slashing_span_b) + .ok_or(Error::::CalculationOverflow)?, + ), _ => return Err(Error::::Unexpected.into()), }, TimeUnit::Kblock(kblock_a) => match b { - TimeUnit::Kblock(kblock_b) => TimeUnit::Kblock(kblock_a + kblock_b), + TimeUnit::Kblock(kblock_b) => TimeUnit::Kblock( + kblock_a.checked_add(kblock_b).ok_or(Error::::CalculationOverflow)?, + ), _ => return Err(Error::::Unexpected.into()), }, TimeUnit::Hour(hour_a) => match b { - TimeUnit::Hour(hour_b) => TimeUnit::Hour(hour_a + hour_b), + TimeUnit::Hour(hour_b) => TimeUnit::Hour( + hour_a.checked_add(hour_b).ok_or(Error::::CalculationOverflow)?, + ), _ => return Err(Error::::Unexpected.into()), }, // _ => return Err(Error::::Unexpected.into()),