mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 22:51:13 +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,11 +19,13 @@
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
|
||||
use sp_runtime::generic::BlockId;
|
||||
use sp_runtime::Justifications;
|
||||
use log::warn;
|
||||
use parking_lot::RwLock;
|
||||
use sp_runtime::{
|
||||
generic::BlockId,
|
||||
traits::{Block as BlockT, Header as HeaderT, NumberFor},
|
||||
Justifications,
|
||||
};
|
||||
|
||||
use crate::header_metadata::HeaderMetadata;
|
||||
|
||||
@@ -38,7 +40,10 @@ pub trait HeaderBackend<Block: BlockT>: Send + Sync {
|
||||
/// Get block status.
|
||||
fn status(&self, id: BlockId<Block>) -> Result<BlockStatus>;
|
||||
/// Get block number by hash. Returns `None` if the header is not in the chain.
|
||||
fn number(&self, hash: Block::Hash) -> Result<Option<<<Block as BlockT>::Header as HeaderT>::Number>>;
|
||||
fn number(
|
||||
&self,
|
||||
hash: Block::Hash,
|
||||
) -> Result<Option<<<Block as BlockT>::Header as HeaderT>::Number>>;
|
||||
/// Get block hash by number. Returns `None` if the header is not in the chain.
|
||||
fn hash(&self, number: NumberFor<Block>) -> Result<Option<Block::Hash>>;
|
||||
|
||||
@@ -60,28 +65,29 @@ pub trait HeaderBackend<Block: BlockT>: Send + Sync {
|
||||
|
||||
/// Get block header. Returns `UnknownBlock` error if block is not found.
|
||||
fn expect_header(&self, id: BlockId<Block>) -> Result<Block::Header> {
|
||||
self.header(id)?.ok_or_else(|| Error::UnknownBlock(format!("Expect header: {}", id)))
|
||||
self.header(id)?
|
||||
.ok_or_else(|| Error::UnknownBlock(format!("Expect header: {}", id)))
|
||||
}
|
||||
|
||||
/// Convert an arbitrary block ID into a block number. Returns `UnknownBlock` error if block is not found.
|
||||
fn expect_block_number_from_id(&self, id: &BlockId<Block>) -> Result<NumberFor<Block>> {
|
||||
self.block_number_from_id(id)
|
||||
.and_then(|n| n.ok_or_else(||
|
||||
Error::UnknownBlock(format!("Expect block number from id: {}", id))
|
||||
))
|
||||
self.block_number_from_id(id).and_then(|n| {
|
||||
n.ok_or_else(|| Error::UnknownBlock(format!("Expect block number from id: {}", id)))
|
||||
})
|
||||
}
|
||||
|
||||
/// Convert an arbitrary block ID into a block hash. Returns `UnknownBlock` error if block is not found.
|
||||
fn expect_block_hash_from_id(&self, id: &BlockId<Block>) -> Result<Block::Hash> {
|
||||
self.block_hash_from_id(id)
|
||||
.and_then(|n| n.ok_or_else(||
|
||||
Error::UnknownBlock(format!("Expect block hash from id: {}", id))
|
||||
))
|
||||
self.block_hash_from_id(id).and_then(|n| {
|
||||
n.ok_or_else(|| Error::UnknownBlock(format!("Expect block hash from id: {}", id)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Blockchain database backend. Does not perform any validation.
|
||||
pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, Error=Error> {
|
||||
pub trait Backend<Block: BlockT>:
|
||||
HeaderBackend<Block> + HeaderMetadata<Block, Error = Error>
|
||||
{
|
||||
/// Get block body. Returns `None` if block is not found.
|
||||
fn body(&self, id: BlockId<Block>) -> Result<Option<Vec<<Block as BlockT>::Extrinsic>>>;
|
||||
/// Get block justifications. Returns `None` if no justification exists.
|
||||
@@ -120,14 +126,14 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
|
||||
match self.header(BlockId::Hash(target_hash))? {
|
||||
Some(x) => x,
|
||||
// target not in blockchain
|
||||
None => { return Ok(None); },
|
||||
None => return Ok(None),
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(max_number) = maybe_max_number {
|
||||
// target outside search range
|
||||
if target_header.number() > &max_number {
|
||||
return Ok(None);
|
||||
return Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,12 +154,12 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
|
||||
// provided, we continue to search from all leaves below.
|
||||
if let Some(max_number) = maybe_max_number {
|
||||
if let Some(header) = self.hash(max_number)? {
|
||||
return Ok(Some(header));
|
||||
return Ok(Some(header))
|
||||
}
|
||||
}
|
||||
} else if info.finalized_number >= *target_header.number() {
|
||||
// header is on a dead fork.
|
||||
return Ok(None);
|
||||
return Ok(None)
|
||||
}
|
||||
|
||||
self.leaves()?
|
||||
@@ -171,12 +177,13 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
|
||||
// waiting until we are <= max_number
|
||||
if let Some(max_number) = maybe_max_number {
|
||||
loop {
|
||||
let current_header = self.header(BlockId::Hash(current_hash.clone()))?
|
||||
let current_header = self
|
||||
.header(BlockId::Hash(current_hash.clone()))?
|
||||
.ok_or_else(|| Error::MissingHeader(current_hash.to_string()))?;
|
||||
|
||||
if current_header.number() <= &max_number {
|
||||
best_hash = current_header.hash();
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
current_hash = *current_header.parent_hash();
|
||||
@@ -187,15 +194,16 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
|
||||
loop {
|
||||
// until we find target
|
||||
if current_hash == target_hash {
|
||||
return Ok(Some(best_hash));
|
||||
return Ok(Some(best_hash))
|
||||
}
|
||||
|
||||
let current_header = self.header(BlockId::Hash(current_hash.clone()))?
|
||||
let current_header = self
|
||||
.header(BlockId::Hash(current_hash.clone()))?
|
||||
.ok_or_else(|| Error::MissingHeader(current_hash.to_string()))?;
|
||||
|
||||
// stop search in this chain once we go below the target's block number
|
||||
if current_header.number() < target_header.number() {
|
||||
break;
|
||||
break
|
||||
}
|
||||
|
||||
current_hash = *current_header.parent_hash();
|
||||
@@ -209,8 +217,7 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
|
||||
warn!(
|
||||
"Block {:?} exists in chain but not found when following all \
|
||||
leaves backwards. Number limit = {:?}",
|
||||
target_hash,
|
||||
maybe_max_number,
|
||||
target_hash, maybe_max_number,
|
||||
);
|
||||
|
||||
Ok(None)
|
||||
@@ -218,10 +225,7 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
|
||||
|
||||
/// Get single indexed transaction by content hash. Note that this will only fetch transactions
|
||||
/// that are indexed by the runtime with `storage_index_transaction`.
|
||||
fn indexed_transaction(
|
||||
&self,
|
||||
hash: &Block::Hash,
|
||||
) -> Result<Option<Vec<u8>>>;
|
||||
fn indexed_transaction(&self, hash: &Block::Hash) -> Result<Option<Vec<u8>>>;
|
||||
|
||||
/// Check if indexed transaction exists.
|
||||
fn has_indexed_transaction(&self, hash: &Block::Hash) -> Result<bool> {
|
||||
@@ -253,7 +257,9 @@ pub trait Cache<Block: BlockT>: Send + Sync {
|
||||
&self,
|
||||
key: &well_known_cache_keys::Id,
|
||||
block: &BlockId<Block>,
|
||||
) -> Result<Option<((NumberFor<Block>, Block::Hash), Option<(NumberFor<Block>, Block::Hash)>, Vec<u8>)>>;
|
||||
) -> Result<
|
||||
Option<((NumberFor<Block>, Block::Hash), Option<(NumberFor<Block>, Block::Hash)>, Vec<u8>)>,
|
||||
>;
|
||||
}
|
||||
|
||||
/// Blockchain info
|
||||
@@ -272,7 +278,7 @@ pub struct Info<Block: BlockT> {
|
||||
/// Last finalized state.
|
||||
pub finalized_state: Option<(Block::Hash, <<Block as BlockT>::Header as HeaderT>::Number)>,
|
||||
/// Number of concurrent leave forks.
|
||||
pub number_leaves: usize
|
||||
pub number_leaves: usize,
|
||||
}
|
||||
|
||||
/// Block status.
|
||||
|
||||
@@ -17,12 +17,12 @@
|
||||
|
||||
//! Substrate client possible errors.
|
||||
|
||||
use std::{self, result};
|
||||
use sp_state_machine;
|
||||
use sp_runtime::transaction_validity::TransactionValidityError;
|
||||
use sp_consensus;
|
||||
use codec::Error as CodecError;
|
||||
use sp_api::ApiError;
|
||||
use sp_consensus;
|
||||
use sp_runtime::transaction_validity::TransactionValidityError;
|
||||
use sp_state_machine;
|
||||
use std::{self, result};
|
||||
|
||||
/// Client Result type alias
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
@@ -205,7 +205,10 @@ impl Error {
|
||||
/// Construct from a state db error.
|
||||
// Can not be done directly, since that would make cargo run out of stack if
|
||||
// `sc-state-db` is lib is added as dependency.
|
||||
pub fn from_state_db<E>(e: E) -> Self where E: std::fmt::Debug {
|
||||
pub fn from_state_db<E>(e: E) -> Self
|
||||
where
|
||||
E: std::fmt::Debug,
|
||||
{
|
||||
Error::StateDatabase(format!("{:?}", e))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
//! Implements tree backend, cached header metadata and algorithms
|
||||
//! to compute routes efficiently over the tree of headers.
|
||||
|
||||
use sp_runtime::traits::{Block as BlockT, NumberFor, Header};
|
||||
use parking_lot::RwLock;
|
||||
use lru::LruCache;
|
||||
use parking_lot::RwLock;
|
||||
use sp_runtime::traits::{Block as BlockT, Header, NumberFor};
|
||||
|
||||
/// Set to the expected max difference between `best` and `finalized` blocks at sync.
|
||||
const LRU_CACHE_SIZE: usize = 5_000;
|
||||
@@ -86,10 +86,7 @@ pub fn lowest_common_ancestor<Block: BlockT, T: HeaderMetadata<Block> + ?Sized>(
|
||||
backend.insert_header_metadata(orig_header_two.hash, orig_header_two);
|
||||
}
|
||||
|
||||
Ok(HashAndNumber {
|
||||
hash: header_one.hash,
|
||||
number: header_one.number,
|
||||
})
|
||||
Ok(HashAndNumber { hash: header_one.hash, number: header_one.number })
|
||||
}
|
||||
|
||||
/// Compute a tree-route between two blocks. See tree-route docs for more details.
|
||||
@@ -105,51 +102,33 @@ pub fn tree_route<Block: BlockT, T: HeaderMetadata<Block>>(
|
||||
let mut to_branch = Vec::new();
|
||||
|
||||
while to.number > from.number {
|
||||
to_branch.push(HashAndNumber {
|
||||
number: to.number,
|
||||
hash: to.hash,
|
||||
});
|
||||
to_branch.push(HashAndNumber { number: to.number, hash: to.hash });
|
||||
|
||||
to = backend.header_metadata(to.parent)?;
|
||||
}
|
||||
|
||||
while from.number > to.number {
|
||||
from_branch.push(HashAndNumber {
|
||||
number: from.number,
|
||||
hash: from.hash,
|
||||
});
|
||||
from_branch.push(HashAndNumber { number: from.number, hash: from.hash });
|
||||
from = backend.header_metadata(from.parent)?;
|
||||
}
|
||||
|
||||
// numbers are equal now. walk backwards until the block is the same
|
||||
|
||||
while to.hash != from.hash {
|
||||
to_branch.push(HashAndNumber {
|
||||
number: to.number,
|
||||
hash: to.hash,
|
||||
});
|
||||
to_branch.push(HashAndNumber { number: to.number, hash: to.hash });
|
||||
to = backend.header_metadata(to.parent)?;
|
||||
|
||||
from_branch.push(HashAndNumber {
|
||||
number: from.number,
|
||||
hash: from.hash,
|
||||
});
|
||||
from_branch.push(HashAndNumber { number: from.number, hash: from.hash });
|
||||
from = backend.header_metadata(from.parent)?;
|
||||
}
|
||||
|
||||
// add the pivot block. and append the reversed to-branch
|
||||
// (note that it's reverse order originals)
|
||||
let pivot = from_branch.len();
|
||||
from_branch.push(HashAndNumber {
|
||||
number: to.number,
|
||||
hash: to.hash,
|
||||
});
|
||||
from_branch.push(HashAndNumber { number: to.number, hash: to.hash });
|
||||
from_branch.extend(to_branch.into_iter().rev());
|
||||
|
||||
Ok(TreeRoute {
|
||||
route: from_branch,
|
||||
pivot,
|
||||
})
|
||||
Ok(TreeRoute { route: from_branch, pivot })
|
||||
}
|
||||
|
||||
/// Hash and number of a block.
|
||||
@@ -204,14 +183,16 @@ impl<Block: BlockT> TreeRoute<Block> {
|
||||
/// Get the common ancestor block. This might be one of the two blocks of the
|
||||
/// route.
|
||||
pub fn common_block(&self) -> &HashAndNumber<Block> {
|
||||
self.route.get(self.pivot).expect("tree-routes are computed between blocks; \
|
||||
self.route.get(self.pivot).expect(
|
||||
"tree-routes are computed between blocks; \
|
||||
which are included in the route; \
|
||||
thus it is never empty; qed")
|
||||
thus it is never empty; qed",
|
||||
)
|
||||
}
|
||||
|
||||
/// Get a slice of enacted blocks (descendents of the common ancestor)
|
||||
pub fn enacted(&self) -> &[HashAndNumber<Block>] {
|
||||
&self.route[self.pivot + 1 ..]
|
||||
&self.route[self.pivot + 1..]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -240,17 +221,13 @@ pub struct HeaderMetadataCache<Block: BlockT> {
|
||||
impl<Block: BlockT> HeaderMetadataCache<Block> {
|
||||
/// Creates a new LRU header metadata cache with `capacity`.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
HeaderMetadataCache {
|
||||
cache: RwLock::new(LruCache::new(capacity)),
|
||||
}
|
||||
HeaderMetadataCache { cache: RwLock::new(LruCache::new(capacity)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<Block: BlockT> Default for HeaderMetadataCache<Block> {
|
||||
fn default() -> Self {
|
||||
HeaderMetadataCache {
|
||||
cache: RwLock::new(LruCache::new(LRU_CACHE_SIZE)),
|
||||
}
|
||||
HeaderMetadataCache { cache: RwLock::new(LruCache::new(LRU_CACHE_SIZE)) }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
//! Substrate blockchain traits and primitives.
|
||||
|
||||
mod backend;
|
||||
mod header_metadata;
|
||||
mod error;
|
||||
mod header_metadata;
|
||||
|
||||
pub use error::*;
|
||||
pub use backend::*;
|
||||
pub use error::*;
|
||||
pub use header_metadata::*;
|
||||
|
||||
Reference in New Issue
Block a user