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

Use BlockNumberProvider trait #1532

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ members = [
"pallets/vtoken-minting",
"pallets/vtoken-voting",
"pallets/system-staking",
"pallets/deprecated/system-maker",
"pallets/fee-share",
"pallets/parachain-staking",
"pallets/bb-bnc",
Expand Down Expand Up @@ -86,7 +85,6 @@ bifrost-slpx = { path = "pallets/slpx", default-featur
bifrost-stable-asset = { path = "pallets/stable-asset", default-features = false }
bifrost-stable-pool = { path = "pallets/stable-pool", default-features = false }
bifrost-stable-pool-rpc-runtime-api = { path = "pallets/stable-pool/rpc/runtime-api", default-features = false }
bifrost-system-maker = { path = "pallets/deprecated/system-maker", default-features = false }
bifrost-system-staking = { path = "pallets/system-staking", default-features = false }
bifrost-token-issuer = { path = "pallets/token-issuer", default-features = false }
bifrost-vbnc-convert = { path = "pallets/vbnc-convert", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/mock/bifrost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ impl bifrost_slp::Config for Runtime {
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = BifrostTreasuryAccount;
type BlockNumberProvider = System;
}

impl bifrost_asset_registry::Config for Runtime {
Expand Down
1 change: 1 addition & 0 deletions pallets/bb-bnc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ impl bifrost_slp::Config for Runtime {
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = TreasuryAccount;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions pallets/buy-back/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl bifrost_slp::Config for Runtime {
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = TreasuryAccount;
type BlockNumberProvider = System;
}

parameter_type_with_key! {
Expand Down
1 change: 1 addition & 0 deletions pallets/fee-share/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ impl bifrost_slp::Config for Runtime {
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = TreasuryAccount;
type BlockNumberProvider = System;
}

parameter_type_with_key! {
Expand Down
9 changes: 6 additions & 3 deletions pallets/slp/src/agents/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ use orml_traits::{MultiCurrency, XcmTransfer};
use polkadot_parachain_primitives::primitives::Sibling;
use sp_core::{Get, U256};
use sp_runtime::{
traits::{AccountIdConversion, CheckedAdd, UniqueSaturatedFrom, UniqueSaturatedInto},
traits::{
AccountIdConversion, BlockNumberProvider, CheckedAdd, UniqueSaturatedFrom,
UniqueSaturatedInto,
},
DispatchResult, Saturating,
};
use xcm::v3::{prelude::*, MultiLocation};
Expand Down Expand Up @@ -348,7 +351,7 @@ impl<T: Config> Pallet<T> {
weight_and_fee: Option<(Weight, BalanceOf<T>)>,
) -> Result<(QueryId, BlockNumberFor<T>, BalanceOf<T>, xcm::v4::Xcm<()>), Error<T>> {
// prepare the query_id for reporting back transact status
let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let timeout = BlockNumberFor::<T>::from(TIMEOUT_BLOCKS).saturating_add(now);
let (query_id, notify_call_weight) =
Self::get_query_id_and_notify_call_weight(currency_id, &operation)?;
Expand Down Expand Up @@ -377,7 +380,7 @@ impl<T: Config> Pallet<T> {
currency_id: CurrencyId,
operation: &XcmOperationType,
) -> Result<(QueryId, Weight), Error<T>> {
let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let timeout = BlockNumberFor::<T>::from(TIMEOUT_BLOCKS).saturating_add(now);
let responder = Self::convert_currency_to_dest_location(currency_id)?;

Expand Down
5 changes: 3 additions & 2 deletions pallets/slp/src/agents/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use bifrost_primitives::{
use frame_support::ensure;
use parity_scale_codec::Encode;
use sp_core::Get;
use sp_runtime::traits::BlockNumberProvider;
use sp_std::prelude::*;
use xcm::v3::{prelude::*, MultiLocation};

Expand Down Expand Up @@ -215,7 +216,7 @@ impl<T: Config> Pallet<T> {
let (entry, timeout) =
DelegatorLedgerXcmUpdateQueue::<T>::get(query_id).ok_or(Error::<T>::QueryNotExist)?;

let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let mut updated = true;
if now <= timeout {
let currency_id = match entry.clone() {
Expand Down Expand Up @@ -249,7 +250,7 @@ impl<T: Config> Pallet<T> {
let (entry, timeout) = ValidatorsByDelegatorXcmUpdateQueue::<T>::get(query_id)
.ok_or(Error::<T>::QueryNotExist)?;

let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let mut updated = true;
if now <= timeout {
let currency_id = match entry.clone() {
Expand Down
12 changes: 8 additions & 4 deletions pallets/slp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub mod pallet {
use frame_support::dispatch::GetDispatchInfo;
use orml_traits::XcmTransfer;
use pallet_xcm::ensure_response;
use sp_runtime::traits::BlockNumberProvider;
use xcm::v3::{MaybeErrorCode, Response};

#[pallet::config]
Expand Down Expand Up @@ -169,6 +170,9 @@ pub mod pallet {
// asset registry to get asset metadata
type AssetIdMaps: CurrencyIdMapping<CurrencyId, AssetMetadata<BalanceOf<Self>>>;

/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;

#[pallet::constant]
type TreasuryAccount: Get<Self::AccountId>;
}
Expand Down Expand Up @@ -1180,7 +1184,7 @@ pub mod pallet {

let last_update_block = LastTimeUpdatedOngoingTimeUnit::<T>::get(currency_id)
.ok_or(Error::<T>::LastTimeUpdatedOngoingTimeUnitNotExist)?;
let current_block = frame_system::Pallet::<T>::block_number();
let current_block = T::BlockNumberProvider::current_block_number();
let blocks_between =
current_block.checked_sub(&last_update_block).ok_or(Error::<T>::UnderFlow)?;

Expand Down Expand Up @@ -1885,7 +1889,7 @@ pub mod pallet {
Pallet::<T>::check_length_and_deduplicate(currency_id, validator_list)?;

// get current block number
let current_block_number = <frame_system::Pallet<T>>::block_number();
let current_block_number = T::BlockNumberProvider::current_block_number();
// get the due block number
let due_block_number = current_block_number
.checked_add(&BlockNumberFor::<T>::from(SIX_MONTHS))
Expand Down Expand Up @@ -1957,7 +1961,7 @@ pub mod pallet {
T::ControlOrigin::ensure_origin(origin)?;

// get current block number
let current_block_number = <frame_system::Pallet<T>>::block_number();
let current_block_number = T::BlockNumberProvider::current_block_number();

// get the due block number if the validator is not in the validator boost list
let mut due_block_number = current_block_number
Expand Down Expand Up @@ -2155,7 +2159,7 @@ pub mod pallet {
0
};

let current_block_number = <frame_system::Pallet<T>>::block_number();
let current_block_number = T::BlockNumberProvider::current_block_number();
let mut remove_num = 0;
// for each validator in the validator boost list, if the due block number is less than
// or equal to the current block number, remove it
Expand Down
1 change: 1 addition & 0 deletions pallets/slp/src/mocks/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ impl Config for Runtime {
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = BifrostTreasuryAccount;
type BlockNumberProvider = System;
}

pub struct XcmDestWeightAndFee;
Expand Down
1 change: 1 addition & 0 deletions pallets/slp/src/mocks/mock_kusama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ impl Config for Runtime {
type StablePoolHandler = StablePool;
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = BifrostTreasuryAccount;
type BlockNumberProvider = System;
}

pub struct XcmDestWeightAndFee;
Expand Down
23 changes: 16 additions & 7 deletions pallets/slpx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ use parity_scale_codec::{Decode, Encode};
use polkadot_parachain_primitives::primitives::{Id, Sibling};
use sp_core::{Hasher, H160, U256};
use sp_runtime::{
traits::{AccountIdConversion, BlakeTwo256, CheckedSub, UniqueSaturatedFrom},
traits::{
AccountIdConversion, BlakeTwo256, BlockNumberProvider, CheckedSub, UniqueSaturatedFrom,
},
BoundedVec, DispatchError,
};
use sp_std::{vec, vec::Vec};
Expand Down Expand Up @@ -81,6 +83,7 @@ pub mod pallet {
weights::WeightMeter,
};
use frame_system::ensure_root;
use sp_runtime::traits::BlockNumberProvider;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

Expand All @@ -104,7 +107,8 @@ pub mod pallet {
CurrencyIdOf<Self>,
BalanceOf<Self>,
>;

/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
/// xtokens xcm transfer interface
type XcmTransfer: XcmTransfer<AccountIdOf<Self>, BalanceOf<Self>, CurrencyIdOf<Self>>;
/// Send Xcm
Expand Down Expand Up @@ -302,7 +306,7 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(n: BlockNumberFor<T>, limit: Weight) -> Weight {
fn on_idle(_: BlockNumberFor<T>, limit: Weight) -> Weight {
let mut weight = Weight::default();

if WeightMeter::with_limit(limit)
Expand All @@ -312,14 +316,19 @@ pub mod pallet {
return weight;
}

let current_block_number = T::BlockNumberProvider::current_block_number();
let mut is_handle_xcm_oracle = false;

if let Err(error) = Self::handle_xcm_oracle(n, &mut is_handle_xcm_oracle, &mut weight) {
if let Err(error) = Self::handle_xcm_oracle(
current_block_number,
&mut is_handle_xcm_oracle,
&mut weight,
) {
Self::deposit_event(Event::<T>::XcmOracleFailed { error });
}

if !is_handle_xcm_oracle {
let _ = Self::handle_order_queue(n, &mut weight);
let _ = Self::handle_order_queue(current_block_number, &mut weight);
}
weight
}
Expand Down Expand Up @@ -547,7 +556,7 @@ pub mod pallet {
xcm_fee,
xcm_weight,
period,
last_block: frame_system::Pallet::<T>::block_number(),
last_block: T::BlockNumberProvider::current_block_number(),
contract,
});
Self::deposit_event(Event::SetXcmOracleConfiguration {
Expand Down Expand Up @@ -828,7 +837,7 @@ impl<T: Config> Pallet<T> {
let order_type = Self::order_type(currency_id)?;
let derivative_account = Self::frontier_derivative_account(&source_chain_caller);
let order = Order {
create_block_number: <frame_system::Pallet<T>>::block_number(),
create_block_number: T::BlockNumberProvider::current_block_number(),
order_type,
currency_id,
currency_amount,
Expand Down
1 change: 1 addition & 0 deletions pallets/slpx/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ impl slpx::Config for Test {
type ParachainId = ParachainId;
type WeightInfo = ();
type MaxOrderSize = ConstU32<500>;
type BlockNumberProvider = System;
}

// Build genesis storage according to the mock runtime.
Expand Down
1 change: 1 addition & 0 deletions pallets/system-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ impl bifrost_slp::Config for Runtime {
type StablePoolHandler = ();
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = TreasuryAccount;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
17 changes: 10 additions & 7 deletions pallets/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ use scale_info::TypeInfo;
use sp_runtime::{
codec::{Decode, Encode, MaxEncodedLen},
traits::{
AtLeast32BitUnsigned, Bounded, Convert, MaybeSerializeDeserialize, One, Saturating,
StaticLookup, Zero,
AtLeast32BitUnsigned, BlockNumberProvider, Bounded, Convert, MaybeSerializeDeserialize,
One, Saturating, StaticLookup, Zero,
},
DispatchError, RuntimeDebug,
};
Expand Down Expand Up @@ -177,6 +177,9 @@ pub mod pallet {
/// the unvested amount.
type UnvestedFundsAllowedWithdrawReasons: Get<WithdrawReasons>;

/// Provider for the block number.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;

/// Maximum number of vesting schedules an account may have at a given moment.
const MAX_VESTING_SCHEDULES: u32;
}
Expand Down Expand Up @@ -428,7 +431,7 @@ pub mod pallet {

let absolute_start =
VestingStartAt::<T>::get().ok_or(Error::<T>::VestingStartAtNotSet)?;
let now = <frame_system::Pallet<T>>::block_number();
let now = T::BlockNumberProvider::current_block_number();

let old_start_at = absolute_start.saturating_add(schedules[index].starting_block());
let remained_vesting =
Expand Down Expand Up @@ -523,7 +526,7 @@ pub mod pallet {
impl<T: Config> Pallet<T> {
fn check_cliff(who: T::AccountId) -> DispatchResult {
if let Some(cliff_block) = Cliff::<T>::get(who.clone()) {
let now = <frame_system::Pallet<T>>::block_number();
let now = T::BlockNumberProvider::current_block_number();
ensure!(cliff_block < now, Error::<T>::WrongCliffVesting);
Cliff::<T>::remove(who);
};
Expand Down Expand Up @@ -641,7 +644,7 @@ impl<T: Config> Pallet<T> {
schedules: Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>,
action: VestingAction,
) -> (Vec<VestingInfo<BalanceOf<T>, BlockNumberFor<T>>>, BalanceOf<T>) {
let now = <frame_system::Pallet<T>>::block_number();
let now = T::BlockNumberProvider::current_block_number();

let mut total_locked_now: BalanceOf<T> = Zero::zero();
let filtered_schedules = action
Expand Down Expand Up @@ -727,7 +730,7 @@ impl<T: Config> Pallet<T> {
let (mut schedules, mut locked_now) =
Self::report_schedule_updates(schedules.to_vec(), action);

let now = <frame_system::Pallet<T>>::block_number();
let now = T::BlockNumberProvider::current_block_number();
if let Some(new_schedule) = Self::merge_vesting_info(now, schedule1, schedule2) {
// Merging created a new schedule so we:
// 1) need to add it to the accounts vesting schedule collection,
Expand Down Expand Up @@ -765,7 +768,7 @@ where
/// Get the amount that is currently being vested and cannot be transferred out of this account.
fn vesting_balance(who: &T::AccountId) -> Option<BalanceOf<T>> {
if let Some(v) = Vesting::<T>::get(who) {
let now = <frame_system::Pallet<T>>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let total_locked_now = v.iter().fold(Zero::zero(), |total, schedule| {
let start_at = VestingStartAt::<T>::get()
.map(|st| st.saturating_add(schedule.starting_block()));
Expand Down
1 change: 1 addition & 0 deletions pallets/vesting/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl Config for Test {
type MinVestedTransfer = MinVestedTransfer;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
type WeightInfo = ();
type BlockNumberProvider = System;
}

pub struct ExtBuilder {
Expand Down
4 changes: 0 additions & 4 deletions runtime/bifrost-kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ bifrost-slp = { workspace = true }
bifrost-slpx = { workspace = true }
bifrost-stable-pool = { workspace = true }
bifrost-stable-pool-rpc-runtime-api = { workspace = true }
bifrost-system-maker = { workspace = true }
bifrost-system-staking = { workspace = true }
bifrost-token-issuer = { workspace = true }
bifrost-vesting = { workspace = true }
Expand Down Expand Up @@ -259,7 +258,6 @@ std = [
"bifrost-slpx/std",
"bifrost-stable-pool-rpc-runtime-api/std",
"bifrost-stable-pool/std",
"bifrost-system-maker/std",
"bifrost-system-staking/std",
"bifrost-token-issuer/std",
"bifrost-vesting/std",
Expand Down Expand Up @@ -312,7 +310,6 @@ runtime-benchmarks = [
"bifrost-vtoken-minting/runtime-benchmarks",
"bifrost-farming/runtime-benchmarks",
"bifrost-system-staking/runtime-benchmarks",
"bifrost-system-maker/runtime-benchmarks",
"bifrost-vstoken-conversion/runtime-benchmarks",
"bifrost-slp/runtime-benchmarks",
"bifrost-asset-registry/runtime-benchmarks",
Expand Down Expand Up @@ -388,7 +385,6 @@ try-runtime = [
"bifrost-vstoken-conversion/try-runtime",
"bifrost-farming/try-runtime",
"bifrost-system-staking/try-runtime",
"bifrost-system-maker/try-runtime",
"bifrost-fee-share/try-runtime",
"bifrost-cross-in-out/try-runtime",
"bifrost-slpx/try-runtime",
Expand Down
3 changes: 3 additions & 0 deletions runtime/bifrost-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ impl bifrost_vesting::Config for Runtime {
type WeightInfo = weights::bifrost_vesting::BifrostWeight<Runtime>;
type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons;
const MAX_VESTING_SCHEDULES: u32 = 28;
type BlockNumberProvider = System;
}

// Bifrost modules start
Expand Down Expand Up @@ -1329,6 +1330,7 @@ impl bifrost_slp::Config for Runtime {
type StablePoolHandler = StablePool;
type AssetIdMaps = AssetIdMaps<Runtime>;
type TreasuryAccount = BifrostTreasuryAccount;
type BlockNumberProvider = System;
}

impl bifrost_vstoken_conversion::Config for Runtime {
Expand Down Expand Up @@ -1573,6 +1575,7 @@ impl bifrost_slpx::Config for Runtime {
type ParachainId = ParachainInfo;
type WeightInfo = weights::bifrost_slpx::BifrostWeight<Runtime>;
type MaxOrderSize = ConstU32<500>;
type BlockNumberProvider = System;
}

pub struct EnsurePoolAssetId;
Expand Down
Loading