Skip to content

Commit

Permalink
Fix migration logic (#1172)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Feb 12, 2024
1 parent d133eb8 commit 5e67f1b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 144 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion bin/collator/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "astar-collator"
version = "5.32.0"
version = "5.32.1"
description = "Astar collator implementation in Rust."
build = "build.rs"
default-run = "astar-collator"
Expand Down
15 changes: 10 additions & 5 deletions pallets/dapp-staking-migration/src/lib.rs
Expand Up @@ -373,17 +373,19 @@ pub mod pallet {
pub(crate) fn migrate_ledger() -> Result<Weight, Weight> {
match OldLedger::<T>::drain().next() {
Some((staker, old_account_ledger)) => {
let locked = old_account_ledger.locked;
let old_locked = old_account_ledger.locked;

// Old unbonding amount can just be released, to keep things simple.
// Alternative is to re-calculate this into unlocking chunks.
let _total_unbonding = old_account_ledger.unbonding_info.sum();
let total_unbonding = old_account_ledger.unbonding_info.sum();

<T as pallet_dapps_staking::Config>::Currency::remove_lock(
pallet_dapps_staking::pallet::STAKING_ID,
&staker,
);

let locked = old_locked.saturating_sub(total_unbonding);

// No point in attempting to lock the old amount into dApp staking v3 if amount is insufficient.
if locked >= <T as pallet_dapp_staking_v3::Config>::MinimumLockedAmount::get() {
match pallet_dapp_staking_v3::Pallet::<T>::lock(
Expand Down Expand Up @@ -587,8 +589,11 @@ pub mod pallet {
let stakers: Vec<_> = pallet_dapps_staking::Ledger::<T>::iter()
.filter_map(|(staker, ledger)| {
total_locked.saturating_accrue(ledger.locked);
if ledger.locked >= min_lock_amount {
Some((staker, ledger.locked))
total_locked.saturating_reduce(ledger.unbonding_info.sum());

let new_lock_amount = ledger.locked.saturating_sub(ledger.unbonding_info.sum());
if new_lock_amount >= min_lock_amount {
Some((staker, new_lock_amount))
} else {
None
}
Expand Down Expand Up @@ -661,7 +666,7 @@ pub mod pallet {

for (staker, old_locked) in &helper.stakers {
let new_locked = pallet_dapp_staking_v3::Ledger::<T>::get(&staker).locked;
assert_eq!(*old_locked, new_locked);
assert!(*old_locked >= new_locked);
}

let total_locked = helper
Expand Down
15 changes: 14 additions & 1 deletion pallets/dapp-staking-migration/src/mock.rs
Expand Up @@ -38,6 +38,8 @@ use astar_primitives::{

pub(crate) type AccountId = u64;

pub(crate) const UNBONDING_ACCOUNT: AccountId = 10;

pub(crate) const EXISTENTIAL_DEPOSIT: Balance = 2;
pub(crate) const MINIMUM_LOCK_AMOUNT: Balance = 10;

Expand Down Expand Up @@ -233,7 +235,7 @@ impl ExtBuilder {
.build_storage::<Test>()
.unwrap();

let balances = vec![1000; 9]
let balances = vec![1000; 11]
.into_iter()
.enumerate()
.map(|(idx, amount)| (idx as u64 + 1, amount))
Expand Down Expand Up @@ -277,4 +279,15 @@ pub fn init() {
1_000,
));
}

assert_ok!(pallet_dapps_staking::Pallet::<Test>::bond_and_stake(
RawOrigin::Signed(UNBONDING_ACCOUNT).into(),
MockSmartContract::Wasm(0),
2000
));
assert_ok!(pallet_dapps_staking::Pallet::<Test>::unbond_and_unstake(
RawOrigin::Signed(UNBONDING_ACCOUNT).into(),
MockSmartContract::Wasm(0),
500
));
}
13 changes: 12 additions & 1 deletion pallets/dapp-staking-migration/src/tests.rs
Expand Up @@ -78,7 +78,12 @@ fn migrate_ledgers_check() {
ExtBuilder::build().execute_with(|| {
init();

// Cleanup all enries, check pre and post states.
// Check for unbonding accounts
let init_unbonding_ledger = pallet_dapps_staking::Ledger::<Test>::get(&UNBONDING_ACCOUNT);
let unbonding_account_init_locked_amount = init_unbonding_ledger.locked;
let unbonding_account_unbonding_amount = init_unbonding_ledger.unbonding_info.sum();

// Cleanup all entries, check pre and post states.
let init_old_count = pallet_dapps_staking::Ledger::<Test>::iter().count();
assert!(init_old_count > 0, "Sanity check.");

Expand Down Expand Up @@ -113,6 +118,12 @@ fn migrate_ledgers_check() {
DappStakingMigration::migrate_ledger(),
Err(<Test as Config>::WeightInfo::migrate_ledger_noop())
);

let post_unbonding_ledger = pallet_dapp_staking_v3::Ledger::<Test>::get(&UNBONDING_ACCOUNT);
assert_eq!(
post_unbonding_ledger.locked,
unbonding_account_init_locked_amount - unbonding_account_unbonding_amount
);
});
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/astar/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "astar-runtime"
version = "5.32.0"
version = "5.32.1"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
129 changes: 2 additions & 127 deletions runtime/astar/src/lib.rs
Expand Up @@ -148,7 +148,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("astar"),
impl_name: create_runtime_str!("astar"),
authoring_version: 1,
spec_version: 79,
spec_version: 80,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down Expand Up @@ -1143,135 +1143,10 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;

parameter_types! {
pub const BlockRewardName: &'static str = "BlockReward";
}
/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = (
pallet_static_price_provider::InitActivePrice<Runtime, InitActivePriceGet>,
pallet_inflation::PalletInflationInitConfig<Runtime, InitInflationParamsHelper>,
pallet_dapp_staking_v3::migrations::DAppStakingV3InitConfig<
Runtime,
InitDappStakingV3Params,
InitActivePriceGet,
>,
// This will handle new pallet storage version setting & it will put the new pallet into maintenance mode.
// But it's most important for testing with try-runtime.
pallet_dapp_staking_migration::DappStakingMigrationHandler<Runtime>,
frame_support::migrations::RemovePallet<
BlockRewardName,
<Runtime as frame_system::Config>::DbWeight,
>,
);

use sp_arithmetic::fixed_point::FixedU64;
pub struct InitActivePriceGet;
impl Get<FixedU64> for InitActivePriceGet {
fn get() -> FixedU64 {
FixedU64::from_rational(18, 100)
}
}

/// Used to initialize inflation parameters for the runtime.
pub struct InitInflationParamsHelper;
impl Get<(pallet_inflation::InflationParameters, EraNumber, Weight)> for InitInflationParamsHelper {
fn get() -> (pallet_inflation::InflationParameters, EraNumber, Weight) {
(
pallet_inflation::InflationParameters {
// Recalculation is done every two weeks, hence the small %.
max_inflation_rate: Perquintill::from_percent(7),
treasury_part: Perquintill::from_percent(5),
collators_part: Perquintill::from_rational(32_u64, 1000),
dapps_part: Perquintill::from_percent(13),
base_stakers_part: Perquintill::from_percent(25),
adjustable_stakers_part: Perquintill::from_percent(40),
bonus_part: Perquintill::from_rational(138_u64, 1000),
ideal_staking_rate: Perquintill::from_percent(50),
},
pallet_dapps_staking::CurrentEra::<Runtime>::get().saturating_add(1),
<Runtime as frame_system::Config>::DbWeight::get().reads(1),
)
}
}

use frame_support::BoundedVec;
use pallet_dapp_staking_v3::{TierParameters, TiersConfiguration};
type NumberOfTiers = <Runtime as pallet_dapp_staking_v3::Config>::NumberOfTiers;
/// Used to initialize dApp staking parameters for the runtime.
pub struct InitDappStakingV3Params;
impl
Get<(
EraNumber,
TierParameters<NumberOfTiers>,
TiersConfiguration<NumberOfTiers>,
)> for InitDappStakingV3Params
{
fn get() -> (
EraNumber,
TierParameters<NumberOfTiers>,
TiersConfiguration<NumberOfTiers>,
) {
// 1. Prepare init values

// Init era of dApp staking v3 should be the next era after dApp staking v2
let init_era = pallet_dapps_staking::CurrentEra::<Runtime>::get().saturating_add(1);

// Reward portions according to the Tokenomics 2.0 report
let reward_portion = BoundedVec::try_from(vec![
Permill::from_percent(25),
Permill::from_percent(47),
Permill::from_percent(25),
Permill::from_percent(3),
])
.unwrap_or_default();

// Tier thresholds adjusted according to numbers observed on Shibuya
let tier_thresholds = BoundedVec::try_from(vec![
TierThreshold::DynamicTvlAmount {
amount: ASTR.saturating_mul(300_000_000),
minimum_amount: ASTR.saturating_mul(200_000_000),
},
TierThreshold::DynamicTvlAmount {
amount: ASTR.saturating_mul(75_000_000),
minimum_amount: ASTR.saturating_mul(50_000_000),
},
TierThreshold::DynamicTvlAmount {
amount: ASTR.saturating_mul(20_000_000),
minimum_amount: ASTR.saturating_mul(15_000_000),
},
TierThreshold::FixedTvlAmount {
amount: ASTR.saturating_mul(1_500_000),
},
])
.unwrap_or_default();

// 2. Tier params
let tier_params =
TierParameters::<<Runtime as pallet_dapp_staking_v3::Config>::NumberOfTiers> {
reward_portion: reward_portion.clone(),
slot_distribution: BoundedVec::try_from(vec![
Permill::from_percent(5),
Permill::from_percent(20),
Permill::from_percent(30),
Permill::from_percent(45),
])
.unwrap_or_default(),
tier_thresholds: tier_thresholds.clone(),
};

// 3. Init tier config
let init_tier_config = TiersConfiguration {
number_of_slots: 100,
slots_per_tier: BoundedVec::try_from(vec![5, 20, 30, 45]).unwrap_or_default(),
reward_portion,
tier_thresholds,
};

(init_era, tier_params, init_tier_config)
}
}
pub type Migrations = ();

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
Expand Down
2 changes: 1 addition & 1 deletion runtime/local/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "local-runtime"
version = "5.32.0"
version = "5.32.1"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion runtime/shibuya/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "shibuya-runtime"
version = "5.32.0"
version = "5.32.1"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion runtime/shiden/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "shiden-runtime"
version = "5.32.0"
version = "5.32.1"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down

0 comments on commit 5e67f1b

Please sign in to comment.