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
+142 -168
View File
@@ -18,25 +18,28 @@
//! Concrete externalities implementation.
use crate::{
StorageKey, StorageValue, OverlayedChanges, IndexOperation,
backend::Backend, overlayed_changes::OverlayedExtensions,
};
use hash_db::Hasher;
use sp_core::{
storage::{well_known_keys::is_child_storage_key, ChildInfo, TrackedStorageKey},
hexdisplay::HexDisplay,
};
use sp_trie::{trie_types::Layout, empty_child_trie_root};
use sp_externalities::{
Externalities, Extensions, Extension, ExtensionStore,
backend::Backend, overlayed_changes::OverlayedExtensions, IndexOperation, OverlayedChanges,
StorageKey, StorageValue,
};
use codec::{Decode, Encode, EncodeAppend};
use hash_db::Hasher;
use sp_core::{
hexdisplay::HexDisplay,
storage::{well_known_keys::is_child_storage_key, ChildInfo, TrackedStorageKey},
};
use sp_externalities::{Extension, ExtensionStore, Extensions, Externalities};
use sp_trie::{empty_child_trie_root, trie_types::Layout};
use sp_std::{fmt, any::{Any, TypeId}, vec::Vec, vec, boxed::Box, cmp::Ordering};
use crate::{warn, trace, log_error};
#[cfg(feature = "std")]
use crate::changes_trie::State as ChangesTrieState;
use crate::StorageTransactionCache;
use crate::{log_error, trace, warn, StorageTransactionCache};
use sp_std::{
any::{Any, TypeId},
boxed::Box,
cmp::Ordering,
fmt, vec,
vec::Vec,
};
#[cfg(feature = "std")]
use std::error;
@@ -46,7 +49,6 @@ const BENCHMARKING_FN: &str = "\
For that reason client started transactions before calling into runtime are not allowed.
Without client transactions the loop condition garantuees the success of the tx close.";
#[cfg(feature = "std")]
fn guard() -> sp_panic_handler::AbortGuard {
sp_panic_handler::AbortGuard::force_abort()
@@ -91,10 +93,10 @@ impl<B: error::Error, E: error::Error> error::Error for Error<B, E> {
/// Wraps a read-only backend, call executor, and current overlayed changes.
pub struct Ext<'a, H, N, B>
where
H: Hasher,
B: 'a + Backend<H>,
N: crate::changes_trie::BlockNumber,
where
H: Hasher,
B: 'a + Backend<H>,
N: crate::changes_trie::BlockNumber,
{
/// The overlayed changes to write to.
overlay: &'a mut OverlayedChanges,
@@ -114,12 +116,11 @@ pub struct Ext<'a, H, N, B>
extensions: Option<OverlayedExtensions<'a>>,
}
impl<'a, H, N, B> Ext<'a, H, N, B>
where
H: Hasher,
B: Backend<H>,
N: crate::changes_trie::BlockNumber,
where
H: Hasher,
B: Backend<H>,
N: crate::changes_trie::BlockNumber,
{
/// Create a new `Ext`.
#[cfg(not(feature = "std"))]
@@ -128,13 +129,7 @@ impl<'a, H, N, B> Ext<'a, H, N, B>
storage_transaction_cache: &'a mut StorageTransactionCache<B::Transaction, H, N>,
backend: &'a B,
) -> Self {
Ext {
overlay,
backend,
id: 0,
storage_transaction_cache,
_phantom: Default::default(),
}
Ext { overlay, backend, id: 0, storage_transaction_cache, _phantom: Default::default() }
}
/// Create a new `Ext` from overlayed changes and read-only backend
@@ -176,7 +171,9 @@ where
pub fn storage_pairs(&self) -> Vec<(StorageKey, StorageValue)> {
use std::collections::HashMap;
self.backend.pairs().iter()
self.backend
.pairs()
.iter()
.map(|&(ref k, ref v)| (k.to_vec(), Some(v.to_vec())))
.chain(self.overlay.changes().map(|(k, v)| (k.clone(), v.value().cloned())))
.collect::<HashMap<_, _>>()
@@ -199,8 +196,11 @@ where
fn storage(&self, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self.overlay.storage(key).map(|x| x.map(|x| x.to_vec())).unwrap_or_else(||
self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL));
let result = self
.overlay
.storage(key)
.map(|x| x.map(|x| x.to_vec()))
.unwrap_or_else(|| self.backend.storage(key).expect(EXT_NOT_ALLOWED_TO_FAIL));
// NOTE: be careful about touching the key names used outside substrate!
trace!(
@@ -222,7 +222,8 @@ where
fn storage_hash(&self, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self.overlay
let result = self
.overlay
.storage(key)
.map(|x| x.map(|x| H::hash(x)))
.unwrap_or_else(|| self.backend.storage_hash(key).expect(EXT_NOT_ALLOWED_TO_FAIL));
@@ -235,19 +236,15 @@ where
result.map(|r| r.encode())
}
fn child_storage(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> Option<StorageValue> {
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageValue> {
let _guard = guard();
let result = self.overlay
let result = self
.overlay
.child_storage(child_info, key)
.map(|x| x.map(|x| x.to_vec()))
.unwrap_or_else(||
self.backend.child_storage(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL)
);
.unwrap_or_else(|| {
self.backend.child_storage(child_info, key).expect(EXT_NOT_ALLOWED_TO_FAIL)
});
trace!(target: "state", "{:04x}: GetChild({}) {}={:?}",
self.id,
@@ -259,19 +256,15 @@ where
result
}
fn child_storage_hash(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> Option<Vec<u8>> {
fn child_storage_hash(&self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>> {
let _guard = guard();
let result = self.overlay
let result = self
.overlay
.child_storage(child_info, key)
.map(|x| x.map(|x| H::hash(x)))
.unwrap_or_else(||
self.backend.child_storage_hash(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL)
);
.unwrap_or_else(|| {
self.backend.child_storage_hash(child_info, key).expect(EXT_NOT_ALLOWED_TO_FAIL)
});
trace!(target: "state", "{:04x}: ChildHash({}) {}={:?}",
self.id,
@@ -299,16 +292,13 @@ where
result
}
fn exists_child_storage(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> bool {
fn exists_child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> bool {
let _guard = guard();
let result = match self.overlay.child_storage(child_info, key) {
Some(x) => x.is_some(),
_ => self.backend
_ => self
.backend
.exists_child_storage(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL),
};
@@ -323,7 +313,8 @@ where
}
fn next_storage_key(&self, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key = self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut next_backend_key =
self.backend.next_storage_key(key).expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes = self.overlay.iter_after(key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
@@ -343,9 +334,10 @@ where
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self.backend.next_storage_key(
&overlay_key.0,
).expect(EXT_NOT_ALLOWED_TO_FAIL);
next_backend_key = self
.backend
.next_storage_key(&overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
@@ -358,18 +350,13 @@ where
}
}
fn next_child_storage_key(
&self,
child_info: &ChildInfo,
key: &[u8],
) -> Option<StorageKey> {
let mut next_backend_key = self.backend
fn next_child_storage_key(&self, child_info: &ChildInfo, key: &[u8]) -> Option<StorageKey> {
let mut next_backend_key = self
.backend
.next_child_storage_key(child_info, key)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
let mut overlay_changes = self.overlay.child_iter_after(
child_info.storage_key(),
key
).peekable();
let mut overlay_changes =
self.overlay.child_iter_after(child_info.storage_key(), key).peekable();
match (&next_backend_key, overlay_changes.peek()) {
(_, None) => next_backend_key,
@@ -388,10 +375,10 @@ where
// If the `backend_key` and `overlay_key` are equal, it means that we need
// to search for the next backend key, because the overlay has overwritten
// this key.
next_backend_key = self.backend.next_child_storage_key(
child_info,
&overlay_key.0,
).expect(EXT_NOT_ALLOWED_TO_FAIL);
next_backend_key = self
.backend
.next_child_storage_key(child_info, &overlay_key.0)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
}
}
@@ -408,7 +395,7 @@ where
let _guard = guard();
if is_child_storage_key(&key) {
warn!(target: "trie", "Refuse to directly set child storage key");
return;
return
}
// NOTE: be careful about touching the key names used outside substrate!
@@ -448,11 +435,7 @@ where
self.overlay.set_child_storage(child_info, key, value);
}
fn kill_child_storage(
&mut self,
child_info: &ChildInfo,
limit: Option<u32>,
) -> (bool, u32) {
fn kill_child_storage(&mut self, child_info: &ChildInfo, limit: Option<u32>) -> (bool, u32) {
trace!(target: "state", "{:04x}: KillChild({})",
self.id,
HexDisplay::from(&child_info.storage_key()),
@@ -472,7 +455,7 @@ where
if sp_core::storage::well_known_keys::starts_with_child_storage_key(prefix) {
warn!(target: "trie", "Refuse to directly clear prefix that is part or contains of child storage key");
return (false, 0);
return (false, 0)
}
self.mark_dirty();
@@ -498,11 +481,7 @@ where
self.limit_remove_from_backend(Some(child_info), Some(prefix), limit)
}
fn storage_append(
&mut self,
key: Vec<u8>,
value: Vec<u8>,
) {
fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>) {
trace!(target: "state", "{:04x}: Append {}={}",
self.id,
HexDisplay::from(&key),
@@ -513,10 +492,9 @@ where
self.mark_dirty();
let backend = &mut self.backend;
let current_value = self.overlay.value_mut_or_insert_with(
&key,
|| backend.storage(&key).expect(EXT_NOT_ALLOWED_TO_FAIL).unwrap_or_default()
);
let current_value = self.overlay.value_mut_or_insert_with(&key, || {
backend.storage(&key).expect(EXT_NOT_ALLOWED_TO_FAIL).unwrap_or_default()
});
StorageAppend::new(current_value).append(value);
}
@@ -527,7 +505,7 @@ where
self.id,
HexDisplay::from(&root.as_ref()),
);
return root.encode();
return root.encode()
}
let root = self.overlay.storage_root(self.backend, self.storage_transaction_cache);
@@ -535,10 +513,7 @@ where
root.encode()
}
fn child_storage_root(
&mut self,
child_info: &ChildInfo,
) -> Vec<u8> {
fn child_storage_root(&mut self, child_info: &ChildInfo) -> Vec<u8> {
let _guard = guard();
let storage_key = child_info.storage_key();
let prefixed_storage_key = child_info.prefixed_storage_key();
@@ -546,9 +521,7 @@ where
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
.unwrap_or_else(
|| empty_child_trie_root::<Layout<H>>()
);
.unwrap_or_else(|| empty_child_trie_root::<Layout<H>>());
trace!(target: "state", "{:04x}: ChildRoot({})(cached) {}",
self.id,
HexDisplay::from(&storage_key),
@@ -587,9 +560,7 @@ where
let root = self
.storage(prefixed_storage_key.as_slice())
.and_then(|k| Decode::decode(&mut &k[..]).ok())
.unwrap_or_else(
|| empty_child_trie_root::<Layout<H>>()
);
.unwrap_or_else(|| empty_child_trie_root::<Layout<H>>());
trace!(target: "state", "{:04x}: ChildRoot({})(no_change) {}",
self.id,
HexDisplay::from(&storage_key.as_ref()),
@@ -625,10 +596,8 @@ where
index,
HexDisplay::from(&hash),
);
self.overlay.add_transaction_index(IndexOperation::Renew {
extrinsic: index,
hash: hash.to_vec(),
});
self.overlay
.add_transaction_index(IndexOperation::Renew { extrinsic: index, hash: hash.to_vec() });
}
#[cfg(not(feature = "std"))]
@@ -639,7 +608,8 @@ where
#[cfg(feature = "std")]
fn storage_changes_root(&mut self, mut parent_hash: &[u8]) -> Result<Option<Vec<u8>>, ()> {
let _guard = guard();
if let Some(ref root) = self.storage_transaction_cache.changes_trie_transaction_storage_root {
if let Some(ref root) = self.storage_transaction_cache.changes_trie_transaction_storage_root
{
trace!(
target: "state",
"{:04x}: ChangesRoot({})(cached) {:?}",
@@ -653,13 +623,13 @@ where
let root = self.overlay.changes_trie_root(
self.backend,
self.changes_trie_state.as_ref(),
Decode::decode(&mut parent_hash).map_err(|e|
Decode::decode(&mut parent_hash).map_err(|e| {
trace!(
target: "state",
"Failed to decode changes root parent hash: {}",
e,
)
)?,
})?,
true,
self.storage_transaction_cache,
);
@@ -693,13 +663,15 @@ where
for _ in 0..self.overlay.transaction_depth() {
self.overlay.rollback_transaction().expect(BENCHMARKING_FN);
}
self.overlay.drain_storage_changes(
self.backend,
#[cfg(feature = "std")]
None,
Default::default(),
self.storage_transaction_cache,
).expect(EXT_NOT_ALLOWED_TO_FAIL);
self.overlay
.drain_storage_changes(
self.backend,
#[cfg(feature = "std")]
None,
Default::default(),
self.storage_transaction_cache,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.wipe().expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
@@ -711,19 +683,24 @@ where
for _ in 0..self.overlay.transaction_depth() {
self.overlay.commit_transaction().expect(BENCHMARKING_FN);
}
let changes = self.overlay.drain_storage_changes(
self.backend,
#[cfg(feature = "std")]
None,
Default::default(),
self.storage_transaction_cache,
).expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
).expect(EXT_NOT_ALLOWED_TO_FAIL);
let changes = self
.overlay
.drain_storage_changes(
self.backend,
#[cfg(feature = "std")]
None,
Default::default(),
self.storage_transaction_cache,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.backend
.commit(
changes.transaction_storage_root,
changes.transaction,
changes.main_storage_changes,
changes.child_storage_changes,
)
.expect(EXT_NOT_ALLOWED_TO_FAIL);
self.mark_dirty();
self.overlay
.enter_runtime()
@@ -775,13 +752,13 @@ where
self.backend.apply_to_keys_while(child_info, prefix, |key| {
if num_deleted == limit {
all_deleted = false;
return false;
return false
}
if let Some(num) = num_deleted.checked_add(1) {
num_deleted = num;
} else {
all_deleted = false;
return false;
return false
}
if let Some(child_info) = child_info {
self.overlay.set_child_storage(child_info, key.to_vec(), None);
@@ -840,7 +817,7 @@ impl<'a> StorageAppend<'a> {
"Failed to append value, resetting storage item to `[value]`.",
);
value.encode()
}
},
};
}
}
@@ -896,7 +873,10 @@ where
}
}
fn deregister_extension_by_type_id(&mut self, type_id: TypeId) -> Result<(), sp_externalities::Error> {
fn deregister_extension_by_type_id(
&mut self,
type_id: TypeId,
) -> Result<(), sp_externalities::Error> {
if let Some(ref mut extensions) = self.extensions {
if extensions.deregister(type_id) {
Ok(())
@@ -912,24 +892,19 @@ where
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
use num_traits::Zero;
use codec::Encode;
use sp_core::{
H256,
Blake2Hasher,
map,
storage::{
Storage,
StorageChild,
well_known_keys::EXTRINSIC_INDEX,
},
};
use crate::{
changes_trie::{
Configuration as ChangesTrieConfiguration,
InMemoryStorage as TestChangesTrieStorage,
}, InMemoryBackend,
Configuration as ChangesTrieConfiguration, InMemoryStorage as TestChangesTrieStorage,
},
InMemoryBackend,
};
use codec::Encode;
use hex_literal::hex;
use num_traits::Zero;
use sp_core::{
map,
storage::{well_known_keys::EXTRINSIC_INDEX, Storage, StorageChild},
Blake2Hasher, H256,
};
type TestBackend = InMemoryBackend<Blake2Hasher>;
@@ -947,10 +922,7 @@ mod tests {
}
fn changes_trie_config() -> ChangesTrieConfiguration {
ChangesTrieConfiguration {
digest_interval: 0,
digest_levels: 0,
}
ChangesTrieConfiguration { digest_interval: 0, digest_levels: 0 }
}
#[test]
@@ -1013,8 +985,9 @@ mod tests {
vec![20] => vec![20],
vec![40] => vec![40]
],
children_default: map![]
}.into();
children_default: map![],
}
.into();
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
@@ -1056,8 +1029,9 @@ mod tests {
top: map![
vec![30] => vec![30]
],
children_default: map![]
}.into();
children_default: map![],
}
.into();
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
@@ -1087,7 +1061,8 @@ mod tests {
child_info: child_info.to_owned(),
}
],
}.into();
}
.into();
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
@@ -1131,7 +1106,8 @@ mod tests {
child_info: child_info.to_owned(),
}
],
}.into();
}
.into();
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);
@@ -1142,10 +1118,7 @@ mod tests {
);
assert_eq!(ext.child_storage(child_info, &[20]), None);
assert_eq!(
ext.child_storage_hash(child_info, &[20]),
None,
);
assert_eq!(ext.child_storage_hash(child_info, &[20]), None,);
assert_eq!(ext.child_storage(child_info, &[30]), Some(vec![31]));
assert_eq!(
@@ -1170,7 +1143,8 @@ mod tests {
child_info: child_info.to_owned(),
}
],
}.into();
}
.into();
let ext = TestExt::new(&mut overlay, &mut cache, &backend, None, None);