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
+17 -21
View File
@@ -18,16 +18,19 @@
//! Blockchain API backend for full nodes.
use std::sync::Arc;
use rpc::futures::future::result;
use jsonrpc_pubsub::manager::SubscriptionManager;
use rpc::futures::future::result;
use std::sync::Arc;
use sc_client_api::{BlockchainEvents, BlockBackend};
use sp_runtime::{generic::{BlockId, SignedBlock}, traits::{Block as BlockT}};
use sc_client_api::{BlockBackend, BlockchainEvents};
use sp_runtime::{
generic::{BlockId, SignedBlock},
traits::Block as BlockT,
};
use super::{ChainBackend, client_err, error::FutureResult};
use std::marker::PhantomData;
use super::{client_err, error::FutureResult, ChainBackend};
use sp_blockchain::HeaderBackend;
use std::marker::PhantomData;
/// Blockchain API backend for full nodes. Reads all the data from local database.
pub struct FullChain<Block: BlockT, Client> {
@@ -42,15 +45,12 @@ pub struct FullChain<Block: BlockT, Client> {
impl<Block: BlockT, Client> FullChain<Block, Client> {
/// Create new Chain API RPC handler.
pub fn new(client: Arc<Client>, subscriptions: SubscriptionManager) -> Self {
Self {
client,
subscriptions,
_phantom: PhantomData,
}
Self { client, subscriptions, _phantom: PhantomData }
}
}
impl<Block, Client> ChainBackend<Client, Block> for FullChain<Block, Client> where
impl<Block, Client> ChainBackend<Client, Block> for FullChain<Block, Client>
where
Block: BlockT + 'static,
Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
@@ -63,18 +63,14 @@ impl<Block, Client> ChainBackend<Client, Block> for FullChain<Block, Client> whe
}
fn header(&self, hash: Option<Block::Hash>) -> FutureResult<Option<Block::Header>> {
Box::new(result(self.client
.header(BlockId::Hash(self.unwrap_or_best(hash)))
.map_err(client_err)
Box::new(result(
self.client.header(BlockId::Hash(self.unwrap_or_best(hash))).map_err(client_err),
))
}
fn block(&self, hash: Option<Block::Hash>)
-> FutureResult<Option<SignedBlock<Block>>>
{
Box::new(result(self.client
.block(&BlockId::Hash(self.unwrap_or_best(hash)))
.map_err(client_err)
fn block(&self, hash: Option<Block::Hash>) -> FutureResult<Option<SignedBlock<Block>>> {
Box::new(result(
self.client.block(&BlockId::Hash(self.unwrap_or_best(hash))).map_err(client_err),
))
}
}
+26 -30
View File
@@ -18,20 +18,20 @@
//! Blockchain API backend for light nodes.
use std::sync::Arc;
use futures::{future::ready, FutureExt, TryFutureExt};
use rpc::futures::future::{result, Future, Either};
use jsonrpc_pubsub::manager::SubscriptionManager;
use rpc::futures::future::{result, Either, Future};
use std::sync::Arc;
use sc_client_api::light::{Fetcher, RemoteBodyRequest, RemoteBlockchain};
use sc_client_api::light::{Fetcher, RemoteBlockchain, RemoteBodyRequest};
use sp_runtime::{
generic::{BlockId, SignedBlock},
traits::{Block as BlockT},
traits::Block as BlockT,
};
use super::{ChainBackend, client_err, error::FutureResult};
use sp_blockchain::HeaderBackend;
use super::{client_err, error::FutureResult, ChainBackend};
use sc_client_api::BlockchainEvents;
use sp_blockchain::HeaderBackend;
/// Blockchain API backend for light nodes. Reads all the data from local
/// database, if available, or fetches it from remote node otherwise.
@@ -54,16 +54,12 @@ impl<Block: BlockT, Client, F: Fetcher<Block>> LightChain<Block, Client, F> {
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> Self {
Self {
client,
subscriptions,
remote_blockchain,
fetcher,
}
Self { client, subscriptions, remote_blockchain, fetcher }
}
}
impl<Block, Client, F> ChainBackend<Client, Block> for LightChain<Block, Client, F> where
impl<Block, Client, F> ChainBackend<Client, Block> for LightChain<Block, Client, F>
where
Block: BlockT + 'static,
Client: BlockchainEvents<Block> + HeaderBackend<Block> + Send + Sync + 'static,
F: Fetcher<Block> + Send + Sync + 'static,
@@ -86,32 +82,32 @@ impl<Block, Client, F> ChainBackend<Client, Block> for LightChain<Block, Client,
BlockId::Hash(hash),
);
Box::new(maybe_header.then(move |result|
ready(result.map_err(client_err)),
).boxed().compat())
Box::new(
maybe_header
.then(move |result| ready(result.map_err(client_err)))
.boxed()
.compat(),
)
}
fn block(&self, hash: Option<Block::Hash>)
-> FutureResult<Option<SignedBlock<Block>>>
{
fn block(&self, hash: Option<Block::Hash>) -> FutureResult<Option<SignedBlock<Block>>> {
let fetcher = self.fetcher.clone();
let block = self.header(hash)
.and_then(move |header| match header {
Some(header) => Either::A(fetcher
let block = self.header(hash).and_then(move |header| match header {
Some(header) => Either::A(
fetcher
.remote_body(RemoteBodyRequest {
header: header.clone(),
retry_count: Default::default(),
})
.boxed()
.compat()
.map(move |body| Some(SignedBlock {
block: Block::new(header, body),
justifications: None,
}))
.map_err(client_err)
),
None => Either::B(result(Ok(None))),
});
.map(move |body| {
Some(SignedBlock { block: Block::new(header, body), justifications: None })
})
.map_err(client_err),
),
None => Either::B(result(Ok(None))),
});
Box::new(block)
}
+80 -59
View File
@@ -24,33 +24,36 @@ mod chain_light;
#[cfg(test)]
mod tests;
use std::sync::Arc;
use futures::{future, StreamExt, TryStreamExt};
use log::warn;
use rpc::{
Result as RpcResult,
futures::{stream, Future, Sink, Stream},
Result as RpcResult,
};
use std::sync::Arc;
use sc_client_api::{BlockchainEvents, light::{Fetcher, RemoteBlockchain}};
use jsonrpc_pubsub::{typed::Subscriber, SubscriptionId, manager::SubscriptionManager};
use sp_rpc::{number::NumberOrHex, list::ListOrValue};
use jsonrpc_pubsub::{manager::SubscriptionManager, typed::Subscriber, SubscriptionId};
use sc_client_api::{
light::{Fetcher, RemoteBlockchain},
BlockchainEvents,
};
use sp_rpc::{list::ListOrValue, number::NumberOrHex};
use sp_runtime::{
generic::{BlockId, SignedBlock},
traits::{Block as BlockT, Header, NumberFor},
};
use self::error::{Result, Error, FutureResult};
use self::error::{Error, FutureResult, Result};
use sc_client_api::BlockBackend;
pub use sc_rpc_api::chain::*;
use sp_blockchain::HeaderBackend;
use sc_client_api::BlockBackend;
/// Blockchain backend API
trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
where
Block: BlockT + 'static,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
where
Block: BlockT + 'static,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
/// Get client reference.
fn client(&self) -> &Arc<Client>;
@@ -94,7 +97,7 @@ trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
.header(BlockId::number(block_num))
.map_err(client_err)?
.map(|h| h.hash()))
}
},
}
}
@@ -114,9 +117,12 @@ trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
self.subscriptions(),
subscriber,
|| self.client().info().best_hash,
|| self.client().import_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
|| {
self.client()
.import_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat()
},
)
}
@@ -140,10 +146,13 @@ trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
self.subscriptions(),
subscriber,
|| self.client().info().best_hash,
|| self.client().import_notification_stream()
.filter(|notification| future::ready(notification.is_new_best))
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
|| {
self.client()
.import_notification_stream()
.filter(|notification| future::ready(notification.is_new_best))
.map(|notification| Ok::<_, ()>(notification.header))
.compat()
},
)
}
@@ -167,9 +176,12 @@ trait ChainBackend<Client, Block: BlockT>: Send + Sync + 'static
self.subscriptions(),
subscriber,
|| self.client().info().finalized_hash,
|| self.client().finality_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat(),
|| {
self.client()
.finality_notification_stream()
.map(|notification| Ok::<_, ()>(notification.header))
.compat()
},
)
}
@@ -188,13 +200,11 @@ pub fn new_full<Block: BlockT, Client>(
client: Arc<Client>,
subscriptions: SubscriptionManager,
) -> Chain<Block, Client>
where
Block: BlockT + 'static,
Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
where
Block: BlockT + 'static,
Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
Chain {
backend: Box::new(self::chain_full::FullChain::new(client, subscriptions)),
}
Chain { backend: Box::new(self::chain_full::FullChain::new(client, subscriptions)) }
}
/// Create new state API that works on light node.
@@ -204,10 +214,10 @@ pub fn new_light<Block: BlockT, Client, F: Fetcher<Block>>(
remote_blockchain: Arc<dyn RemoteBlockchain<Block>>,
fetcher: Arc<F>,
) -> Chain<Block, Client>
where
Block: BlockT + 'static,
Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
F: Send + Sync + 'static,
where
Block: BlockT + 'static,
Client: BlockBackend<Block> + HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
F: Send + Sync + 'static,
{
Chain {
backend: Box::new(self::chain_light::LightChain::new(
@@ -224,11 +234,11 @@ pub struct Chain<Block: BlockT, Client> {
backend: Box<dyn ChainBackend<Client, Block>>,
}
impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>> for
Chain<Block, Client>
where
Block: BlockT + 'static,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, SignedBlock<Block>>
for Chain<Block, Client>
where
Block: BlockT + 'static,
Client: HeaderBackend<Block> + BlockchainEvents<Block> + 'static,
{
type Metadata = crate::Metadata;
@@ -236,8 +246,7 @@ impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Signe
self.backend.header(hash)
}
fn block(&self, hash: Option<Block::Hash>) -> FutureResult<Option<SignedBlock<Block>>>
{
fn block(&self, hash: Option<Block::Hash>) -> FutureResult<Option<SignedBlock<Block>>> {
self.backend.block(hash)
}
@@ -247,12 +256,13 @@ impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Signe
) -> Result<ListOrValue<Option<Block::Hash>>> {
match number {
None => self.backend.block_hash(None).map(ListOrValue::Value),
Some(ListOrValue::Value(number)) => self.backend.block_hash(Some(number)).map(ListOrValue::Value),
Some(ListOrValue::List(list)) => Ok(ListOrValue::List(list
.into_iter()
.map(|number| self.backend.block_hash(Some(number)))
.collect::<Result<_>>()?
))
Some(ListOrValue::Value(number)) =>
self.backend.block_hash(Some(number)).map(ListOrValue::Value),
Some(ListOrValue::List(list)) => Ok(ListOrValue::List(
list.into_iter()
.map(|number| self.backend.block_hash(Some(number)))
.collect::<Result<_>>()?,
)),
}
}
@@ -264,7 +274,11 @@ impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Signe
self.backend.subscribe_all_heads(metadata, subscriber)
}
fn unsubscribe_all_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool> {
fn unsubscribe_all_heads(
&self,
metadata: Option<Self::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
self.backend.unsubscribe_all_heads(metadata, id)
}
@@ -272,15 +286,27 @@ impl<Block, Client> ChainApi<NumberFor<Block>, Block::Hash, Block::Header, Signe
self.backend.subscribe_new_heads(metadata, subscriber)
}
fn unsubscribe_new_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool> {
fn unsubscribe_new_heads(
&self,
metadata: Option<Self::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
self.backend.unsubscribe_new_heads(metadata, id)
}
fn subscribe_finalized_heads(&self, metadata: Self::Metadata, subscriber: Subscriber<Block::Header>) {
fn subscribe_finalized_heads(
&self,
metadata: Self::Metadata,
subscriber: Subscriber<Block::Header>,
) {
self.backend.subscribe_finalized_heads(metadata, subscriber)
}
fn unsubscribe_finalized_heads(&self, metadata: Option<Self::Metadata>, id: SubscriptionId) -> RpcResult<bool> {
fn unsubscribe_finalized_heads(
&self,
metadata: Option<Self::Metadata>,
id: SubscriptionId,
) -> RpcResult<bool> {
self.backend.unsubscribe_finalized_heads(metadata, id)
}
}
@@ -298,15 +324,14 @@ fn subscribe_headers<Block, Client, F, G, S, ERR>(
F: FnOnce() -> S,
G: FnOnce() -> Block::Hash,
ERR: ::std::fmt::Debug,
S: Stream<Item=Block::Header, Error=ERR> + Send + 'static,
S: Stream<Item = Block::Header, Error = ERR> + Send + 'static,
{
subscriptions.add(subscriber, |sink| {
// send current head right at the start.
let header = client.header(BlockId::Hash(best_block_hash()))
let header = client
.header(BlockId::Hash(best_block_hash()))
.map_err(client_err)
.and_then(|header| {
header.ok_or_else(|| "Best header missing.".to_owned().into())
})
.and_then(|header| header.ok_or_else(|| "Best header missing.".to_owned().into()))
.map_err(Into::into);
// send further subscriptions
@@ -314,12 +339,8 @@ fn subscribe_headers<Block, Client, F, G, S, ERR>(
.map(|res| Ok(res))
.map_err(|e| warn!("Block notification stream error: {:?}", e));
sink
.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(
stream::iter_result(vec![Ok(header)])
.chain(stream)
)
sink.sink_map_err(|e| warn!("Error sending notifications: {:?}", e))
.send_all(stream::iter_result(vec![Ok(header)]).chain(stream))
// we ignore the resulting Stream (if the first stream is over we are unsubscribed)
.map(|_| ())
});
+12 -23
View File
@@ -17,16 +17,19 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use super::*;
use crate::testing::TaskExecutor;
use assert_matches::assert_matches;
use futures::{
compat::{Future01CompatExt, Stream01CompatExt},
executor,
};
use sc_block_builder::BlockBuilderProvider;
use sp_rpc::list::ListOrValue;
use substrate_test_runtime_client::{
prelude::*,
runtime::{Block, Header, H256},
sp_consensus::BlockOrigin,
runtime::{H256, Block, Header},
};
use sp_rpc::list::ListOrValue;
use sc_block_builder::BlockBuilderProvider;
use futures::{executor, compat::{Future01CompatExt, Stream01CompatExt}};
use crate::testing::TaskExecutor;
#[test]
fn should_return_header() {
@@ -105,10 +108,7 @@ fn should_return_a_block() {
}
);
assert_matches!(
api.block(Some(H256::from_low_u64_be(5)).into()).wait(),
Ok(None)
);
assert_matches!(api.block(Some(H256::from_low_u64_be(5)).into()).wait(), Ok(None));
}
#[test]
@@ -121,7 +121,6 @@ fn should_return_block_hash() {
Ok(ListOrValue::Value(Some(ref x))) if x == &client.genesis_hash()
);
assert_matches!(
api.block_hash(Some(ListOrValue::Value(0u64.into())).into()),
Ok(ListOrValue::Value(Some(ref x))) if x == &client.genesis_hash()
@@ -154,7 +153,6 @@ fn should_return_block_hash() {
);
}
#[test]
fn should_return_finalized_hash() {
let mut client = Arc::new(substrate_test_runtime_client::new());
@@ -193,10 +191,7 @@ fn should_notify_about_latest_block() {
api.subscribe_all_heads(Default::default(), subscriber);
// assert id assigned
assert!(matches!(
executor::block_on(id.compat()),
Ok(Ok(SubscriptionId::String(_)))
));
assert!(matches!(executor::block_on(id.compat()), Ok(Ok(SubscriptionId::String(_)))));
let block = client.new_block(Default::default()).unwrap().build().unwrap().block;
executor::block_on(client.import(BlockOrigin::Own, block)).unwrap();
@@ -223,10 +218,7 @@ fn should_notify_about_best_block() {
api.subscribe_new_heads(Default::default(), subscriber);
// assert id assigned
assert!(matches!(
executor::block_on(id.compat()),
Ok(Ok(SubscriptionId::String(_)))
));
assert!(matches!(executor::block_on(id.compat()), Ok(Ok(SubscriptionId::String(_)))));
let block = client.new_block(Default::default()).unwrap().build().unwrap().block;
executor::block_on(client.import(BlockOrigin::Own, block)).unwrap();
@@ -253,10 +245,7 @@ fn should_notify_about_finalized_block() {
api.subscribe_finalized_heads(Default::default(), subscriber);
// assert id assigned
assert!(matches!(
executor::block_on(id.compat()),
Ok(Ok(SubscriptionId::String(_)))
));
assert!(matches!(executor::block_on(id.compat()), Ok(Ok(SubscriptionId::String(_)))));
let block = client.new_block(Default::default()).unwrap().build().unwrap().block;
executor::block_on(client.import(BlockOrigin::Own, block)).unwrap();