Skip to content

Commit

Permalink
Stake/unstake fix (#1187)
Browse files Browse the repository at this point in the history
* Reproduce issue

* Update unstake logic

* Fix

* Fix cont.

* Improve reproduction test

* Improvements start

* Fix for the issue cont.

* Expanded test, cleanup

* Additional checks

* More checks, resolve TODOs

* Working solution

* Remove println

* Refactoring

* Remove redundant args

* Tests, comments, improvements

* Passing type tests

* More tests & docs

* Full fix

* Improved tests & docs

* Additional test

* Comments

* Implemented migration

* Merge issue resolution

* Vec dep

* Fixes, formatting

* Fix

* Update docs

* Comment
  • Loading branch information
Dinonard authored Mar 18, 2024
1 parent 208e233 commit b392564
Show file tree
Hide file tree
Showing 18 changed files with 824 additions and 1,410 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

3 changes: 0 additions & 3 deletions pallets/dapp-staking-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ sp-std = { workspace = true }

astar-primitives = { workspace = true }
pallet-dapp-staking-v3 = { workspace = true }
pallet-dapps-staking = { workspace = true }

[dev-dependencies]
pallet-balances = { workspace = true }
Expand All @@ -39,7 +38,6 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-dapp-staking-v3/std",
"pallet-dapps-staking/std",
"frame-benchmarking/std",
"astar-primitives/std",
"sp-core/std",
Expand All @@ -51,7 +49,6 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-dapp-staking-v3/runtime-benchmarks",
"pallet-dapps-staking/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
122 changes: 24 additions & 98 deletions pallets/dapp-staking-migration/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,34 @@

use super::{Pallet as Migration, *};

use astar_primitives::{dapp_staking::SmartContractHandle, Balance};
use frame_benchmarking::{account as benchmark_account, v2::*};
use frame_support::{assert_ok, traits::Currency};
use sp_std::prelude::*;
use pallet_dapp_staking_v3::StakeAmount;
use sp_std::vec;

/// Generate an unique smart contract using the provided index as a sort-of indetifier
fn smart_contract<T: pallet_dapps_staking::Config>(index: u8) -> T::SmartContract {
// This is a hacky approach to provide different smart contracts without touching the smart contract trait.
let mut encoded_smart_contract = T::SmartContract::default().encode();
*encoded_smart_contract.last_mut().unwrap() = index;

Decode::decode(&mut TrailingZeroInput::new(encoded_smart_contract.as_ref()))
.expect("Shouldn't occur as long as EVM is the default type.")
fn smart_contract<T: Config>(idx: u8) -> T::SmartContract {
let address: T::AccountId = benchmark_account("smart_contract", idx.into(), 456);
T::SmartContract::wasm(address)
}

/// Initialize the old dApp staking pallet with some storage.
pub(super) fn initial_config<T: Config>() {
let dapps_number = <T as pallet_dapp_staking_v3::Config>::MaxNumberOfContracts::get();
let dapps_number = (dapps_number as u8).min(100);

// Add some dummy dApps to the old pallet.
for idx in 0..dapps_number {
let developer: T::AccountId = benchmark_account("developer", idx.into(), 123);
<T as pallet_dapps_staking::Config>::Currency::make_free_balance_be(
&developer,
<T as pallet_dapps_staking::Config>::RegisterDeposit::get() * 2,
);
for idx in 0..10 {
let account: T::AccountId = benchmark_account("developer", idx.into(), 123);
let smart_contract = smart_contract::<T>(idx);
assert_ok!(pallet_dapps_staking::Pallet::<T>::register(
RawOrigin::Root.into(),
developer,
smart_contract.clone(),
));

let staker: T::AccountId = benchmark_account("staker", idx.into(), 123);
let lock_amount = <T as pallet_dapps_staking::Config>::MinimumStakingAmount::get()
.max(<T as pallet_dapp_staking_v3::Config>::MinimumLockedAmount::get());
<T as pallet_dapps_staking::Config>::Currency::make_free_balance_be(
&staker,
lock_amount * 100,
v5::StakerInfo::<T>::insert(
&account,
&smart_contract,
v5::SingularStakingInfo {
staked: StakeAmount {
voting: 123 * (idx as Balance + 1),
build_and_earn: 345 * (idx as Balance + 1),
era: 1,
period: 2,
},
loyal_staker: true,
},
);
assert_ok!(pallet_dapps_staking::Pallet::<T>::bond_and_stake(
RawOrigin::Signed(staker.clone()).into(),
smart_contract,
lock_amount,
));
}
}

Expand All @@ -71,77 +54,20 @@ mod benchmarks {
use super::*;

#[benchmark]
fn migrate_dapps_success() {
fn translate_staking_info_success() {
initial_config::<T>();

#[block]
{
assert!(Migration::<T>::migrate_dapps().is_ok());
assert!(Migration::<T>::translate_staking_info(None).is_ok());
}
}

#[benchmark]
fn migrate_dapps_noop() {
fn translate_staking_info_success_noop() {
#[block]
{
assert!(Migration::<T>::migrate_dapps().is_err());
assert!(Migration::<T>::translate_staking_info(None).is_err());
}
}

#[benchmark]
fn migrate_ledger_success() {
initial_config::<T>();

#[block]
{
assert!(Migration::<T>::migrate_ledger().is_ok());
}
}

#[benchmark]
fn migrate_ledger_noop() {
#[block]
{
assert!(Migration::<T>::migrate_ledger().is_err());
}
}

#[benchmark]
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(x.into());
}
}

#[benchmark]
fn cleanup_old_storage_noop() {
let hashed_prefix = twox_128(pallet_dapps_staking::Pallet::<T>::name().as_bytes());
let _ = clear_prefix(&hashed_prefix, None);

#[block]
{
assert!(Migration::<T>::cleanup_old_storage(1).is_err());
}
}

impl_benchmark_test_suite!(
Pallet,
crate::benchmarking::tests::new_test_ext(),
crate::mock::Test,
);
}

#[cfg(test)]
mod tests {
use crate::mock;
use sp_io::TestExternalities;

pub fn new_test_ext() -> TestExternalities {
mock::ExtBuilder::build()
}
}
Loading

0 comments on commit b392564

Please sign in to comment.