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
+85 -109
View File
@@ -21,13 +21,14 @@
#![cfg_attr(not(feature = "std"), no_std)]
use sp_std::{result, prelude::*, collections::btree_set::BTreeSet};
use codec::{Decode, Encode};
use frame_support::{
dispatch, traits::{FindAuthor, VerifySeal, Get},
dispatch,
traits::{FindAuthor, Get, VerifySeal},
};
use codec::{Encode, Decode};
use sp_authorship::{InherentError, UnclesInherentData, INHERENT_IDENTIFIER};
use sp_runtime::traits::{Header as HeaderT, One, Saturating};
use sp_authorship::{INHERENT_IDENTIFIER, UnclesInherentData, InherentError};
use sp_std::{collections::btree_set::BTreeSet, prelude::*, result};
const MAX_UNCLES: usize = 10;
@@ -56,15 +57,15 @@ pub trait FilterUncle<Header, Author> {
/// Do additional filtering on a seal-checked uncle block, with the accumulated
/// filter.
fn filter_uncle(header: &Header, acc: &mut Self::Accumulator)
-> Result<Option<Author>, &'static str>;
fn filter_uncle(
header: &Header,
acc: &mut Self::Accumulator,
) -> Result<Option<Author>, &'static str>;
}
impl<H, A> FilterUncle<H, A> for () {
type Accumulator = ();
fn filter_uncle(_: &H, _acc: &mut Self::Accumulator)
-> Result<Option<A>, &'static str>
{
fn filter_uncle(_: &H, _acc: &mut Self::Accumulator) -> Result<Option<A>, &'static str> {
Ok(None)
}
}
@@ -74,14 +75,10 @@ impl<H, A> FilterUncle<H, A> for () {
/// equivocating is high.
pub struct SealVerify<T>(sp_std::marker::PhantomData<T>);
impl<Header, Author, T: VerifySeal<Header, Author>> FilterUncle<Header, Author>
for SealVerify<T>
{
impl<Header, Author, T: VerifySeal<Header, Author>> FilterUncle<Header, Author> for SealVerify<T> {
type Accumulator = ();
fn filter_uncle(header: &Header, _acc: &mut ())
-> Result<Option<Author>, &'static str>
{
fn filter_uncle(header: &Header, _acc: &mut ()) -> Result<Option<Author>, &'static str> {
T::verify_seal(header)
}
}
@@ -92,8 +89,7 @@ impl<Header, Author, T: VerifySeal<Header, Author>> FilterUncle<Header, Author>
/// This does O(n log n) work in the number of uncles included.
pub struct OnePerAuthorPerHeight<T, N>(sp_std::marker::PhantomData<(T, N)>);
impl<Header, Author, T> FilterUncle<Header, Author>
for OnePerAuthorPerHeight<T, Header::Number>
impl<Header, Author, T> FilterUncle<Header, Author> for OnePerAuthorPerHeight<T, Header::Number>
where
Header: HeaderT + PartialEq,
Header::Number: Ord,
@@ -102,15 +98,16 @@ where
{
type Accumulator = BTreeSet<(Header::Number, Author)>;
fn filter_uncle(header: &Header, acc: &mut Self::Accumulator)
-> Result<Option<Author>, &'static str>
{
fn filter_uncle(
header: &Header,
acc: &mut Self::Accumulator,
) -> Result<Option<Author>, &'static str> {
let author = T::verify_seal(header)?;
let number = header.number();
if let Some(ref author) = author {
if !acc.insert((number.clone(), author.clone())) {
return Err("more than one uncle per number per author included");
return Err("more than one uncle per number per author included")
}
}
@@ -126,9 +123,9 @@ enum UncleEntryItem<BlockNumber, Hash, Author> {
}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
@@ -161,10 +158,8 @@ pub mod pallet {
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(now: T::BlockNumber) -> Weight {
let uncle_generations = T::UncleGenerations::get();
// prune uncles that are older than the allowed number of generations.
@@ -189,11 +184,8 @@ pub mod pallet {
#[pallet::storage]
/// Uncles
pub(super) type Uncles<T: Config> = StorageValue<
_,
Vec<UncleEntryItem<T::BlockNumber, T::Hash, T::AccountId>>,
ValueQuery,
>;
pub(super) type Uncles<T: Config> =
StorageValue<_, Vec<UncleEntryItem<T::BlockNumber, T::Hash, T::AccountId>>, ValueQuery>;
#[pallet::storage]
/// Author of current block.
@@ -203,7 +195,6 @@ pub mod pallet {
/// Whether uncles were already set in this block.
pub(super) type DidSetUncles<T: Config> = StorageValue<_, bool, ValueQuery>;
#[pallet::error]
pub enum Error<T> {
/// The uncle parent not in the chain.
@@ -251,14 +242,16 @@ pub mod pallet {
if !uncles.is_empty() {
let prev_uncles = <Uncles<T>>::get();
let mut existing_hashes: Vec<_> = prev_uncles.into_iter().filter_map(|entry|
match entry {
let mut existing_hashes: Vec<_> = prev_uncles
.into_iter()
.filter_map(|entry| match entry {
UncleEntryItem::InclusionHeight(_) => None,
UncleEntryItem::Uncle(h, _) => Some(h),
}
).collect();
})
.collect();
let mut acc: <T::FilterUncle as FilterUncle<_, _>>::Accumulator = Default::default();
let mut acc: <T::FilterUncle as FilterUncle<_, _>>::Accumulator =
Default::default();
for uncle in uncles {
match Self::verify_uncle(&uncle, &existing_hashes, &mut acc) {
@@ -270,10 +263,10 @@ pub mod pallet {
if set_uncles.len() == MAX_UNCLES {
break
}
}
},
Err(_) => {
// skip this uncle
}
},
}
}
}
@@ -285,14 +278,14 @@ pub mod pallet {
}
}
fn check_inherent(call: &Self::Call, _data: &InherentData) -> result::Result<(), Self::Error> {
fn check_inherent(
call: &Self::Call,
_data: &InherentData,
) -> result::Result<(), Self::Error> {
match call {
Call::set_uncles(ref uncles) if uncles.len() > MAX_UNCLES => {
Err(InherentError::Uncles(Error::<T>::TooManyUncles.as_str().into()))
},
_ => {
Ok(())
},
Call::set_uncles(ref uncles) if uncles.len() > MAX_UNCLES =>
Err(InherentError::Uncles(Error::<T>::TooManyUncles.as_str().into())),
_ => Ok(()),
}
}
@@ -310,7 +303,7 @@ impl<T: Config> Pallet<T> {
pub fn author() -> T::AccountId {
// Check the memoized storage value.
if let Some(author) = <Author<T>>::get() {
return author;
return author
}
let digest = <frame_system::Pallet<T>>::digest();
@@ -332,11 +325,10 @@ impl<T: Config> Pallet<T> {
let mut acc: <T::FilterUncle as FilterUncle<_, _>>::Accumulator = Default::default();
for uncle in new_uncles {
let prev_uncles = uncles.iter().filter_map(|entry|
match entry {
UncleEntryItem::InclusionHeight(_) => None,
UncleEntryItem::Uncle(h, _) => Some(h),
});
let prev_uncles = uncles.iter().filter_map(|entry| match entry {
UncleEntryItem::InclusionHeight(_) => None,
UncleEntryItem::Uncle(h, _) => Some(h),
});
let author = Self::verify_uncle(&uncle, prev_uncles, &mut acc)?;
let hash = uncle.hash();
@@ -351,7 +343,7 @@ impl<T: Config> Pallet<T> {
Ok(())
}
fn verify_uncle<'a, I: IntoIterator<Item=&'a T::Hash>>(
fn verify_uncle<'a, I: IntoIterator<Item = &'a T::Hash>>(
uncle: &T::Header,
existing_uncles: I,
accumulator: &mut <T::FilterUncle as FilterUncle<T::Header, T::AccountId>>::Accumulator,
@@ -368,23 +360,23 @@ impl<T: Config> Pallet<T> {
let hash = uncle.hash();
if uncle.number() < &One::one() {
return Err(Error::<T>::GenesisUncle.into());
return Err(Error::<T>::GenesisUncle.into())
}
if uncle.number() > &maximum_height {
return Err(Error::<T>::TooHighUncle.into());
return Err(Error::<T>::TooHighUncle.into())
}
{
let parent_number = uncle.number().clone() - One::one();
let parent_hash = <frame_system::Pallet<T>>::block_hash(&parent_number);
if &parent_hash != uncle.parent_hash() {
return Err(Error::<T>::InvalidUncleParent.into());
return Err(Error::<T>::InvalidUncleParent.into())
}
}
if uncle.number() < &minimum_height {
return Err(Error::<T>::OldUncle.into());
return Err(Error::<T>::OldUncle.into())
}
let duplicate = existing_uncles.into_iter().any(|h| *h == hash);
@@ -412,13 +404,15 @@ impl<T: Config> Pallet<T> {
#[cfg(test)]
mod tests {
use crate as pallet_authorship;
use super::*;
use crate as pallet_authorship;
use frame_support::{parameter_types, ConsensusEngineId};
use sp_core::H256;
use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup}, testing::Header, generic::DigestItem,
generic::DigestItem,
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};
use frame_support::{parameter_types, ConsensusEngineId};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
@@ -483,11 +477,12 @@ mod tests {
impl FindAuthor<u64> for AuthorGiven {
fn find_author<'a, I>(digests: I) -> Option<u64>
where I: 'a + IntoIterator<Item=(ConsensusEngineId, &'a [u8])>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
for (id, data) in digests {
if id == TEST_ID {
return u64::decode(&mut &data[..]).ok();
return u64::decode(&mut &data[..]).ok()
}
}
@@ -502,7 +497,8 @@ mod tests {
let pre_runtime_digests = header.digest.logs.iter().filter_map(|d| d.as_pre_runtime());
let seals = header.digest.logs.iter().filter_map(|d| d.as_seal());
let author = AuthorGiven::find_author(pre_runtime_digests).ok_or_else(|| "no author")?;
let author =
AuthorGiven::find_author(pre_runtime_digests).ok_or_else(|| "no author")?;
for (id, seal) in seals {
if id == TEST_ID {
@@ -510,10 +506,10 @@ mod tests {
Err(_) => return Err("wrong seal"),
Ok(a) => {
if a != author {
return Err("wrong author in seal");
return Err("wrong author in seal")
}
break
}
},
}
}
}
@@ -533,13 +529,7 @@ mod tests {
}
fn create_header(number: u64, parent_hash: H256, state_root: H256) -> Header {
Header::new(
number,
Default::default(),
state_root,
parent_hash,
Default::default(),
)
Header::new(number, Default::default(), state_root, parent_hash, Default::default())
}
fn new_test_ext() -> sp_io::TestExternalities {
@@ -554,9 +544,14 @@ mod tests {
let hash = Default::default();
let author = Default::default();
let uncles = vec![
InclusionHeight(1u64), Uncle(hash, Some(author)), Uncle(hash, None), Uncle(hash, None),
InclusionHeight(2u64), Uncle(hash, None),
InclusionHeight(3u64), Uncle(hash, None),
InclusionHeight(1u64),
Uncle(hash, Some(author)),
Uncle(hash, None),
Uncle(hash, None),
InclusionHeight(2u64),
Uncle(hash, None),
InclusionHeight(3u64),
Uncle(hash, None),
];
<Authorship as Store>::Uncles::put(uncles);
@@ -595,15 +590,15 @@ mod tests {
}
let mut canon_chain = CanonChain {
inner: vec![seal_header(create_header(0, Default::default(), Default::default()), 999)],
inner: vec![seal_header(
create_header(0, Default::default(), Default::default()),
999,
)],
};
let initialize_block = |number, hash: H256| System::initialize(
&number,
&hash,
&Default::default(),
Default::default()
);
let initialize_block = |number, hash: H256| {
System::initialize(&number, &hash, &Default::default(), Default::default())
};
for number in 1..8 {
initialize_block(number, canon_chain.best_hash());
@@ -691,18 +686,11 @@ mod tests {
fn sets_author_lazily() {
new_test_ext().execute_with(|| {
let author = 42;
let mut header = seal_header(
create_header(1, Default::default(), [1; 32].into()),
author,
);
let mut header =
seal_header(create_header(1, Default::default(), [1; 32].into()), author);
header.digest_mut().pop(); // pop the seal off.
System::initialize(
&1,
&Default::default(),
header.digest(),
Default::default(),
);
System::initialize(&1, &Default::default(), header.digest(), Default::default());
assert_eq!(Authorship::author(), author);
});
@@ -716,27 +704,15 @@ mod tests {
let author_b = 43;
let mut acc: <Filter as FilterUncle<Header, u64>>::Accumulator = Default::default();
let header_a1 = seal_header(
create_header(1, Default::default(), [1; 32].into()),
author_a,
);
let header_b1 = seal_header(
create_header(1, Default::default(), [1; 32].into()),
author_b,
);
let header_a1 = seal_header(create_header(1, Default::default(), [1; 32].into()), author_a);
let header_b1 = seal_header(create_header(1, Default::default(), [1; 32].into()), author_b);
let header_a2_1 = seal_header(
create_header(2, Default::default(), [1; 32].into()),
author_a,
);
let header_a2_2 = seal_header(
create_header(2, Default::default(), [2; 32].into()),
author_a,
);
let header_a2_1 =
seal_header(create_header(2, Default::default(), [1; 32].into()), author_a);
let header_a2_2 =
seal_header(create_header(2, Default::default(), [2; 32].into()), author_a);
let mut check_filter = move |uncle| {
Filter::filter_uncle(uncle, &mut acc)
};
let mut check_filter = move |uncle| Filter::filter_uncle(uncle, &mut acc);
// same height, different author is OK.
assert_eq!(check_filter(&header_a1), Ok(Some(author_a)));