Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into fix/linear-pricing
Browse files Browse the repository at this point in the history
  • Loading branch information
mustermeiszer committed May 6, 2024
2 parents c978951 + 987aa11 commit e2dd3ca
Show file tree
Hide file tree
Showing 66 changed files with 1,644 additions and 3,341 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ members = [
"runtime/development",
"runtime/common",
"runtime/integration-tests",
"runtime/integration-tests/procedural",
]

[workspace.package]
Expand Down Expand Up @@ -275,6 +276,7 @@ runtime-common = { path = "runtime/common", default-features = false }
development-runtime = { path = "runtime/development", default-features = false }
altair-runtime = { path = "runtime/altair", default-features = false }
centrifuge-runtime = { path = "runtime/centrifuge", default-features = false }
runtime-integration-tests-proc-macro = { path = "runtime/integration-tests/procedural" }

# Build dependencies
substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0" }
Expand Down
2 changes: 1 addition & 1 deletion ci/run-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ case $TARGET in
;;

test-integration)
cargo test --release --package runtime-integration-tests --features fast-runtime
cargo test --release --package runtime-integration-tests
;;

lint-fmt)
Expand Down
20 changes: 9 additions & 11 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,18 +431,16 @@ where
let meta_variant =
AssetInspect::metadata(variant_currency).ok_or(DispatchError::CannotLookup)?;

let local: Self = meta_variant
.additional
.local_representation
.ok_or(DispatchError::Other("Missing local representation"))?
.into();

frame_support::ensure!(
meta_local.decimals == meta_variant.decimals,
DispatchError::Other("Mismatching decimals")
);
if let Some(local) = meta_variant.additional.local_representation {
frame_support::ensure!(
meta_local.decimals == meta_variant.decimals,
DispatchError::Other("Mismatching decimals")
);

Ok(self == &local)
Ok(self == &local.into())
} else {
Ok(false)
}
}
}

Expand Down
18 changes: 11 additions & 7 deletions pallets/block-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use frame_support::{
storage::transactional,
traits::{
fungible::{Inspect as FungibleInspect, Mutate as FungibleMutate},
fungibles::Mutate,
fungibles::{Inspect, Mutate},
tokens::{Balance, Fortitude, Precision},
OneSessionHandler,
},
Expand Down Expand Up @@ -132,9 +132,6 @@ pub mod pallet {
/// Type used to handle balances.
type Balance: Balance + MaxEncodedLen + FixedPointOperand + MaybeSerializeDeserialize;

#[pallet::constant]
type ExistentialDeposit: Get<Self::Balance>;

/// Type used to handle group weights.
type Weight: Parameter + MaxEncodedLen + EnsureAdd + Unsigned + FixedPointOperand + Default;

Expand All @@ -147,8 +144,12 @@ pub mod pallet {
> + CurrencyGroupChange<GroupId = u32, CurrencyId = <Self as Config>::CurrencyId>;

/// The type used to handle currency minting and burning for collators.
type Tokens: Mutate<Self::AccountId, AssetId = <Self as Config>::CurrencyId, Balance = Self::Balance>
+ FungibleMutate<Self::AccountId>
type Tokens: Mutate<Self::AccountId>
+ Inspect<
Self::AccountId,
AssetId = <Self as Config>::CurrencyId,
Balance = Self::Balance,
> + FungibleMutate<Self::AccountId>
+ FungibleInspect<Self::AccountId, Balance = Self::Balance>;

/// The currency type of the artificial block rewards currency.
Expand Down Expand Up @@ -314,10 +315,13 @@ impl<T: Config> Pallet<T> {
/// * deposit_stake (4 reads, 4 writes): Currency, Group, StakeAccount,
/// Account
pub(crate) fn do_init_collator(who: &T::AccountId) -> DispatchResult {
let existential_deposit =
<T::Tokens as Inspect<T::AccountId>>::minimum_balance(T::StakeCurrencyId::get());

<T::Tokens as Mutate<T::AccountId>>::mint_into(
T::StakeCurrencyId::get(),
who,
T::StakeAmount::get().saturating_add(T::ExistentialDeposit::get()),
T::StakeAmount::get().saturating_add(existential_deposit),
)?;
T::Rewards::deposit_stake(T::StakeCurrencyId::get(), who, T::StakeAmount::get())
}
Expand Down
120 changes: 65 additions & 55 deletions pallets/block-rewards/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

use cfg_traits::TimeAsSecs;
use frame_support::{
pallet_prelude::{StorageVersion, Weight},
pallet_prelude::Weight,
traits::{Get, GetStorageVersion, OnRuntimeUpgrade},
};
#[cfg(feature = "try-runtime")]
use num_traits::Zero;
#[cfg(feature = "try-runtime")]
use parity_scale_codec::{Decode, Encode};
use sp_runtime::FixedPointNumber;
#[cfg(feature = "try-runtime")]
Expand All @@ -31,9 +29,8 @@ fn inflation_rate<T: Config>(percent: u32) -> T::Rate {
}

pub mod init {
#[cfg(feature = "try-runtime")]
use cfg_traits::rewards::AccountRewards;
use cfg_traits::rewards::CurrencyGroupChange;
use cfg_traits::rewards::{AccountRewards, CurrencyGroupChange, GroupRewards};
use num_traits::Zero;
use sp_runtime::{BoundedVec, SaturatedConversion};

use super::*;
Expand All @@ -60,17 +57,12 @@ pub mod init {
for InitBlockRewards<T, CollatorReward, AnnualTreasuryInflationPercent>
where
T: frame_system::Config + Config<Balance = u128> + pallet_collator_selection::Config,
<T as Config>::Balance: From<u128>,
CollatorReward: Get<<T as Config>::Balance>,
T::Balance: From<u128>,
CollatorReward: Get<T::Balance>,
AnnualTreasuryInflationPercent: Get<u32>,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
assert_eq!(
Pallet::<T>::on_chain_storage_version(),
StorageVersion::new(0),
"On-chain storage version should be 0 (default)"
);
let collators = get_collators::<T>();
assert!(!collators.is_empty());

Expand All @@ -83,19 +75,46 @@ pub mod init {

// Weight: 2 + collator_count reads and writes
fn on_runtime_upgrade() -> Weight {
if Pallet::<T>::on_chain_storage_version() == StorageVersion::new(0) {
log::info!("{LOG_PREFIX} Initiating migration");
let mut weight: Weight = Weight::zero();
log::info!("{LOG_PREFIX} Initiating migration");

log::info!("{LOG_PREFIX} Checking whether group is ready");
let group_is_ready = T::Rewards::is_ready(T::StakeGroupId::get());

log::info!("{LOG_PREFIX} Checking whether session data is set");
let session_data_already_set =
pallet::ActiveSessionData::<T>::get() != Default::default();

log::info!("{LOG_PREFIX} Getting list of collators");
let collators = get_collators::<T>();

let collators_are_staked = collators.clone().into_iter().all(|c| {
log::info!("{LOG_PREFIX} Checking stake of collator {c:?}");
!T::Rewards::account_stake(T::StakeCurrencyId::get(), &c).is_zero()
});
let mut weight =
T::DbWeight::get().reads(2u64.saturating_add(collators.len().saturated_into()));

if group_is_ready && session_data_already_set && collators_are_staked {
log::info!(
"{LOG_PREFIX} Migration not necessary because all data is already initialized"
);
return weight;
}

if !group_is_ready {
log::info!("{LOG_PREFIX} Attaching currency to collator group");

T::Rewards::attach_currency(T::StakeCurrencyId::get(), T::StakeGroupId::get())
.map_err(|e| {
log::error!("Failed to attach currency to collator group: {:?}", e)
})
.ok();

let collators = get_collators::<T>();
weight.saturating_accrue(T::DbWeight::get().reads(2));
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

<T as Config>::Rewards::attach_currency(
<T as Config>::StakeCurrencyId::get(),
<T as Config>::StakeGroupId::get(),
)
.map_err(|e| log::error!("Failed to attach currency to collator group: {:?}", e))
.ok();
if !session_data_already_set {
log::info!("{LOG_PREFIX} Setting session data");

pallet::ActiveSessionData::<T>::set(SessionData::<T> {
collator_count: collators.len().saturated_into(),
Expand All @@ -106,25 +125,30 @@ pub mod init {
last_update: T::Time::now(),
});
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

if !collators_are_staked {
for collator in collators.iter() {
// NOTE: Benching not required as num of collators <= 10.
Pallet::<T>::do_init_collator(collator)
.map_err(|e| {
log::error!("Failed to init genesis collators for rewards: {:?}", e);
})
.ok();
weight.saturating_accrue(T::DbWeight::get().reads_writes(6, 6));
if T::Rewards::account_stake(T::StakeCurrencyId::get(), collator).is_zero() {
log::info!("{LOG_PREFIX} Adding stake for collator {collator:?}");
// NOTE: Benching not required as num of collators <= 10.
Pallet::<T>::do_init_collator(collator)
.map_err(|e| {
log::error!(
"Failed to init genesis collators for rewards: {:?}",
e
);
})
.ok();
weight.saturating_accrue(T::DbWeight::get().reads_writes(6, 6));
}
}
Pallet::<T>::current_storage_version().put::<Pallet<T>>();
weight.saturating_add(T::DbWeight::get().writes(1))
} else {
// wrong storage version
log::info!(
"{LOG_PREFIX} Migration did not execute. This probably should be removed"
);
T::DbWeight::get().reads_writes(1, 0)
}

log::info!("{LOG_PREFIX} Migration complete");

Pallet::<T>::current_storage_version().put::<Pallet<T>>();
weight.saturating_add(T::DbWeight::get().writes(1))
}

#[cfg(feature = "try-runtime")]
Expand All @@ -137,24 +161,10 @@ pub mod init {
let collators: Vec<T::AccountId> = Decode::decode(&mut pre_state.as_slice())
.expect("pre_upgrade provides a valid state; qed");

assert_eq!(
Pallet::<T>::active_session_data(),
SessionData::<T> {
collator_count: collators.len().saturated_into(),
collator_reward: CollatorReward::get(),
treasury_inflation_rate: inflation_rate::<T>(
AnnualTreasuryInflationPercent::get()
),
last_update: T::Time::now(),
}
);
assert_ne!(Pallet::<T>::active_session_data(), Default::default());

for collator in collators.iter() {
assert!(!<T as Config>::Rewards::account_stake(
<T as Config>::StakeCurrencyId::get(),
collator,
)
.is_zero())
assert!(!T::Rewards::account_stake(T::StakeCurrencyId::get(), collator,).is_zero())
}

log::info!("{LOG_PREFIX} Post migration checks successful");
Expand Down
30 changes: 13 additions & 17 deletions pallets/block-rewards/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use cfg_types::{
};
use frame_support::{
derive_impl, parameter_types,
traits::{fungibles::Inspect, tokens::WithdrawConsequence, ConstU32, OnFinalize, OnInitialize},
traits::{
fungibles::Inspect, tokens::WithdrawConsequence, ConstU128, ConstU32, OnFinalize,
OnInitialize,
},
PalletId,
};
use frame_system::EnsureRoot;
Expand Down Expand Up @@ -81,26 +84,24 @@ impl pallet_session::Config for Test {
type WeightInfo = ();
}

parameter_types! {
// the minimum fee for an anchor is 500,000ths of a CFG.
// This is set to a value so you can still get some return without getting your account removed.
pub const ExistentialDeposit: Balance = 1 * cfg_primitives::MICRO_CFG;
}
pub const BALANCE_ED: Balance = 23;
pub const REWARD_CURRENCY_ED: Balance = 42;

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig as pallet_balances::DefaultConfig)]
impl pallet_balances::Config for Test {
type AccountStore = System;
type Balance = Balance;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type ExistentialDeposit = ConstU128<BALANCE_ED>;
type RuntimeHoldReason = ();
}

orml_traits::parameter_type_with_key! {
pub ExistentialDeposits: |currency_id: CurrencyId| -> Balance {
match currency_id {
CurrencyId::Native => ExistentialDeposit::get(),
_ => 1,
CurrencyId::Native => BALANCE_ED,
CurrencyId::Staking(BlockRewardsCurrency) => REWARD_CURRENCY_ED,
_ => unreachable!()
}
};
}
Expand Down Expand Up @@ -188,7 +189,6 @@ impl pallet_block_rewards::Config for Test {
type AuthorityId = UintAuthorityId;
type Balance = Balance;
type CurrencyId = CurrencyId;
type ExistentialDeposit = ExistentialDeposit;
type MaxCollators = MaxCollators;
type Rate = Rate;
type Rewards = Rewards;
Expand All @@ -207,13 +207,13 @@ pub(crate) fn assert_staked(who: &AccountId) {
assert_eq!(
// NOTE: This is now the ED instead of 0, as we collators need ED now.
Tokens::balance(BlockRewardCurrency::get(), who),
ExistentialDeposit::get()
REWARD_CURRENCY_ED
);
assert_eq!(
<Test as Config>::Tokens::can_withdraw(
<Test as Config>::StakeCurrencyId::get(),
who,
ExistentialDeposit::get() * 2
REWARD_CURRENCY_ED * 2
),
WithdrawConsequence::BalanceLow
);
Expand All @@ -229,11 +229,7 @@ pub(crate) fn assert_not_staked(who: &AccountId, was_before: bool) {
<Test as Config>::Tokens::balance(<Test as Config>::StakeCurrencyId::get(), who),
// NOTE: IF a collator has been staked before the system already granted them ED
// of `StakeCurrency`.
if was_before {
ExistentialDeposit::get()
} else {
0
}
if was_before { REWARD_CURRENCY_ED } else { 0 }
);
}

Expand Down
Loading

0 comments on commit e2dd3ca

Please sign in to comment.