Skip to content

Commit

Permalink
Merge pull request #13 from Cerebellum-Network/feature/ddc-staking-pa…
Browse files Browse the repository at this point in the history
…rameters

A set of updates for pallet-ddc-staking
  • Loading branch information
khssnv authored Jun 16, 2023
2 parents 5253cd1 + 2435fec commit 8f05bdb
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 285 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [4.3.0]

- ...
### Added

- DDC Staking `CurrentEra` follows DAC era counter

### Changed

- Preferences parameter removed from `pallet-ddc-staking` calls, `value` parameter returned back to `bond` and `unbond` calls
- Distinct bond size requirements for `Storage` and `Edge` roles with default to 100 CERE

## [4.3.0]

Expand Down
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.

2 changes: 1 addition & 1 deletion pallets/ddc-staking/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-ddc-staking"
version = "0.1.0"
version = "4.3.0"
edition = "2021"

[dependencies]
Expand Down
113 changes: 23 additions & 90 deletions pallets/ddc-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//! DdcStaking pallet benchmarking.
use super::*;
use crate::Pallet as DddcStaking;
use crate::Pallet as DdcStaking;
use testing_utils::*;

use frame_support::{ensure, traits::Currency};
use frame_support::traits::{Currency, Get};
use sp_runtime::traits::StaticLookup;
use sp_std::prelude::*;

Expand All @@ -13,63 +13,6 @@ pub use frame_benchmarking::{
};
use frame_system::RawOrigin;

const SEED: u32 = 0;

pub fn clear_storages_with_edges<T: Config>(
n_storages: u32,
n_edges: u32,
) -> Result<
(Vec<<T::Lookup as StaticLookup>::Source>, Vec<<T::Lookup as StaticLookup>::Source>),
&'static str,
> {
// Clean up any existing state.
clear_storages_and_edges::<T>();

// Create new storages
let mut storages: Vec<<T::Lookup as StaticLookup>::Source> =
Vec::with_capacity(n_storages as usize);
for i in 0..n_storages {
let (stash, controller) = create_stash_controller::<T>(i + SEED, 100)?;
let storage_prefs = StoragePrefs { foo: true };
DddcStaking::<T>::store(RawOrigin::Signed(controller).into(), storage_prefs)?;
let stash_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(stash);
storages.push(stash_lookup);
}

// Create new edges
let mut edges: Vec<<T::Lookup as StaticLookup>::Source> = Vec::with_capacity(n_edges as usize);
for i in 0..n_edges {
let (stash, controller) = create_stash_controller::<T>(i + SEED, 100)?;
let edge_prefs = EdgePrefs { foo: true };
DddcStaking::<T>::serve(RawOrigin::Signed(controller).into(), edge_prefs)?;
let stash_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(stash);
edges.push(stash_lookup);
}

Ok((storages, edges))
}

struct AccountsScenario<T: Config> {
origin_stash1: T::AccountId,
origin_controller1: T::AccountId,
}

impl<T: Config> AccountsScenario<T> {
fn new(origin_balance: BalanceOf<T>) -> Result<Self, &'static str> {
ensure!(!origin_balance.is_zero(), "origin weight must be greater than 0");

// burn the entire issuance.
let i = T::Currency::burn(T::Currency::total_issuance());
sp_std::mem::forget(i);

// create accounts with the origin balance
let (origin_stash1, origin_controller1) =
create_stash_controller_with_balance::<T>(USER_SEED + 2, origin_balance)?;

Ok(AccountsScenario { origin_stash1, origin_controller1 })
}
}

const USER_SEED: u32 = 999666;

benchmarks! {
Expand All @@ -78,8 +21,9 @@ benchmarks! {
let controller = create_funded_user::<T>("controller", USER_SEED, 100);
let controller_lookup: <T::Lookup as StaticLookup>::Source
= T::Lookup::unlookup(controller.clone());
let amount = T::Currency::minimum_balance() * 10u32.into();
whitelist_account!(stash);
}: _(RawOrigin::Signed(stash.clone()), controller_lookup)
}: _(RawOrigin::Signed(stash.clone()), controller_lookup, amount)
verify {
assert!(Bonded::<T>::contains_key(stash));
assert!(Ledger::<T>::contains_key(controller));
Expand All @@ -89,22 +33,13 @@ benchmarks! {
// clean up any existing state.
clear_storages_and_edges::<T>();

let total_issuance = T::Currency::total_issuance();

// Constant taken from original benchmark staking code (/frame/staking/src/benchmarking.rs)
let origin_balance = BalanceOf::<T>::try_from(952_994_955_240_703u128)
.map_err(|_| "balance expected to be a u128")
.unwrap();
let scenario = AccountsScenario::<T>::new(origin_balance)?;

let stash = scenario.origin_stash1.clone();
let controller = scenario.origin_controller1.clone();
// unbond half of initial balance
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
let original_bonded: BalanceOf<T> = ledger.active;
let amount = T::Currency::minimum_balance() * 5u32.into(); // Half of total

whitelist_account!(controller);
}: _(RawOrigin::Signed(controller.clone()))
}: _(RawOrigin::Signed(controller.clone()), amount)
verify {
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created after")?;
let new_bonded: BalanceOf<T> = ledger.active;
Expand All @@ -113,8 +48,9 @@ benchmarks! {

withdraw_unbonded {
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
DddcStaking::<T>::unbond(RawOrigin::Signed(controller.clone()).into())?;
CurrentEra::<T>::put(EraIndex::max_value());
let amount = T::Currency::minimum_balance() * 5u32.into(); // Half of total
DdcStaking::<T>::unbond(RawOrigin::Signed(controller.clone()).into(), amount)?;
CurrentEra::<T>::put(EraIndex::max_value());
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
let original_total: BalanceOf<T> = ledger.total;
whitelist_account!(controller);
Expand All @@ -126,38 +62,35 @@ benchmarks! {
}

store {
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
let (stash, controller) = create_stash_controller_with_balance::<T>(0, T::DefaultStorageBondSize::get())?;

let prefs = StoragePrefs::default();
whitelist_account!(controller);
}: _(RawOrigin::Signed(controller), prefs)
}: _(RawOrigin::Signed(controller))
verify {
assert!(Storages::<T>::contains_key(&stash));
assert!(Storages::<T>::get().contains(&stash));
}

serve {
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
serve {
let (stash, controller) = create_stash_controller_with_balance::<T>(0, T::DefaultEdgeBondSize::get())?;

let prefs = EdgePrefs::default();
whitelist_account!(controller);
}: _(RawOrigin::Signed(controller), prefs)
}: _(RawOrigin::Signed(controller))
verify {
assert!(Edges::<T>::contains_key(&stash));
assert!(Edges::<T>::get().contains(&stash));
}

chill {
// clean up any existing state.
clear_storages_and_edges::<T>();

let origin_balance = BondSize::<T>::get().max(T::Currency::minimum_balance());

let scenario = AccountsScenario::<T>::new(origin_balance)?;
let controller = scenario.origin_controller1.clone();
let stash = scenario.origin_stash1.clone();
let (edge_stash, edge_controller) = create_stash_controller_with_balance::<T>(0, T::DefaultEdgeBondSize::get())?;
DdcStaking::<T>::serve(RawOrigin::Signed(edge_controller.clone()).into())?;
assert!(Edges::<T>::get().contains(&edge_stash));

whitelist_account!(controller);
}: _(RawOrigin::Signed(controller))
whitelist_account!(edge_controller);
}: _(RawOrigin::Signed(edge_controller))
verify {
assert!(!Edges::<T>::get().contains(&edge_stash));
}

set_controller {
Expand Down
Loading

0 comments on commit 8f05bdb

Please sign in to comment.