Skip to content

Commit

Permalink
dApp Staking v3 improvements (#1114)
Browse files Browse the repository at this point in the history
* Optimized cleanup

* Benchmarks

* Increase safety margin

* Add missing current era info setting

* Force calls

* Cleanup migrations

* New weights

* Check & error
  • Loading branch information
Dinonard authored Dec 22, 2023
1 parent b487f00 commit 036c146
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 185 deletions.
4 changes: 2 additions & 2 deletions pallets/dapp-staking-migration/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ mod benchmarks {
}

#[benchmark]
fn cleanup_old_storage_success() {
fn cleanup_old_storage_success(x: Linear<1, 5>) {
initial_config::<T>();

#[block]
{
// TODO: for some reason, tests always fail here, nothing gets removed from storage.
// When tested against real runtime, it works just fine.
let _ = Migration::<T>::cleanup_old_storage(1);
let _ = Migration::<T>::cleanup_old_storage(x.into());
}
}

Expand Down
19 changes: 12 additions & 7 deletions pallets/dapp-staking-migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ pub mod pallet {
},
MigrationState::Cleanup => {
// Ensure we don't attempt to delete too much at once.
const SAFETY_MARGIN: u32 = 1000;
const SAFETY_MARGIN: u32 = 2000;
let remaining_weight = weight_limit.saturating_sub(consumed_weight);
let capacity = match remaining_weight.checked_div_per_component(
&<T as Config>::WeightInfo::cleanup_old_storage_success(),
&<T as Config>::WeightInfo::cleanup_old_storage_success(1),
) {
Some(entries_to_delete) => {
SAFETY_MARGIN.min(entries_to_delete.unique_saturated_into())
Expand Down Expand Up @@ -271,9 +271,15 @@ pub mod pallet {
Self::deposit_event(Event::<T>::EntriesDeleted(entries_deleted));
}

// Put the pallet back into maintenance mode.
// Put the pallet back into maintenance mode in case we're still migration the old storage over,
// otherwise disable the maintenance mode.
pallet_dapp_staking_v3::ActiveProtocolState::<T>::mutate(|state| {
state.maintenance = true;
state.maintenance = match migration_state {
MigrationState::NotInProgress
| MigrationState::RegisteredDApps
| MigrationState::Ledgers => true,
MigrationState::Cleanup | MigrationState::Finished => false,
};
});

if migration_state != init_migration_state {
Expand Down Expand Up @@ -448,8 +454,7 @@ pub mod pallet {

if !done {
Ok((
<T as Config>::WeightInfo::cleanup_old_storage_success()
.saturating_mul(keys_removed.into()),
<T as Config>::WeightInfo::cleanup_old_storage_success(keys_removed),
keys_removed as u32,
))
} else {
Expand Down Expand Up @@ -494,7 +499,7 @@ pub mod pallet {
// Consider the weight of all steps
<T as Config>::WeightInfo::migrate_dapps_success()
.max(<T as Config>::WeightInfo::migrate_ledger_success())
.max(<T as Config>::WeightInfo::cleanup_old_storage_success())
.max(<T as Config>::WeightInfo::cleanup_old_storage_success(1))
// and add the weight of updating migration status
.saturating_add(T::DbWeight::get().writes(1))
}
Expand Down
18 changes: 14 additions & 4 deletions pallets/dapp-staking-migration/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,20 @@ fn migrate_call_works() {
Some(Weight::from_parts(1, 1))
));

assert!(
pallet_dapp_staking_v3::ActiveProtocolState::<Test>::get().maintenance,
"Maintenance must always be returned after migrate call finishes."
);
match MigrationStateStorage::<Test>::get() {
MigrationState::RegisteredDApps | MigrationState::Ledgers => {
assert!(
pallet_dapp_staking_v3::ActiveProtocolState::<Test>::get().maintenance,
"Pallet must be in the maintenance mode during old storage migration."
);
}
_ => {
assert!(
!pallet_dapp_staking_v3::ActiveProtocolState::<Test>::get().maintenance,
"Maintenance mode is disabled during old storage cleanup."
);
}
}
}

// Check post-state
Expand Down
86 changes: 48 additions & 38 deletions pallets/dapp-staking-migration/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! Autogenerated weights for pallet_dapp_staking_migration
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-12-20, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2023-12-21, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! WORST CASE MAP SIZE: `1000000`
//! HOSTNAME: `devserver-01`, CPU: `Intel(R) Xeon(R) E-2236 CPU @ 3.40GHz`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("shibuya-dev"), DB CACHE: 1024
Expand Down Expand Up @@ -53,7 +53,7 @@ pub trait WeightInfo {
fn migrate_dapps_noop() -> Weight;
fn migrate_ledger_success() -> Weight;
fn migrate_ledger_noop() -> Weight;
fn cleanup_old_storage_success() -> Weight;
fn cleanup_old_storage_success(x: u32, ) -> Weight;
fn cleanup_old_storage_noop() -> Weight;
}

Expand All @@ -74,8 +74,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `558`
// Estimated: `6112`
// Minimum execution time: 46_218_000 picoseconds.
Weight::from_parts(47_610_000, 6112)
// Minimum execution time: 46_832_000 picoseconds.
Weight::from_parts(47_613_000, 6112)
.saturating_add(T::DbWeight::get().reads(6_u64))
.saturating_add(T::DbWeight::get().writes(5_u64))
}
Expand All @@ -85,8 +85,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `3551`
// Minimum execution time: 3_385_000 picoseconds.
Weight::from_parts(3_552_000, 3551)
// Minimum execution time: 6_296_000 picoseconds.
Weight::from_parts(6_626_000, 3551)
.saturating_add(T::DbWeight::get().reads(1_u64))
}
/// Storage: DappsStaking Ledger (r:2 w:1)
Expand All @@ -105,8 +105,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `1748`
// Estimated: `6472`
// Minimum execution time: 69_553_000 picoseconds.
Weight::from_parts(70_319_000, 6472)
// Minimum execution time: 70_686_000 picoseconds.
Weight::from_parts(71_976_000, 6472)
.saturating_add(T::DbWeight::get().reads(7_u64))
.saturating_add(T::DbWeight::get().writes(6_u64))
}
Expand All @@ -116,27 +116,32 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `3731`
// Minimum execution time: 2_918_000 picoseconds.
Weight::from_parts(3_022_000, 3731)
// Minimum execution time: 2_801_000 picoseconds.
Weight::from_parts(3_072_000, 3731)
.saturating_add(T::DbWeight::get().reads(1_u64))
}
/// Storage: DappsStaking Ledger (r:2 w:1)
/// Storage: DappsStaking Ledger (r:6 w:5)
/// Proof: DappsStaking Ledger (max_values: None, max_size: Some(266), added: 2741, mode: MaxEncodedLen)
fn cleanup_old_storage_success() -> Weight {
/// The range of component `x` is `[1, 5]`.
fn cleanup_old_storage_success(x: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `739`
// Estimated: `6472`
// Minimum execution time: 7_109_000 picoseconds.
Weight::from_parts(7_383_000, 6472)
.saturating_add(T::DbWeight::get().reads(2_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
// Measured: `699 + x * (39 ±0)`
// Estimated: `3731 + x * (2741 ±0)`
// Minimum execution time: 7_303_000 picoseconds.
Weight::from_parts(6_916_878, 3731)
// Standard Error: 7_249
.saturating_add(Weight::from_parts(675_810, 0).saturating_mul(x.into()))
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(x.into())))
.saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(x.into())))
.saturating_add(Weight::from_parts(0, 2741).saturating_mul(x.into()))
}
fn cleanup_old_storage_noop() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 2_095_000 picoseconds.
Weight::from_parts(2_213_000, 0)
// Minimum execution time: 2_094_000 picoseconds.
Weight::from_parts(2_182_000, 0)
}
}

Expand All @@ -156,8 +161,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `558`
// Estimated: `6112`
// Minimum execution time: 46_218_000 picoseconds.
Weight::from_parts(47_610_000, 6112)
// Minimum execution time: 46_832_000 picoseconds.
Weight::from_parts(47_613_000, 6112)
.saturating_add(RocksDbWeight::get().reads(6_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}
Expand All @@ -167,8 +172,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `3551`
// Minimum execution time: 3_385_000 picoseconds.
Weight::from_parts(3_552_000, 3551)
// Minimum execution time: 6_296_000 picoseconds.
Weight::from_parts(6_626_000, 3551)
.saturating_add(RocksDbWeight::get().reads(1_u64))
}
/// Storage: DappsStaking Ledger (r:2 w:1)
Expand All @@ -187,8 +192,8 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `1748`
// Estimated: `6472`
// Minimum execution time: 69_553_000 picoseconds.
Weight::from_parts(70_319_000, 6472)
// Minimum execution time: 70_686_000 picoseconds.
Weight::from_parts(71_976_000, 6472)
.saturating_add(RocksDbWeight::get().reads(7_u64))
.saturating_add(RocksDbWeight::get().writes(6_u64))
}
Expand All @@ -198,26 +203,31 @@ impl WeightInfo for () {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `3731`
// Minimum execution time: 2_918_000 picoseconds.
Weight::from_parts(3_022_000, 3731)
// Minimum execution time: 2_801_000 picoseconds.
Weight::from_parts(3_072_000, 3731)
.saturating_add(RocksDbWeight::get().reads(1_u64))
}
/// Storage: DappsStaking Ledger (r:2 w:1)
/// Storage: DappsStaking Ledger (r:6 w:5)
/// Proof: DappsStaking Ledger (max_values: None, max_size: Some(266), added: 2741, mode: MaxEncodedLen)
fn cleanup_old_storage_success() -> Weight {
/// The range of component `x` is `[1, 5]`.
fn cleanup_old_storage_success(x: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `739`
// Estimated: `6472`
// Minimum execution time: 7_109_000 picoseconds.
Weight::from_parts(7_383_000, 6472)
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
// Measured: `699 + x * (39 ±0)`
// Estimated: `3731 + x * (2741 ±0)`
// Minimum execution time: 7_303_000 picoseconds.
Weight::from_parts(6_916_878, 3731)
// Standard Error: 7_249
.saturating_add(Weight::from_parts(675_810, 0).saturating_mul(x.into()))
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(x.into())))
.saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(x.into())))
.saturating_add(Weight::from_parts(0, 2741).saturating_mul(x.into()))
}
fn cleanup_old_storage_noop() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 2_095_000 picoseconds.
Weight::from_parts(2_213_000, 0)
// Minimum execution time: 2_094_000 picoseconds.
Weight::from_parts(2_182_000, 0)
}
}
77 changes: 71 additions & 6 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ pub mod pallet {
TooManyStakedContracts,
/// There are no expired entries to cleanup for the account.
NoExpiredEntries,
// TODO: remove this prior to the launch
/// Tier parameters aren't valid.
InvalidTierParameters,
}

/// General information about dApp staking protocol state.
Expand Down Expand Up @@ -1501,6 +1504,37 @@ pub mod pallet {

Ok(())
}

// TODO: remove this prior to Astar launch, to be only used for testing
#[pallet::call_index(100)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn force_set_tier_params(
origin: OriginFor<T>,
value: TierParameters<T::NumberOfTiers>,
) -> DispatchResult {
Self::ensure_pallet_enabled()?;
T::ManagerOrigin::ensure_origin(origin)?;
ensure!(value.is_valid(), Error::<T>::InvalidTierParameters);

StaticTierParams::<T>::put(value);

Ok(())
}

// TODO: remove this prior to Astar launch, to be only used for testing
#[pallet::call_index(101)]
#[pallet::weight(T::DbWeight::get().writes(1))]
pub fn force_set_tier_config(
origin: OriginFor<T>,
value: TiersConfiguration<T::NumberOfTiers>,
) -> DispatchResult {
Self::ensure_pallet_enabled()?;
T::ManagerOrigin::ensure_origin(origin)?;

TierConfig::<T>::put(value);

Ok(())
}
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -1974,28 +2008,48 @@ impl<
// 0. Unwrap arguments
let (init_era, tier_params, init_tier_config) = G::get();

// 1. Prepare active protocol state
// 1. Prepare init active protocol state
let now = frame_system::Pallet::<T>::block_number();
let voting_period_length = Pallet::<T>::blocks_per_voting_period();

let period_number = 1;
let protocol_state = ProtocolState {
era: init_era,
next_era_start: now.saturating_add(voting_period_length),
period_info: PeriodInfo {
number: 1,
number: period_number,
subperiod: Subperiod::Voting,
next_subperiod_start_era: init_era.saturating_add(1),
},
maintenance: true,
};

// 2. Write necessary items into storage
// 2. Prepare init current era info - need to set correct eras
let init_era_info = EraInfo {
total_locked: 0,
unlocking: 0,
current_stake_amount: StakeAmount {
voting: 0,
build_and_earn: 0,
era: init_era,
period: period_number,
},
next_stake_amount: StakeAmount {
voting: 0,
build_and_earn: 0,
era: init_era.saturating_add(1),
period: period_number,
},
};

// 3. Write necessary items into storage
ActiveProtocolState::<T>::put(protocol_state);
StaticTierParams::<T>::put(tier_params);
TierConfig::<T>::put(init_tier_config);
STORAGE_VERSION.put::<Pallet<T>>();
CurrentEraInfo::<T>::put(init_era_info);

// 3. Emit events to make indexers happy
// 4. Emit events to make indexers happy
Pallet::<T>::deposit_event(Event::<T>::NewEra { era: init_era });
Pallet::<T>::deposit_event(Event::<T>::NewSubperiod {
subperiod: Subperiod::Voting,
Expand All @@ -2004,13 +2058,14 @@ impl<

log::info!("dApp Staking v3 storage initialized.");

T::DbWeight::get().reads_writes(1, 4)
T::DbWeight::get().reads_writes(2, 5)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
assert_eq!(Pallet::<T>::on_chain_storage_version(), STORAGE_VERSION);
assert!(ActiveProtocolState::<T>::get().maintenance);
let protocol_state = ActiveProtocolState::<T>::get();
assert!(protocol_state.maintenance);

let number_of_tiers = T::NumberOfTiers::get();

Expand All @@ -2023,6 +2078,16 @@ impl<
assert_eq!(tier_config.slots_per_tier.len(), number_of_tiers as usize);
assert_eq!(tier_config.tier_thresholds.len(), number_of_tiers as usize);

let current_era_info = CurrentEraInfo::<T>::get();
assert_eq!(
current_era_info.current_stake_amount.era,
protocol_state.era
);
assert_eq!(
current_era_info.next_stake_amount.era,
protocol_state.era + 1
);

Ok(())
}
}
Loading

0 comments on commit 036c146

Please sign in to comment.