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

Change block number to relaychain #1555

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions pallets/bb-bnc/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

ord_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 @@ -304,6 +304,7 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
22 changes: 16 additions & 6 deletions pallets/channel-commission/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ use orml_traits::MultiCurrency;
use sp_io::MultiRemovalResults;
use sp_runtime::{
helpers_128bit::multiply_by_rational_with_rounding,
traits::{AccountIdConversion, CheckedAdd, UniqueSaturatedFrom, UniqueSaturatedInto, Zero},
traits::{
AccountIdConversion, BlockNumberProvider, CheckedAdd, UniqueSaturatedFrom,
UniqueSaturatedInto, Zero,
},
PerThing, Percent, Permill, Rounding, SaturatedConversion, Saturating,
};
pub use weights::WeightInfo;
Expand Down Expand Up @@ -83,6 +86,8 @@ pub mod pallet {
// The maximum bytes length of channel name
#[pallet::constant]
type NameLengthLimit: Get<u32>;
/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

#[pallet::error]
Expand Down Expand Up @@ -299,23 +304,28 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
let channel_count: u32 = ChannelNextId::<T>::get().into();
let current_block_number = T::BlockNumberProvider::current_block_number();

// get the commission token count
let commission_token_count = CommissionTokens::<T>::iter().count() as u32;

// If the current block number is the first block of a new clearing period, we need to
// prepare data for clearing.
if (n % T::ClearingDuration::get()).is_zero() {
if (current_block_number % T::ClearingDuration::get()).is_zero() {
Self::set_clearing_environment();
} else if (n % T::ClearingDuration::get()) < (channel_count + 1).into() {
let channel_index = n % T::ClearingDuration::get() - 1u32.into();
} else if (current_block_number % T::ClearingDuration::get())
< (channel_count + 1).into()
{
let channel_index = current_block_number % T::ClearingDuration::get() - 1u32.into();
let channel_id: ChannelId =
BlockNumberFor::<T>::unique_saturated_into(channel_index);
Self::clear_channel_commissions(channel_id);
Self::update_channel_vtoken_shares(channel_id);
} else if (n % T::ClearingDuration::get()) == (channel_count + 1).into() {
} else if (current_block_number % T::ClearingDuration::get())
== (channel_count + 1).into()
{
Self::clear_bifrost_commissions();
}

Expand Down
1 change: 1 addition & 0 deletions pallets/channel-commission/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl bifrost_channel_commission::Config for Runtime {
type WeightInfo = ();
type ClearingDuration = ClearingDuration;
type NameLengthLimit = NameLengthLimit;
type BlockNumberProvider = System;
}

pub struct ExtBuilder {
Expand Down
4 changes: 4 additions & 0 deletions pallets/channel-commission/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,7 @@ fn on_initialize_hook_should_work() {
assert_eq!(Currencies::free_balance(VKSM, &commission_account), 11000);

// set block number to 100
System::set_block_number(100);
ChannelCommission::on_initialize(100);
// set_clearing_environment already been called in block 100
// check whether the clearing environment is set correctly for block 100
Expand All @@ -1076,6 +1077,7 @@ fn on_initialize_hook_should_work() {
// get channel B's vtoken share before being cleared
let channel_b_vtoken_share_before = ChannelVtokenShares::<Runtime>::get(1, VKSM);

System::set_block_number(101);
ChannelCommission::on_initialize(101);

let channel_a_commission = 4;
Expand All @@ -1100,6 +1102,7 @@ fn on_initialize_hook_should_work() {
channel_b_vtoken_share_before
);

System::set_block_number(102);
ChannelCommission::on_initialize(102);

let channel_b_commission = 2;
Expand Down Expand Up @@ -1127,6 +1130,7 @@ fn on_initialize_hook_should_work() {
Currencies::free_balance(KSM, &bifrost_commission_receiver);
assert_eq!(bifrost_account_balance_before, 0);

System::set_block_number(103);
ChannelCommission::on_initialize(103);

// cleared commissions should be none
Expand Down
16 changes: 10 additions & 6 deletions pallets/fee-share/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ use frame_support::{
pallet_prelude::*,
sp_runtime::{
traits::{
AccountIdConversion, CheckedAdd, CheckedMul, SaturatedConversion, Saturating, Zero,
AccountIdConversion, BlockNumberProvider, CheckedAdd, CheckedMul, SaturatedConversion,
Saturating, Zero,
},
ArithmeticError, FixedU128, Perbill,
},
Expand Down Expand Up @@ -103,6 +104,8 @@ pub mod pallet {

/// The oracle price feeder
type OraclePriceProvider: OraclePriceProvider;
/// The current block number provider.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
}

#[pallet::event]
Expand Down Expand Up @@ -207,16 +210,17 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(bn: BlockNumberFor<T>, _remaining_weight: Weight) -> Weight {
fn on_idle(_: BlockNumberFor<T>, _remaining_weight: Weight) -> Weight {
let current_block_number = T::BlockNumberProvider::current_block_number();
DollarStandardInfos::<T>::iter().for_each(|(distribution_id, mut info)| {
if bn.eq(&info.target_block) {
if current_block_number.eq(&info.target_block) {
info.target_block = info.target_block.saturating_add(info.interval);
info.cumulative = Zero::zero();
DollarStandardInfos::<T>::insert(distribution_id, info);
}
});
let (era_length, next_era) = AutoEra::<T>::get();
if bn.eq(&next_era) {
if current_block_number.eq(&next_era) {
DistributionInfos::<T>::iter().for_each(|(distribution_id, info)| {
if info.if_auto {
if let Some(e) =
Expand Down Expand Up @@ -353,7 +357,7 @@ pub mod pallet {
) -> DispatchResult {
T::ControlOrigin::ensure_origin(origin)?;

let current_block = frame_system::Pallet::<T>::block_number();
let current_block = T::BlockNumberProvider::current_block_number();
let next_era = current_block
.checked_add(&era_length)
.ok_or(ArithmeticError::Overflow)?;
Expand Down Expand Up @@ -430,7 +434,7 @@ pub mod pallet {
ensure!(interval > Zero::zero(), Error::<T>::IntervalIsZero);
ensure!(target_value > 0, Error::<T>::ValueIsZero);

let now = frame_system::Pallet::<T>::block_number();
let now = T::BlockNumberProvider::current_block_number();
let info = DollarStandardInfo {
target_value,
cumulative: Zero::zero(),
Expand Down
2 changes: 2 additions & 0 deletions pallets/fee-share/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ impl bifrost_fee_share::Config for Runtime {
type WeightInfo = ();
type FeeSharePalletId = FeeSharePalletId;
type OraclePriceProvider = MockOraclePriceProvider;
type BlockNumberProvider = System;
}

impl pallet_prices::Config for Runtime {
Expand Down Expand Up @@ -421,6 +422,7 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
35 changes: 15 additions & 20 deletions pallets/fee-share/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ fn on_idle() {
<Runtime as Config>::FeeSharePalletId::get().into_sub_account_truncating(0);

assert_ok!(FeeShare::set_era_length(RuntimeOrigin::signed(ALICE), 1));
FeeShare::on_idle(
<frame_system::Pallet<Runtime>>::block_number() + 1,
Weight::zero(),
);
let current_block_number = System::block_number() + 1;
System::set_block_number(current_block_number);
FeeShare::on_idle(current_block_number, Weight::zero());
assert_ok!(<Tokens as MultiCurrency<AccountId>>::transfer(
KSM, &ALICE, &keeper, 100,
));
FeeShare::on_idle(
<frame_system::Pallet<Runtime>>::block_number() + 2,
Weight::zero(),
);
let current_block_number = System::block_number() + 1;
System::set_block_number(current_block_number);
FeeShare::on_idle(current_block_number, Weight::zero());
assert_eq!(Tokens::free_balance(KSM, &keeper), 0);
});
}
Expand Down Expand Up @@ -142,10 +140,9 @@ fn set_usd_config_should_work() {

assert_ok!(FeeShare::set_era_length(RuntimeOrigin::signed(ALICE), 1));
assert_eq!(Tokens::free_balance(KSM, &BOB), 100);
FeeShare::on_idle(
<frame_system::Pallet<Runtime>>::block_number() + 1,
Weight::zero(),
);
let current_block_number = System::block_number() + 1;
System::set_block_number(current_block_number);
FeeShare::on_idle(current_block_number, Weight::zero());
assert_ok!(<Tokens as MultiCurrency<AccountId>>::transfer(
KSM, &ALICE, &keeper, 100,
));
Expand All @@ -154,10 +151,9 @@ fn set_usd_config_should_work() {
DollarStandardInfos::<Runtime>::get(0).unwrap().cumulative,
10000
);
FeeShare::on_idle(
<frame_system::Pallet<Runtime>>::block_number() + 2,
Weight::zero(),
);
let current_block_number = System::block_number() + 1;
System::set_block_number(current_block_number);
FeeShare::on_idle(current_block_number, Weight::zero());
assert_eq!(Tokens::free_balance(KSM, &keeper), 0);
assert_eq!(Tokens::free_balance(KSM, &BOB), 10100);
assert_eq!(
Expand All @@ -167,10 +163,9 @@ fn set_usd_config_should_work() {
assert_ok!(<Tokens as MultiCurrency<AccountId>>::transfer(
KSM, &ALICE, &keeper, 100,
));
FeeShare::on_idle(
<frame_system::Pallet<Runtime>>::block_number() + 10,
Weight::zero(),
);
let current_block_number = System::block_number() + 8;
System::set_block_number(current_block_number);
FeeShare::on_idle(current_block_number, Weight::zero());
assert_eq!(
DollarStandardInfos::<Runtime>::get(0).unwrap().cumulative,
0
Expand Down
1 change: 1 addition & 0 deletions pallets/leverage-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

pub struct Slp;
Expand Down
1 change: 1 addition & 0 deletions pallets/slp-v2/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

parameter_types! {
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 @@ -207,6 +207,7 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

parameter_types! {
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 @@ -263,6 +263,7 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

parameter_types! {
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 @@ -191,6 +191,7 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions pallets/stable-pool/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ impl bifrost_vtoken_minting::Config for Test {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

pub struct Slp;
Expand Down
15 changes: 10 additions & 5 deletions pallets/system-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_system::pallet_prelude::BlockNumberFor;
use orml_traits::MultiCurrency;
pub use pallet::*;
use sp_runtime::{
traits::{AccountIdConversion, Saturating, Zero},
traits::{AccountIdConversion, BlockNumberProvider, Saturating, Zero},
BoundedVec,
};
use sp_std::vec::Vec;
Expand Down Expand Up @@ -106,6 +106,9 @@ pub mod pallet {
/// This value is set to 1500 in the runtime configuration.
#[pallet::constant]
type BlocksPerRound: Get<u32>;

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

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
Expand Down Expand Up @@ -309,9 +312,10 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
// Get token list
let token_list = TokenList::<T>::get();
let current_block_number = T::BlockNumberProvider::current_block_number();

//Get round info, if can't find it in the storage, a new one will be created.
let mut round = if let Some(round) = <Round<T>>::get() {
Expand All @@ -323,11 +327,11 @@ pub mod pallet {

// New round start
// Current blockNumber - BlockNumber of Round Start >= Length of Round
if round.should_update(n) {
if round.should_update(current_block_number) {
// Mutate round
// Set current round index -= 1
// BlockNumber of Round Start = Current blockNumber
round.update(n);
round.update(current_block_number);
<Round<T>>::put(round);

// Iterate through the token list
Expand Down Expand Up @@ -359,7 +363,8 @@ pub mod pallet {
if let Some(token_info) = TokenStatus::<T>::get(i) {
// Current blockNumber - BlockNumber of Round Start ==
// token_info.current_config.exec_delay ===> true
if round.check_delay(n, token_info.current_config.exec_delay) {
if round.check_delay(current_block_number, token_info.current_config.exec_delay)
{
Self::process_token_info(pallet_account.clone(), token_info, i).ok();

if let Err(_) = Self::do_payout(i) {
Expand Down
2 changes: 2 additions & 0 deletions pallets/system-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ impl bifrost_vtoken_minting::Config for Runtime {
type MaxLockRecords = ConstU32<100>;
type IncentivePoolAccount = IncentivePoolAccount;
type BbBNC = ();
type BlockNumberProvider = System;
}

ord_parameter_types! {
Expand Down Expand Up @@ -318,6 +319,7 @@ impl system_staking::Config for Runtime {
type BlocksPerRound = BlocksPerRound;
type MaxTokenLen = MaxTokenLen;
type MaxFarmingPoolIdLen = MaxFarmingPoolIdLen;
type BlockNumberProvider = System;
}

parameter_types! {
Expand Down
6 changes: 4 additions & 2 deletions pallets/vtoken-minting/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -762,7 +763,7 @@ impl<T: Config> Pallet<T> {
// get the vtoken lock duration from VtokenIncentiveCoef
let lock_duration = MintWithLockBlocks::<T>::get(v_currency_id)
.ok_or(Error::<T>::IncentiveLockBlocksNotSet)?;
let current_block = frame_system::Pallet::<T>::block_number();
let current_block = T::BlockNumberProvider::current_block_number();
let due_block = current_block
.checked_add(&lock_duration)
.ok_or(Error::<T>::CalculationOverflow)?;
Expand Down Expand Up @@ -816,7 +817,8 @@ impl<T: Config> Pallet<T> {
);

// get current block number
let current_block_number: BlockNumberFor<T> = frame_system::Pallet::<T>::block_number();
let current_block_number: BlockNumberFor<T> =
T::BlockNumberProvider::current_block_number();
// get the veBNC total amount
let vebnc_total_issuance = T::BbBNC::total_supply(current_block_number)
.map_err(|_| Error::<T>::VeBNCCheckingError)?;
Expand Down
Loading