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
@@ -20,11 +20,11 @@
#![cfg_attr(not(feature = "std"), no_std)]
use sp_std::{result::Result, prelude::*};
use sp_std::{prelude::*, result::Result};
use codec::{Encode, Decode};
use sp_inherents::{InherentIdentifier, InherentData, IsFatalError};
use sp_runtime::{traits::{Block as BlockT, NumberFor}};
use codec::{Decode, Encode};
use sp_inherents::{InherentData, InherentIdentifier, IsFatalError};
use sp_runtime::traits::{Block as BlockT, NumberFor};
pub use sp_inherents::Error;
@@ -40,7 +40,7 @@ pub const CHUNK_SIZE: usize = 256;
#[cfg_attr(feature = "std", derive(Decode))]
pub enum InherentError {
InvalidProof,
TrieError
TrieError,
}
impl IsFatalError for InherentError {
@@ -130,26 +130,20 @@ pub trait IndexedBody<B: BlockT> {
///
/// Note that this will only fetch transactions
/// that are indexed by the runtime with `storage_index_transaction`.
fn block_indexed_body(
&self,
number: NumberFor<B>,
) -> Result<Option<Vec<Vec<u8>>>, Error>;
fn block_indexed_body(&self, number: NumberFor<B>) -> Result<Option<Vec<Vec<u8>>>, Error>;
/// Get block number for a block hash.
fn number(
&self,
hash: B::Hash,
) -> Result<Option<NumberFor<B>>, Error>;
fn number(&self, hash: B::Hash) -> Result<Option<NumberFor<B>>, Error>;
}
#[cfg(feature = "std")]
pub mod registration {
use sp_runtime::{traits::{Block as BlockT, Saturating, Zero, One}};
use sp_trie::TrieMut;
use super::*;
use sp_runtime::traits::{Block as BlockT, One, Saturating, Zero};
use sp_trie::TrieMut;
type Hasher = sp_core::Blake2Hasher;
type TrieLayout = sp_trie::Layout::<Hasher>;
type TrieLayout = sp_trie::Layout<Hasher>;
/// Create a new inherent data provider instance for a given parent block hash.
pub fn new_data_provider<B, C>(
@@ -166,25 +160,24 @@ pub mod registration {
.saturating_sub(DEFAULT_STORAGE_PERIOD.into());
if number.is_zero() {
// Too early to collect proofs.
return Ok(InherentDataProvider::new(None));
return Ok(InherentDataProvider::new(None))
}
let proof = match client.block_indexed_body(number)? {
Some(transactions) => {
Some(build_proof(parent.as_ref(), transactions)?)
},
Some(transactions) => Some(build_proof(parent.as_ref(), transactions)?),
None => {
// Nothing was indexed in that block.
None
}
},
};
Ok(InherentDataProvider::new(proof))
}
/// Build a proof for a given source of randomness and indexed transactions.
pub fn build_proof(random_hash: &[u8], transactions: Vec<Vec<u8>>)
-> Result<TransactionStorageProof, Error>
{
pub fn build_proof(
random_hash: &[u8],
transactions: Vec<Vec<u8>>,
) -> Result<TransactionStorageProof, Error> {
let mut db = sp_trie::MemoryDB::<Hasher>::default();
let mut target_chunk = None;
@@ -192,22 +185,25 @@ pub mod registration {
let mut target_chunk_key = Default::default();
let mut chunk_proof = Default::default();
let total_chunks: u64 = transactions.iter().map(|t| ((t.len() + CHUNK_SIZE - 1) / CHUNK_SIZE) as u64).sum();
let total_chunks: u64 = transactions
.iter()
.map(|t| ((t.len() + CHUNK_SIZE - 1) / CHUNK_SIZE) as u64)
.sum();
let mut buf = [0u8; 8];
buf.copy_from_slice(&random_hash[0..8]);
let random_u64 = u64::from_be_bytes(buf);
let target_chunk_index = random_u64 % total_chunks;
//Generate tries for each transaction.
// Generate tries for each transaction.
let mut chunk_index = 0;
for transaction in transactions {
let mut transaction_root = sp_trie::empty_trie_root::<TrieLayout>();
{
let mut trie = sp_trie::TrieDBMut::<TrieLayout>::new(&mut db, &mut transaction_root);
let mut trie =
sp_trie::TrieDBMut::<TrieLayout>::new(&mut db, &mut transaction_root);
let chunks = transaction.chunks(CHUNK_SIZE).map(|c| c.to_vec());
for (index, chunk) in chunks.enumerate() {
let index = encode_index(index as u32);
trie.insert(&index, &chunk)
.map_err(|e| Error::Application(Box::new(e)))?;
trie.insert(&index, &chunk).map_err(|e| Error::Application(Box::new(e)))?;
if chunk_index == target_chunk_index {
target_chunk = Some(chunk);
target_chunk_key = index;
@@ -221,15 +217,13 @@ pub mod registration {
chunk_proof = sp_trie::generate_trie_proof::<TrieLayout, _, _, _>(
&db,
transaction_root.clone(),
&[target_chunk_key.clone()]
).map_err(|e| Error::Application(Box::new(e)))?;
&[target_chunk_key.clone()],
)
.map_err(|e| Error::Application(Box::new(e)))?;
}
};
}
Ok(TransactionStorageProof {
proof: chunk_proof,
chunk: target_chunk.unwrap(),
})
Ok(TransactionStorageProof { proof: chunk_proof, chunk: target_chunk.unwrap() })
}
#[test]
@@ -237,11 +231,15 @@ pub mod registration {
use std::str::FromStr;
let random = [0u8; 32];
let proof = build_proof(&random, vec![vec![42]]).unwrap();
let root = sp_core::H256::from_str("0xff8611a4d212fc161dae19dd57f0f1ba9309f45d6207da13f2d3eab4c6839e91").unwrap();
let root = sp_core::H256::from_str(
"0xff8611a4d212fc161dae19dd57f0f1ba9309f45d6207da13f2d3eab4c6839e91",
)
.unwrap();
sp_trie::verify_trie_proof::<TrieLayout, _, _, _>(
&root,
&proof.proof,
&[(encode_index(0), Some(proof.chunk))],
).unwrap();
)
.unwrap();
}
}