From 12a70398cff01c6ae8d91386da875a344b1db76e Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 26 Mar 2020 10:11:32 +0100 Subject: [PATCH 01/16] Add initial report_ofence bench. --- Cargo.lock | 4 ++ bin/node/runtime/Cargo.toml | 1 + bin/node/runtime/src/lib.rs | 8 +++ frame/offences/Cargo.toml | 9 +++ frame/offences/src/benchmarking.rs | 93 ++++++++++++++++++++++++++++++ frame/offences/src/lib.rs | 27 ++++++++- frame/staking/src/lib.rs | 4 +- 7 files changed, 142 insertions(+), 4 deletions(-) create mode 100644 frame/offences/src/benchmarking.rs diff --git a/Cargo.lock b/Cargo.lock index 76fabe80eb1be..0ff8b4c191c40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4375,9 +4375,13 @@ dependencies = [ name = "pallet-offences" version = "2.0.0-alpha.5" dependencies = [ + "frame-benchmarking", "frame-support", "frame-system", "pallet-balances", + "pallet-im-online", + "pallet-session", + "pallet-staking", "parity-scale-codec", "serde", "sp-core", diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index 24ae560607a8d..a87df4fe05667 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -143,6 +143,7 @@ runtime-benchmarks = [ "pallet-staking/runtime-benchmarks", "pallet-vesting/runtime-benchmarks", "pallet-collective/runtime-benchmarks", + "pallet-offences/runtime-benchmarks", "pallet-session-benchmarking", "pallet-staking/runtime-benchmarks", "pallet-im-online/runtime-benchmarks", diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 7bb230ec0fc67..80a660e7c8441 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -858,6 +858,7 @@ impl_runtime_apis! { // we need these two lines below. use pallet_session_benchmarking::Module as SessionBench; impl pallet_session_benchmarking::Trait for Runtime {} + impl pallet_offences::benchmarking::Trait for Runtime {} let result = match module.as_slice() { b"pallet-balances" | b"balances" => Balances::run_benchmark( @@ -923,6 +924,13 @@ impl_runtime_apis! { steps, repeat, ), + b"pallet-offences" | b"offences" => Offences::run_benchmark( + extrinsic, + lowest_range_values, + highest_range_values, + steps, + repeat, + ), _ => Err("Benchmark not found for this pallet."), }; diff --git a/frame/offences/Cargo.toml b/frame/offences/Cargo.toml index 31f7b1d8f44e5..f64e5e7ab1146 100644 --- a/frame/offences/Cargo.toml +++ b/frame/offences/Cargo.toml @@ -15,8 +15,12 @@ sp-std = { version = "2.0.0-alpha.5", default-features = false, path = "../../pr serde = { version = "1.0.101", optional = true } sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../../primitives/staking" } +frame-benchmarking = { version = "2.0.0-alpha.5", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "2.0.0-alpha.5", default-features = false, path = "../support" } frame-system = { version = "2.0.0-alpha.5", default-features = false, path = "../system" } +pallet-im-online = { version = "2.0.0-alpha.5", default-features = false, path = "../im-online" } +pallet-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../staking" } +pallet-session = { version = "2.0.0-alpha.5", default-features = false, path = "../session" } [dev-dependencies] sp-io = { version = "2.0.0-alpha.5", path = "../../primitives/io" } @@ -26,11 +30,16 @@ sp-core = { version = "2.0.0-alpha.5", path = "../../primitives/core" } default = ["std"] std = [ "pallet-balances/std", + "pallet-im-online/std", + "pallet-staking/std", + "pallet-session/std", "codec/std", "sp-std/std", "serde", "sp-runtime/std", "sp-staking/std", + "frame-benchmarking/std", "frame-support/std", "frame-system/std", ] +runtime-benchmarks = ["frame-benchmarking"] diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs new file mode 100644 index 0000000000000..14fb5cc6df2de --- /dev/null +++ b/frame/offences/src/benchmarking.rs @@ -0,0 +1,93 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// This file is part of Substrate. + +// Substrate is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Substrate is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Substrate. If not, see . + +//! Offences pallet benchmarking. + +use super::*; + +use sp_std::prelude::*; +use frame_benchmarking::{benchmarks, account}; +use frame_support::traits::{Currency, Get}; +use frame_system::{RawOrigin, Module as System, self}; +use sp_runtime::traits::{Bounded, EnsureOrigin, Convert, StaticLookup}; +use sp_runtime::Perbill; +use pallet_im_online::UnresponsivenessOffence; +use pallet_staking::{Module as Staking, ExposureOf, RewardDestination, ValidatorPrefs}; +use sp_staking::offence::Offence; +use pallet_session::historical::IdentificationTuple; + +use crate::Module as Offences; + +const SEED: u32 = 0; +const MAX_USERS: u32 = 1000; +const MAX_REPORTERS: u32 = 1000; +const MAX_OFFENDERS: u32 = 1000; + +pub trait Trait: pallet_staking::Trait + crate::Trait + pallet_im_online::Trait {} + +pub fn create_stash_controller(n: u32) -> Result<(T::AccountId, T::AccountId), &'static str> { + let stash: T::AccountId = account("stash", n, 0); + let controller: T::AccountId = account("controller", n, 0); + let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); + let reward_destination = RewardDestination::Staked; + let amount = T::Currency::minimum_balance() * 10.into(); + Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?; + let validator_prefs = ValidatorPrefs { + commission: Perbill::from_percent(50), + }; + Staking::::validate(RawOrigin::Signed(controller.clone()).into(), validator_prefs)?; + + return Ok((stash, controller)) +} + +benchmarks! { + _ { + let r in 1 .. MAX_REPORTERS => (); + let o in 1 .. MAX_OFFENDERS => (); + } + + report_offence { + let r in ...; + let o in ...; + + let mut offenders: Vec = vec![]; + for i in 0 .. o { + offenders.push(account("controller", i, 0)); + create_stash_controller::(i)?; + } + + let offenders = offenders.iter() + .map(|id | (id, ::ValidatorIdOf::convert(id.clone()).expect("validator exist for this id"))) + .map(|(id, validator_id)| ::FullIdentificationOf::convert(validator_id.clone()).map(|full_id| (validator_id, full_id)).expect("full identification exist for this validator")) + .collect::>>(); + + + let offence = UnresponsivenessOffence { + session_index: 0, + validator_set_count: 0, + offenders, + }; + + let mut reporters = vec![]; + for i in 0 .. r { + let reporter = account("reporter", i, SEED); + reporters.push(reporter); + } + + }: { + let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); + } +} \ No newline at end of file diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 3e7f8c9537118..137833b0c4f0e 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -23,13 +23,15 @@ mod mock; mod tests; +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; use sp_std::vec::Vec; use frame_support::{ decl_module, decl_event, decl_storage, Parameter, weights::{Weight, SimpleDispatchInfo, WeighData}, }; -use sp_runtime::traits::Hash; +use sp_runtime::{traits::Hash, RuntimeDebug}; use sp_staking::{ offence::{Offence, ReportOffence, Kind, OnOffenceHandler, OffenceDetails, OffenceError}, }; @@ -47,7 +49,7 @@ pub trait Trait: frame_system::Trait { /// The overarching event type. type Event: From + Into<::Event>; /// Full identification of the validator. - type IdentificationTuple: Parameter + Ord; + type IdentificationTuple: Parameter + Ord + Default; /// A handler called for every offence report. type OnOffenceHandler: OnOffenceHandler; } @@ -158,6 +160,9 @@ impl Module { time_slot: &O::TimeSlot, offenders: Vec, ) -> Option> { + #[cfg(feature = "std")] + println!("executing triage"); + let mut storage = ReportIndexStorage::::load(time_slot); let mut any_new = false; @@ -165,6 +170,9 @@ impl Module { let report_id = Self::report_id::(time_slot, &offender); if !>::contains_key(&report_id) { + #[cfg(feature = "std")] + println!("triage inserting report {:?}", report_id); + any_new = true; >::insert( &report_id, @@ -179,12 +187,18 @@ impl Module { } if any_new { + #[cfg(feature = "std")] + println!("triage any_new"); + // Load report details for the all reports happened at the same time. let concurrent_offenders = storage.concurrent_reports .iter() .filter_map(|report_id| >::get(report_id)) .collect::>(); + #[cfg(feature = "std")] + println!("triage concurrent offenders {:?}", concurrent_offenders.len()); + storage.save(); Some(TriageOutcome { @@ -216,6 +230,9 @@ struct ReportIndexStorage> { impl> ReportIndexStorage { /// Preload indexes from the storage for the specific `time_slot` and the kind of the offence. fn load(time_slot: &O::TimeSlot) -> Self { + #[cfg(feature = "std")] + println!("treportindexstorage load"); + let opaque_time_slot = time_slot.encode(); let same_kind_reports = ::get(&O::ID); @@ -234,6 +251,9 @@ impl> ReportIndexStorage { /// Insert a new report to the index. fn insert(&mut self, time_slot: &O::TimeSlot, report_id: ReportIdOf) { + #[cfg(feature = "std")] + println!("treportindexstorage insert"); + // Insert the report id into the list while maintaining the ordering by the time // slot. let pos = match self @@ -252,6 +272,9 @@ impl> ReportIndexStorage { /// Dump the indexes to the storage. fn save(self) { + #[cfg(feature = "std")] + println!("treportindexstorage save"); + ::insert(&O::ID, self.same_kind_reports.encode()); >::insert( &O::ID, diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 53ef7b41e4360..85fb86afc84df 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -983,7 +983,7 @@ decl_module! { /// the `origin` falls below _existential deposit_ and gets removed as dust. /// # #[weight = SimpleDispatchInfo::FixedNormal(500_000)] - fn bond(origin, + pub fn bond(origin, controller: ::Source, #[compact] value: BalanceOf, payee: RewardDestination @@ -1173,7 +1173,7 @@ decl_module! { /// - Writes are limited to the `origin` account key. /// # #[weight = SimpleDispatchInfo::FixedNormal(750_000)] - fn validate(origin, prefs: ValidatorPrefs) { + pub fn validate(origin, prefs: ValidatorPrefs) { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let stash = &ledger.stash; From 9d01d481d08898023598bbac3757ab08667120a9 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 26 Mar 2020 10:22:09 +0100 Subject: [PATCH 02/16] Remove unused imports --- frame/im-online/src/lib.rs | 6 +++--- frame/offences/src/benchmarking.rs | 22 ++++++++-------------- frame/offences/src/lib.rs | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/frame/im-online/src/lib.rs b/frame/im-online/src/lib.rs index 861c57e5b61bc..5944d7e8c18cb 100644 --- a/frame/im-online/src/lib.rs +++ b/frame/im-online/src/lib.rs @@ -674,11 +674,11 @@ pub struct UnresponsivenessOffence { /// /// It acts as a time measure for unresponsiveness reports and effectively will always point /// at the end of the session. - session_index: SessionIndex, + pub session_index: SessionIndex, /// The size of the validator set in current session/era. - validator_set_count: u32, + pub validator_set_count: u32, /// Authorities that were unresponsive during the current era. - offenders: Vec, + pub offenders: Vec, } impl Offence for UnresponsivenessOffence { diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs index 14fb5cc6df2de..f435c4d6604ae 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/src/benchmarking.rs @@ -20,25 +20,19 @@ use super::*; use sp_std::prelude::*; use frame_benchmarking::{benchmarks, account}; -use frame_support::traits::{Currency, Get}; -use frame_system::{RawOrigin, Module as System, self}; -use sp_runtime::traits::{Bounded, EnsureOrigin, Convert, StaticLookup}; -use sp_runtime::Perbill; +use frame_support::traits::Currency; +use frame_system::RawOrigin; +use sp_runtime::{Perbill, traits::{Convert, StaticLookup}}; use pallet_im_online::UnresponsivenessOffence; -use pallet_staking::{Module as Staking, ExposureOf, RewardDestination, ValidatorPrefs}; -use sp_staking::offence::Offence; -use pallet_session::historical::IdentificationTuple; - -use crate::Module as Offences; +use pallet_staking::{Module as Staking, RewardDestination, ValidatorPrefs}; const SEED: u32 = 0; -const MAX_USERS: u32 = 1000; const MAX_REPORTERS: u32 = 1000; const MAX_OFFENDERS: u32 = 1000; pub trait Trait: pallet_staking::Trait + crate::Trait + pallet_im_online::Trait {} -pub fn create_stash_controller(n: u32) -> Result<(T::AccountId, T::AccountId), &'static str> { +pub fn create_offender(n: u32) -> Result { let stash: T::AccountId = account("stash", n, 0); let controller: T::AccountId = account("controller", n, 0); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); @@ -50,7 +44,7 @@ pub fn create_stash_controller(n }; Staking::::validate(RawOrigin::Signed(controller.clone()).into(), validator_prefs)?; - return Ok((stash, controller)) + return Ok(controller) } benchmarks! { @@ -65,8 +59,8 @@ benchmarks! { let mut offenders: Vec = vec![]; for i in 0 .. o { - offenders.push(account("controller", i, 0)); - create_stash_controller::(i)?; + let offender = create_offender::(i)?; + offenders.push(offender); } let offenders = offenders.iter() diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 137833b0c4f0e..1512ec41fa1db 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -31,7 +31,7 @@ use frame_support::{ decl_module, decl_event, decl_storage, Parameter, weights::{Weight, SimpleDispatchInfo, WeighData}, }; -use sp_runtime::{traits::Hash, RuntimeDebug}; +use sp_runtime::traits::Hash; use sp_staking::{ offence::{Offence, ReportOffence, Kind, OnOffenceHandler, OffenceDetails, OffenceError}, }; From d7c84858fea79f471beb8a04f7fa2510899217b6 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 26 Mar 2020 10:24:58 +0100 Subject: [PATCH 03/16] Style nit --- frame/offences/src/benchmarking.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs index f435c4d6604ae..08fc0c2b7bae6 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/src/benchmarking.rs @@ -64,8 +64,13 @@ benchmarks! { } let offenders = offenders.iter() - .map(|id | (id, ::ValidatorIdOf::convert(id.clone()).expect("validator exist for this id"))) - .map(|(id, validator_id)| ::FullIdentificationOf::convert(validator_id.clone()).map(|full_id| (validator_id, full_id)).expect("full identification exist for this validator")) + .map(|id | + (id, ::ValidatorIdOf::convert(id.clone()) + .expect("failed to get validator id from account id"))) + .map(|(id, validator_id)| + ::FullIdentificationOf::convert(validator_id.clone()) + .map(|full_id| (validator_id, full_id)) + .expect("failed to convert validator id to full identification")) .collect::>>(); From 38f6c75a28e3c0e00e01dfbfb966ec06f1bb6d7a Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 26 Mar 2020 10:38:22 +0100 Subject: [PATCH 04/16] Add nominators --- frame/offences/src/benchmarking.rs | 36 ++++++++++++++++--- frame/offences/src/lib.rs | 26 +++++++------- frame/staking/src/lib.rs | 55 ++++++++++++++++++++++++++---- frame/staking/src/slashing.rs | 7 ++++ 4 files changed, 99 insertions(+), 25 deletions(-) diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs index 08fc0c2b7bae6..6b1581bce4374 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/src/benchmarking.rs @@ -20,7 +20,7 @@ use super::*; use sp_std::prelude::*; use frame_benchmarking::{benchmarks, account}; -use frame_support::traits::Currency; +use frame_support::{traits::Currency, StorageValue}; use frame_system::RawOrigin; use sp_runtime::{Perbill, traits::{Convert, StaticLookup}}; use pallet_im_online::UnresponsivenessOffence; @@ -29,21 +29,47 @@ use pallet_staking::{Module as Staking, RewardDestination, ValidatorPrefs}; const SEED: u32 = 0; const MAX_REPORTERS: u32 = 1000; const MAX_OFFENDERS: u32 = 1000; +const MAX_NOMINATORS: u32 = 1000; pub trait Trait: pallet_staking::Trait + crate::Trait + pallet_im_online::Trait {} -pub fn create_offender(n: u32) -> Result { +pub fn create_offender(n: u32) -> Result { let stash: T::AccountId = account("stash", n, 0); let controller: T::AccountId = account("controller", n, 0); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); let reward_destination = RewardDestination::Staked; let amount = T::Currency::minimum_balance() * 10.into(); - Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?; + Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup.clone(), amount.clone(), reward_destination.clone())?; let validator_prefs = ValidatorPrefs { commission: Perbill::from_percent(50), }; Staking::::validate(RawOrigin::Signed(controller.clone()).into(), validator_prefs)?; + let mut individual_exposures = vec![]; + + // Create n nominators + for j in 0 .. n { + let nominator_stash: T::AccountId = account("nominator stash", n * MAX_NOMINATORS + j, 0); + let nominator_controller: T::AccountId = account("nominator controller", n * MAX_NOMINATORS + j, 0); + let nominator_controller_lookup: ::Source = T::Lookup::unlookup(nominator_controller.clone()); + Staking::::bond(RawOrigin::Signed(nominator_stash.clone()).into(), nominator_controller_lookup.clone(), amount, reward_destination)?; + let selected_validators: Vec<::Source> = vec![controller_lookup.clone()]; + Staking::::nominate(RawOrigin::Signed(nominator_controller.clone()).into(), selected_validators)?; + + individual_exposures.push(pallet_staking::IndividualExposure { + who: nominator_controller.clone(), + value: amount.clone(), + }); + } + + let exposure = pallet_staking::Exposure { + total: amount.clone() * n.into(), + own: amount.clone(), + others: individual_exposures, + }; + + Staking::::add_era_stakers(0u32.into(), stash.clone().into(), exposure); + return Ok(controller) } @@ -76,7 +102,7 @@ benchmarks! { let offence = UnresponsivenessOffence { session_index: 0, - validator_set_count: 0, + validator_set_count: offenders.len() as u32 / 2, offenders, }; @@ -89,4 +115,4 @@ benchmarks! { }: { let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); } -} \ No newline at end of file +} diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 1512ec41fa1db..e681f36a3b020 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -49,7 +49,7 @@ pub trait Trait: frame_system::Trait { /// The overarching event type. type Event: From + Into<::Event>; /// Full identification of the validator. - type IdentificationTuple: Parameter + Ord + Default; + type IdentificationTuple: Parameter + Ord; /// A handler called for every offence report. type OnOffenceHandler: OnOffenceHandler; } @@ -161,7 +161,7 @@ impl Module { offenders: Vec, ) -> Option> { #[cfg(feature = "std")] - println!("executing triage"); + println!("triage, reporters: {}, offenders: {}", reporters.len(), offenders.len()); let mut storage = ReportIndexStorage::::load(time_slot); @@ -170,9 +170,6 @@ impl Module { let report_id = Self::report_id::(time_slot, &offender); if !>::contains_key(&report_id) { - #[cfg(feature = "std")] - println!("triage inserting report {:?}", report_id); - any_new = true; >::insert( &report_id, @@ -229,10 +226,7 @@ struct ReportIndexStorage> { impl> ReportIndexStorage { /// Preload indexes from the storage for the specific `time_slot` and the kind of the offence. - fn load(time_slot: &O::TimeSlot) -> Self { - #[cfg(feature = "std")] - println!("treportindexstorage load"); - + fn load(time_slot: &O::TimeSlot) -> Self { let opaque_time_slot = time_slot.encode(); let same_kind_reports = ::get(&O::ID); @@ -241,6 +235,12 @@ impl> ReportIndexStorage { .unwrap_or_default(); let concurrent_reports = >::get(&O::ID, &opaque_time_slot); + #[cfg(feature = "std")] + println!("load() = opaque_time_slot: {}, concurrent_reports: {}, same_kind_reports: {}", + opaque_time_slot.len(), + concurrent_reports.len(), + same_kind_reports.len(), + ); Self { opaque_time_slot, @@ -251,9 +251,6 @@ impl> ReportIndexStorage { /// Insert a new report to the index. fn insert(&mut self, time_slot: &O::TimeSlot, report_id: ReportIdOf) { - #[cfg(feature = "std")] - println!("treportindexstorage insert"); - // Insert the report id into the list while maintaining the ordering by the time // slot. let pos = match self @@ -263,6 +260,9 @@ impl> ReportIndexStorage { Ok(pos) => pos, Err(pos) => pos, }; + #[cfg(feature = "std")] + println!("insert(), same_kind_reports: {}, pos: {}", self.same_kind_reports.len(), pos); + self.same_kind_reports .insert(pos, (time_slot.clone(), report_id)); @@ -273,7 +273,7 @@ impl> ReportIndexStorage { /// Dump the indexes to the storage. fn save(self) { #[cfg(feature = "std")] - println!("treportindexstorage save"); + println!("save(), same_kind_reports: {}, concurrent_reports {}", self.same_kind_reports.len(), self.concurrent_reports.len()); ::insert(&O::ID, self.same_kind_reports.encode()); >::insert( diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 85fb86afc84df..253d267527de0 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -266,7 +266,7 @@ use frame_support::{ dispatch::DispatchResult, storage::IterableStorageMap, traits::{ Currency, LockIdentifier, LockableCurrency, WithdrawReasons, OnUnbalanced, Imbalance, Get, Time - } + }, StorageValue }; use pallet_session::historical::SessionManager; use sp_runtime::{ @@ -523,10 +523,10 @@ pub struct Nominations { #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, RuntimeDebug)] pub struct IndividualExposure { /// The stash account of the nominator in question. - who: AccountId, + pub who: AccountId, /// Amount of funds exposed. #[codec(compact)] - value: Balance, + pub value: Balance, } /// A snapshot of the stake backing a single validator in the system. @@ -1193,7 +1193,7 @@ decl_module! { /// - Both the reads and writes follow a similar pattern. /// # #[weight = SimpleDispatchInfo::FixedNormal(750_000)] - fn nominate(origin, targets: Vec<::Source>) { + pub fn nominate(origin, targets: Vec<::Source>) { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or(Error::::NotController)?; let stash = &ledger.stash; @@ -1981,6 +1981,10 @@ impl Module { _ => ForceEra::put(Forcing::ForceNew), } } + + pub fn add_era_stakers(current_era: EraIndex, controller: T::AccountId, exposure: Exposure>) { + >::insert(¤t_era, &controller, &exposure); + } } /// In this implementation `new_session(session)` must be called before `end_session(session-1)` @@ -2061,18 +2065,38 @@ impl Convert> for StashOf { /// `active_era`. It can differ from the latest planned exposure in `current_era`. pub struct ExposureOf(sp_std::marker::PhantomData); +// #[cfg(not(feature = "runtime-benchmarks"))] impl Convert>>> for ExposureOf { fn convert(validator: T::AccountId) -> Option>> { if let Some(active_era) = >::active_era() { - Some(>::eras_stakers(active_era.index, &validator)) + let exposure = >::eras_stakers(active_era.index, &validator); + #[cfg(feature = "std")] + println!("active era {:?} validator {:?} exposure = {:?}", active_era, validator, exposure); + Some(exposure) } else { + #[cfg(feature = "std")] + println!("no exposure"); None } } } + +// #[cfg(feature = "runtime-benchmarks")] +// impl Convert>>> +// for ExposureOf +// { +// fn convert(_validator: T::AccountId) -> Option>> { +// Some(Exposure { +// total: 1000.into(), +// own: 1000.into(), +// others: vec![], +// }) +// } +// } + /// This is intended to be used with `FilterHistoricalOffences`. impl OnOffenceHandler> for Module where T: pallet_session::Trait::AccountId>, @@ -2089,15 +2113,23 @@ impl OnOffenceHandler OnOffenceHandler return, // before bonding period. defensive - should be filtered out. @@ -2120,6 +2154,10 @@ impl OnOffenceHandler::EarliestUnappliedSlash::mutate(|earliest| { if earliest.is_none() { *earliest = Some(active_era) @@ -2149,10 +2187,13 @@ impl OnOffenceHandler(unapplied); } else { + #[cfg(feature = "std")] + println!("on_offence defering slash, stash {:?}", stash); + // defer to end of some `slash_defer_duration` from now. ::UnappliedSlashes::mutate( active_era, diff --git a/frame/staking/src/slashing.rs b/frame/staking/src/slashing.rs index 160c08246aedf..9464f06c316ec 100644 --- a/frame/staking/src/slashing.rs +++ b/frame/staking/src/slashing.rs @@ -216,6 +216,7 @@ pub(crate) struct SlashParams<'a, T: 'a + Trait> { pub(crate) fn compute_slash(params: SlashParams) -> Option>> { + let SlashParams { stash, slash, @@ -355,6 +356,9 @@ fn slash_nominators( let mut reward_payout = Zero::zero(); nominators_slashed.reserve(exposure.others.len()); + #[cfg(feature = "std")] + println!("slash_nominators others: {}", exposure.others.len()); + for nominator in &exposure.others { let stash = &nominator.who; let mut nom_slashed = Zero::zero(); @@ -604,6 +608,9 @@ pub fn do_slash( /// Apply a previously-unapplied slash. pub(crate) fn apply_slash(unapplied_slash: UnappliedSlash>) { + #[cfg(feature = "std")] + println!("apply_slash(), others {:?}", unapplied_slash.others.len()); + let mut slashed_imbalance = NegativeImbalanceOf::::zero(); let mut reward_payout = unapplied_slash.payout; From ecf9a87a1b674f11c8a2a4639fa1bb121ebd51ae Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 30 Mar 2020 13:45:49 +0200 Subject: [PATCH 05/16] Remove logs. --- frame/offences/src/lib.rs | 20 ------------------- frame/staking/src/lib.rs | 41 +++------------------------------------ 2 files changed, 3 insertions(+), 58 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index e681f36a3b020..9c26396d0013b 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -160,9 +160,6 @@ impl Module { time_slot: &O::TimeSlot, offenders: Vec, ) -> Option> { - #[cfg(feature = "std")] - println!("triage, reporters: {}, offenders: {}", reporters.len(), offenders.len()); - let mut storage = ReportIndexStorage::::load(time_slot); let mut any_new = false; @@ -184,18 +181,12 @@ impl Module { } if any_new { - #[cfg(feature = "std")] - println!("triage any_new"); - // Load report details for the all reports happened at the same time. let concurrent_offenders = storage.concurrent_reports .iter() .filter_map(|report_id| >::get(report_id)) .collect::>(); - #[cfg(feature = "std")] - println!("triage concurrent offenders {:?}", concurrent_offenders.len()); - storage.save(); Some(TriageOutcome { @@ -235,12 +226,6 @@ impl> ReportIndexStorage { .unwrap_or_default(); let concurrent_reports = >::get(&O::ID, &opaque_time_slot); - #[cfg(feature = "std")] - println!("load() = opaque_time_slot: {}, concurrent_reports: {}, same_kind_reports: {}", - opaque_time_slot.len(), - concurrent_reports.len(), - same_kind_reports.len(), - ); Self { opaque_time_slot, @@ -260,8 +245,6 @@ impl> ReportIndexStorage { Ok(pos) => pos, Err(pos) => pos, }; - #[cfg(feature = "std")] - println!("insert(), same_kind_reports: {}, pos: {}", self.same_kind_reports.len(), pos); self.same_kind_reports .insert(pos, (time_slot.clone(), report_id)); @@ -272,9 +255,6 @@ impl> ReportIndexStorage { /// Dump the indexes to the storage. fn save(self) { - #[cfg(feature = "std")] - println!("save(), same_kind_reports: {}, concurrent_reports {}", self.same_kind_reports.len(), self.concurrent_reports.len()); - ::insert(&O::ID, self.same_kind_reports.encode()); >::insert( &O::ID, diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 253d267527de0..ddac1dea3b220 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2071,32 +2071,13 @@ impl Convert> { fn convert(validator: T::AccountId) -> Option>> { if let Some(active_era) = >::active_era() { - let exposure = >::eras_stakers(active_era.index, &validator); - #[cfg(feature = "std")] - println!("active era {:?} validator {:?} exposure = {:?}", active_era, validator, exposure); - Some(exposure) + Some(>::eras_stakers(active_era.index, &validator)) } else { - #[cfg(feature = "std")] - println!("no exposure"); None } } } - -// #[cfg(feature = "runtime-benchmarks")] -// impl Convert>>> -// for ExposureOf -// { -// fn convert(_validator: T::AccountId) -> Option>> { -// Some(Exposure { -// total: 1000.into(), -// own: 1000.into(), -// others: vec![], -// }) -// } -// } - /// This is intended to be used with `FilterHistoricalOffences`. impl OnOffenceHandler> for Module where T: pallet_session::Trait::AccountId>, @@ -2113,22 +2094,15 @@ impl OnOffenceHandler OnOffenceHandler return, // before bonding period. defensive - should be filtered out. @@ -2154,10 +2126,6 @@ impl OnOffenceHandler::EarliestUnappliedSlash::mutate(|earliest| { if earliest.is_none() { *earliest = Some(active_era) @@ -2187,13 +2155,10 @@ impl OnOffenceHandler(unapplied); } else { - #[cfg(feature = "std")] - println!("on_offence defering slash, stash {:?}", stash); - // defer to end of some `slash_defer_duration` from now. ::UnappliedSlashes::mutate( active_era, From 440379d9613a2b634b2d2c000146308bbc3f65f5 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 30 Mar 2020 13:58:52 +0200 Subject: [PATCH 06/16] Nits. --- frame/offences/src/benchmarking.rs | 2 +- frame/offences/src/lib.rs | 2 +- frame/staking/src/lib.rs | 1 - frame/staking/src/slashing.rs | 6 ------ 4 files changed, 2 insertions(+), 9 deletions(-) diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs index 6b1581bce4374..10dd45293ace2 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/src/benchmarking.rs @@ -52,7 +52,7 @@ pub fn create_offender::Source = T::Lookup::unlookup(nominator_controller.clone()); - Staking::::bond(RawOrigin::Signed(nominator_stash.clone()).into(), nominator_controller_lookup.clone(), amount, reward_destination)?; + Staking::::bond(RawOrigin::Signed(nominator_stash.clone()).into(), nominator_controller_lookup.clone(), amount, reward_destination)?; let selected_validators: Vec<::Source> = vec![controller_lookup.clone()]; Staking::::nominate(RawOrigin::Signed(nominator_controller.clone()).into(), selected_validators)?; diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 2dc9709bbf2ec..ef009cfa06528 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -272,7 +272,7 @@ struct ReportIndexStorage> { impl> ReportIndexStorage { /// Preload indexes from the storage for the specific `time_slot` and the kind of the offence. - fn load(time_slot: &O::TimeSlot) -> Self { + fn load(time_slot: &O::TimeSlot) -> Self { let opaque_time_slot = time_slot.encode(); let same_kind_reports = ::get(&O::ID); diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index ee9df7dc426d4..55cdbb32da37b 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2760,7 +2760,6 @@ impl Convert> for StashOf { /// `active_era`. It can differ from the latest planned exposure in `current_era`. pub struct ExposureOf(sp_std::marker::PhantomData); -// #[cfg(not(feature = "runtime-benchmarks"))] impl Convert>>> for ExposureOf { diff --git a/frame/staking/src/slashing.rs b/frame/staking/src/slashing.rs index 96a32ccc3d013..86e74ea63e902 100644 --- a/frame/staking/src/slashing.rs +++ b/frame/staking/src/slashing.rs @@ -216,7 +216,6 @@ pub(crate) struct SlashParams<'a, T: 'a + Trait> { pub(crate) fn compute_slash(params: SlashParams) -> Option>> { - let SlashParams { stash, slash, @@ -356,8 +355,6 @@ fn slash_nominators( let mut reward_payout = Zero::zero(); nominators_slashed.reserve(exposure.others.len()); - #[cfg(feature = "std")] - println!("slash_nominators others: {}", exposure.others.len()); for nominator in &exposure.others { let stash = &nominator.who; @@ -608,9 +605,6 @@ pub fn do_slash( /// Apply a previously-unapplied slash. pub(crate) fn apply_slash(unapplied_slash: UnappliedSlash>) { - #[cfg(feature = "std")] - println!("apply_slash(), others {:?}", unapplied_slash.others.len()); - let mut slashed_imbalance = NegativeImbalanceOf::::zero(); let mut reward_payout = unapplied_slash.payout; From 55d744ee8035249a857bb5f4674862bfb48d166d Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 30 Mar 2020 14:02:19 +0200 Subject: [PATCH 07/16] Add nominators param. --- frame/offences/src/benchmarking.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs index 10dd45293ace2..3b3d3d2f70a56 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/src/benchmarking.rs @@ -33,7 +33,7 @@ const MAX_NOMINATORS: u32 = 1000; pub trait Trait: pallet_staking::Trait + crate::Trait + pallet_im_online::Trait {} -pub fn create_offender(n: u32) -> Result { +pub fn create_offender(n: u32, nominators: u32) -> Result { let stash: T::AccountId = account("stash", n, 0); let controller: T::AccountId = account("controller", n, 0); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); @@ -48,7 +48,7 @@ pub fn create_offender::Source = T::Lookup::unlookup(nominator_controller.clone()); @@ -77,15 +77,17 @@ benchmarks! { _ { let r in 1 .. MAX_REPORTERS => (); let o in 1 .. MAX_OFFENDERS => (); + let n in 1 .. MAX_OFFENDERS => (); } report_offence { let r in ...; let o in ...; + let n in ...; let mut offenders: Vec = vec![]; for i in 0 .. o { - let offender = create_offender::(i)?; + let offender = create_offender::(i, n)?; offenders.push(offender); } From aaf5d3d1bd0ba34ac33c6a7c8f95e9f2cfb81e77 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 30 Mar 2020 14:04:55 +0200 Subject: [PATCH 08/16] Reorg, comment. --- frame/offences/src/benchmarking.rs | 77 +++++++++++++++++------------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/src/benchmarking.rs index 3b3d3d2f70a56..cf64bfd5464e6 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/src/benchmarking.rs @@ -20,11 +20,12 @@ use super::*; use sp_std::prelude::*; use frame_benchmarking::{benchmarks, account}; -use frame_support::{traits::Currency, StorageValue}; +use frame_support::traits::Currency; use frame_system::RawOrigin; use sp_runtime::{Perbill, traits::{Convert, StaticLookup}}; use pallet_im_online::UnresponsivenessOffence; use pallet_staking::{Module as Staking, RewardDestination, ValidatorPrefs}; +use pallet_session::historical::IdentificationTuple; const SEED: u32 = 0; const MAX_REPORTERS: u32 = 1000; @@ -33,12 +34,13 @@ const MAX_NOMINATORS: u32 = 1000; pub trait Trait: pallet_staking::Trait + crate::Trait + pallet_im_online::Trait {} -pub fn create_offender(n: u32, nominators: u32) -> Result { +fn create_offender(n: u32, nominators: u32) -> Result { let stash: T::AccountId = account("stash", n, 0); let controller: T::AccountId = account("controller", n, 0); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); let reward_destination = RewardDestination::Staked; let amount = T::Currency::minimum_balance() * 10.into(); + Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup.clone(), amount.clone(), reward_destination.clone())?; let validator_prefs = ValidatorPrefs { commission: Perbill::from_percent(50), @@ -52,7 +54,9 @@ pub fn create_offender::Source = T::Lookup::unlookup(nominator_controller.clone()); + Staking::::bond(RawOrigin::Signed(nominator_stash.clone()).into(), nominator_controller_lookup.clone(), amount, reward_destination)?; + let selected_validators: Vec<::Source> = vec![controller_lookup.clone()]; Staking::::nominate(RawOrigin::Signed(nominator_controller.clone()).into(), selected_validators)?; @@ -73,11 +77,48 @@ pub fn create_offender(r: u32, o: u32, n: u32) + -> Result<(Vec, UnresponsivenessOffence>), &'static str> { + + // Make reporters. + let mut reporters = vec![]; + for i in 0 .. r { + let reporter = account("reporter", i, SEED); + reporters.push(reporter); + } + + // Make offence with `o` offenders and `n` nominators each one. + let mut offenders: Vec = vec![]; + for i in 0 .. o { + let offender = create_offender::(i, n)?; + offenders.push(offender); + } + + let offenders = offenders.iter() + .map(|id| + ::ValidatorIdOf::convert(id.clone()) + .expect("failed to get validator id from account id")) + .map(|validator_id| + ::FullIdentificationOf::convert(validator_id.clone()) + .map(|full_id| (validator_id, full_id)) + .expect("failed to convert validator id to full identification")) + .collect::>>(); + + + let offence = UnresponsivenessOffence { + session_index: 0, + validator_set_count: offenders.len() as u32 / 2, + offenders, + }; + + Ok((reporters, offence)) +} + benchmarks! { _ { let r in 1 .. MAX_REPORTERS => (); let o in 1 .. MAX_OFFENDERS => (); - let n in 1 .. MAX_OFFENDERS => (); + let n in 1 .. MAX_NOMINATORS => (); } report_offence { @@ -85,35 +126,7 @@ benchmarks! { let o in ...; let n in ...; - let mut offenders: Vec = vec![]; - for i in 0 .. o { - let offender = create_offender::(i, n)?; - offenders.push(offender); - } - - let offenders = offenders.iter() - .map(|id | - (id, ::ValidatorIdOf::convert(id.clone()) - .expect("failed to get validator id from account id"))) - .map(|(id, validator_id)| - ::FullIdentificationOf::convert(validator_id.clone()) - .map(|full_id| (validator_id, full_id)) - .expect("failed to convert validator id to full identification")) - .collect::>>(); - - - let offence = UnresponsivenessOffence { - session_index: 0, - validator_set_count: offenders.len() as u32 / 2, - offenders, - }; - - let mut reporters = vec![]; - for i in 0 .. r { - let reporter = account("reporter", i, SEED); - reporters.push(reporter); - } - + let (reporters, offence) = make_inputs::(r, o, n)?; }: { let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); } From 6c8a49c5be6f8aee4cc73ec9a6b2c361e3d3ef29 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 30 Mar 2020 14:43:19 +0200 Subject: [PATCH 09/16] Remove whitespaces. --- frame/offences/src/lib.rs | 1 - frame/staking/src/lib.rs | 1 - frame/staking/src/slashing.rs | 1 - 3 files changed, 3 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index ef009cfa06528..01056cc96fbc1 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -300,7 +300,6 @@ impl> ReportIndexStorage { Ok(pos) => pos, Err(pos) => pos, }; - self.same_kind_reports .insert(pos, (time_slot.clone(), report_id)); diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 55cdbb32da37b..b6477bc8c9a7e 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2802,7 +2802,6 @@ impl OnOffenceHandler( let mut reward_payout = Zero::zero(); nominators_slashed.reserve(exposure.others.len()); - for nominator in &exposure.others { let stash = &nominator.who; let mut nom_slashed = Zero::zero(); From 88b4c30528634ad9b70dcf475fc2acdef5a8c70b Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 2 Apr 2020 13:27:31 +0200 Subject: [PATCH 10/16] Apply review suggestion: move benchmark to own crate. --- Cargo.lock | 21 ++++-- bin/node/runtime/Cargo.toml | 3 +- bin/node/runtime/src/lib.rs | 7 +- frame/offences/Cargo.toml | 9 --- frame/offences/benchmarking/Cargo.toml | 39 +++++++++++ .../src/lib.rs} | 70 +++++++++++++------ frame/scored-pool/src/lib.rs | 4 +- 7 files changed, 112 insertions(+), 41 deletions(-) create mode 100644 frame/offences/benchmarking/Cargo.toml rename frame/offences/{src/benchmarking.rs => benchmarking/src/lib.rs} (65%) diff --git a/Cargo.lock b/Cargo.lock index 2d4d689846018..d1c20642d8665 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3510,6 +3510,7 @@ dependencies = [ "pallet-indices", "pallet-membership", "pallet-offences", + "pallet-offences-benchmarking", "pallet-randomness-collective-flip", "pallet-recovery", "pallet-scheduler", @@ -4275,13 +4276,9 @@ dependencies = [ name = "pallet-offences" version = "2.0.0-alpha.5" dependencies = [ - "frame-benchmarking", "frame-support", "frame-system", "pallet-balances", - "pallet-im-online", - "pallet-session", - "pallet-staking", "parity-scale-codec", "serde", "sp-core", @@ -4291,6 +4288,22 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-offences-benchmarking" +version = "2.0.0-alpha.5" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "pallet-im-online", + "pallet-offences", + "pallet-session", + "pallet-staking", + "sp-runtime", + "sp-staking", + "sp-std", +] + [[package]] name = "pallet-randomness-collective-flip" version = "2.0.0-alpha.5" diff --git a/bin/node/runtime/Cargo.toml b/bin/node/runtime/Cargo.toml index e9700689494d6..b8e5f70629145 100644 --- a/bin/node/runtime/Cargo.toml +++ b/bin/node/runtime/Cargo.toml @@ -55,6 +55,7 @@ pallet-indices = { version = "2.0.0-alpha.5", default-features = false, path = " pallet-identity = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/identity" } pallet-membership = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/membership" } pallet-offences = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/offences" } +pallet-offences-benchmarking = { version = "2.0.0-alpha.5", path = "../../../frame/offences/benchmarking", default-features = false, optional = true } pallet-randomness-collective-flip = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/randomness-collective-flip" } pallet-recovery = { version = "2.0.0-alpha.5", default-features = false, path = "../../../frame/recovery" } pallet-session = { version = "2.0.0-alpha.5", features = ["historical"], path = "../../../frame/session", default-features = false } @@ -150,7 +151,7 @@ runtime-benchmarks = [ "pallet-utility/runtime-benchmarks", "pallet-vesting/runtime-benchmarks", "pallet-collective/runtime-benchmarks", - "pallet-offences/runtime-benchmarks", + "pallet-offences-benchmarking", "pallet-session-benchmarking", ] diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index a1171fafa3403..483c8c6c97e47 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -887,11 +887,14 @@ impl_runtime_apis! { // To get around that, we separated the Session benchmarks into its own crate, which is why // we need these two lines below. use pallet_session_benchmarking::Module as SessionBench; + use pallet_offences_benchmarking::Module as OffencesBench; + impl pallet_session_benchmarking::Trait for Runtime {} - impl pallet_offences::benchmarking::Trait for Runtime {} + impl pallet_offences_benchmarking::Trait for Runtime {} let mut batches = Vec::::new(); let params = (&pallet, &benchmark, &lowest_range_values, &highest_range_values, &steps, repeat); + add_benchmark!(params, batches, b"balances", Balances); add_benchmark!(params, batches, b"collective", Council); add_benchmark!(params, batches, b"democracy", Democracy); @@ -903,7 +906,7 @@ impl_runtime_apis! { add_benchmark!(params, batches, b"treasury", Treasury); add_benchmark!(params, batches, b"utility", Utility); add_benchmark!(params, batches, b"vesting", Vesting); - add_benchmark!(params, batches, b"offences", Offences); + add_benchmark!(params, batches, b"offences", OffencesBench::); if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } Ok(batches) diff --git a/frame/offences/Cargo.toml b/frame/offences/Cargo.toml index 452dcedae00ba..b858c03ba5ac0 100644 --- a/frame/offences/Cargo.toml +++ b/frame/offences/Cargo.toml @@ -15,12 +15,8 @@ sp-std = { version = "2.0.0-alpha.5", default-features = false, path = "../../pr serde = { version = "1.0.101", optional = true } sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../primitives/runtime" } sp-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../../primitives/staking" } -frame-benchmarking = { version = "2.0.0-alpha.5", default-features = false, path = "../benchmarking", optional = true } frame-support = { version = "2.0.0-alpha.5", default-features = false, path = "../support" } frame-system = { version = "2.0.0-alpha.5", default-features = false, path = "../system" } -pallet-im-online = { version = "2.0.0-alpha.5", default-features = false, path = "../im-online" } -pallet-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../staking" } -pallet-session = { version = "2.0.0-alpha.5", default-features = false, path = "../session" } [dev-dependencies] sp-io = { version = "2.0.0-alpha.5", path = "../../primitives/io" } @@ -30,19 +26,14 @@ sp-core = { version = "2.0.0-alpha.5", path = "../../primitives/core" } default = ["std"] std = [ "pallet-balances/std", - "pallet-im-online/std", - "pallet-staking/std", - "pallet-session/std", "codec/std", "sp-std/std", "serde", "sp-runtime/std", "sp-staking/std", - "frame-benchmarking/std", "frame-support/std", "frame-system/std", ] -runtime-benchmarks = ["frame-benchmarking"] [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml new file mode 100644 index 0000000000000..11bb04da34c6e --- /dev/null +++ b/frame/offences/benchmarking/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "pallet-offences-benchmarking" +version = "2.0.0-alpha.5" +authors = ["Parity Technologies "] +edition = "2018" +license = "GPL-3.0" +homepage = "https://substrate.dev" +repository = "https://github.com/paritytech/substrate/" +description = "FRAME offences pallet benchmarking" + +[dependencies] +sp-std = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/std" } +sp-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/staking" } +sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/runtime" } +frame-benchmarking = { version = "2.0.0-alpha.5", default-features = false, path = "../../benchmarking" } +frame-system = { version = "2.0.0-alpha.5", default-features = false, path = "../../system" } +frame-support = { version = "2.0.0-alpha.5", default-features = false, path = "../../support" } +pallet-im-online = { version = "2.0.0-alpha.5", default-features = false, path = "../../im-online" } +pallet-offences = { version = "2.0.0-alpha.5", default-features = false, path = "../../offences" } +pallet-staking = { version = "2.0.0-alpha.5", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } +pallet-session = { version = "2.0.0-alpha.5", default-features = false, path = "../../session" } + +[features] +default = ["std"] +std = [ + "sp-runtime/std", + "sp-std/std", + "sp-staking/std", + "frame-benchmarking/std", + "frame-support/std", + "frame-system/std", + "pallet-offences/std", + "pallet-im-online/std", + "pallet-staking/std", + "pallet-session/std", +] + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] diff --git a/frame/offences/src/benchmarking.rs b/frame/offences/benchmarking/src/lib.rs similarity index 65% rename from frame/offences/src/benchmarking.rs rename to frame/offences/benchmarking/src/lib.rs index cf64bfd5464e6..00dcda3add740 100644 --- a/frame/offences/src/benchmarking.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -16,32 +16,51 @@ //! Offences pallet benchmarking. -use super::*; +#![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; +use sp_std::vec; + +use frame_system::RawOrigin; use frame_benchmarking::{benchmarks, account}; use frame_support::traits::Currency; -use frame_system::RawOrigin; + use sp_runtime::{Perbill, traits::{Convert, StaticLookup}}; -use pallet_im_online::UnresponsivenessOffence; -use pallet_staking::{Module as Staking, RewardDestination, ValidatorPrefs}; -use pallet_session::historical::IdentificationTuple; +use sp_staking::offence::ReportOffence; + +use pallet_im_online::{Trait as ImOnlineTrait, UnresponsivenessOffence}; +use pallet_offences::{Trait as OffencesTrait, Module as OffencesModule}; +use pallet_staking::{ + Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, + Exposure, IndividualExposure, +}; +use pallet_session::Trait as SessionTrait; +use pallet_session::historical::{Trait as HistoricalTrait, IdentificationTuple}; const SEED: u32 = 0; + const MAX_REPORTERS: u32 = 1000; const MAX_OFFENDERS: u32 = 1000; const MAX_NOMINATORS: u32 = 1000; -pub trait Trait: pallet_staking::Trait + crate::Trait + pallet_im_online::Trait {} +pub struct Module(OffencesModule); + +pub trait Trait: SessionTrait + StakingTrait + OffencesTrait + ImOnlineTrait {} -fn create_offender(n: u32, nominators: u32) -> Result { - let stash: T::AccountId = account("stash", n, 0); - let controller: T::AccountId = account("controller", n, 0); +fn create_offender(n: u32, nominators: u32) -> Result { + let stash: T::AccountId = account("stash", n, SEED); + let controller: T::AccountId = account("controller", n, SEED); let controller_lookup: ::Source = T::Lookup::unlookup(controller.clone()); let reward_destination = RewardDestination::Staked; - let amount = T::Currency::minimum_balance() * 10.into(); + let amount = T::Currency::minimum_balance(); + + Staking::::bond( + RawOrigin::Signed(stash.clone()).into(), + controller_lookup.clone(), + amount.clone(), + reward_destination.clone(), + )?; - Staking::::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup.clone(), amount.clone(), reward_destination.clone())?; let validator_prefs = ValidatorPrefs { commission: Perbill::from_percent(50), }; @@ -50,29 +69,34 @@ fn create_offender::Source = T::Lookup::unlookup(nominator_controller.clone()); - Staking::::bond(RawOrigin::Signed(nominator_stash.clone()).into(), nominator_controller_lookup.clone(), amount, reward_destination)?; + Staking::::bond( + RawOrigin::Signed(nominator_stash.clone()).into(), + nominator_controller_lookup.clone(), + amount, + reward_destination, + )?; let selected_validators: Vec<::Source> = vec![controller_lookup.clone()]; Staking::::nominate(RawOrigin::Signed(nominator_controller.clone()).into(), selected_validators)?; - individual_exposures.push(pallet_staking::IndividualExposure { + individual_exposures.push(IndividualExposure { who: nominator_controller.clone(), value: amount.clone(), }); } - let exposure = pallet_staking::Exposure { + let exposure = Exposure { total: amount.clone() * n.into(), - own: amount.clone(), + own: amount, others: individual_exposures, }; - - Staking::::add_era_stakers(0u32.into(), stash.clone().into(), exposure); + let current_era = 0u32; + Staking::::add_era_stakers(current_era.into(), stash.clone().into(), exposure); return Ok(controller) } @@ -96,10 +120,10 @@ fn make_inputs(r: u32, o: u32, n: u32) let offenders = offenders.iter() .map(|id| - ::ValidatorIdOf::convert(id.clone()) + ::ValidatorIdOf::convert(id.clone()) .expect("failed to get validator id from account id")) .map(|validator_id| - ::FullIdentificationOf::convert(validator_id.clone()) + ::FullIdentificationOf::convert(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) .collect::>>(); @@ -128,6 +152,6 @@ benchmarks! { let (reporters, offence) = make_inputs::(r, o, n)?; }: { - let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); + let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); } } diff --git a/frame/scored-pool/src/lib.rs b/frame/scored-pool/src/lib.rs index 2c2bfc6a12bb8..c5750850f6b2b 100644 --- a/frame/scored-pool/src/lib.rs +++ b/frame/scored-pool/src/lib.rs @@ -191,8 +191,8 @@ decl_storage! { >::insert(who, true); }); - /// Sorts the `Pool` by score in a descending order. Entities which - /// have a score of `None` are sorted to the beginning of the vec. + // Sorts the `Pool` by score in a descending order. Entities which + // have a score of `None` are sorted to the beginning of the vec. pool.sort_by_key(|(_, maybe_score)| Reverse(maybe_score.unwrap_or_default()) ); From 6a2ff619079e149591c7366dcec3a64feaad2b9a Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 2 Apr 2020 18:04:19 +0200 Subject: [PATCH 11/16] Remove import. --- frame/offences/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 01056cc96fbc1..c4e3aa2db0583 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -23,8 +23,6 @@ mod mock; mod tests; -#[cfg(feature = "runtime-benchmarks")] -pub mod benchmarking; use sp_std::vec::Vec; use frame_support::{ @@ -190,6 +188,8 @@ impl Module { ) { Ok(_) => true, Err(_) => { + #[cfg(feature = "std")] + println!("HERE DEFERRED"); >::mutate(|d| d.push((concurrent_offenders.to_vec(), slash_perbill.to_vec(), session_index)) ); From 4a745b4f6c6ac435dd67eae11e40fccff932d001 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 2 Apr 2020 18:05:32 +0200 Subject: [PATCH 12/16] Remove line. --- frame/offences/benchmarking/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 00dcda3add740..be04868a3b611 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -128,7 +128,6 @@ fn make_inputs(r: u32, o: u32, n: u32) .expect("failed to convert validator id to full identification")) .collect::>>(); - let offence = UnresponsivenessOffence { session_index: 0, validator_set_count: offenders.len() as u32 / 2, From bb0e432631fb06824434b59fcd47a70140bbb108 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Thu, 2 Apr 2020 18:06:56 +0200 Subject: [PATCH 13/16] Add feature flag. --- frame/staking/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index b6477bc8c9a7e..2f756a491826b 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2679,6 +2679,7 @@ impl Module { } } + #[cfg(feature = "runtime-benchmarks")] pub fn add_era_stakers(current_era: EraIndex, controller: T::AccountId, exposure: Exposure>) { >::insert(¤t_era, &controller, &exposure); } From 2d9f327869ebc77c4f0eb466003756fcc40894c6 Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Sat, 4 Apr 2020 00:23:59 +0200 Subject: [PATCH 14/16] Pass can_report. --- frame/offences/benchmarking/src/lib.rs | 21 ++++++++++++++++----- frame/offences/src/lib.rs | 5 +++++ frame/staking/src/lib.rs | 5 +++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index be04868a3b611..737648e8b353d 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -23,7 +23,7 @@ use sp_std::vec; use frame_system::RawOrigin; use frame_benchmarking::{benchmarks, account}; -use frame_support::traits::Currency; +use frame_support::traits::{Currency, OnInitialize}; use sp_runtime::{Perbill, traits::{Convert, StaticLookup}}; use sp_staking::offence::ReportOffence; @@ -32,16 +32,18 @@ use pallet_im_online::{Trait as ImOnlineTrait, UnresponsivenessOffence}; use pallet_offences::{Trait as OffencesTrait, Module as OffencesModule}; use pallet_staking::{ Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, - Exposure, IndividualExposure, + Exposure, IndividualExposure, ElectionStatus }; use pallet_session::Trait as SessionTrait; use pallet_session::historical::{Trait as HistoricalTrait, IdentificationTuple}; const SEED: u32 = 0; -const MAX_REPORTERS: u32 = 1000; -const MAX_OFFENDERS: u32 = 1000; -const MAX_NOMINATORS: u32 = 1000; + +const MAX_USERS: u32 = 1000; +const MAX_REPORTERS: u32 = 100; +const MAX_OFFENDERS: u32 = 100; +const MAX_NOMINATORS: u32 = 100; pub struct Module(OffencesModule); @@ -139,6 +141,7 @@ fn make_inputs(r: u32, o: u32, n: u32) benchmarks! { _ { + let u in 1 .. MAX_USERS => (); let r in 1 .. MAX_REPORTERS => (); let o in 1 .. MAX_OFFENDERS => (); let n in 1 .. MAX_NOMINATORS => (); @@ -153,4 +156,12 @@ benchmarks! { }: { let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); } + + on_initialize { + let u in ...; + + Staking::::put_election_status(ElectionStatus::Closed); + }: { + OffencesModule::::on_initialize(u.into()); + } } diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index c4e3aa2db0583..11b3a89f4a9a4 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -110,7 +110,12 @@ decl_module! { fn on_initialize(now: T::BlockNumber) -> Weight { // only decode storage if we can actually submit anything again. if T::OnOffenceHandler::can_report() { + #[cfg(feature = "std")] + println!("inside can_report()"); + >::mutate(|deferred| { + #[cfg(feature = "std")] + println!("deferred offences len = {}", deferred.len()); // keep those that fail to be reported again. An error log is emitted here; this // should not happen if staking's `can_report` is implemented properly. deferred.retain(|(o, p, s)| { diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 2f756a491826b..6b392a3fc59ed 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -2683,6 +2683,11 @@ impl Module { pub fn add_era_stakers(current_era: EraIndex, controller: T::AccountId, exposure: Exposure>) { >::insert(¤t_era, &controller, &exposure); } + + #[cfg(feature = "runtime-benchmarks")] + pub fn put_election_status(status: ElectionStatus::) { + >::put(status); + } } /// In this implementation `new_session(session)` must be called before `end_session(session-1)` From dbdb00bc45b0cd5a29743e0ca3d35fc9d0d91a4d Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 6 Apr 2020 18:43:03 +0200 Subject: [PATCH 15/16] Cleaning up. --- Cargo.lock | 2 + frame/offences/Cargo.toml | 1 + frame/offences/benchmarking/Cargo.toml | 6 ++- frame/offences/benchmarking/src/lib.rs | 72 ++++++++++++++------------ frame/offences/src/lib.rs | 7 ++- frame/session/src/historical.rs | 2 +- frame/session/src/lib.rs | 2 +- 7 files changed, 56 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d1c20642d8665..350970e210d7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4299,6 +4299,8 @@ dependencies = [ "pallet-offences", "pallet-session", "pallet-staking", + "parity-scale-codec", + "sp-io", "sp-runtime", "sp-staking", "sp-std", diff --git a/frame/offences/Cargo.toml b/frame/offences/Cargo.toml index b858c03ba5ac0..eab95dbd048e1 100644 --- a/frame/offences/Cargo.toml +++ b/frame/offences/Cargo.toml @@ -34,6 +34,7 @@ std = [ "frame-support/std", "frame-system/std", ] +runtime-benchmarks = [] [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/frame/offences/benchmarking/Cargo.toml b/frame/offences/benchmarking/Cargo.toml index 11bb04da34c6e..e343f1ff0c1ee 100644 --- a/frame/offences/benchmarking/Cargo.toml +++ b/frame/offences/benchmarking/Cargo.toml @@ -9,6 +9,8 @@ repository = "https://github.com/paritytech/substrate/" description = "FRAME offences pallet benchmarking" [dependencies] +codec = { package = "parity-scale-codec", version = "1.3.0", default-features = false } + sp-std = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/std" } sp-staking = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/staking" } sp-runtime = { version = "2.0.0-alpha.5", default-features = false, path = "../../../primitives/runtime" } @@ -16,9 +18,11 @@ frame-benchmarking = { version = "2.0.0-alpha.5", default-features = false, path frame-system = { version = "2.0.0-alpha.5", default-features = false, path = "../../system" } frame-support = { version = "2.0.0-alpha.5", default-features = false, path = "../../support" } pallet-im-online = { version = "2.0.0-alpha.5", default-features = false, path = "../../im-online" } -pallet-offences = { version = "2.0.0-alpha.5", default-features = false, path = "../../offences" } +pallet-offences = { version = "2.0.0-alpha.5", default-features = false, features = ["runtime-benchmarks"], path = "../../offences" } pallet-staking = { version = "2.0.0-alpha.5", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" } pallet-session = { version = "2.0.0-alpha.5", default-features = false, path = "../../session" } +sp-io = { path = "../../../primitives/io", default-features = false, version = "2.0.0-alpha.5"} + [features] default = ["std"] diff --git a/frame/offences/benchmarking/src/lib.rs b/frame/offences/benchmarking/src/lib.rs index 737648e8b353d..a88714a89a7fa 100644 --- a/frame/offences/benchmarking/src/lib.rs +++ b/frame/offences/benchmarking/src/lib.rs @@ -28,8 +28,8 @@ use frame_support::traits::{Currency, OnInitialize}; use sp_runtime::{Perbill, traits::{Convert, StaticLookup}}; use sp_staking::offence::ReportOffence; -use pallet_im_online::{Trait as ImOnlineTrait, UnresponsivenessOffence}; -use pallet_offences::{Trait as OffencesTrait, Module as OffencesModule}; +use pallet_im_online::{Trait as ImOnlineTrait, Module as ImOnline, UnresponsivenessOffence}; +use pallet_offences::{Trait as OffencesTrait, Module as Offences}; use pallet_staking::{ Module as Staking, Trait as StakingTrait, RewardDestination, ValidatorPrefs, Exposure, IndividualExposure, ElectionStatus @@ -39,15 +39,15 @@ use pallet_session::historical::{Trait as HistoricalTrait, IdentificationTuple}; const SEED: u32 = 0; - const MAX_USERS: u32 = 1000; const MAX_REPORTERS: u32 = 100; const MAX_OFFENDERS: u32 = 100; const MAX_NOMINATORS: u32 = 100; +const MAX_DEFERRED_OFFENCES: u32 = 100; -pub struct Module(OffencesModule); +pub struct Module(Offences); -pub trait Trait: SessionTrait + StakingTrait + OffencesTrait + ImOnlineTrait {} +pub trait Trait: SessionTrait + StakingTrait + OffencesTrait + ImOnlineTrait + HistoricalTrait {} fn create_offender(n: u32, nominators: u32) -> Result { let stash: T::AccountId = account("stash", n, SEED); @@ -100,27 +100,18 @@ fn create_offender(n: u32, nominators: u32) -> Result::add_era_stakers(current_era.into(), stash.clone().into(), exposure); - return Ok(controller) + Ok(controller) } -fn make_inputs(r: u32, o: u32, n: u32) - -> Result<(Vec, UnresponsivenessOffence>), &'static str> { - - // Make reporters. - let mut reporters = vec![]; - for i in 0 .. r { - let reporter = account("reporter", i, SEED); - reporters.push(reporter); - } - - // Make offence with `o` offenders and `n` nominators each one. +fn make_offenders(num_offenders: u32, num_nominators: u32) -> Result>, &'static str> { let mut offenders: Vec = vec![]; - for i in 0 .. o { - let offender = create_offender::(i, n)?; + + for i in 0 .. num_offenders { + let offender = create_offender::(i, num_nominators)?; offenders.push(offender); } - let offenders = offenders.iter() + Ok(offenders.iter() .map(|id| ::ValidatorIdOf::convert(id.clone()) .expect("failed to get validator id from account id")) @@ -128,15 +119,7 @@ fn make_inputs(r: u32, o: u32, n: u32) ::FullIdentificationOf::convert(validator_id.clone()) .map(|full_id| (validator_id, full_id)) .expect("failed to convert validator id to full identification")) - .collect::>>(); - - let offence = UnresponsivenessOffence { - session_index: 0, - validator_set_count: offenders.len() as u32 / 2, - offenders, - }; - - Ok((reporters, offence)) + .collect::>>()) } benchmarks! { @@ -145,6 +128,7 @@ benchmarks! { let r in 1 .. MAX_REPORTERS => (); let o in 1 .. MAX_OFFENDERS => (); let n in 1 .. MAX_NOMINATORS => (); + let d in 1 .. MAX_DEFERRED_OFFENCES => (); } report_offence { @@ -152,16 +136,40 @@ benchmarks! { let o in ...; let n in ...; - let (reporters, offence) = make_inputs::(r, o, n)?; + let mut reporters = vec![]; + + for i in 0 .. r { + let reporter = account("reporter", i, SEED); + reporters.push(reporter); + } + + let offenders = make_offenders::(o, n).expect("failed to create offenders"); + let keys = ImOnline::::keys(); + + let offence = UnresponsivenessOffence { + session_index: 0, + validator_set_count: keys.len() as u32, + offenders, + }; + }: { let _ = ::ReportUnresponsiveness::report_offence(reporters, offence); } on_initialize { - let u in ...; + let d in ...; Staking::::put_election_status(ElectionStatus::Closed); + + let mut deferred_offences = vec![]; + + for i in 0 .. d { + deferred_offences.push((vec![], vec![], 0u32)); + } + + Offences::::set_deferred_offences(deferred_offences); + }: { - OffencesModule::::on_initialize(u.into()); + Offences::::on_initialize(u.into()); } } diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index 11b3a89f4a9a4..d668bcf449305 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -44,7 +44,7 @@ type OpaqueTimeSlot = Vec; type ReportIdOf = ::Hash; /// Type of data stored as a deferred offence -type DeferredOffenceOf = ( +pub type DeferredOffenceOf = ( Vec::AccountId, ::IdentificationTuple>>, Vec, SessionIndex, @@ -256,6 +256,11 @@ impl Module { None } } + + #[cfg(feature = "runtime-benchmarks")] + pub fn set_deferred_offences(offences: Vec>) { + >::put(offences); + } } struct TriageOutcome { diff --git a/frame/session/src/historical.rs b/frame/session/src/historical.rs index f9990dd1e8a7a..91cfdc79f42a9 100644 --- a/frame/session/src/historical.rs +++ b/frame/session/src/historical.rs @@ -40,7 +40,7 @@ type ValidatorCount = u32; /// Trait necessary for the historical module. pub trait Trait: super::Trait { /// Full identification of the validator. - type FullIdentification: Parameter; + type FullIdentification: Parameter + Ord; /// A conversion from validator ID to full identification. /// diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index 9346b060fa48a..e3e6550a9fb0c 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -347,7 +347,7 @@ pub trait Trait: frame_system::Trait { type Event: From + Into<::Event>; /// A stable ID for a validator. - type ValidatorId: Member + Parameter; + type ValidatorId: Member + Parameter + Ord; /// A conversion from account ID to validator ID. type ValidatorIdOf: Convert>; From 935315528cb36897dfca1189205431722e2836de Mon Sep 17 00:00:00 2001 From: marcio-diaz Date: Mon, 6 Apr 2020 18:47:03 +0200 Subject: [PATCH 16/16] More cleaning --- frame/offences/src/lib.rs | 7 ------- frame/session/src/historical.rs | 2 +- frame/session/src/lib.rs | 2 +- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/frame/offences/src/lib.rs b/frame/offences/src/lib.rs index d668bcf449305..40f39ab5f2a0b 100644 --- a/frame/offences/src/lib.rs +++ b/frame/offences/src/lib.rs @@ -110,12 +110,7 @@ decl_module! { fn on_initialize(now: T::BlockNumber) -> Weight { // only decode storage if we can actually submit anything again. if T::OnOffenceHandler::can_report() { - #[cfg(feature = "std")] - println!("inside can_report()"); - >::mutate(|deferred| { - #[cfg(feature = "std")] - println!("deferred offences len = {}", deferred.len()); // keep those that fail to be reported again. An error log is emitted here; this // should not happen if staking's `can_report` is implemented properly. deferred.retain(|(o, p, s)| { @@ -193,8 +188,6 @@ impl Module { ) { Ok(_) => true, Err(_) => { - #[cfg(feature = "std")] - println!("HERE DEFERRED"); >::mutate(|d| d.push((concurrent_offenders.to_vec(), slash_perbill.to_vec(), session_index)) ); diff --git a/frame/session/src/historical.rs b/frame/session/src/historical.rs index 91cfdc79f42a9..f9990dd1e8a7a 100644 --- a/frame/session/src/historical.rs +++ b/frame/session/src/historical.rs @@ -40,7 +40,7 @@ type ValidatorCount = u32; /// Trait necessary for the historical module. pub trait Trait: super::Trait { /// Full identification of the validator. - type FullIdentification: Parameter + Ord; + type FullIdentification: Parameter; /// A conversion from validator ID to full identification. /// diff --git a/frame/session/src/lib.rs b/frame/session/src/lib.rs index e3e6550a9fb0c..9346b060fa48a 100644 --- a/frame/session/src/lib.rs +++ b/frame/session/src/lib.rs @@ -347,7 +347,7 @@ pub trait Trait: frame_system::Trait { type Event: From + Into<::Event>; /// A stable ID for a validator. - type ValidatorId: Member + Parameter + Ord; + type ValidatorId: Member + Parameter; /// A conversion from account ID to validator ID. type ValidatorIdOf: Convert>;