Skip to content

Commit

Permalink
Mesh 2149/staking-pallet-update-part-1 (#1623)
Browse files Browse the repository at this point in the history
* Update to new macro

* Add polymesh specific types

* Add polymesh changes to extrinsics

* Make methods and structs pub; Add slash impl

* Add missing impl; Change visibility

* Add new associated types

* Fix tests; Change vec to BoundedVec; Change Module<T> to Pallet<T>

* Add genesis build; Add hooks; Compile tests

* Change storage to OptionQuery

* Fix some tests

* Compile benchmarks

* Fix benchmark run

* Fix warnings

* Remove files

* Remove warnings; Add benchmarks

* Cargo fmt

* Cargo fmt

* Remove breaking changes; Fix integration tests

* Fix integration tests

* Use StorageMap instead of CountedMap; Add PolymeshStorageVersion

* Fix test functions

* Fix storage version check.

* Remove old code; Move comment

---------

Co-authored-by: Robert G. Jakabosky <rjakabosky+neopallium@neoawareness.com>
  • Loading branch information
HenriqueNogara and Neopallium committed Apr 16, 2024
1 parent 9077a2a commit 2ef2fc0
Show file tree
Hide file tree
Showing 30 changed files with 6,349 additions and 5,002 deletions.
5 changes: 2 additions & 3 deletions pallets/runtime/common/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ macro_rules! misc_pallet_impls {
}

impl pallet_staking::Config for Runtime {
const MAX_NOMINATIONS: u32 = pallet_staking::MAX_NOMINATIONS;
type Currency = Balances;
type UnixTime = Timestamp;
type CurrencyToVote = frame_support::traits::U128CurrencyToVote;
Expand Down Expand Up @@ -266,10 +265,10 @@ macro_rules! misc_pallet_impls {
type FixedYearlyReward = FixedYearlyReward;
type PalletsOrigin = OriginCaller;
type MinimumBond = MinimumBond;
// The unsigned solution weight targeted by the OCW. We set it to the maximum possible value of
// a single extrinsic.
type OffchainSolutionWeightLimit = polymesh_runtime_common::OffchainSolutionWeightLimit;
type WeightInfo = polymesh_weights::pallet_staking::SubstrateWeight;
type MaxNominations = pallet_staking::MaxNominations;
type MaxUnlockingChunks = pallet_staking::MaxUnlockingChunks;
}

impl pallet_authority_discovery::Config for Runtime {
Expand Down
14 changes: 6 additions & 8 deletions pallets/runtime/develop/src/benchmarks/pallet_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
use codec::Decode;
use core::convert::TryInto;
use frame_benchmarking::benchmarks;
use frame_support::traits::{Currency, OnInitialize};
use frame_support::traits::{Currency, Get, OnInitialize};
use frame_system::RawOrigin;
use pallet_session::{Call, Pallet as Session};
use pallet_staking::{
benchmarking::create_validator_with_nominators_with_balance, MAX_NOMINATIONS,
};
use pallet_staking::benchmarking::create_validator_with_nominators_with_balance;
use polymesh_common_utilities::{benchs::AccountIdOf, TestUtilsFn};
use sp_runtime::traits::TrailingZeroInput;
use sp_std::prelude::*;
Expand Down Expand Up @@ -65,14 +63,14 @@ impl<T: Config + TestUtilsFn<AccountIdOf<T>>> ValidatorInfo<T> {
let balance: u32 = (4_000 * POLY).try_into().unwrap();
let stash = create_validator_with_nominators_with_balance::<T>(
nominators,
MAX_NOMINATIONS as u32,
<T as pallet_staking::Config>::MaxNominations::get(),
balance,
false,
)
.unwrap()
.0
.account();
let controller = pallet_staking::Module::<T>::bonded(&stash).expect("not stash");
let controller = pallet_staking::Pallet::<T>::bonded(&stash).expect("not stash");

let keys = T::Keys::decode(&mut TrailingZeroInput::zeroes()).unwrap();
let proof: Vec<u8> = vec![0, 1, 2, 3];
Expand All @@ -97,15 +95,15 @@ benchmarks! {
}

set_keys {
let n = MAX_NOMINATIONS as u32;
let n = <T as pallet_staking::Config>::MaxNominations::get() as u32;
let validator = ValidatorInfo::<T>::build(n).unwrap();
let proof = validator.proof.clone();
let keys = validator.keys.clone();

}: _(RawOrigin::Signed(validator.controller), keys, proof)

purge_keys {
let n = MAX_NOMINATIONS as u32;
let n = <T as pallet_staking::Config>::MaxNominations::get() as u32;
let validator = ValidatorInfo::<T>::build(n).unwrap();
let controller = RawOrigin::Signed(validator.controller.clone());

Expand Down
23 changes: 12 additions & 11 deletions pallets/runtime/tests/src/staking/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use frame_support::{
OnFinalize, OnInitialize, OnUnbalanced, OneSessionHandler, SortedMembers,
},
weights::constants::RocksDbWeight,
IterableStorageMap, StorageDoubleMap, StorageMap, StorageValue,
StorageDoubleMap, StorageMap,
};
use frame_system::{EnsureRoot, EnsureSignedBy};
use pallet_group as group;
Expand Down Expand Up @@ -69,8 +69,10 @@ use sp_staking::{
};
use std::{cell::RefCell, collections::BTreeMap};

use pallet_staking::types::SlashingSwitch;

pub const INIT_TIMESTAMP: u64 = 30_000;
pub const BLOCK_TIME: u64 = 1000;
pub const BLOCK_TIME: u64 = 1_000;

/// The AccountId alias in this test module.
pub(crate) type AccountId = u64;
Expand Down Expand Up @@ -272,7 +274,7 @@ impl pallet_treasury::Config for Test {

impl pallet_authorship::Config for Test {
type FindAuthor = Author11;
type EventHandler = pallet_staking::Module<Test>;
type EventHandler = pallet_staking::Pallet<Test>;
}
parameter_types! {
pub const MinimumPeriod: u64 = 5;
Expand Down Expand Up @@ -586,7 +588,6 @@ impl SortedMembers<u64> for TwoThousand {
}

impl Config for Test {
const MAX_NOMINATIONS: u32 = pallet_staking::MAX_NOMINATIONS;
type Currency = Balances;
type UnixTime = Timestamp;
type CurrencyToVote = frame_support::traits::SaturatingCurrencyToVote;
Expand Down Expand Up @@ -619,6 +620,8 @@ impl Config for Test {
type MaxVariableInflationTotalIssuance = MaxVariableInflationTotalIssuance;
type FixedYearlyReward = FixedYearlyReward;
type MinimumBond = MinimumBond;
type MaxNominations = pallet_staking::MaxNominations;
type MaxUnlockingChunks = pallet_staking::MaxUnlockingChunks;
}

impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Test
Expand Down Expand Up @@ -1275,7 +1278,7 @@ pub(crate) fn reward_all_elected() {
.into_iter()
.map(|v| (v, 1));

<Module<Test>>::reward_by_ids(rewards)
<Pallet<Test>>::reward_by_ids(rewards)
}

pub(crate) fn validator_controllers() -> Vec<AccountId> {
Expand All @@ -1293,7 +1296,7 @@ pub(crate) fn on_offence_in_era(
slash_fraction: &[Perbill],
era: EraIndex,
) {
let bonded_eras = staking::BondedEras::get();
let bonded_eras = staking::BondedEras::<Test>::get();
for &(bonded_era, start_session) in bonded_eras.iter() {
if bonded_era == era {
let _ = Staking::on_offence(
Expand Down Expand Up @@ -1478,10 +1481,8 @@ pub(crate) fn prepare_submission_with(
} = Staking::do_phragmen::<OffchainAccuracy>(iterations).unwrap();
let winners = winners.into_iter().map(|(who, _)| who).collect::<Vec<_>>();

let mut staked = sp_npos_elections::assignment_ratio_to_staked(
assignments,
Staking::slashable_balance_of_fn(),
);
let mut staked =
sp_npos_elections::assignment_ratio_to_staked(assignments, Staking::weight_of_fn());

// apply custom tweaks. awesome for testing.
tweak(&mut staked);
Expand Down Expand Up @@ -1518,7 +1519,7 @@ pub(crate) fn prepare_submission_with(
let score = if compute_real_score {
let staked = sp_npos_elections::assignment_ratio_to_staked(
assignments_reduced.clone(),
Staking::slashable_balance_of_fn(),
Staking::weight_of_fn(),
);

let support_map = to_supports::<AccountId>(staked.as_slice());
Expand Down

0 comments on commit 2ef2fc0

Please sign in to comment.