mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-04 02:17:23 +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:
@@ -19,38 +19,44 @@
|
||||
//! Light client backend. Only stores headers and justifications of blocks.
|
||||
//! Everything else is requested from full nodes on demand.
|
||||
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::sync::Arc;
|
||||
use parking_lot::RwLock;
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
|
||||
use sp_core::ChangesTrieConfiguration;
|
||||
use sp_core::storage::{well_known_keys, ChildInfo};
|
||||
use sp_core::offchain::storage::InMemOffchainStorage;
|
||||
use sp_state_machine::{
|
||||
Backend as StateBackend, TrieBackend, InMemoryBackend, ChangesTrieTransaction,
|
||||
StorageCollection, ChildStorageCollection, IndexOperation,
|
||||
};
|
||||
use sp_runtime::{generic::BlockId, Justification, Justifications, Storage};
|
||||
use sp_runtime::traits::{Block as BlockT, NumberFor, Zero, Header, HashFor};
|
||||
use sp_blockchain::{Error as ClientError, Result as ClientResult};
|
||||
use sc_client_api::{
|
||||
backend::{
|
||||
AuxStore, Backend as ClientBackend, BlockImportOperation, RemoteBackend, NewBlockState,
|
||||
PrunableStateChangesTrieStorage,
|
||||
},
|
||||
blockchain::{
|
||||
HeaderBackend as BlockchainHeaderBackend, well_known_cache_keys,
|
||||
},
|
||||
light::Storage as BlockchainStorage,
|
||||
in_mem::check_genesis_storage,
|
||||
UsageInfo,
|
||||
};
|
||||
use super::blockchain::Blockchain;
|
||||
use hash_db::Hasher;
|
||||
use sc_client_api::{
|
||||
backend::{
|
||||
AuxStore, Backend as ClientBackend, BlockImportOperation, NewBlockState,
|
||||
PrunableStateChangesTrieStorage, RemoteBackend,
|
||||
},
|
||||
blockchain::{well_known_cache_keys, HeaderBackend as BlockchainHeaderBackend},
|
||||
in_mem::check_genesis_storage,
|
||||
light::Storage as BlockchainStorage,
|
||||
UsageInfo,
|
||||
};
|
||||
use sp_blockchain::{Error as ClientError, Result as ClientResult};
|
||||
use sp_core::{
|
||||
offchain::storage::InMemOffchainStorage,
|
||||
storage::{well_known_keys, ChildInfo},
|
||||
ChangesTrieConfiguration,
|
||||
};
|
||||
use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, HashFor, Header, NumberFor, Zero},
|
||||
Justification, Justifications, Storage,
|
||||
};
|
||||
use sp_state_machine::{
|
||||
Backend as StateBackend, ChangesTrieTransaction, ChildStorageCollection, InMemoryBackend,
|
||||
IndexOperation, StorageCollection, TrieBackend,
|
||||
};
|
||||
|
||||
const IN_MEMORY_EXPECT_PROOF: &str = "InMemory state backend has Void error type and always succeeds; qed";
|
||||
const IN_MEMORY_EXPECT_PROOF: &str =
|
||||
"InMemory state backend has Void error type and always succeeds; qed";
|
||||
|
||||
/// Light client backend.
|
||||
pub struct Backend<S, H: Hasher> {
|
||||
@@ -84,11 +90,7 @@ pub enum GenesisOrUnavailableState<H: Hasher> {
|
||||
impl<S, H: Hasher> Backend<S, H> {
|
||||
/// Create new light backend.
|
||||
pub fn new(blockchain: Arc<Blockchain<S>>) -> Self {
|
||||
Self {
|
||||
blockchain,
|
||||
genesis_state: RwLock::new(None),
|
||||
import_lock: Default::default(),
|
||||
}
|
||||
Self { blockchain, genesis_state: RwLock::new(None), import_lock: Default::default() }
|
||||
}
|
||||
|
||||
/// Get shared blockchain reference.
|
||||
@@ -102,9 +104,13 @@ impl<S: AuxStore, H: Hasher> AuxStore for Backend<S, H> {
|
||||
'a,
|
||||
'b: 'a,
|
||||
'c: 'a,
|
||||
I: IntoIterator<Item=&'a(&'c [u8], &'c [u8])>,
|
||||
D: IntoIterator<Item=&'a &'b [u8]>,
|
||||
>(&self, insert: I, delete: D) -> ClientResult<()> {
|
||||
I: IntoIterator<Item = &'a (&'c [u8], &'c [u8])>,
|
||||
D: IntoIterator<Item = &'a &'b [u8]>,
|
||||
>(
|
||||
&self,
|
||||
insert: I,
|
||||
delete: D,
|
||||
) -> ClientResult<()> {
|
||||
self.blockchain.storage().insert_aux(insert, delete)
|
||||
}
|
||||
|
||||
@@ -114,10 +120,10 @@ impl<S: AuxStore, H: Hasher> AuxStore for Backend<S, H> {
|
||||
}
|
||||
|
||||
impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
where
|
||||
Block: BlockT,
|
||||
S: BlockchainStorage<Block>,
|
||||
Block::Hash: Ord,
|
||||
where
|
||||
Block: BlockT,
|
||||
S: BlockchainStorage<Block>,
|
||||
Block::Hash: Ord,
|
||||
{
|
||||
type BlockImportOperation = ImportOperation<Block, S>;
|
||||
type Blockchain = Blockchain<S>;
|
||||
@@ -141,15 +147,12 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
fn begin_state_operation(
|
||||
&self,
|
||||
_operation: &mut Self::BlockImportOperation,
|
||||
_block: BlockId<Block>
|
||||
_block: BlockId<Block>,
|
||||
) -> ClientResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn commit_operation(
|
||||
&self,
|
||||
mut operation: Self::BlockImportOperation,
|
||||
) -> ClientResult<()> {
|
||||
fn commit_operation(&self, mut operation: Self::BlockImportOperation) -> ClientResult<()> {
|
||||
if !operation.finalized_blocks.is_empty() {
|
||||
for block in operation.finalized_blocks {
|
||||
self.blockchain.storage().finalize_header(block)?;
|
||||
@@ -159,7 +162,9 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
if let Some(header) = operation.header {
|
||||
let is_genesis_import = header.number().is_zero();
|
||||
if let Some(new_config) = operation.changes_trie_config_update {
|
||||
operation.cache.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_config.encode());
|
||||
operation
|
||||
.cache
|
||||
.insert(well_known_cache_keys::CHANGES_TRIE_CONFIG, new_config.encode());
|
||||
}
|
||||
self.blockchain.storage().import_header(
|
||||
header,
|
||||
@@ -175,11 +180,12 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
} else {
|
||||
for (key, maybe_val) in operation.aux_ops {
|
||||
match maybe_val {
|
||||
Some(val) => self.blockchain.storage().insert_aux(
|
||||
&[(&key[..], &val[..])],
|
||||
std::iter::empty(),
|
||||
)?,
|
||||
None => self.blockchain.storage().insert_aux(std::iter::empty(), &[&key[..]])?,
|
||||
Some(val) => self
|
||||
.blockchain
|
||||
.storage()
|
||||
.insert_aux(&[(&key[..], &val[..])], std::iter::empty())?,
|
||||
None =>
|
||||
self.blockchain.storage().insert_aux(std::iter::empty(), &[&key[..]])?,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,7 +235,7 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
// special case for genesis block
|
||||
if block_number.is_zero() {
|
||||
if let Some(genesis_state) = self.genesis_state.read().clone() {
|
||||
return Ok(GenesisOrUnavailableState::Genesis(genesis_state));
|
||||
return Ok(GenesisOrUnavailableState::Genesis(genesis_state))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,10 +252,7 @@ impl<S, Block> ClientBackend<Block> for Backend<S, HashFor<Block>>
|
||||
Err(ClientError::NotAvailableOnLightClient)
|
||||
}
|
||||
|
||||
fn remove_leaf_block(
|
||||
&self,
|
||||
_hash: &Block::Hash,
|
||||
) -> ClientResult<()> {
|
||||
fn remove_leaf_block(&self, _hash: &Block::Hash) -> ClientResult<()> {
|
||||
Err(ClientError::NotAvailableOnLightClient)
|
||||
}
|
||||
|
||||
@@ -265,8 +268,9 @@ where
|
||||
Block::Hash: Ord,
|
||||
{
|
||||
fn is_local_state_available(&self, block: &BlockId<Block>) -> bool {
|
||||
self.genesis_state.read().is_some()
|
||||
&& self.blockchain.expect_block_number_from_id(block)
|
||||
self.genesis_state.read().is_some() &&
|
||||
self.blockchain
|
||||
.expect_block_number_from_id(block)
|
||||
.map(|num| num.is_zero())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
@@ -277,10 +281,10 @@ where
|
||||
}
|
||||
|
||||
impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
where
|
||||
Block: BlockT,
|
||||
S: BlockchainStorage<Block>,
|
||||
Block::Hash: Ord,
|
||||
where
|
||||
Block: BlockT,
|
||||
S: BlockchainStorage<Block>,
|
||||
Block::Hash: Ord,
|
||||
{
|
||||
type State = GenesisOrUnavailableState<HashFor<Block>>;
|
||||
|
||||
@@ -326,10 +330,14 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
check_genesis_storage(&input)?;
|
||||
|
||||
// changes trie configuration
|
||||
let changes_trie_config = input.top.iter()
|
||||
let changes_trie_config = input
|
||||
.top
|
||||
.iter()
|
||||
.find(|(k, _)| &k[..] == well_known_keys::CHANGES_TRIE_CONFIG)
|
||||
.map(|(_, v)| Decode::decode(&mut &v[..])
|
||||
.expect("changes trie configuration is encoded properly at genesis"));
|
||||
.map(|(_, v)| {
|
||||
Decode::decode(&mut &v[..])
|
||||
.expect("changes trie configuration is encoded properly at genesis")
|
||||
});
|
||||
self.changes_trie_config_update = Some(changes_trie_config);
|
||||
|
||||
// this is only called when genesis block is imported => shouldn't be performance bottleneck
|
||||
@@ -337,7 +345,8 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
storage.insert(None, input.top);
|
||||
|
||||
// create a list of children keys to re-compute roots for
|
||||
let child_delta = input.children_default
|
||||
let child_delta = input
|
||||
.children_default
|
||||
.iter()
|
||||
.map(|(_storage_key, storage_child)| (&storage_child.child_info, std::iter::empty()));
|
||||
|
||||
@@ -360,7 +369,8 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
}
|
||||
|
||||
fn insert_aux<I>(&mut self, ops: I) -> ClientResult<()>
|
||||
where I: IntoIterator<Item=(Vec<u8>, Option<Vec<u8>>)>
|
||||
where
|
||||
I: IntoIterator<Item = (Vec<u8>, Option<Vec<u8>>)>,
|
||||
{
|
||||
self.aux_ops.append(&mut ops.into_iter().collect());
|
||||
Ok(())
|
||||
@@ -389,7 +399,10 @@ impl<S, Block> BlockImportOperation<Block> for ImportOperation<Block, S>
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_transaction_index(&mut self, _index: Vec<IndexOperation>) -> sp_blockchain::Result<()> {
|
||||
fn update_transaction_index(
|
||||
&mut self,
|
||||
_index: Vec<IndexOperation>,
|
||||
) -> sp_blockchain::Result<()> {
|
||||
// noop for the light client
|
||||
Ok(())
|
||||
}
|
||||
@@ -405,8 +418,8 @@ impl<H: Hasher> std::fmt::Debug for GenesisOrUnavailableState<H> {
|
||||
}
|
||||
|
||||
impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
where
|
||||
H::Out: Ord + codec::Codec,
|
||||
where
|
||||
H::Out: Ord + codec::Codec,
|
||||
{
|
||||
type Error = ClientError;
|
||||
type Transaction = <InMemoryBackend<H> as StateBackend<H>>::Transaction;
|
||||
@@ -420,11 +433,7 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
}
|
||||
}
|
||||
|
||||
fn child_storage(
|
||||
&self,
|
||||
child_info: &ChildInfo,
|
||||
key: &[u8],
|
||||
) -> ClientResult<Option<Vec<u8>>> {
|
||||
fn child_storage(&self, child_info: &ChildInfo, key: &[u8]) -> ClientResult<Option<Vec<u8>>> {
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) =>
|
||||
Ok(state.child_storage(child_info, key).expect(IN_MEMORY_EXPECT_PROOF)),
|
||||
@@ -446,24 +455,24 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
key: &[u8],
|
||||
) -> Result<Option<Vec<u8>>, Self::Error> {
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) => Ok(
|
||||
state.next_child_storage_key(child_info, key)
|
||||
.expect(IN_MEMORY_EXPECT_PROOF)
|
||||
),
|
||||
GenesisOrUnavailableState::Genesis(ref state) =>
|
||||
Ok(state.next_child_storage_key(child_info, key).expect(IN_MEMORY_EXPECT_PROOF)),
|
||||
GenesisOrUnavailableState::Unavailable => Err(ClientError::NotAvailableOnLightClient),
|
||||
}
|
||||
}
|
||||
|
||||
fn for_keys_with_prefix<A: FnMut(&[u8])>(&self, prefix: &[u8], action: A) {
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) => state.for_keys_with_prefix(prefix, action),
|
||||
GenesisOrUnavailableState::Genesis(ref state) =>
|
||||
state.for_keys_with_prefix(prefix, action),
|
||||
GenesisOrUnavailableState::Unavailable => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn for_key_values_with_prefix<A: FnMut(&[u8], &[u8])>(&self, prefix: &[u8], action: A) {
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) => state.for_key_values_with_prefix(prefix, action),
|
||||
GenesisOrUnavailableState::Genesis(ref state) =>
|
||||
state.for_key_values_with_prefix(prefix, action),
|
||||
GenesisOrUnavailableState::Unavailable => (),
|
||||
}
|
||||
}
|
||||
@@ -477,9 +486,9 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
allow_missing: bool,
|
||||
) -> ClientResult<bool> {
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) =>
|
||||
Ok(state.apply_to_key_values_while(child_info, prefix, start_at, action, allow_missing)
|
||||
.expect(IN_MEMORY_EXPECT_PROOF)),
|
||||
GenesisOrUnavailableState::Genesis(ref state) => Ok(state
|
||||
.apply_to_key_values_while(child_info, prefix, start_at, action, allow_missing)
|
||||
.expect(IN_MEMORY_EXPECT_PROOF)),
|
||||
GenesisOrUnavailableState::Unavailable => Err(ClientError::NotAvailableOnLightClient),
|
||||
}
|
||||
}
|
||||
@@ -512,11 +521,13 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
|
||||
fn storage_root<'a>(
|
||||
&self,
|
||||
delta: impl Iterator<Item=(&'a [u8], Option<&'a [u8]>)>,
|
||||
) -> (H::Out, Self::Transaction) where H::Out: Ord {
|
||||
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
|
||||
) -> (H::Out, Self::Transaction)
|
||||
where
|
||||
H::Out: Ord,
|
||||
{
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) =>
|
||||
state.storage_root(delta),
|
||||
GenesisOrUnavailableState::Genesis(ref state) => state.storage_root(delta),
|
||||
GenesisOrUnavailableState::Unavailable => Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -524,15 +535,17 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
fn child_storage_root<'a>(
|
||||
&self,
|
||||
child_info: &ChildInfo,
|
||||
delta: impl Iterator<Item=(&'a [u8], Option<&'a [u8]>)>,
|
||||
) -> (H::Out, bool, Self::Transaction) where H::Out: Ord {
|
||||
delta: impl Iterator<Item = (&'a [u8], Option<&'a [u8]>)>,
|
||||
) -> (H::Out, bool, Self::Transaction)
|
||||
where
|
||||
H::Out: Ord,
|
||||
{
|
||||
match *self {
|
||||
GenesisOrUnavailableState::Genesis(ref state) => {
|
||||
let (root, is_equal, _) = state.child_storage_root(child_info, delta);
|
||||
(root, is_equal, Default::default())
|
||||
},
|
||||
GenesisOrUnavailableState::Unavailable =>
|
||||
(H::Out::default(), true, Default::default()),
|
||||
GenesisOrUnavailableState::Unavailable => (H::Out::default(), true, Default::default()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -550,7 +563,7 @@ impl<H: Hasher> StateBackend<H> for GenesisOrUnavailableState<H>
|
||||
}
|
||||
}
|
||||
|
||||
fn register_overlay_stats(&self, _stats: &sp_state_machine::StateMachineStats) { }
|
||||
fn register_overlay_stats(&self, _stats: &sp_state_machine::StateMachineStats) {}
|
||||
|
||||
fn usage_info(&self) -> sp_state_machine::UsageInfo {
|
||||
sp_state_machine::UsageInfo::empty()
|
||||
|
||||
Reference in New Issue
Block a user