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
+136 -144
View File
@@ -18,14 +18,14 @@
//! Substrate transaction pool implementation.
#![recursion_limit="256"]
#![recursion_limit = "256"]
#![warn(missing_docs)]
#![warn(unused_extern_crates)]
mod api;
mod graph;
mod revalidation;
mod metrics;
mod revalidation;
pub mod error;
@@ -33,53 +33,60 @@ pub mod error;
#[cfg(feature = "test-helpers")]
pub mod test_helpers {
pub use super::{
graph::{ChainApi, Pool, NumberFor, BlockHash, ExtrinsicFor},
graph::{BlockHash, ChainApi, ExtrinsicFor, NumberFor, Pool},
revalidation::RevalidationQueue,
};
}
pub use graph::{Options, Transaction};
pub use crate::api::{FullChainApi, LightChainApi};
use std::{collections::{HashMap, HashSet}, sync::Arc, pin::Pin, convert::TryInto};
use futures::{prelude::*, future::{self, ready}, channel::oneshot};
use futures::{
channel::oneshot,
future::{self, ready},
prelude::*,
};
pub use graph::{Options, Transaction};
use parking_lot::Mutex;
use std::{
collections::{HashMap, HashSet},
convert::TryInto,
pin::Pin,
sync::Arc,
};
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, NumberFor, AtLeast32Bit, Extrinsic, Zero, Header as HeaderT},
use graph::{ExtrinsicHash, IsValidator};
use sc_transaction_pool_api::{
ChainEvent, ImportNotificationStream, MaintainedTransactionPool, PoolFuture, PoolStatus,
TransactionFor, TransactionPool, TransactionSource, TransactionStatusStreamFor, TxHash,
};
use sp_core::traits::SpawnEssentialNamed;
use sc_transaction_pool_api::{
TransactionPool, PoolStatus, ImportNotificationStream, TxHash, TransactionFor,
TransactionStatusStreamFor, MaintainedTransactionPool, PoolFuture, ChainEvent,
TransactionSource,
use sp_runtime::{
generic::BlockId,
traits::{AtLeast32Bit, Block as BlockT, Extrinsic, Header as HeaderT, NumberFor, Zero},
};
use graph::{IsValidator, ExtrinsicHash};
use wasm_timer::Instant;
use prometheus_endpoint::Registry as PrometheusRegistry;
use crate::metrics::MetricsLink as PrometheusMetrics;
use prometheus_endpoint::Registry as PrometheusRegistry;
type BoxedReadyIterator<Hash, Data> = Box<
dyn Iterator<Item=Arc<graph::base_pool::Transaction<Hash, Data>>> + Send
>;
type BoxedReadyIterator<Hash, Data> =
Box<dyn Iterator<Item = Arc<graph::base_pool::Transaction<Hash, Data>>> + Send>;
type ReadyIteratorFor<PoolApi> = BoxedReadyIterator<
graph::ExtrinsicHash<PoolApi>, graph::ExtrinsicFor<PoolApi>
>;
type ReadyIteratorFor<PoolApi> =
BoxedReadyIterator<graph::ExtrinsicHash<PoolApi>, graph::ExtrinsicFor<PoolApi>>;
type PolledIterator<PoolApi> = Pin<Box<dyn Future<Output=ReadyIteratorFor<PoolApi>> + Send>>;
type PolledIterator<PoolApi> = Pin<Box<dyn Future<Output = ReadyIteratorFor<PoolApi>> + Send>>;
/// A transaction pool for a full node.
pub type FullPool<Block, Client> = BasicPool<FullChainApi<Client, Block>, Block>;
/// A transaction pool for a light node.
pub type LightPool<Block, Client, Fetcher> = BasicPool<LightChainApi<Client, Fetcher, Block>, Block>;
pub type LightPool<Block, Client, Fetcher> =
BasicPool<LightChainApi<Client, Fetcher, Block>, Block>;
/// Basic implementation of transaction pool that can be customized by providing PoolApi.
pub struct BasicPool<PoolApi, Block>
where
Block: BlockT,
PoolApi: graph::ChainApi<Block=Block>,
where
Block: BlockT,
PoolApi: graph::ChainApi<Block = Block>,
{
pool: Arc<graph::Pool<PoolApi>>,
api: Arc<PoolApi>,
@@ -96,19 +103,13 @@ struct ReadyPoll<T, Block: BlockT> {
impl<T, Block: BlockT> Default for ReadyPoll<T, Block> {
fn default() -> Self {
Self {
updated_at: NumberFor::<Block>::zero(),
pollers: Default::default(),
}
Self { updated_at: NumberFor::<Block>::zero(), pollers: Default::default() }
}
}
impl<T, Block: BlockT> ReadyPoll<T, Block> {
fn new(best_block_number: NumberFor<Block>) -> Self {
Self {
updated_at: best_block_number,
pollers: Default::default(),
}
Self { updated_at: best_block_number, pollers: Default::default() }
}
fn trigger(&mut self, number: NumberFor<Block>, iterator_factory: impl Fn() -> T) {
@@ -140,7 +141,7 @@ impl<T, Block: BlockT> ReadyPoll<T, Block> {
#[cfg(not(target_os = "unknown"))]
impl<PoolApi, Block> parity_util_mem::MallocSizeOf for BasicPool<PoolApi, Block>
where
PoolApi: graph::ChainApi<Block=Block>,
PoolApi: graph::ChainApi<Block = Block>,
Block: BlockT,
{
fn size_of(&self, ops: &mut parity_util_mem::MallocSizeOfOps) -> usize {
@@ -167,15 +168,15 @@ pub enum RevalidationType {
}
impl<PoolApi, Block> BasicPool<PoolApi, Block>
where
Block: BlockT,
PoolApi: graph::ChainApi<Block=Block> + 'static,
where
Block: BlockT,
PoolApi: graph::ChainApi<Block = Block> + 'static,
{
/// Create new basic transaction pool with provided api, for tests.
#[cfg(feature = "test-helpers")]
pub fn new_test(
pool_api: Arc<PoolApi>,
) -> (Self, Pin<Box<dyn Future<Output=()> + Send>>, intervalier::BackSignalControl) {
) -> (Self, Pin<Box<dyn Future<Output = ()> + Send>>, intervalier::BackSignalControl) {
let pool = Arc::new(graph::Pool::new(Default::default(), true.into(), pool_api.clone()));
let (revalidation_queue, background_task, notifier) =
revalidation::RevalidationQueue::new_test(pool_api.clone(), pool.clone());
@@ -206,15 +207,11 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
) -> Self {
let pool = Arc::new(graph::Pool::new(options, is_validator, pool_api.clone()));
let (revalidation_queue, background_task) = match revalidation_type {
RevalidationType::Light => (
revalidation::RevalidationQueue::new(pool_api.clone(), pool.clone()),
None,
),
RevalidationType::Light =>
(revalidation::RevalidationQueue::new(pool_api.clone(), pool.clone()), None),
RevalidationType::Full => {
let (queue, background) = revalidation::RevalidationQueue::new_background(
pool_api.clone(),
pool.clone(),
);
let (queue, background) =
revalidation::RevalidationQueue::new_background(pool_api.clone(), pool.clone());
(queue, Some(background))
},
};
@@ -227,12 +224,11 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
api: pool_api,
pool,
revalidation_queue: Arc::new(revalidation_queue),
revalidation_strategy: Arc::new(Mutex::new(
match revalidation_type {
RevalidationType::Light => RevalidationStrategy::Light(RevalidationStatus::NotScheduled),
RevalidationType::Full => RevalidationStrategy::Always,
}
)),
revalidation_strategy: Arc::new(Mutex::new(match revalidation_type {
RevalidationType::Light =>
RevalidationStrategy::Light(RevalidationStatus::NotScheduled),
RevalidationType::Full => RevalidationStrategy::Always,
})),
ready_poll: Arc::new(Mutex::new(ReadyPoll::new(best_block_number))),
metrics: PrometheusMetrics::new(prometheus),
}
@@ -251,15 +247,13 @@ impl<PoolApi, Block> BasicPool<PoolApi, Block>
}
impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block=Block>,
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block = Block>,
{
type Block = PoolApi::Block;
type Hash = graph::ExtrinsicHash<PoolApi>;
type InPoolTransaction = graph::base_pool::Transaction<
TxHash<Self>, TransactionFor<Self>
>;
type InPoolTransaction = graph::base_pool::Transaction<TxHash<Self>, TransactionFor<Self>>;
type Error = PoolApi::Error;
fn submit_at(
@@ -271,7 +265,8 @@ impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
let pool = self.pool.clone();
let at = *at;
self.metrics.report(|metrics| metrics.submitted_transactions.inc_by(xts.len() as u64));
self.metrics
.report(|metrics| metrics.submitted_transactions.inc_by(xts.len() as u64));
async move { pool.submit_at(&at, source, xts).await }.boxed()
}
@@ -305,12 +300,14 @@ impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
pool.submit_and_watch(&at, source, xt)
.map(|result| result.map(|watcher| Box::new(watcher.into_stream()) as _))
.await
}.boxed()
}
.boxed()
}
fn remove_invalid(&self, hashes: &[TxHash<Self>]) -> Vec<Arc<Self::InPoolTransaction>> {
let removed = self.pool.validated_pool().remove_invalid(hashes);
self.metrics.report(|metrics| metrics.validations_invalid.inc_by(removed.len() as u64));
self.metrics
.report(|metrics| metrics.validations_invalid.inc_by(removed.len() as u64));
removed
}
@@ -347,16 +344,18 @@ impl<PoolApi, Block> TransactionPool for BasicPool<PoolApi, Block>
if self.ready_poll.lock().updated_at() >= at {
log::trace!(target: "txpool", "Transaction pool already processed block #{}", at);
let iterator: ReadyIteratorFor<PoolApi> = Box::new(self.pool.validated_pool().ready());
return async move { iterator }.boxed();
return async move { iterator }.boxed()
}
self.ready_poll
.lock()
.add(at)
.map(|received| received.unwrap_or_else(|e| {
log::warn!("Error receiving pending set: {:?}", e);
Box::new(std::iter::empty())
}))
.map(|received| {
received.unwrap_or_else(|e| {
log::warn!("Error receiving pending set: {:?}", e);
Box::new(std::iter::empty())
})
})
.boxed()
}
@@ -452,9 +451,10 @@ where
at: &BlockId<Self::Block>,
xt: sc_transaction_pool_api::LocalTransactionFor<Self>,
) -> Result<Self::Hash, Self::Error> {
use graph::{ValidatedTransaction, ChainApi};
use sp_runtime::traits::SaturatedConversion;
use sp_runtime::transaction_validity::TransactionValidityError;
use graph::{ChainApi, ValidatedTransaction};
use sp_runtime::{
traits::SaturatedConversion, transaction_validity::TransactionValidityError,
};
let validity = self
.api
@@ -527,10 +527,7 @@ impl<N: Clone + Copy + AtLeast32Bit> RevalidationStrategy<N> {
),
resubmit: false,
},
Self::Always => RevalidationAction {
revalidate: true,
resubmit: true,
}
Self::Always => RevalidationAction { revalidate: true, resubmit: true },
}
}
}
@@ -555,15 +552,16 @@ impl<N: Clone + Copy + AtLeast32Bit> RevalidationStatus<N> {
revalidate_block_period.map(|period| block + period),
);
false
}
},
Self::Scheduled(revalidate_at_time, revalidate_at_block) => {
let is_required = revalidate_at_time.map(|at| Instant::now() >= at).unwrap_or(false)
|| revalidate_at_block.map(|at| block >= at).unwrap_or(false);
let is_required =
revalidate_at_time.map(|at| Instant::now() >= at).unwrap_or(false) ||
revalidate_at_block.map(|at| block >= at).unwrap_or(false);
if is_required {
*self = Self::InProgress;
}
is_required
}
},
Self::InProgress => false,
}
}
@@ -575,16 +573,16 @@ async fn prune_known_txs_for_block<Block: BlockT, Api: graph::ChainApi<Block = B
api: &Api,
pool: &graph::Pool<Api>,
) -> Vec<ExtrinsicHash<Api>> {
let extrinsics = api.block_body(&block_id).await
let extrinsics = api
.block_body(&block_id)
.await
.unwrap_or_else(|e| {
log::warn!("Prune known transactions: error request {:?}!", e);
None
})
.unwrap_or_default();
let hashes = extrinsics.iter()
.map(|tx| pool.hash_of(&tx))
.collect::<Vec<_>>();
let hashes = extrinsics.iter().map(|tx| pool.hash_of(&tx)).collect::<Vec<_>>();
log::trace!(target: "txpool", "Pruning transactions: {:?}", hashes);
@@ -597,10 +595,11 @@ async fn prune_known_txs_for_block<Block: BlockT, Api: graph::ChainApi<Block = B
Err(e) => {
log::debug!(target: "txpool", "Error retrieving header for {:?}: {:?}", block_id, e);
return hashes
}
},
};
if let Err(e) = pool.prune(&block_id, &BlockId::hash(*header.parent_hash()), &extrinsics).await {
if let Err(e) = pool.prune(&block_id, &BlockId::hash(*header.parent_hash()), &extrinsics).await
{
log::error!("Cannot prune known in the pool {:?}!", e);
}
@@ -608,11 +607,11 @@ async fn prune_known_txs_for_block<Block: BlockT, Api: graph::ChainApi<Block = B
}
impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block=Block>,
where
Block: BlockT,
PoolApi: 'static + graph::ChainApi<Block = Block>,
{
fn maintain(&self, event: ChainEvent<Self::Block>) -> Pin<Box<dyn Future<Output=()> + Send>> {
fn maintain(&self, event: ChainEvent<Self::Block>) -> Pin<Box<dyn Future<Output = ()> + Send>> {
match event {
ChainEvent::NewBestBlock { hash, tree_route } => {
let pool = self.pool.clone();
@@ -627,8 +626,8 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
"Skipping chain event - no number for that block {:?}",
id,
);
return Box::pin(ready(()));
}
return Box::pin(ready(()))
},
};
let next_action = self.revalidation_strategy.lock().next(
@@ -657,27 +656,21 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
pool.validated_pool().on_block_retracted(retracted.hash.clone());
}
future::join_all(
tree_route
.enacted()
.iter()
.map(|h|
prune_known_txs_for_block(
BlockId::Hash(h.hash.clone()),
&*api,
&*pool,
),
),
).await.into_iter().for_each(|enacted_log|{
future::join_all(tree_route.enacted().iter().map(|h| {
prune_known_txs_for_block(BlockId::Hash(h.hash.clone()), &*api, &*pool)
}))
.await
.into_iter()
.for_each(|enacted_log| {
pruned_log.extend(enacted_log);
})
}
pruned_log.extend(prune_known_txs_for_block(id.clone(), &*api, &*pool).await);
metrics.report(
|metrics| metrics.block_transactions_pruned.inc_by(pruned_log.len() as u64)
);
metrics.report(|metrics| {
metrics.block_transactions_pruned.inc_by(pruned_log.len() as u64)
});
if let (true, Some(tree_route)) = (next_action.resubmit, tree_route) {
let mut resubmit_transactions = Vec::new();
@@ -685,7 +678,8 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
for retracted in tree_route.retracted() {
let hash = retracted.hash.clone();
let block_transactions = api.block_body(&BlockId::hash(hash))
let block_transactions = api
.block_body(&BlockId::hash(hash))
.await
.unwrap_or_else(|e| {
log::warn!("Failed to fetch block body {:?}!", e);
@@ -697,8 +691,8 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
let mut resubmitted_to_report = 0;
resubmit_transactions.extend(
block_transactions.into_iter().filter(|tx| {
resubmit_transactions.extend(block_transactions.into_iter().filter(
|tx| {
let tx_hash = pool.hash_of(&tx);
let contains = pruned_log.contains(&tx_hash);
@@ -714,21 +708,24 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
);
}
!contains
})
);
},
));
metrics.report(
|metrics| metrics.block_transactions_resubmitted.inc_by(resubmitted_to_report)
);
metrics.report(|metrics| {
metrics.block_transactions_resubmitted.inc_by(resubmitted_to_report)
});
}
if let Err(e) = pool.resubmit_at(
&id,
// These transactions are coming from retracted blocks, we should
// simply consider them external.
TransactionSource::External,
resubmit_transactions,
).await {
if let Err(e) = pool
.resubmit_at(
&id,
// These transactions are coming from retracted blocks, we should
// simply consider them external.
TransactionSource::External,
resubmit_transactions,
)
.await
{
log::debug!(
target: "txpool",
"[{:?}] Error re-submitting transactions: {:?}",
@@ -741,22 +738,20 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
let extra_pool = pool.clone();
// After #5200 lands, this arguably might be moved to the
// handler of "all blocks notification".
ready_poll.lock().trigger(
block_number,
move || Box::new(extra_pool.validated_pool().ready()),
);
ready_poll.lock().trigger(block_number, move || {
Box::new(extra_pool.validated_pool().ready())
});
if next_action.revalidate {
let hashes = pool.validated_pool()
.ready()
.map(|tx| tx.hash.clone())
.collect();
let hashes =
pool.validated_pool().ready().map(|tx| tx.hash.clone()).collect();
revalidation_queue.revalidate_later(block_number, hashes).await;
revalidation_strategy.lock().clear();
}
}.boxed()
}
}
.boxed()
},
ChainEvent::Finalized { hash } => {
let pool = self.pool.clone();
async move {
@@ -767,28 +762,25 @@ impl<PoolApi, Block> MaintainedTransactionPool for BasicPool<PoolApi, Block>
e, hash
)
}
}.boxed()
}
}
.boxed()
},
}
}
}
/// Inform the transaction pool about imported and finalized blocks.
pub async fn notification_future<Client, Pool, Block>(
client: Arc<Client>,
txpool: Arc<Pool>
)
where
Block: BlockT,
Client: sc_client_api::BlockchainEvents<Block>,
Pool: MaintainedTransactionPool<Block=Block>,
pub async fn notification_future<Client, Pool, Block>(client: Arc<Client>, txpool: Arc<Pool>)
where
Block: BlockT,
Client: sc_client_api::BlockchainEvents<Block>,
Pool: MaintainedTransactionPool<Block = Block>,
{
let import_stream = client.import_notification_stream()
let import_stream = client
.import_notification_stream()
.filter_map(|n| ready(n.try_into().ok()))
.fuse();
let finality_stream = client.finality_notification_stream()
.map(Into::into)
.fuse();
let finality_stream = client.finality_notification_stream().map(Into::into).fuse();
futures::stream::select(import_stream, finality_stream)
.for_each(|evt| txpool.maintain(evt))