From 51bf31c7042cc70171a003cc3b014f30217f21b1 Mon Sep 17 00:00:00 2001 From: yooml Date: Tue, 17 Dec 2024 14:23:08 +0800 Subject: [PATCH 1/3] Update current block (#1552) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor: 💡 bb-bnc buy-back farming * refactor: 💡 stable-asset lend-market * fix: 🐛 omission * fix: 🐛 clippy * fix: 🐛 test --- pallets/bb-bnc/src/incentive.rs | 9 +++-- pallets/bb-bnc/src/lib.rs | 46 +++++++++++++++++-------- pallets/bb-bnc/src/mock.rs | 1 + pallets/buy-back/src/lib.rs | 10 ++++-- pallets/buy-back/src/mock.rs | 2 ++ pallets/buy-back/src/tests.rs | 10 ++++-- pallets/clouds-convert/src/mock.rs | 1 + pallets/farming/src/boost.rs | 8 +++-- pallets/farming/src/gauge.rs | 2 +- pallets/farming/src/lib.rs | 15 +++++--- pallets/farming/src/mock.rs | 2 ++ pallets/farming/src/rewards.rs | 10 +++--- pallets/lend-market/src/benchmarking.rs | 4 +-- pallets/lend-market/src/farming.rs | 4 +-- pallets/lend-market/src/lib.rs | 7 ++-- pallets/lend-market/src/mock.rs | 1 + pallets/leverage-staking/src/mock.rs | 2 ++ pallets/slp/src/mocks/mock_kusama.rs | 1 + pallets/stable-asset/src/lib.rs | 16 ++++++--- pallets/stable-asset/src/mock.rs | 1 + pallets/stable-pool/src/mock.rs | 1 + pallets/system-staking/src/mock.rs | 1 + runtime/bifrost-kusama/src/lib.rs | 3 ++ runtime/bifrost-polkadot/src/lib.rs | 5 +++ 24 files changed, 115 insertions(+), 47 deletions(-) diff --git a/pallets/bb-bnc/src/incentive.rs b/pallets/bb-bnc/src/incentive.rs index d31b971f5..57f6b4f92 100644 --- a/pallets/bb-bnc/src/incentive.rs +++ b/pallets/bb-bnc/src/incentive.rs @@ -63,7 +63,8 @@ where impl Pallet { /// Check if the current block number is within the end time of the reward pool pub fn last_time_reward_applicable(pool_id: PoolId) -> BlockNumberFor { - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); if current_block_number < IncentiveConfigs::::get(pool_id).period_finish { current_block_number } else { @@ -76,7 +77,8 @@ impl Pallet { pool_id: PoolId, ) -> Result, BalanceOf>, DispatchError> { let mut conf = IncentiveConfigs::::get(pool_id); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let total_supply = Self::total_supply(current_block_number)?; if total_supply == BalanceOf::::zero() { return Ok(conf.reward_per_token_stored); @@ -262,7 +264,8 @@ impl Pallet { }; Self::update_reward(pool_id, None, None)?; let mut conf = IncentiveConfigs::::get(pool_id); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); if current_block_number >= conf.period_finish { Self::add_reward(&account, &mut conf, &rewards, Zero::zero())?; diff --git a/pallets/bb-bnc/src/lib.rs b/pallets/bb-bnc/src/lib.rs index f21c1db00..c0e8d18e1 100644 --- a/pallets/bb-bnc/src/lib.rs +++ b/pallets/bb-bnc/src/lib.rs @@ -37,8 +37,8 @@ use frame_support::{ pallet_prelude::*, sp_runtime::{ traits::{ - AccountIdConversion, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, Convert, - Saturating, UniqueSaturatedInto, Zero, + AccountIdConversion, BlockNumberProvider, CheckedAdd, CheckedDiv, CheckedMul, + CheckedSub, Convert, Saturating, UniqueSaturatedInto, Zero, }, ArithmeticError, DispatchError, FixedPointNumber, FixedU128, SaturatedConversion, }, @@ -151,6 +151,9 @@ pub mod pallet { #[pallet::constant] type FourYears: Get>; + + /// The current block number provider. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::event] @@ -396,7 +399,8 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - fn on_initialize(n: BlockNumberFor) -> Weight { + fn on_initialize(_n: BlockNumberFor) -> Weight { + let n: BlockNumberFor = T::BlockNumberProvider::current_block_number(); let conf = IncentiveConfigs::::get(BB_BNC_SYSTEM_POOL_ID); if n == conf.period_finish { if let Some(e) = Self::notify_reward_amount( @@ -590,7 +594,8 @@ pub mod pallet { if !TotalLock::::contains_key(currency_id) { TotalLock::::insert(currency_id, BalanceOf::::zero()); } - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); MarkupCoefficient::::insert( currency_id, MarkupCoefficientInfo { @@ -661,7 +666,8 @@ pub mod pallet { let mut u_new = Point::, BlockNumberFor>::default(); let mut new_dslope = 0_i128; let mut g_epoch: U256 = Epoch::::get(); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); if old_locked.end > current_block_number && old_locked.amount > BalanceOf::::zero() { u_old.slope = U256::from(old_locked.amount.saturated_into::()) @@ -835,7 +841,8 @@ pub mod pallet { unlock_time: BlockNumberFor, locked_balance: LockedBalance, BlockNumberFor>, ) -> DispatchResult { - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let mut _locked = locked_balance; let supply_before = Supply::::get(); let supply_after = supply_before @@ -892,7 +899,8 @@ pub mod pallet { pub(crate) fn balance_of_position_current_block( position: PositionId, ) -> Result, DispatchError> { - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let u_epoch = UserPointEpoch::::get(position); if u_epoch == U256::zero() { return Ok(Zero::zero()); @@ -929,7 +937,8 @@ pub mod pallet { position: PositionId, block: BlockNumberFor, ) -> Result, DispatchError> { - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); ensure!(block <= current_block_number, Error::::Expired); // Binary search @@ -1051,7 +1060,8 @@ pub mod pallet { Ok(()) })?; - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let mut user_markup_info = UserMarkupInfos::::get(&who).unwrap_or_default(); let mut locked_token = @@ -1174,7 +1184,8 @@ pub mod pallet { pub fn refresh_inner(currency_id: CurrencyIdOf) -> DispatchResult { let markup_coefficient = MarkupCoefficient::::get(currency_id).ok_or(Error::::ArgumentsError)?; - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let limit = T::MarkupRefreshLimit::get(); let mut all_refreshed = true; let mut refresh_count = 0; @@ -1339,7 +1350,8 @@ pub mod pallet { /// This function will check the lock and redeem it regardless of whether it has expired. pub fn redeem_unlock_inner(who: &AccountIdOf, position: PositionId) -> DispatchResult { let mut _locked = Locked::::get(position); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); ensure!(_locked.end > current_block_number, Error::::Expired); let fast = Self::redeem_commission(_locked.end - current_block_number)?; Self::withdraw_no_ensure(who, position, _locked, Some(fast)) @@ -1386,7 +1398,8 @@ impl BbBNCInterface, CurrencyIdOf, BalanceOf, Bl let bb_config = BbConfigs::::get(); ensure!(value >= bb_config.min_mint, Error::::BelowMinimumMint); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let _locked: LockedBalance, BlockNumberFor> = Locked::::get(new_position); let real_unlock_time: BlockNumberFor = unlock_time @@ -1432,7 +1445,8 @@ impl BbBNCInterface, CurrencyIdOf, BalanceOf, Bl ) -> DispatchResult { let bb_config = BbConfigs::::get(); let locked: LockedBalance, BlockNumberFor> = Locked::::get(position); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); ensure!(locked.end > current_block_number, Error::::Expired); // Cannot add to expired/non-existent lock let real_unlock_time: BlockNumberFor = unlock_time @@ -1487,7 +1501,8 @@ impl BbBNCInterface, CurrencyIdOf, BalanceOf, Bl _locked.amount > BalanceOf::::zero(), Error::::LockNotExist ); // Need to be executed after create_lock - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); ensure!(_locked.end > current_block_number, Error::::Expired); // Cannot add to expired/non-existent lock Self::_deposit_for(who, position, value, Zero::zero(), _locked)?; Self::deposit_event(Event::AmountIncreased { @@ -1507,7 +1522,8 @@ impl BbBNCInterface, CurrencyIdOf, BalanceOf, Bl #[transactional] fn withdraw_inner(who: &AccountIdOf, position: u128) -> DispatchResult { let mut _locked = Locked::::get(position); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); ensure!(current_block_number >= _locked.end, Error::::Expired); Self::withdraw_no_ensure(who, position, _locked, None) } diff --git a/pallets/bb-bnc/src/mock.rs b/pallets/bb-bnc/src/mock.rs index 631717f00..09a82e777 100644 --- a/pallets/bb-bnc/src/mock.rs +++ b/pallets/bb-bnc/src/mock.rs @@ -261,6 +261,7 @@ impl bb_bnc::Config for Runtime { type FarmingInfo = (); type FourYears = FourYears; type OneYear = OneYear; + type BlockNumberProvider = System; } pub struct ParachainId; diff --git a/pallets/buy-back/src/lib.rs b/pallets/buy-back/src/lib.rs index 9ad800000..4631c3733 100644 --- a/pallets/buy-back/src/lib.rs +++ b/pallets/buy-back/src/lib.rs @@ -36,7 +36,7 @@ use cumulus_primitives_core::ParaId; use frame_support::{ pallet_prelude::*, sp_runtime::{ - traits::{AccountIdConversion, One, Zero}, + traits::{AccountIdConversion, BlockNumberProvider, One, Zero}, Permill, SaturatedConversion, Saturating, }, transactional, PalletId, @@ -95,6 +95,9 @@ pub mod pallet { BalanceOf, BlockNumberFor, >; + + /// The current block number provider. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::event] @@ -196,7 +199,8 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - fn on_initialize(n: BlockNumberFor) -> Weight { + fn on_initialize(_n: BlockNumberFor) -> Weight { + let n: BlockNumberFor = T::BlockNumberProvider::current_block_number(); let buyback_address = T::BuyBackAccount::get().into_account_truncating(); let liquidity_address = T::LiquidityAccount::get().into_account_truncating(); for (currency_id, mut info) in Infos::::iter() { @@ -357,7 +361,7 @@ pub mod pallet { Error::::ZeroDuration ); - let now = frame_system::Pallet::::block_number(); + let now = T::BlockNumberProvider::current_block_number(); let info = Info { min_swap_value, diff --git a/pallets/buy-back/src/mock.rs b/pallets/buy-back/src/mock.rs index 725582d82..587368321 100644 --- a/pallets/buy-back/src/mock.rs +++ b/pallets/buy-back/src/mock.rs @@ -178,6 +178,7 @@ impl bifrost_buy_back::Config for Runtime { type ParachainId = ParaInfo; type CurrencyIdRegister = AssetIdMaps; type BbBNC = BbBNC; + type BlockNumberProvider = System; } pub struct ParaInfo; @@ -490,6 +491,7 @@ impl bb_bnc::Config for Runtime { type FarmingInfo = (); type FourYears = MaxBlock; type OneYear = OneYear; + type BlockNumberProvider = System; } pub struct ExtBuilder { diff --git a/pallets/buy-back/src/tests.rs b/pallets/buy-back/src/tests.rs index 724469afe..917fd03e2 100644 --- a/pallets/buy-back/src/tests.rs +++ b/pallets/buy-back/src/tests.rs @@ -243,8 +243,11 @@ fn on_initialize_no_burn_should_work() { Some(buyback_account.clone()), ); assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000)); + System::set_block_number(1); BuyBack::on_initialize(1); + System::set_block_number(2); BuyBack::on_initialize(2); + System::set_block_number(3); BuyBack::on_initialize(3); assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000); assert_eq!( @@ -301,10 +304,10 @@ fn on_initialize_with_burn_should_work() { Some(buyback_account.clone()), ); assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000)); - BuyBack::on_initialize(>::block_number() + 1); System::set_block_number(System::block_number() + 1); - BuyBack::on_initialize(>::block_number() + 1); + BuyBack::on_initialize(0); System::set_block_number(System::block_number() + 1); + BuyBack::on_initialize(0); assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000); assert_eq!( Currencies::free_balance(VKSM, &zenlink_pair_account_id), @@ -360,6 +363,7 @@ fn on_initialize_with_bias_should_work() { Some(buyback_account.clone()), ); assert_ok!(BuyBack::charge(RuntimeOrigin::signed(ALICE), VKSM, 1000)); + System::set_block_number(1); BuyBack::on_initialize(1); let path = vec![ AssetId::try_convert_from(VKSM, PARAID).unwrap(), @@ -374,7 +378,9 @@ fn on_initialize_with_bias_should_work() { >::block_number() + BlockNumberFor::::from(1u32) )); + System::set_block_number(2); BuyBack::on_initialize(2); + System::set_block_number(3); BuyBack::on_initialize(3); assert_eq!(Currencies::free_balance(VKSM, &buyback_account), 9000); assert_eq!( diff --git a/pallets/clouds-convert/src/mock.rs b/pallets/clouds-convert/src/mock.rs index 58588780e..356c35034 100644 --- a/pallets/clouds-convert/src/mock.rs +++ b/pallets/clouds-convert/src/mock.rs @@ -205,6 +205,7 @@ impl bb_bnc::Config for Runtime { type FarmingInfo = (); type FourYears = MaxBlock; type OneYear = OneYear; + type BlockNumberProvider = System; } pub struct ExtBuilder { diff --git a/pallets/farming/src/boost.rs b/pallets/farming/src/boost.rs index f9b33a158..517b8ff21 100644 --- a/pallets/farming/src/boost.rs +++ b/pallets/farming/src/boost.rs @@ -109,7 +109,8 @@ impl Pallet { ); } - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); boost_pool_info.start_round = current_block_number; boost_pool_info.end_round = current_block_number.saturating_add(round_length); boost_pool_info.total_votes = Zero::zero(); @@ -156,7 +157,8 @@ impl Pallet { Self::deposit_event(Event::RoundStartError { info: e }); }) .ok(); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); boost_pool_info.start_round = current_block_number; boost_pool_info.end_round = current_block_number.saturating_add(boost_pool_info.round_length); @@ -211,7 +213,7 @@ impl Pallet { who: &AccountIdOf, vote_list: Vec<(PoolId, Percent)>, ) -> DispatchResult { - let current_block_number = frame_system::Pallet::::block_number(); + let current_block_number = T::BlockNumberProvider::current_block_number(); let mut boost_pool_info = BoostPoolInfos::::get(); if let Some(user_boost_info) = UserBoostInfos::::get(who) { diff --git a/pallets/farming/src/gauge.rs b/pallets/farming/src/gauge.rs index 79b3f74c4..ee79c7d6f 100644 --- a/pallets/farming/src/gauge.rs +++ b/pallets/farming/src/gauge.rs @@ -189,7 +189,7 @@ where None => (), Some(gid) => { let current_block_number: BlockNumberFor = - frame_system::Pallet::::block_number(); + T::BlockNumberProvider::current_block_number(); let gauge_pool_info = GaugePoolInfos::::get(gid).ok_or(Error::::GaugePoolNotExist)?; let gauge_info = diff --git a/pallets/farming/src/lib.rs b/pallets/farming/src/lib.rs index 7178a8e17..d29e50044 100644 --- a/pallets/farming/src/lib.rs +++ b/pallets/farming/src/lib.rs @@ -39,8 +39,8 @@ use frame_support::{ pallet_prelude::*, sp_runtime::{ traits::{ - AccountIdConversion, AtLeast32BitUnsigned, CheckedAdd, CheckedDiv, CheckedMul, - CheckedSub, Convert, Saturating, Zero, + AccountIdConversion, AtLeast32BitUnsigned, BlockNumberProvider, CheckedAdd, CheckedDiv, + CheckedMul, CheckedSub, Convert, Saturating, Zero, }, ArithmeticError, Perbill, Percent, }, @@ -126,6 +126,9 @@ pub mod pallet { #[pallet::constant] type GaugeRewardIssuer: Get; + + /// The current block number provider. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::event] @@ -410,7 +413,8 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - fn on_initialize(n: BlockNumberFor) -> Weight { + fn on_initialize(_n: BlockNumberFor) -> Weight { + let n: BlockNumberFor = T::BlockNumberProvider::current_block_number(); PoolInfos::::iter().for_each(|(pid, mut pool_info)| match pool_info.state { PoolState::Ongoing => { pool_info.basic_rewards.clone().iter_mut().for_each( @@ -622,7 +626,7 @@ pub mod pallet { if let PoolState::Charged = pool_info.state { let current_block_number: BlockNumberFor = - frame_system::Pallet::::block_number(); + T::BlockNumberProvider::current_block_number(); ensure!( current_block_number >= pool_info.after_block_to_start, Error::::CanNotDeposit @@ -758,7 +762,8 @@ pub mod pallet { Error::::InvalidPoolState ); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let share_info = SharesAndWithdrawnRewards::::get(&pid, &exchanger) .ok_or(Error::::ShareInfoNotExists)?; ensure!( diff --git a/pallets/farming/src/mock.rs b/pallets/farming/src/mock.rs index 08a01260b..824d97e4b 100644 --- a/pallets/farming/src/mock.rs +++ b/pallets/farming/src/mock.rs @@ -150,6 +150,7 @@ impl bifrost_farming::Config for Runtime { type BlockNumberToBalance = ConvertInto; type WhitelistMaximumLimit = WhitelistMaximumLimit; type GaugeRewardIssuer = FarmingGaugeRewardIssuerPalletId; + type BlockNumberProvider = System; } parameter_types! { @@ -182,6 +183,7 @@ impl bb_bnc::Config for Runtime { type FarmingInfo = Farming; type FourYears = MaxBlock; type OneYear = OneYear; + type BlockNumberProvider = System; } ord_parameter_types! { diff --git a/pallets/farming/src/rewards.rs b/pallets/farming/src/rewards.rs index edfcf917d..cd850da0a 100644 --- a/pallets/farming/src/rewards.rs +++ b/pallets/farming/src/rewards.rs @@ -225,7 +225,8 @@ impl Pallet { }, ); - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); let mut share_info = SharesAndWithdrawnRewards::::get(pid, who) .unwrap_or_else(|| ShareInfo::new(who.clone(), current_block_number)); @@ -262,7 +263,8 @@ impl Pallet { Self::claim_rewards(who, pool)?; SharesAndWithdrawnRewards::::mutate(pool, who, |share_info_old| -> DispatchResult { - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); if let Some(mut share_info) = share_info_old.take() { let remove_amount; if let Some(remove_amount_input) = remove_amount_input { @@ -340,7 +342,7 @@ impl Pallet { who, |maybe_share_withdrawn| -> DispatchResult { let current_block_number: BlockNumberFor = - frame_system::Pallet::::block_number(); + T::BlockNumberProvider::current_block_number(); if let Some(share_info) = maybe_share_withdrawn { if share_info.share.is_zero() { return Ok(()); @@ -431,7 +433,7 @@ impl Pallet { |share_info_old| -> DispatchResult { if let Some(mut share_info) = share_info_old.take() { let current_block_number: BlockNumberFor = - frame_system::Pallet::::block_number(); + T::BlockNumberProvider::current_block_number(); let mut tmp: Vec<(BlockNumberFor, BalanceOf)> = Default::default(); share_info.withdraw_list.iter().try_for_each( |(dest_block, remove_value)| -> DispatchResult { diff --git a/pallets/lend-market/src/benchmarking.rs b/pallets/lend-market/src/benchmarking.rs index 77466ff65..b6748310d 100644 --- a/pallets/lend-market/src/benchmarking.rs +++ b/pallets/lend-market/src/benchmarking.rs @@ -198,7 +198,7 @@ benchmarks! { assert_ok!(LendMarket::::mint(SystemOrigin::Signed(caller.clone()).into(), DOT_U, 100_000_000)); assert_ok!(LendMarket::::add_reward(SystemOrigin::Signed(caller.clone()).into(), 1_000_000_000_000_u128)); assert_ok!(LendMarket::::update_market_reward_speed(SystemOrigin::Root.into(), DOT_U, Some(1_000_000), Some(1_000_000))); - let target_height = frame_system::Pallet::::block_number().saturating_add(One::one()); + let target_height = T::BlockNumberProvider::current_block_number().saturating_add(One::one()); frame_system::Pallet::::set_block_number(target_height); }: _(SystemOrigin::Signed(caller.clone())) verify { @@ -213,7 +213,7 @@ benchmarks! { assert_ok!(LendMarket::::mint(SystemOrigin::Signed(caller.clone()).into(), DOT_U, 100_000_000)); assert_ok!(LendMarket::::add_reward(SystemOrigin::Signed(caller.clone()).into(), 1_000_000_000_000_u128)); assert_ok!(LendMarket::::update_market_reward_speed(SystemOrigin::Root.into(), DOT_U, Some(1_000_000), Some(1_000_000))); - let target_height = frame_system::Pallet::::block_number().saturating_add(One::one()); + let target_height = T::BlockNumberProvider::current_block_number().saturating_add(One::one()); frame_system::Pallet::::set_block_number(target_height); }: _(SystemOrigin::Signed(caller.clone()), DOT_U) verify { diff --git a/pallets/lend-market/src/farming.rs b/pallets/lend-market/src/farming.rs index 1cf4736f2..57277aeb1 100644 --- a/pallets/lend-market/src/farming.rs +++ b/pallets/lend-market/src/farming.rs @@ -61,7 +61,7 @@ impl Pallet { } pub(crate) fn update_reward_supply_index(asset_id: AssetIdOf) -> DispatchResult { - let current_block_number = >::block_number(); + let current_block_number = T::BlockNumberProvider::current_block_number(); RewardSupplyState::::try_mutate(asset_id, |supply_state| -> DispatchResult { let delta_block = current_block_number.saturating_sub(supply_state.block); if delta_block.is_zero() { @@ -84,7 +84,7 @@ impl Pallet { } pub(crate) fn update_reward_borrow_index(asset_id: AssetIdOf) -> DispatchResult { - let current_block_number = >::block_number(); + let current_block_number = T::BlockNumberProvider::current_block_number(); RewardBorrowState::::try_mutate(asset_id, |borrow_state| -> DispatchResult { let delta_block = current_block_number.saturating_sub(borrow_state.block); if delta_block.is_zero() { diff --git a/pallets/lend-market/src/lib.rs b/pallets/lend-market/src/lib.rs index acab6c23c..466e0b836 100644 --- a/pallets/lend-market/src/lib.rs +++ b/pallets/lend-market/src/lib.rs @@ -48,8 +48,8 @@ use pallet_traits::{ use sp_core::bounded::BoundedVec; use sp_runtime::{ traits::{ - AccountIdConversion, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, - SaturatedConversion, Saturating, StaticLookup, Zero, + AccountIdConversion, BlockNumberProvider, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, + One, SaturatedConversion, Saturating, StaticLookup, Zero, }, ArithmeticError, FixedPointNumber, FixedU128, }; @@ -128,6 +128,9 @@ pub mod pallet { #[pallet::constant] type MaxLengthLimit: Get; + + /// The current block number provider. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::error] diff --git a/pallets/lend-market/src/mock.rs b/pallets/lend-market/src/mock.rs index 25eed1836..8830570c8 100644 --- a/pallets/lend-market/src/mock.rs +++ b/pallets/lend-market/src/mock.rs @@ -328,6 +328,7 @@ impl Config for Test { type RewardAssetId = RewardAssetId; type LiquidationFreeAssetId = LiquidationFreeAssetId; type MaxLengthLimit = MaxLengthLimit; + type BlockNumberProvider = System; } pub(crate) fn new_test_ext() -> sp_io::TestExternalities { diff --git a/pallets/leverage-staking/src/mock.rs b/pallets/leverage-staking/src/mock.rs index e641510df..79a082ddd 100644 --- a/pallets/leverage-staking/src/mock.rs +++ b/pallets/leverage-staking/src/mock.rs @@ -252,6 +252,7 @@ impl bifrost_stable_asset::Config for Test { type WeightInfo = (); type ListingOrigin = EnsureSignedBy; type EnsurePoolAssetId = EnsurePoolAssetId; + type BlockNumberProvider = System; } impl bifrost_stable_pool::Config for Test { @@ -454,6 +455,7 @@ impl lend_market::Config for Test { type RewardAssetId = RewardAssetId; type LiquidationFreeAssetId = LiquidationFreeAssetId; type MaxLengthLimit = MaxLengthLimit; + type BlockNumberProvider = System; } impl pallet_prices::Config for Test { diff --git a/pallets/slp/src/mocks/mock_kusama.rs b/pallets/slp/src/mocks/mock_kusama.rs index 793c9c5d1..fdae37acb 100644 --- a/pallets/slp/src/mocks/mock_kusama.rs +++ b/pallets/slp/src/mocks/mock_kusama.rs @@ -109,6 +109,7 @@ impl bifrost_stable_asset::Config for Runtime { type WeightInfo = (); type ListingOrigin = EnsureSignedBy; type EnsurePoolAssetId = EnsurePoolAssetId; + type BlockNumberProvider = System; } parameter_types! { diff --git a/pallets/stable-asset/src/lib.rs b/pallets/stable-asset/src/lib.rs index 6a1e43532..10ade71df 100644 --- a/pallets/stable-asset/src/lib.rs +++ b/pallets/stable-asset/src/lib.rs @@ -40,7 +40,10 @@ use parity_scale_codec::{Decode, Encode}; use scale_info::TypeInfo; use sp_core::{U256, U512}; use sp_runtime::{ - traits::{AccountIdConversion, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, Zero}, + traits::{ + AccountIdConversion, BlockNumberProvider, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, + One, Zero, + }, ArithmeticError, DispatchError, SaturatedConversion, }; use sp_std::prelude::*; @@ -352,7 +355,7 @@ pub mod pallet { use orml_traits::MultiCurrency; use parity_scale_codec::Codec; use sp_runtime::{ - traits::{CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, Zero}, + traits::{BlockNumberProvider, CheckedAdd, CheckedDiv, CheckedMul, CheckedSub, One, Zero}, FixedPointOperand, Permill, }; use sp_std::prelude::*; @@ -399,6 +402,9 @@ pub mod pallet { /// The origin which may create pool or modify pool. type ListingOrigin: EnsureOrigin; + + /// The current block number provider. + type BlockNumberProvider: BlockNumberProvider>; } #[pallet::pallet] @@ -984,7 +990,7 @@ impl Pallet { a1: T::AtLeast64BitUnsigned, t1: BlockNumberFor, ) -> Option { - let current_block = frame_system::Pallet::::block_number(); + let current_block = T::BlockNumberProvider::current_block_number(); if current_block < t1 { let time_diff: u128 = current_block.checked_sub(&t0)?.saturated_into(); let time_diff: T::AtLeast64BitUnsigned = time_diff.into(); @@ -1981,7 +1987,7 @@ impl StableAsset for Pallet { let balances = sp_std::vec![Zero::zero(); assets.len()]; frame_system::Pallet::::inc_providers(&swap_id); - let current_block = frame_system::Pallet::::block_number(); + let current_block = T::BlockNumberProvider::current_block_number(); *maybe_pool_info = Some(StableAssetPoolInfo { pool_id, pool_asset, @@ -2391,7 +2397,7 @@ impl StableAsset for Pallet { future_a_block > pool_info.a_block, Error::::ArgumentsError ); - let current_block = frame_system::Pallet::::block_number(); + let current_block = T::BlockNumberProvider::current_block_number(); let initial_a: T::AtLeast64BitUnsigned = Self::get_a( pool_info.a, pool_info.a_block, diff --git a/pallets/stable-asset/src/mock.rs b/pallets/stable-asset/src/mock.rs index 0dbbd5775..951677e05 100644 --- a/pallets/stable-asset/src/mock.rs +++ b/pallets/stable-asset/src/mock.rs @@ -301,6 +301,7 @@ impl stable_asset::Config for Test { type WeightInfo = (); type ListingOrigin = EnsureStableAsset; type EnsurePoolAssetId = EnsurePoolAssetId; + type BlockNumberProvider = System; } // Build genesis storage according to the mock runtime. diff --git a/pallets/stable-pool/src/mock.rs b/pallets/stable-pool/src/mock.rs index 2fbab5916..1196288bc 100644 --- a/pallets/stable-pool/src/mock.rs +++ b/pallets/stable-pool/src/mock.rs @@ -240,6 +240,7 @@ impl bifrost_stable_asset::Config for Test { type WeightInfo = (); type ListingOrigin = EnsureSignedBy; type EnsurePoolAssetId = EnsurePoolAssetId; + type BlockNumberProvider = System; } impl bifrost_stable_pool::Config for Test { diff --git a/pallets/system-staking/src/mock.rs b/pallets/system-staking/src/mock.rs index 05039c96f..f07f22d7f 100644 --- a/pallets/system-staking/src/mock.rs +++ b/pallets/system-staking/src/mock.rs @@ -298,6 +298,7 @@ impl bifrost_farming::Config for Runtime { type BlockNumberToBalance = ConvertInto; type WhitelistMaximumLimit = WhitelistMaximumLimit; type GaugeRewardIssuer = FarmingGaugeRewardIssuerPalletId; + type BlockNumberProvider = System; } parameter_types! { diff --git a/runtime/bifrost-kusama/src/lib.rs b/runtime/bifrost-kusama/src/lib.rs index fb44e0a96..1d2a2a202 100644 --- a/runtime/bifrost-kusama/src/lib.rs +++ b/runtime/bifrost-kusama/src/lib.rs @@ -1378,6 +1378,7 @@ impl bifrost_farming::Config for Runtime { type BlockNumberToBalance = ConvertInto; type WhitelistMaximumLimit = WhitelistMaximumLimit; type GaugeRewardIssuer = FarmingGaugeRewardIssuerPalletId; + type BlockNumberProvider = System; } parameter_types! { @@ -1621,6 +1622,7 @@ impl bifrost_stable_asset::Config for Runtime { type WeightInfo = (); type ListingOrigin = TechAdminOrCouncil; type EnsurePoolAssetId = EnsurePoolAssetId; + type BlockNumberProvider = System; } impl bifrost_stable_pool::Config for Runtime { @@ -1715,6 +1717,7 @@ impl lend_market::Config for Runtime { type RewardAssetId = NativeCurrencyId; type LiquidationFreeAssetId = RelayCurrencyId; type MaxLengthLimit = MaxLengthLimit; + type BlockNumberProvider = System; } parameter_types! { diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index 561716032..77306b1e8 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -1221,6 +1221,7 @@ impl bifrost_farming::Config for Runtime { type BlockNumberToBalance = ConvertInto; type WhitelistMaximumLimit = WhitelistMaximumLimit; type GaugeRewardIssuer = FarmingGaugeRewardIssuerPalletId; + type BlockNumberProvider = System; } parameter_types! { @@ -1302,6 +1303,7 @@ impl bifrost_stable_asset::Config for Runtime { type WeightInfo = (); type ListingOrigin = TechAdminOrCouncil; type EnsurePoolAssetId = EnsurePoolAssetId; + type BlockNumberProvider = System; } impl bifrost_stable_pool::Config for Runtime { @@ -1461,6 +1463,7 @@ impl bb_bnc::Config for Runtime { type FarmingInfo = Farming; type FourYears = MaxBlock; type OneYear = OneYear; + type BlockNumberProvider = System; } parameter_types! { @@ -1544,6 +1547,7 @@ impl lend_market::Config for Runtime { type RewardAssetId = NativeCurrencyId; type LiquidationFreeAssetId = RelayCurrencyId; type MaxLengthLimit = MaxLengthLimit; + type BlockNumberProvider = System; } parameter_types! { @@ -1612,6 +1616,7 @@ impl bifrost_buy_back::Config for Runtime { type ParachainId = ParachainInfo; type CurrencyIdRegister = AssetIdMaps; type BbBNC = BbBNC; + type BlockNumberProvider = System; } impl bifrost_slp_v2::Config for Runtime { From c7ef4412cae298f52132ac977622fe2dc0c307d4 Mon Sep 17 00:00:00 2001 From: NingBo Wang <2536935847@qq.com> Date: Tue, 17 Dec 2024 19:05:04 +0800 Subject: [PATCH 2/3] Fix evm validate (#1553) --- runtime/bifrost-polkadot/src/evm/runner.rs | 10 +++++----- runtime/bifrost-polkadot/src/lib.rs | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/runtime/bifrost-polkadot/src/evm/runner.rs b/runtime/bifrost-polkadot/src/evm/runner.rs index c1746f33a..4c8477241 100644 --- a/runtime/bifrost-polkadot/src/evm/runner.rs +++ b/runtime/bifrost-polkadot/src/evm/runner.rs @@ -121,11 +121,11 @@ where let account_id = T::AddressMapping::into_account_id(source); let account_nonce = frame_system::Pallet::::account_nonce(&account_id); - let (balance, b_weight) = B::get_balance_in_currency(evm_currency, &account_id, base_fee) - .map_err(|_| RunnerError { - error: R::Error::from(TransactionValidationError::BalanceTooLow), - weight, - })?; + let (balance, b_weight) = + match B::get_balance_in_currency(evm_currency, &account_id, base_fee) { + Ok((balance, b_weight)) => (balance, b_weight), + Err(_) => (0, T::DbWeight::get().reads(2)), + }; let (source_account, inner_weight) = ( Account { diff --git a/runtime/bifrost-polkadot/src/lib.rs b/runtime/bifrost-polkadot/src/lib.rs index 77306b1e8..d96b51bdd 100644 --- a/runtime/bifrost-polkadot/src/lib.rs +++ b/runtime/bifrost-polkadot/src/lib.rs @@ -2147,7 +2147,7 @@ impl fp_rpc::EthereumRuntimeRPCApi for Runtime { }; // don't allow calling EVM RPC or Runtime API from a bound address - if EVMAccounts::bound_account_id(from).is_some() { + if !estimate && EVMAccounts::bound_account_id(from).is_some() { return Err(pallet_evm_accounts::Error::::BoundAddressCannotBeUsed.into()) }; @@ -2228,9 +2228,9 @@ impl fp_rpc::EthereumRuntimeRPCApi for Runtime { }; // don't allow calling EVM RPC or Runtime API from a bound address - if EVMAccounts::bound_account_id(from).is_some() { + if !estimate && EVMAccounts::bound_account_id(from).is_some() { return Err(pallet_evm_accounts::Error::::BoundAddressCannotBeUsed.into()) - }; + }; // the address needs to have a permission to deploy smart contract if !EVMAccounts::can_deploy_contracts(from) { From a728bbf70d3b77242671dae24cba1ba605784f9e Mon Sep 17 00:00:00 2001 From: SunTiebing <87381708+SunTiebing@users.noreply.github.com> Date: Wed, 18 Dec 2024 10:37:57 +0800 Subject: [PATCH 3/3] change vtoken-minting blocknumber type (#1556) --- pallets/vtoken-minting/src/impls.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pallets/vtoken-minting/src/impls.rs b/pallets/vtoken-minting/src/impls.rs index 5efd7658c..c8f537126 100644 --- a/pallets/vtoken-minting/src/impls.rs +++ b/pallets/vtoken-minting/src/impls.rs @@ -41,6 +41,7 @@ use frame_support::{ use frame_system::pallet_prelude::*; use orml_traits::{MultiCurrency, MultiLockableCurrency, XcmTransfer}; use sp_core::U256; +use sp_runtime::traits::BlockNumberProvider; use sp_runtime::{helpers_128bit::multiply_by_rational_with_rounding, Rounding}; use sp_std::{vec, vec::Vec}; use xcm::{prelude::*, v4::Location}; @@ -762,7 +763,7 @@ impl Pallet { // get the vtoken lock duration from VtokenIncentiveCoef let lock_duration = MintWithLockBlocks::::get(v_currency_id) .ok_or(Error::::IncentiveLockBlocksNotSet)?; - let current_block = frame_system::Pallet::::block_number(); + let current_block = T::BlockNumberProvider::current_block_number(); let due_block = current_block .checked_add(&lock_duration) .ok_or(Error::::CalculationOverflow)?; @@ -816,7 +817,8 @@ impl Pallet { ); // get current block number - let current_block_number: BlockNumberFor = frame_system::Pallet::::block_number(); + let current_block_number: BlockNumberFor = + T::BlockNumberProvider::current_block_number(); // get the veBNC total amount let vebnc_total_issuance = T::BbBNC::total_supply(current_block_number) .map_err(|_| Error::::VeBNCCheckingError)?;