mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 17:21:08 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -21,29 +21,30 @@
|
||||
|
||||
mod mock;
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use sp_std::vec;
|
||||
use sp_std::{prelude::*, vec};
|
||||
|
||||
use frame_system::{RawOrigin, Pallet as System, Config as SystemConfig};
|
||||
use frame_benchmarking::{benchmarks, account, impl_benchmark_test_suite};
|
||||
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
|
||||
use frame_support::traits::{Currency, ValidatorSet, ValidatorSetWithIdentification};
|
||||
use frame_system::{Config as SystemConfig, Pallet as System, RawOrigin};
|
||||
|
||||
use sp_runtime::{
|
||||
traits::{Convert, Saturating, StaticLookup, UniqueSaturatedInto},
|
||||
Perbill,
|
||||
traits::{Convert, StaticLookup, Saturating, UniqueSaturatedInto},
|
||||
};
|
||||
use sp_staking::offence::{ReportOffence, Offence};
|
||||
use sp_staking::offence::{Offence, ReportOffence};
|
||||
|
||||
use pallet_balances::Config as BalancesConfig;
|
||||
use pallet_babe::BabeEquivocationOffence;
|
||||
use pallet_balances::Config as BalancesConfig;
|
||||
use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot};
|
||||
use pallet_im_online::{Config as ImOnlineConfig, Pallet as ImOnline, UnresponsivenessOffence};
|
||||
use pallet_offences::{Config as OffencesConfig, Pallet as Offences};
|
||||
use pallet_session::historical::{Config as HistoricalConfig, IdentificationTuple};
|
||||
use pallet_session::{Config as SessionConfig, SessionManager};
|
||||
use pallet_session::{
|
||||
historical::{Config as HistoricalConfig, IdentificationTuple},
|
||||
Config as SessionConfig, SessionManager,
|
||||
};
|
||||
use pallet_staking::{
|
||||
Pallet as Staking, Config as StakingConfig, RewardDestination, ValidatorPrefs, Exposure,
|
||||
IndividualExposure, Event as StakingEvent,
|
||||
Config as StakingConfig, Event as StakingEvent, Exposure, IndividualExposure,
|
||||
Pallet as Staking, RewardDestination, ValidatorPrefs,
|
||||
};
|
||||
|
||||
const SEED: u32 = 0;
|
||||
@@ -62,7 +63,8 @@ pub trait Config:
|
||||
+ HistoricalConfig
|
||||
+ BalancesConfig
|
||||
+ IdTupleConvert<Self>
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
/// A helper trait to make sure we can convert `IdentificationTuple` coming from historical
|
||||
/// and the one required by offences.
|
||||
@@ -71,8 +73,9 @@ pub trait IdTupleConvert<T: HistoricalConfig + OffencesConfig> {
|
||||
fn convert(id: IdentificationTuple<T>) -> <T as OffencesConfig>::IdentificationTuple;
|
||||
}
|
||||
|
||||
impl<T: HistoricalConfig + OffencesConfig> IdTupleConvert<T> for T where
|
||||
<T as OffencesConfig>::IdentificationTuple: From<IdentificationTuple<T>>
|
||||
impl<T: HistoricalConfig + OffencesConfig> IdTupleConvert<T> for T
|
||||
where
|
||||
<T as OffencesConfig>::IdentificationTuple: From<IdentificationTuple<T>>,
|
||||
{
|
||||
fn convert(id: IdentificationTuple<T>) -> <T as OffencesConfig>::IdentificationTuple {
|
||||
id.into()
|
||||
@@ -80,7 +83,8 @@ impl<T: HistoricalConfig + OffencesConfig> IdTupleConvert<T> for T where
|
||||
}
|
||||
|
||||
type LookupSourceOf<T> = <<T as SystemConfig>::Lookup as StaticLookup>::Source;
|
||||
type BalanceOf<T> = <<T as StakingConfig>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
|
||||
type BalanceOf<T> =
|
||||
<<T as StakingConfig>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
|
||||
|
||||
struct Offender<T: Config> {
|
||||
pub controller: T::AccountId,
|
||||
@@ -109,19 +113,20 @@ fn create_offender<T: Config>(n: u32, nominators: u32) -> Result<Offender<T>, &'
|
||||
reward_destination.clone(),
|
||||
)?;
|
||||
|
||||
let validator_prefs = ValidatorPrefs {
|
||||
commission: Perbill::from_percent(50),
|
||||
.. Default::default()
|
||||
};
|
||||
let validator_prefs =
|
||||
ValidatorPrefs { commission: Perbill::from_percent(50), ..Default::default() };
|
||||
Staking::<T>::validate(RawOrigin::Signed(controller.clone()).into(), validator_prefs)?;
|
||||
|
||||
let mut individual_exposures = vec![];
|
||||
let mut nominator_stashes = vec![];
|
||||
// Create n nominators
|
||||
for i in 0 .. nominators {
|
||||
let nominator_stash: T::AccountId = account("nominator stash", n * MAX_NOMINATORS + i, SEED);
|
||||
let nominator_controller: T::AccountId = account("nominator controller", n * MAX_NOMINATORS + i, SEED);
|
||||
let nominator_controller_lookup: LookupSourceOf<T> = T::Lookup::unlookup(nominator_controller.clone());
|
||||
for i in 0..nominators {
|
||||
let nominator_stash: T::AccountId =
|
||||
account("nominator stash", n * MAX_NOMINATORS + i, SEED);
|
||||
let nominator_controller: T::AccountId =
|
||||
account("nominator controller", n * MAX_NOMINATORS + i, SEED);
|
||||
let nominator_controller_lookup: LookupSourceOf<T> =
|
||||
T::Lookup::unlookup(nominator_controller.clone());
|
||||
T::Currency::make_free_balance_be(&nominator_stash, free_amount.into());
|
||||
|
||||
Staking::<T>::bond(
|
||||
@@ -132,76 +137,82 @@ fn create_offender<T: Config>(n: u32, nominators: u32) -> Result<Offender<T>, &'
|
||||
)?;
|
||||
|
||||
let selected_validators: Vec<LookupSourceOf<T>> = vec![controller_lookup.clone()];
|
||||
Staking::<T>::nominate(RawOrigin::Signed(nominator_controller.clone()).into(), selected_validators)?;
|
||||
Staking::<T>::nominate(
|
||||
RawOrigin::Signed(nominator_controller.clone()).into(),
|
||||
selected_validators,
|
||||
)?;
|
||||
|
||||
individual_exposures.push(IndividualExposure {
|
||||
who: nominator_stash.clone(),
|
||||
value: amount.clone(),
|
||||
});
|
||||
individual_exposures
|
||||
.push(IndividualExposure { who: nominator_stash.clone(), value: amount.clone() });
|
||||
nominator_stashes.push(nominator_stash.clone());
|
||||
}
|
||||
|
||||
let exposure = Exposure {
|
||||
total: amount.clone() * n.into(),
|
||||
own: amount,
|
||||
others: individual_exposures,
|
||||
};
|
||||
let exposure =
|
||||
Exposure { total: amount.clone() * n.into(), own: amount, others: individual_exposures };
|
||||
let current_era = 0u32;
|
||||
Staking::<T>::add_era_stakers(current_era.into(), stash.clone().into(), exposure);
|
||||
|
||||
Ok(Offender { controller, stash, nominator_stashes })
|
||||
}
|
||||
|
||||
fn make_offenders<T: Config>(num_offenders: u32, num_nominators: u32) -> Result<
|
||||
(Vec<IdentificationTuple<T>>, Vec<Offender<T>>),
|
||||
&'static str
|
||||
> {
|
||||
fn make_offenders<T: Config>(
|
||||
num_offenders: u32,
|
||||
num_nominators: u32,
|
||||
) -> Result<(Vec<IdentificationTuple<T>>, Vec<Offender<T>>), &'static str> {
|
||||
Staking::<T>::new_session(0);
|
||||
|
||||
let mut offenders = vec![];
|
||||
for i in 0 .. num_offenders {
|
||||
for i in 0..num_offenders {
|
||||
let offender = create_offender::<T>(i + 1, num_nominators)?;
|
||||
offenders.push(offender);
|
||||
}
|
||||
|
||||
Staking::<T>::start_session(0);
|
||||
|
||||
let id_tuples = offenders.iter()
|
||||
.map(|offender|
|
||||
let id_tuples = offenders
|
||||
.iter()
|
||||
.map(|offender| {
|
||||
<T as SessionConfig>::ValidatorIdOf::convert(offender.controller.clone())
|
||||
.expect("failed to get validator id from account id"))
|
||||
.map(|validator_id|
|
||||
.expect("failed to get validator id from account id")
|
||||
})
|
||||
.map(|validator_id| {
|
||||
<T as HistoricalConfig>::FullIdentificationOf::convert(validator_id.clone())
|
||||
.map(|full_id| (validator_id, full_id))
|
||||
.expect("failed to convert validator id to full identification"))
|
||||
.map(|full_id| (validator_id, full_id))
|
||||
.expect("failed to convert validator id to full identification")
|
||||
})
|
||||
.collect::<Vec<IdentificationTuple<T>>>();
|
||||
Ok((id_tuples, offenders))
|
||||
}
|
||||
|
||||
fn make_offenders_im_online<T: Config>(num_offenders: u32, num_nominators: u32) -> Result<
|
||||
(Vec<pallet_im_online::IdentificationTuple<T>>, Vec<Offender<T>>),
|
||||
&'static str
|
||||
> {
|
||||
fn make_offenders_im_online<T: Config>(
|
||||
num_offenders: u32,
|
||||
num_nominators: u32,
|
||||
) -> Result<(Vec<pallet_im_online::IdentificationTuple<T>>, Vec<Offender<T>>), &'static str> {
|
||||
Staking::<T>::new_session(0);
|
||||
|
||||
let mut offenders = vec![];
|
||||
for i in 0 .. num_offenders {
|
||||
for i in 0..num_offenders {
|
||||
let offender = create_offender::<T>(i + 1, num_nominators)?;
|
||||
offenders.push(offender);
|
||||
}
|
||||
|
||||
Staking::<T>::start_session(0);
|
||||
|
||||
let id_tuples = offenders.iter()
|
||||
.map(|offender| <
|
||||
let id_tuples = offenders
|
||||
.iter()
|
||||
.map(|offender| {
|
||||
<
|
||||
<T as ImOnlineConfig>::ValidatorSet as ValidatorSet<T::AccountId>
|
||||
>::ValidatorIdOf::convert(offender.controller.clone())
|
||||
.expect("failed to get validator id from account id"))
|
||||
.map(|validator_id| <
|
||||
.expect("failed to get validator id from account id")
|
||||
})
|
||||
.map(|validator_id| {
|
||||
<
|
||||
<T as ImOnlineConfig>::ValidatorSet as ValidatorSetWithIdentification<T::AccountId>
|
||||
>::IdentificationOf::convert(validator_id.clone())
|
||||
.map(|full_id| (validator_id, full_id))
|
||||
.expect("failed to convert validator id to full identification"))
|
||||
.expect("failed to convert validator id to full identification")
|
||||
})
|
||||
.collect::<Vec<pallet_im_online::IdentificationTuple<T>>>();
|
||||
Ok((id_tuples, offenders))
|
||||
}
|
||||
@@ -224,7 +235,9 @@ fn check_events<T: Config, I: Iterator<Item = <T as SystemConfig>::Event>>(expec
|
||||
pretty("--Got:", &events);
|
||||
pretty("--Expected:", &expected);
|
||||
format!("Mismatching length. Got: {}, expected: {}", lengths.0, lengths.1)
|
||||
} else { Default::default() };
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
|
||||
for (idx, (a, b)) in events.into_iter().zip(expected).enumerate() {
|
||||
assert_eq!(a, b, "Mismatch at: {}. {}", idx, length_mismatch);
|
||||
@@ -388,8 +401,4 @@ benchmarks! {
|
||||
}
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(
|
||||
Pallet,
|
||||
crate::mock::new_test_ext(),
|
||||
crate::mock::Test,
|
||||
);
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test,);
|
||||
|
||||
@@ -20,17 +20,14 @@
|
||||
#![cfg(test)]
|
||||
|
||||
use super::*;
|
||||
use frame_support::{
|
||||
parameter_types,
|
||||
weights::constants::WEIGHT_PER_SECOND,
|
||||
};
|
||||
use frame_system as system;
|
||||
use sp_runtime::{
|
||||
traits::IdentityLookup,
|
||||
testing::{Header, UintAuthorityId},
|
||||
};
|
||||
use frame_election_provider_support::onchain;
|
||||
use frame_support::{parameter_types, weights::constants::WEIGHT_PER_SECOND};
|
||||
use frame_system as system;
|
||||
use pallet_session::historical as pallet_session_historical;
|
||||
use sp_runtime::{
|
||||
testing::{Header, UintAuthorityId},
|
||||
traits::IdentityLookup,
|
||||
};
|
||||
|
||||
type AccountId = u64;
|
||||
type AccountIndex = u32;
|
||||
@@ -112,7 +109,8 @@ impl pallet_session::SessionHandler<AccountId> for TestSessionHandler {
|
||||
_: bool,
|
||||
_: &[(AccountId, Ks)],
|
||||
_: &[(AccountId, Ks)],
|
||||
) {}
|
||||
) {
|
||||
}
|
||||
|
||||
fn on_disabled(_: usize) {}
|
||||
}
|
||||
@@ -198,7 +196,10 @@ impl pallet_offences::Config for Test {
|
||||
type OnOffenceHandler = Staking;
|
||||
}
|
||||
|
||||
impl<T> frame_system::offchain::SendTransactionTypes<T> for Test where Call: From<T> {
|
||||
impl<T> frame_system::offchain::SendTransactionTypes<T> for Test
|
||||
where
|
||||
Call: From<T>,
|
||||
{
|
||||
type Extrinsic = Extrinsic;
|
||||
type OverarchingCall = Call;
|
||||
}
|
||||
|
||||
@@ -22,18 +22,18 @@
|
||||
// Ensure we're `no_std` when compiling for Wasm.
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
mod migration;
|
||||
mod mock;
|
||||
mod tests;
|
||||
mod migration;
|
||||
|
||||
use sp_std::prelude::*;
|
||||
use codec::{Decode, Encode};
|
||||
use frame_support::weights::Weight;
|
||||
use sp_runtime::{traits::Hash, Perbill};
|
||||
use sp_staking::{
|
||||
offence::{Kind, Offence, OffenceDetails, OffenceError, OnOffenceHandler, ReportOffence},
|
||||
SessionIndex
|
||||
SessionIndex,
|
||||
};
|
||||
use codec::{Decode, Encode};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
pub use pallet::*;
|
||||
|
||||
@@ -44,17 +44,25 @@ type OpaqueTimeSlot = Vec<u8>;
|
||||
type ReportIdOf<T> = <T as frame_system::Config>::Hash;
|
||||
|
||||
pub trait WeightInfo {
|
||||
fn report_offence_im_online(r: u32, o: u32, n: u32, ) -> Weight;
|
||||
fn report_offence_grandpa(r: u32, n: u32, ) -> Weight;
|
||||
fn report_offence_babe(r: u32, n: u32, ) -> Weight;
|
||||
fn on_initialize(d: u32, ) -> Weight;
|
||||
fn report_offence_im_online(r: u32, o: u32, n: u32) -> Weight;
|
||||
fn report_offence_grandpa(r: u32, n: u32) -> Weight;
|
||||
fn report_offence_babe(r: u32, n: u32) -> Weight;
|
||||
fn on_initialize(d: u32) -> Weight;
|
||||
}
|
||||
|
||||
impl WeightInfo for () {
|
||||
fn report_offence_im_online(_r: u32, _o: u32, _n: u32, ) -> Weight { 1_000_000_000 }
|
||||
fn report_offence_grandpa(_r: u32, _n: u32, ) -> Weight { 1_000_000_000 }
|
||||
fn report_offence_babe(_r: u32, _n: u32, ) -> Weight { 1_000_000_000 }
|
||||
fn on_initialize(_d: u32, ) -> Weight { 1_000_000_000 }
|
||||
fn report_offence_im_online(_r: u32, _o: u32, _n: u32) -> Weight {
|
||||
1_000_000_000
|
||||
}
|
||||
fn report_offence_grandpa(_r: u32, _n: u32) -> Weight {
|
||||
1_000_000_000
|
||||
}
|
||||
fn report_offence_babe(_r: u32, _n: u32) -> Weight {
|
||||
1_000_000_000
|
||||
}
|
||||
fn on_initialize(_d: u32) -> Weight {
|
||||
1_000_000_000
|
||||
}
|
||||
}
|
||||
|
||||
#[frame_support::pallet]
|
||||
@@ -145,22 +153,20 @@ where
|
||||
|
||||
// Go through all offenders in the offence report and find all offenders that were spotted
|
||||
// in unique reports.
|
||||
let TriageOutcome {
|
||||
concurrent_offenders,
|
||||
} = match Self::triage_offence_report::<O>(reporters, &time_slot, offenders) {
|
||||
Some(triage) => triage,
|
||||
// The report contained only duplicates, so there is no need to slash again.
|
||||
None => return Err(OffenceError::DuplicateReport),
|
||||
};
|
||||
let TriageOutcome { concurrent_offenders } =
|
||||
match Self::triage_offence_report::<O>(reporters, &time_slot, offenders) {
|
||||
Some(triage) => triage,
|
||||
// The report contained only duplicates, so there is no need to slash again.
|
||||
None => return Err(OffenceError::DuplicateReport),
|
||||
};
|
||||
|
||||
let offenders_count = concurrent_offenders.len() as u32;
|
||||
|
||||
// The amount new offenders are slashed
|
||||
let new_fraction = O::slash_fraction(offenders_count, validator_set_count);
|
||||
|
||||
let slash_perbill: Vec<_> = (0..concurrent_offenders.len())
|
||||
.map(|_| new_fraction.clone())
|
||||
.collect();
|
||||
let slash_perbill: Vec<_> =
|
||||
(0..concurrent_offenders.len()).map(|_| new_fraction.clone()).collect();
|
||||
|
||||
T::OnOffenceHandler::on_offence(
|
||||
&concurrent_offenders,
|
||||
@@ -212,10 +218,7 @@ impl<T: Config> Pallet<T> {
|
||||
any_new = true;
|
||||
<Reports<T>>::insert(
|
||||
&report_id,
|
||||
OffenceDetails {
|
||||
offender,
|
||||
reporters: reporters.clone(),
|
||||
},
|
||||
OffenceDetails { offender, reporters: reporters.clone() },
|
||||
);
|
||||
|
||||
storage.insert(time_slot, report_id);
|
||||
@@ -232,9 +235,7 @@ impl<T: Config> Pallet<T> {
|
||||
|
||||
storage.save();
|
||||
|
||||
Some(TriageOutcome {
|
||||
concurrent_offenders,
|
||||
})
|
||||
Some(TriageOutcome { concurrent_offenders })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
@@ -270,20 +271,14 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
|
||||
|
||||
let concurrent_reports = <ConcurrentReportsIndex<T>>::get(&O::ID, &opaque_time_slot);
|
||||
|
||||
Self {
|
||||
opaque_time_slot,
|
||||
concurrent_reports,
|
||||
same_kind_reports,
|
||||
}
|
||||
Self { opaque_time_slot, concurrent_reports, same_kind_reports }
|
||||
}
|
||||
|
||||
/// Insert a new report to the index.
|
||||
fn insert(&mut self, time_slot: &O::TimeSlot, report_id: ReportIdOf<T>) {
|
||||
// Insert the report id into the list while maintaining the ordering by the time
|
||||
// slot.
|
||||
let pos = self
|
||||
.same_kind_reports
|
||||
.partition_point(|&(ref when, _)| when <= time_slot);
|
||||
let pos = self.same_kind_reports.partition_point(|&(ref when, _)| when <= time_slot);
|
||||
self.same_kind_reports.insert(pos, (time_slot.clone(), report_id));
|
||||
|
||||
// Update the list of concurrent reports.
|
||||
|
||||
@@ -16,18 +16,13 @@
|
||||
// limitations under the License.
|
||||
|
||||
use super::{Config, OffenceDetails, Perbill, SessionIndex};
|
||||
use frame_support::{traits::Get, weights::Weight, generate_storage_alias};
|
||||
use frame_support::{generate_storage_alias, traits::Get, weights::Weight};
|
||||
use sp_staking::offence::OnOffenceHandler;
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
/// Type of data stored as a deferred offence
|
||||
type DeferredOffenceOf<T> = (
|
||||
Vec<
|
||||
OffenceDetails<
|
||||
<T as frame_system::Config>::AccountId,
|
||||
<T as Config>::IdentificationTuple,
|
||||
>,
|
||||
>,
|
||||
Vec<OffenceDetails<<T as frame_system::Config>::AccountId, <T as Config>::IdentificationTuple>>,
|
||||
Vec<Perbill>,
|
||||
SessionIndex,
|
||||
);
|
||||
|
||||
@@ -19,22 +19,27 @@
|
||||
|
||||
#![cfg(test)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
use crate as offences;
|
||||
use crate::Config;
|
||||
use codec::Encode;
|
||||
use sp_runtime::Perbill;
|
||||
use sp_staking::{
|
||||
SessionIndex,
|
||||
offence::{self, Kind, OffenceDetails},
|
||||
};
|
||||
use sp_runtime::testing::Header;
|
||||
use sp_runtime::traits::{IdentityLookup, BlakeTwo256};
|
||||
use sp_core::H256;
|
||||
use frame_support::{
|
||||
parameter_types,
|
||||
weights::{Weight, constants::{WEIGHT_PER_SECOND, RocksDbWeight}},
|
||||
weights::{
|
||||
constants::{RocksDbWeight, WEIGHT_PER_SECOND},
|
||||
Weight,
|
||||
},
|
||||
};
|
||||
use crate as offences;
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, IdentityLookup},
|
||||
Perbill,
|
||||
};
|
||||
use sp_staking::{
|
||||
offence::{self, Kind, OffenceDetails},
|
||||
SessionIndex,
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
|
||||
pub struct OnOffenceHandler;
|
||||
|
||||
@@ -43,8 +48,8 @@ thread_local! {
|
||||
pub static OFFENCE_WEIGHT: RefCell<Weight> = RefCell::new(Default::default());
|
||||
}
|
||||
|
||||
impl<Reporter, Offender>
|
||||
offence::OnOffenceHandler<Reporter, Offender, Weight> for OnOffenceHandler
|
||||
impl<Reporter, Offender> offence::OnOffenceHandler<Reporter, Offender, Weight>
|
||||
for OnOffenceHandler
|
||||
{
|
||||
fn on_offence(
|
||||
_offenders: &[OffenceDetails<Reporter, Offender>],
|
||||
@@ -60,9 +65,7 @@ impl<Reporter, Offender>
|
||||
}
|
||||
|
||||
pub fn with_on_offence_fractions<R, F: FnOnce(&mut Vec<Perbill>) -> R>(f: F) -> R {
|
||||
ON_OFFENCE_PERBILL.with(|fractions| {
|
||||
f(&mut *fractions.borrow_mut())
|
||||
})
|
||||
ON_OFFENCE_PERBILL.with(|fractions| f(&mut *fractions.borrow_mut()))
|
||||
}
|
||||
|
||||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Runtime>;
|
||||
@@ -163,10 +166,7 @@ impl<T: Clone> offence::Offence<T> for Offence<T> {
|
||||
1
|
||||
}
|
||||
|
||||
fn slash_fraction(
|
||||
offenders_count: u32,
|
||||
validator_set_count: u32,
|
||||
) -> Perbill {
|
||||
fn slash_fraction(offenders_count: u32, validator_set_count: u32) -> Perbill {
|
||||
Perbill::from_percent(5 + offenders_count * 100 / validator_set_count)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,11 +21,11 @@
|
||||
|
||||
use super::*;
|
||||
use crate::mock::{
|
||||
Offences, System, Offence, Event, KIND, new_test_ext, with_on_offence_fractions,
|
||||
offence_reports, report_id,
|
||||
new_test_ext, offence_reports, report_id, with_on_offence_fractions, Event, Offence, Offences,
|
||||
System, KIND,
|
||||
};
|
||||
use sp_runtime::Perbill;
|
||||
use frame_system::{EventRecord, Phase};
|
||||
use sp_runtime::Perbill;
|
||||
|
||||
#[test]
|
||||
fn should_report_an_authority_and_trigger_on_offence() {
|
||||
@@ -34,11 +34,7 @@ fn should_report_an_authority_and_trigger_on_offence() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
|
||||
// when
|
||||
Offences::report_offence(vec![], offence).unwrap();
|
||||
@@ -57,11 +53,7 @@ fn should_not_report_the_same_authority_twice_in_the_same_slot() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
Offences::report_offence(vec![], offence.clone()).unwrap();
|
||||
with_on_offence_fractions(|f| {
|
||||
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
|
||||
@@ -79,7 +71,6 @@ fn should_not_report_the_same_authority_twice_in_the_same_slot() {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn should_report_in_different_time_slot() {
|
||||
new_test_ext().execute_with(|| {
|
||||
@@ -87,11 +78,7 @@ fn should_report_in_different_time_slot() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let mut offence = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let mut offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
Offences::report_offence(vec![], offence.clone()).unwrap();
|
||||
with_on_offence_fractions(|f| {
|
||||
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
|
||||
@@ -117,11 +104,7 @@ fn should_deposit_event() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
|
||||
// when
|
||||
Offences::report_offence(vec![], offence).unwrap();
|
||||
@@ -145,11 +128,7 @@ fn doesnt_deposit_event_for_dups() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
Offences::report_offence(vec![], offence.clone()).unwrap();
|
||||
with_on_offence_fractions(|f| {
|
||||
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
|
||||
@@ -181,33 +160,26 @@ fn reports_if_an_offence_is_dup() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence = |time_slot, offenders| TestOffence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders,
|
||||
};
|
||||
let offence =
|
||||
|time_slot, offenders| TestOffence { validator_set_count: 5, time_slot, offenders };
|
||||
|
||||
let mut test_offence = offence(time_slot, vec![0]);
|
||||
|
||||
// the report for authority 0 at time slot 42 should not be a known
|
||||
// offence
|
||||
assert!(
|
||||
!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence.offenders,
|
||||
&test_offence.time_slot
|
||||
)
|
||||
);
|
||||
assert!(!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence.offenders,
|
||||
&test_offence.time_slot
|
||||
));
|
||||
|
||||
// we report an offence for authority 0 at time slot 42
|
||||
Offences::report_offence(vec![], test_offence.clone()).unwrap();
|
||||
|
||||
// the same report should be a known offence now
|
||||
assert!(
|
||||
<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence.offenders,
|
||||
&test_offence.time_slot
|
||||
)
|
||||
);
|
||||
assert!(<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence.offenders,
|
||||
&test_offence.time_slot
|
||||
));
|
||||
|
||||
// and reporting it again should yield a duplicate report error
|
||||
assert_eq!(
|
||||
@@ -219,28 +191,21 @@ fn reports_if_an_offence_is_dup() {
|
||||
test_offence.offenders.push(1);
|
||||
|
||||
// it should not be a known offence anymore
|
||||
assert!(
|
||||
!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence.offenders,
|
||||
&test_offence.time_slot
|
||||
)
|
||||
);
|
||||
assert!(!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence.offenders,
|
||||
&test_offence.time_slot
|
||||
));
|
||||
|
||||
// and reporting it again should work without any error
|
||||
assert_eq!(
|
||||
Offences::report_offence(vec![], test_offence.clone()),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(Offences::report_offence(vec![], test_offence.clone()), Ok(()));
|
||||
|
||||
// creating a new offence for the same authorities on the next slot
|
||||
// should be considered a new offence and thefore not known
|
||||
let test_offence_next_slot = offence(time_slot + 1, vec![0, 1]);
|
||||
assert!(
|
||||
!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence_next_slot.offenders,
|
||||
&test_offence_next_slot.time_slot
|
||||
)
|
||||
);
|
||||
assert!(!<Offences as ReportOffence<_, _, TestOffence>>::is_known_offence(
|
||||
&test_offence_next_slot.offenders,
|
||||
&test_offence_next_slot.time_slot
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -253,16 +218,8 @@ fn should_properly_count_offences() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence1 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence2 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![4],
|
||||
};
|
||||
let offence1 = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
let offence2 = Offence { validator_set_count: 5, time_slot, offenders: vec![4] };
|
||||
Offences::report_offence(vec![], offence1).unwrap();
|
||||
with_on_offence_fractions(|f| {
|
||||
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
|
||||
@@ -294,26 +251,12 @@ fn should_properly_sort_offences() {
|
||||
let time_slot = 42;
|
||||
assert_eq!(offence_reports(KIND, time_slot), vec![]);
|
||||
|
||||
let offence1 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![5],
|
||||
};
|
||||
let offence2 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot,
|
||||
offenders: vec![4],
|
||||
};
|
||||
let offence3 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot: time_slot + 1,
|
||||
offenders: vec![6, 7],
|
||||
};
|
||||
let offence4 = Offence {
|
||||
validator_set_count: 5,
|
||||
time_slot: time_slot - 1,
|
||||
offenders: vec![3],
|
||||
};
|
||||
let offence1 = Offence { validator_set_count: 5, time_slot, offenders: vec![5] };
|
||||
let offence2 = Offence { validator_set_count: 5, time_slot, offenders: vec![4] };
|
||||
let offence3 =
|
||||
Offence { validator_set_count: 5, time_slot: time_slot + 1, offenders: vec![6, 7] };
|
||||
let offence4 =
|
||||
Offence { validator_set_count: 5, time_slot: time_slot - 1, offenders: vec![3] };
|
||||
Offences::report_offence(vec![], offence1).unwrap();
|
||||
with_on_offence_fractions(|f| {
|
||||
assert_eq!(f.clone(), vec![Perbill::from_percent(25)]);
|
||||
@@ -327,10 +270,10 @@ fn should_properly_sort_offences() {
|
||||
Offences::report_offence(vec![], offence4).unwrap();
|
||||
|
||||
// then
|
||||
let same_kind_reports =
|
||||
Vec::<(u128, sp_core::H256)>::decode(
|
||||
&mut &crate::ReportsByKindIndex::<crate::mock::Runtime>::get(KIND)[..],
|
||||
).unwrap();
|
||||
let same_kind_reports = Vec::<(u128, sp_core::H256)>::decode(
|
||||
&mut &crate::ReportsByKindIndex::<crate::mock::Runtime>::get(KIND)[..],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
same_kind_reports,
|
||||
vec![
|
||||
|
||||
Reference in New Issue
Block a user