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:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
+32 -37
View File
@@ -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.
+2 -7
View File
@@ -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,
);
+20 -20
View File
@@ -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)
}
}
+39 -96
View File
@@ -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![