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
@@ -19,17 +19,18 @@
#![cfg(feature = "runtime-benchmarks")]
use sp_std::*;
use super::*;
use sp_runtime::traits::{Zero, One, Bounded};
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::traits::{Currency, OnFinalize, OnInitialize};
use frame_system::{EventRecord, Pallet as System, RawOrigin};
use sp_runtime::traits::{Bounded, One, Zero};
use sp_std::*;
use sp_transaction_storage_proof::TransactionStorageProof;
use frame_system::{RawOrigin, Pallet as System, EventRecord};
use frame_benchmarking::{benchmarks, whitelisted_caller, impl_benchmark_test_suite};
use frame_support::{traits::{Currency, OnFinalize, OnInitialize}};
use crate::Pallet as TransactionStorage;
const PROOF: &[u8] = &hex_literal::hex!("
const PROOF: &[u8] = &hex_literal::hex!(
"
0104000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@@ -75,9 +76,11 @@ const PROOF: &[u8] = &hex_literal::hex!("
0c8e67d9b280f2b31a5707d52b892977acaac84d530bd188544c5f9b80b4f23ac50c8e67d9b280f2b31a5707d52b89297
7acaac84d530bd188544c5f9b80b4f23ac50c8e67d9b280f2b31a5707d52b892977acaac84d530bd188544c5f9b104401
0000
");
"
);
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = System::<T>::events();
@@ -90,7 +93,9 @@ pub fn run_to_block<T: Config>(n: T::BlockNumber) {
while frame_system::Pallet::<T>::block_number() < n {
crate::Pallet::<T>::on_finalize(frame_system::Pallet::<T>::block_number());
frame_system::Pallet::<T>::on_finalize(frame_system::Pallet::<T>::block_number());
frame_system::Pallet::<T>::set_block_number(frame_system::Pallet::<T>::block_number() + One::one());
frame_system::Pallet::<T>::set_block_number(
frame_system::Pallet::<T>::block_number() + One::one(),
);
frame_system::Pallet::<T>::on_initialize(frame_system::Pallet::<T>::block_number());
crate::Pallet::<T>::on_initialize(frame_system::Pallet::<T>::block_number());
}
@@ -140,8 +145,4 @@ benchmarks! {
}
}
impl_benchmark_test_suite!(
TransactionStorage,
crate::mock::new_test_ext(),
crate::mock::Test,
);
impl_benchmark_test_suite!(TransactionStorage, crate::mock::new_test_ext(), crate::mock::Test,);
+42 -42
View File
@@ -28,24 +28,24 @@ mod mock;
#[cfg(test)]
mod tests;
use codec::{Decode, Encode};
use frame_support::{
traits::{ReservableCurrency, Currency, OnUnbalanced},
dispatch::{Dispatchable, GetDispatchInfo},
traits::{Currency, OnUnbalanced, ReservableCurrency},
};
use sp_std::prelude::*;
use sp_std::{result};
use codec::{Encode, Decode};
use sp_runtime::traits::{Saturating, BlakeTwo256, Hash, Zero, One};
use sp_runtime::traits::{BlakeTwo256, Hash, One, Saturating, Zero};
use sp_std::{prelude::*, result};
use sp_transaction_storage_proof::{
TransactionStorageProof, InherentError,
random_chunk, encode_index,
CHUNK_SIZE, INHERENT_IDENTIFIER, DEFAULT_STORAGE_PERIOD,
encode_index, random_chunk, InherentError, TransactionStorageProof, CHUNK_SIZE,
DEFAULT_STORAGE_PERIOD, INHERENT_IDENTIFIER,
};
/// A type alias for the balance type from this pallet's point of view.
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>
::NegativeImbalance;
type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
// Re-export pallet items so that they can be accessed from the crate namespace.
pub use pallet::*;
@@ -76,16 +76,19 @@ fn num_chunks(bytes: u32) -> u32 {
#[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 {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// A dispatchable call.
type Call: Parameter + Dispatchable<Origin=Self::Origin> + GetDispatchInfo + From<frame_system::Call<Self>>;
type Call: Parameter
+ Dispatchable<Origin = Self::Origin>
+ GetDispatchInfo
+ From<frame_system::Call<Self>>;
/// The currency trait.
type Currency: ReservableCurrency<Self::AccountId>;
/// Handler for the unbalanced decrease when fees are burned.
@@ -145,8 +148,7 @@ pub mod pallet {
fn on_finalize(n: T::BlockNumber) {
assert!(
<ProofChecked<T>>::take()
|| {
<ProofChecked<T>>::take() || {
// Proof is not required for early or empty blocks.
let number = <frame_system::Pallet<T>>::block_number();
let period = <StoragePeriod<T>>::get();
@@ -174,12 +176,12 @@ pub mod pallet {
/// Additionally contains a DB write.
/// # </weight>
#[pallet::weight(T::WeightInfo::store(data.len() as u32))]
pub fn store(
origin: OriginFor<T>,
data: Vec<u8>,
) -> DispatchResult {
pub fn store(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
ensure!(data.len() > 0, Error::<T>::EmptyTransaction);
ensure!(data.len() <= MaxTransactionSize::<T>::get() as usize, Error::<T>::TransactionTooLarge);
ensure!(
data.len() <= MaxTransactionSize::<T>::get() as usize,
Error::<T>::TransactionTooLarge
);
let sender = ensure_signed(origin)?;
Self::apply_fee(sender, data.len() as u32)?;
@@ -189,8 +191,8 @@ pub mod pallet {
let root = sp_io::trie::blake2_256_ordered_root(chunks);
let content_hash = sp_io::hashing::blake2_256(&data);
let extrinsic_index = <frame_system::Pallet<T>>::extrinsic_index().ok_or_else(
|| Error::<T>::BadContext)?;
let extrinsic_index = <frame_system::Pallet<T>>::extrinsic_index()
.ok_or_else(|| Error::<T>::BadContext)?;
sp_io::transaction_index::index(extrinsic_index, data.len() as u32, content_hash);
let mut index = 0;
@@ -277,11 +279,14 @@ pub mod pallet {
let selected_chunk_index = random_chunk(parent_hash.as_ref(), total_chunks);
let (info, chunk_index) = match <Transactions<T>>::get(target_number) {
Some(infos) => {
let index = match infos.binary_search_by_key(&selected_chunk_index, |info| info.block_chunks) {
let index = match infos
.binary_search_by_key(&selected_chunk_index, |info| info.block_chunks)
{
Ok(index) => index,
Err(index) => index,
};
let info = infos.get(index).ok_or_else(|| Error::<T>::MissingStateData)?.clone();
let info =
infos.get(index).ok_or_else(|| Error::<T>::MissingStateData)?.clone();
let chunks = num_chunks(info.size);
let prev_chunks = info.block_chunks - chunks;
(info, selected_chunk_index - prev_chunks)
@@ -317,23 +322,13 @@ pub mod pallet {
/// Collection of transaction metadata by block number.
#[pallet::storage]
#[pallet::getter(fn transaction_roots)]
pub(super) type Transactions<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::BlockNumber,
Vec<TransactionInfo>,
OptionQuery,
>;
pub(super) type Transactions<T: Config> =
StorageMap<_, Blake2_128Concat, T::BlockNumber, Vec<TransactionInfo>, OptionQuery>;
/// Count indexed chunks for each block.
#[pallet::storage]
pub(super) type ChunkCount<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::BlockNumber,
u32,
ValueQuery,
>;
pub(super) type ChunkCount<T: Config> =
StorageMap<_, Blake2_128Concat, T::BlockNumber, u32, ValueQuery>;
#[pallet::storage]
#[pallet::getter(fn byte_fee)]
@@ -362,13 +357,13 @@ pub mod pallet {
// Intermediates
#[pallet::storage]
pub(super) type BlockTransactions<T: Config> = StorageValue<_, Vec<TransactionInfo>, ValueQuery>;
pub(super) type BlockTransactions<T: Config> =
StorageValue<_, Vec<TransactionInfo>, ValueQuery>;
/// Was the proof checked in this block?
#[pallet::storage]
pub(super) type ProofChecked<T: Config> = StorageValue<_, bool, ValueQuery>;
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub byte_fee: BalanceOf<T>,
@@ -409,11 +404,16 @@ pub mod pallet {
const INHERENT_IDENTIFIER: InherentIdentifier = INHERENT_IDENTIFIER;
fn create_inherent(data: &InherentData) -> Option<Self::Call> {
let proof = data.get_data::<TransactionStorageProof>(&Self::INHERENT_IDENTIFIER).unwrap_or(None);
let proof = data
.get_data::<TransactionStorageProof>(&Self::INHERENT_IDENTIFIER)
.unwrap_or(None);
proof.map(Call::check_proof)
}
fn check_inherent(_call: &Self::Call, _data: &InherentData) -> result::Result<(), Self::Error> {
fn check_inherent(
_call: &Self::Call,
_data: &InherentData,
) -> result::Result<(), Self::Error> {
Ok(())
}
@@ -19,13 +19,16 @@
use crate as pallet_transaction_storage;
use crate::TransactionStorageProof;
use sp_core::H256;
use sp_runtime::{traits::{BlakeTwo256, IdentityLookup}, testing::Header, BuildStorage};
use frame_support::{
parameter_types,
traits::{OnInitialize, OnFinalize},
traits::{OnFinalize, OnInitialize},
};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
pub type Block = frame_system::mocking::MockBlock<Test>;
@@ -104,7 +107,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
let t = GenesisConfig {
system: Default::default(),
balances: pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 1000000000), (2, 100), (3, 100), (4, 100)]
balances: vec![(1, 1000000000), (2, 100), (3, 100), (4, 100)],
},
transaction_storage: pallet_transaction_storage::GenesisConfig::<Test> {
storage_period: 10,
@@ -113,7 +116,9 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
max_block_transactions: crate::DEFAULT_MAX_BLOCK_TRANSACTIONS,
max_transaction_size: crate::DEFAULT_MAX_TRANSACTION_SIZE,
},
}.build_storage().unwrap();
}
.build_storage()
.unwrap();
t.into()
}
@@ -17,10 +17,9 @@
//! Tests for transction-storage pallet.
use super::*;
use super::{Pallet as TransactionStorage, *};
use crate::mock::*;
use super::Pallet as TransactionStorage;
use frame_support::{assert_ok, assert_noop};
use frame_support::{assert_noop, assert_ok};
use frame_system::RawOrigin;
use sp_transaction_storage_proof::registration::build_proof;
@@ -41,9 +40,12 @@ fn discards_data() {
));
let proof_provider = || {
let block_num = <frame_system::Pallet<Test>>::block_number();
if block_num == 11 {
if block_num == 11 {
let parent_hash = <frame_system::Pallet<Test>>::parent_hash();
Some(build_proof(parent_hash.as_ref(), vec![vec![0u8; 2000], vec![0u8; 2000]]).unwrap())
Some(
build_proof(parent_hash.as_ref(), vec![vec![0u8; 2000], vec![0u8; 2000]])
.unwrap(),
)
} else {
None
}
@@ -64,15 +66,16 @@ fn burns_fee() {
new_test_ext().execute_with(|| {
run_to_block(1, || None);
let caller = 1;
assert_noop!(TransactionStorage::<Test>::store(
assert_noop!(
TransactionStorage::<Test>::store(
RawOrigin::Signed(5).into(),
vec![0u8; 2000 as usize]
),
Error::<Test>::InsufficientFunds,
);
assert_ok!(TransactionStorage::<Test>::store(
RawOrigin::Signed(caller.clone()).into(),
vec![0u8; 2000 as usize]
RawOrigin::Signed(caller.clone()).into(),
vec![0u8; 2000 as usize]
));
assert_eq!(Balances::free_balance(1), 1_000_000_000 - 2000 * 2 - 200);
});
@@ -89,34 +92,23 @@ fn checks_proof() {
));
run_to_block(10, || None);
let parent_hash = <frame_system::Pallet<Test>>::parent_hash();
let proof = build_proof(
parent_hash.as_ref(),
vec![vec![0u8; MAX_DATA_SIZE as usize]]
).unwrap();
assert_noop!(TransactionStorage::<Test>::check_proof(
Origin::none(),
proof,
),
let proof =
build_proof(parent_hash.as_ref(), vec![vec![0u8; MAX_DATA_SIZE as usize]]).unwrap();
assert_noop!(
TransactionStorage::<Test>::check_proof(Origin::none(), proof,),
Error::<Test>::UnexpectedProof,
);
run_to_block(11, || None);
let parent_hash = <frame_system::Pallet<Test>>::parent_hash();
let invalid_proof = build_proof(
parent_hash.as_ref(),
vec![vec![0u8; 1000]]
).unwrap();
assert_noop!(TransactionStorage::<Test>::check_proof(
Origin::none(),
invalid_proof,
),
Error::<Test>::InvalidProof,
let invalid_proof = build_proof(parent_hash.as_ref(), vec![vec![0u8; 1000]]).unwrap();
assert_noop!(
TransactionStorage::<Test>::check_proof(Origin::none(), invalid_proof,),
Error::<Test>::InvalidProof,
);
let proof = build_proof(
parent_hash.as_ref(),
vec![vec![0u8; MAX_DATA_SIZE as usize]]
).unwrap();
let proof =
build_proof(parent_hash.as_ref(), vec![vec![0u8; MAX_DATA_SIZE as usize]]).unwrap();
assert_ok!(TransactionStorage::<Test>::check_proof(Origin::none(), proof));
});
}
@@ -127,20 +119,20 @@ fn renews_data() {
run_to_block(1, || None);
let caller = 1;
assert_ok!(TransactionStorage::<Test>::store(
RawOrigin::Signed(caller.clone()).into(),
vec![0u8; 2000]
RawOrigin::Signed(caller.clone()).into(),
vec![0u8; 2000]
));
let info = BlockTransactions::<Test>::get().last().unwrap().clone();
run_to_block(6, || None);
assert_ok!(TransactionStorage::<Test>::renew(
RawOrigin::Signed(caller.clone()).into(),
1, // block
0, // transaction
RawOrigin::Signed(caller.clone()).into(),
1, // block
0, // transaction
));
assert_eq!(Balances::free_balance(1), 1_000_000_000 - 4000 * 2 - 200 * 2);
let proof_provider = || {
let block_num = <frame_system::Pallet<Test>>::block_number();
if block_num == 11 || block_num == 16 {
if block_num == 11 || block_num == 16 {
let parent_hash = <frame_system::Pallet<Test>>::parent_hash();
Some(build_proof(parent_hash.as_ref(), vec![vec![0u8; 2000]]).unwrap())
} else {
@@ -154,4 +146,3 @@ fn renews_data() {
assert!(Transactions::<Test>::get(6).is_none());
});
}
@@ -36,6 +36,7 @@
// --template=./.maintain/frame-weight-template.hbs
#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]