mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 18:07:58 +00:00
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:
@@ -17,17 +17,19 @@
|
||||
|
||||
//! Test implementation for Externalities.
|
||||
|
||||
use std::{any::{Any, TypeId}, panic::{AssertUnwindSafe, UnwindSafe}};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
panic::{AssertUnwindSafe, UnwindSafe},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
backend::Backend, OverlayedChanges, StorageTransactionCache, ext::Ext, InMemoryBackend,
|
||||
StorageKey, StorageValue,
|
||||
backend::Backend,
|
||||
changes_trie::{
|
||||
Configuration as ChangesTrieConfiguration,
|
||||
InMemoryStorage as ChangesTrieInMemoryStorage,
|
||||
BlockNumber as ChangesTrieBlockNumber,
|
||||
State as ChangesTrieState,
|
||||
BlockNumber as ChangesTrieBlockNumber, Configuration as ChangesTrieConfiguration,
|
||||
InMemoryStorage as ChangesTrieInMemoryStorage, State as ChangesTrieState,
|
||||
},
|
||||
ext::Ext,
|
||||
InMemoryBackend, OverlayedChanges, StorageKey, StorageTransactionCache, StorageValue,
|
||||
};
|
||||
|
||||
use codec::Decode;
|
||||
@@ -35,13 +37,13 @@ use hash_db::Hasher;
|
||||
use sp_core::{
|
||||
offchain::testing::TestPersistentOffchainDB,
|
||||
storage::{
|
||||
well_known_keys::{CHANGES_TRIE_CONFIG, CODE, is_child_storage_key},
|
||||
well_known_keys::{is_child_storage_key, CHANGES_TRIE_CONFIG, CODE},
|
||||
Storage,
|
||||
},
|
||||
traits::TaskExecutorExt,
|
||||
testing::TaskExecutor,
|
||||
traits::TaskExecutorExt,
|
||||
};
|
||||
use sp_externalities::{Extensions, Extension, ExtensionStore};
|
||||
use sp_externalities::{Extension, ExtensionStore, Extensions};
|
||||
|
||||
/// Simple HashMap-based Externalities impl.
|
||||
pub struct TestExternalities<H: Hasher, N: ChangesTrieBlockNumber = u64>
|
||||
@@ -96,7 +98,9 @@ where
|
||||
/// Create a new instance of `TestExternalities` with code and storage.
|
||||
pub fn new_with_code(code: &[u8], mut storage: Storage) -> Self {
|
||||
let mut overlay = OverlayedChanges::default();
|
||||
let changes_trie_config = storage.top.get(CHANGES_TRIE_CONFIG)
|
||||
let changes_trie_config = storage
|
||||
.top
|
||||
.get(CHANGES_TRIE_CONFIG)
|
||||
.and_then(|v| Decode::decode(&mut &v[..]).ok());
|
||||
overlay.set_collect_extrinsics(changes_trie_config.is_some());
|
||||
|
||||
@@ -156,17 +160,14 @@ where
|
||||
/// In contrast to [`commit_all`](Self::commit_all) this will not panic if there are open
|
||||
/// transactions.
|
||||
fn as_backend(&self) -> InMemoryBackend<H> {
|
||||
let top: Vec<_> = self.overlay.changes()
|
||||
.map(|(k, v)| (k.clone(), v.value().cloned()))
|
||||
.collect();
|
||||
let top: Vec<_> =
|
||||
self.overlay.changes().map(|(k, v)| (k.clone(), v.value().cloned())).collect();
|
||||
let mut transaction = vec![(None, top)];
|
||||
|
||||
for (child_changes, child_info) in self.overlay.children() {
|
||||
transaction.push((
|
||||
Some(child_info.clone()),
|
||||
child_changes
|
||||
.map(|(k, v)| (k.clone(), v.value().cloned()))
|
||||
.collect(),
|
||||
child_changes.map(|(k, v)| (k.clone(), v.value().cloned())).collect(),
|
||||
))
|
||||
}
|
||||
|
||||
@@ -186,7 +187,8 @@ where
|
||||
&mut Default::default(),
|
||||
)?;
|
||||
|
||||
self.backend.apply_transaction(changes.transaction_storage_root, changes.transaction);
|
||||
self.backend
|
||||
.apply_transaction(changes.transaction_storage_root, changes.transaction);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -202,18 +204,21 @@ where
|
||||
///
|
||||
/// Returns the result of the given closure, if no panics occured.
|
||||
/// Otherwise, returns `Err`.
|
||||
pub fn execute_with_safe<R>(&mut self, f: impl FnOnce() -> R + UnwindSafe) -> Result<R, String> {
|
||||
pub fn execute_with_safe<R>(
|
||||
&mut self,
|
||||
f: impl FnOnce() -> R + UnwindSafe,
|
||||
) -> Result<R, String> {
|
||||
let mut ext = AssertUnwindSafe(self.ext());
|
||||
std::panic::catch_unwind(move ||
|
||||
std::panic::catch_unwind(move || {
|
||||
sp_externalities::set_and_run_with_externalities(&mut *ext, f)
|
||||
).map_err(|e| {
|
||||
format!("Closure panicked: {:?}", e)
|
||||
})
|
||||
.map_err(|e| format!("Closure panicked: {:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: Hasher, N: ChangesTrieBlockNumber> std::fmt::Debug for TestExternalities<H, N>
|
||||
where H::Out: Ord + codec::Codec,
|
||||
where
|
||||
H::Out: Ord + codec::Codec,
|
||||
{
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "overlay: {:?}\nbackend: {:?}", self.overlay, self.backend.pairs())
|
||||
@@ -221,8 +226,8 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> std::fmt::Debug for TestExternalities
|
||||
}
|
||||
|
||||
impl<H: Hasher, N: ChangesTrieBlockNumber> PartialEq for TestExternalities<H, N>
|
||||
where
|
||||
H::Out: Ord + 'static + codec::Codec
|
||||
where
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
{
|
||||
/// This doesn't test if they are in the same state, only if they contains the
|
||||
/// same data at this state
|
||||
@@ -232,22 +237,25 @@ impl<H: Hasher, N: ChangesTrieBlockNumber> PartialEq for TestExternalities<H, N>
|
||||
}
|
||||
|
||||
impl<H: Hasher, N: ChangesTrieBlockNumber> Default for TestExternalities<H, N>
|
||||
where
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
where
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
{
|
||||
fn default() -> Self { Self::new(Default::default()) }
|
||||
fn default() -> Self {
|
||||
Self::new(Default::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<H: Hasher, N: ChangesTrieBlockNumber> From<Storage> for TestExternalities<H, N>
|
||||
where
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
where
|
||||
H::Out: Ord + 'static + codec::Codec,
|
||||
{
|
||||
fn from(storage: Storage) -> Self {
|
||||
Self::new(storage)
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, N> sp_externalities::ExtensionStore for TestExternalities<H, N> where
|
||||
impl<H, N> sp_externalities::ExtensionStore for TestExternalities<H, N>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + codec::Codec,
|
||||
N: ChangesTrieBlockNumber,
|
||||
@@ -264,7 +272,10 @@ impl<H, N> sp_externalities::ExtensionStore for TestExternalities<H, N> where
|
||||
self.extensions.register_with_type_id(type_id, extension)
|
||||
}
|
||||
|
||||
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 self.extensions.deregister(type_id) {
|
||||
Ok(())
|
||||
} else {
|
||||
@@ -274,14 +285,13 @@ impl<H, N> sp_externalities::ExtensionStore for TestExternalities<H, N> where
|
||||
}
|
||||
|
||||
impl<H, N> sp_externalities::ExternalitiesExt for TestExternalities<H, N>
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + codec::Codec,
|
||||
N: ChangesTrieBlockNumber,
|
||||
where
|
||||
H: Hasher,
|
||||
H::Out: Ord + codec::Codec,
|
||||
N: ChangesTrieBlockNumber,
|
||||
{
|
||||
fn extension<T: Any + Extension>(&mut self) -> Option<&mut T> {
|
||||
self.extension_by_type_id(TypeId::of::<T>())
|
||||
.and_then(<dyn Any>::downcast_mut)
|
||||
self.extension_by_type_id(TypeId::of::<T>()).and_then(<dyn Any>::downcast_mut)
|
||||
}
|
||||
|
||||
fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), sp_externalities::Error> {
|
||||
@@ -296,9 +306,9 @@ impl<H, N> sp_externalities::ExternalitiesExt for TestExternalities<H, N>
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_core::{H256, traits::Externalities, storage::ChildInfo};
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
use hex_literal::hex;
|
||||
use sp_core::{storage::ChildInfo, traits::Externalities, H256};
|
||||
use sp_runtime::traits::BlakeTwo256;
|
||||
|
||||
#[test]
|
||||
fn commit_should_work() {
|
||||
@@ -307,7 +317,8 @@ mod tests {
|
||||
ext.set_storage(b"doe".to_vec(), b"reindeer".to_vec());
|
||||
ext.set_storage(b"dog".to_vec(), b"puppy".to_vec());
|
||||
ext.set_storage(b"dogglesworth".to_vec(), b"cat".to_vec());
|
||||
let root = H256::from(hex!("ed4d8c799d996add422395a6abd7545491d40bd838d738afafa1b8a4de625489"));
|
||||
let root =
|
||||
H256::from(hex!("ed4d8c799d996add422395a6abd7545491d40bd838d738afafa1b8a4de625489"));
|
||||
assert_eq!(H256::from_slice(ext.storage_root().as_slice()), root);
|
||||
}
|
||||
|
||||
@@ -325,7 +336,7 @@ mod tests {
|
||||
#[test]
|
||||
fn check_send() {
|
||||
fn assert_send<T: Send>() {}
|
||||
assert_send::<TestExternalities::<BlakeTwo256, u64>>();
|
||||
assert_send::<TestExternalities<BlakeTwo256, u64>>();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user