mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 16:57:58 +00:00
cargo +nightly fmt (#3540)
* cargo +nightly fmt * add cargo-fmt check to ci * update ci * fmt * fmt * skip macro * ignore bridges
This commit is contained in:
@@ -40,9 +40,7 @@ use sp_blockchain::HeaderBackend;
|
||||
use polkadot_node_subsystem_util::metrics::{self, prometheus};
|
||||
use polkadot_primitives::v1::{Block, BlockId};
|
||||
use polkadot_subsystem::{
|
||||
overseer,
|
||||
messages::ChainApiMessage,
|
||||
FromOverseer, OverseerSignal, SpawnedSubsystem,
|
||||
messages::ChainApiMessage, overseer, FromOverseer, OverseerSignal, SpawnedSubsystem,
|
||||
SubsystemContext, SubsystemError, SubsystemResult,
|
||||
};
|
||||
|
||||
@@ -60,10 +58,7 @@ pub struct ChainApiSubsystem<Client> {
|
||||
impl<Client> ChainApiSubsystem<Client> {
|
||||
/// Create a new Chain API subsystem with the given client.
|
||||
pub fn new(client: Arc<Client>, metrics: Metrics) -> Self {
|
||||
ChainApiSubsystem {
|
||||
client,
|
||||
metrics,
|
||||
}
|
||||
ChainApiSubsystem { client, metrics }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +72,7 @@ where
|
||||
let future = run::<Client, Context>(ctx, self)
|
||||
.map_err(|e| SubsystemError::with_origin("chain-api", e))
|
||||
.boxed();
|
||||
SpawnedSubsystem {
|
||||
future,
|
||||
name: "chain-api-subsystem",
|
||||
}
|
||||
SpawnedSubsystem { future, name: "chain-api-subsystem" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,7 +99,8 @@ where
|
||||
},
|
||||
ChainApiMessage::BlockHeader(hash, response_channel) => {
|
||||
let _timer = subsystem.metrics.time_block_header();
|
||||
let result = subsystem.client
|
||||
let result = subsystem
|
||||
.client
|
||||
.header(BlockId::Hash(hash))
|
||||
.map_err(|e| e.to_string().into());
|
||||
subsystem.metrics.on_request(result.is_ok());
|
||||
@@ -119,7 +112,7 @@ where
|
||||
.map_err(|e| e.to_string().into());
|
||||
subsystem.metrics.on_request(result.is_ok());
|
||||
let _ = response_channel.send(result);
|
||||
}
|
||||
},
|
||||
ChainApiMessage::FinalizedBlockHash(number, response_channel) => {
|
||||
let _timer = subsystem.metrics.time_finalized_block_hash();
|
||||
// Note: we don't verify it's finalized
|
||||
@@ -158,7 +151,7 @@ where
|
||||
hash = header.parent_hash;
|
||||
Some(Ok(hash))
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
@@ -166,7 +159,7 @@ where
|
||||
subsystem.metrics.on_request(result.is_ok());
|
||||
let _ = response_channel.send(result);
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -218,7 +211,9 @@ impl Metrics {
|
||||
}
|
||||
|
||||
/// Provide a timer for `finalized_block_number` which observes on drop.
|
||||
fn time_finalized_block_number(&self) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
|
||||
fn time_finalized_block_number(
|
||||
&self,
|
||||
) -> Option<metrics::prometheus::prometheus::HistogramTimer> {
|
||||
self.0.as_ref().map(|metrics| metrics.finalized_block_number.start_timer())
|
||||
}
|
||||
|
||||
@@ -242,57 +237,45 @@ impl metrics::Metrics for Metrics {
|
||||
registry,
|
||||
)?,
|
||||
block_number: prometheus::register(
|
||||
prometheus::Histogram::with_opts(
|
||||
prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_block_number",
|
||||
"Time spent within `chain_api::block_number`",
|
||||
)
|
||||
)?,
|
||||
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_block_number",
|
||||
"Time spent within `chain_api::block_number`",
|
||||
))?,
|
||||
registry,
|
||||
)?,
|
||||
block_header: prometheus::register(
|
||||
prometheus::Histogram::with_opts(
|
||||
prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_block_headers",
|
||||
"Time spent within `chain_api::block_headers`",
|
||||
)
|
||||
)?,
|
||||
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_block_headers",
|
||||
"Time spent within `chain_api::block_headers`",
|
||||
))?,
|
||||
registry,
|
||||
)?,
|
||||
block_weight: prometheus::register(
|
||||
prometheus::Histogram::with_opts(
|
||||
prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_block_weight",
|
||||
"Time spent within `chain_api::block_weight`",
|
||||
)
|
||||
)?,
|
||||
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_block_weight",
|
||||
"Time spent within `chain_api::block_weight`",
|
||||
))?,
|
||||
registry,
|
||||
)?,
|
||||
finalized_block_hash: prometheus::register(
|
||||
prometheus::Histogram::with_opts(
|
||||
prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_finalized_block_hash",
|
||||
"Time spent within `chain_api::finalized_block_hash`",
|
||||
)
|
||||
)?,
|
||||
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_finalized_block_hash",
|
||||
"Time spent within `chain_api::finalized_block_hash`",
|
||||
))?,
|
||||
registry,
|
||||
)?,
|
||||
finalized_block_number: prometheus::register(
|
||||
prometheus::Histogram::with_opts(
|
||||
prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_finalized_block_number",
|
||||
"Time spent within `chain_api::finalized_block_number`",
|
||||
)
|
||||
)?,
|
||||
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_finalized_block_number",
|
||||
"Time spent within `chain_api::finalized_block_number`",
|
||||
))?,
|
||||
registry,
|
||||
)?,
|
||||
ancestors: prometheus::register(
|
||||
prometheus::Histogram::with_opts(
|
||||
prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_ancestors",
|
||||
"Time spent within `chain_api::ancestors`",
|
||||
)
|
||||
)?,
|
||||
prometheus::Histogram::with_opts(prometheus::HistogramOpts::new(
|
||||
"parachain_chain_api_ancestors",
|
||||
"Time spent within `chain_api::ancestors`",
|
||||
))?,
|
||||
registry,
|
||||
)?,
|
||||
};
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
use super::*;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use futures::{future::BoxFuture, channel::oneshot};
|
||||
use futures::{channel::oneshot, future::BoxFuture};
|
||||
use parity_scale_codec::Encode;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use polkadot_primitives::v1::{Hash, BlockNumber, BlockId, Header};
|
||||
use polkadot_node_primitives::BlockWeight;
|
||||
use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TestSubsystemContextHandle};
|
||||
use polkadot_primitives::v1::{BlockId, BlockNumber, Hash, Header};
|
||||
use sp_blockchain::Info as BlockInfo;
|
||||
use sp_core::testing::TaskExecutor;
|
||||
|
||||
@@ -79,10 +79,7 @@ impl Default for TestClient {
|
||||
|
||||
fn last_key_value<K: Clone, V: Clone>(map: &BTreeMap<K, V>) -> (K, V) {
|
||||
assert!(!map.is_empty());
|
||||
map.iter()
|
||||
.last()
|
||||
.map(|(k, v)| (k.clone(), v.clone()))
|
||||
.unwrap()
|
||||
map.iter().last().map(|(k, v)| (k.clone(), v.clone())).unwrap()
|
||||
}
|
||||
|
||||
impl HeaderBackend<Block> for TestClient {
|
||||
@@ -110,12 +107,9 @@ impl HeaderBackend<Block> for TestClient {
|
||||
fn header(&self, id: BlockId) -> sp_blockchain::Result<Option<Header>> {
|
||||
match id {
|
||||
// for error path testing
|
||||
BlockId::Hash(hash) if hash.is_zero() => {
|
||||
Err(sp_blockchain::Error::Backend("Zero hashes are illegal!".into()))
|
||||
}
|
||||
BlockId::Hash(hash) => {
|
||||
Ok(self.headers.get(&hash).cloned())
|
||||
}
|
||||
BlockId::Hash(hash) if hash.is_zero() =>
|
||||
Err(sp_blockchain::Error::Backend("Zero hashes are illegal!".into())),
|
||||
BlockId::Hash(hash) => Ok(self.headers.get(&hash).cloned()),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
@@ -125,8 +119,10 @@ impl HeaderBackend<Block> for TestClient {
|
||||
}
|
||||
|
||||
fn test_harness(
|
||||
test: impl FnOnce(Arc<TestClient>, TestSubsystemContextHandle<ChainApiMessage>)
|
||||
-> BoxFuture<'static, ()>,
|
||||
test: impl FnOnce(
|
||||
Arc<TestClient>,
|
||||
TestSubsystemContextHandle<ChainApiMessage>,
|
||||
) -> BoxFuture<'static, ()>,
|
||||
) {
|
||||
let (ctx, ctx_handle) = make_subsystem_context(TaskExecutor::new());
|
||||
let client = Arc::new(TestClient::default());
|
||||
@@ -174,15 +170,18 @@ fn request_block_number() {
|
||||
for (hash, expected) in &test_cases {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::BlockNumber(*hash, tx),
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::BlockNumber(*hash, tx),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(rx.await.unwrap().unwrap(), *expected);
|
||||
}
|
||||
|
||||
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
|
||||
}.boxed()
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -198,15 +197,18 @@ fn request_block_header() {
|
||||
for (hash, expected) in &test_cases {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::BlockHeader(*hash, tx),
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::BlockHeader(*hash, tx),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(rx.await.unwrap().unwrap(), *expected);
|
||||
}
|
||||
|
||||
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
|
||||
}.boxed()
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -223,15 +225,18 @@ fn request_block_weight() {
|
||||
for (hash, expected) in &test_cases {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::BlockWeight(*hash, tx),
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::BlockWeight(*hash, tx),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(rx.await.unwrap().unwrap(), *expected);
|
||||
}
|
||||
|
||||
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
|
||||
}.boxed()
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -246,15 +251,18 @@ fn request_finalized_hash() {
|
||||
for (number, expected) in &test_cases {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::FinalizedBlockHash(*number, tx),
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::FinalizedBlockHash(*number, tx),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(rx.await.unwrap().unwrap(), *expected);
|
||||
}
|
||||
|
||||
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
|
||||
}.boxed()
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -265,14 +273,17 @@ fn request_last_finalized_number() {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
|
||||
let expected = client.info().finalized_number;
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::FinalizedBlockNumber(tx),
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::FinalizedBlockNumber(tx),
|
||||
})
|
||||
.await;
|
||||
|
||||
assert_eq!(rx.await.unwrap().unwrap(), expected);
|
||||
|
||||
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
|
||||
}.boxed()
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
}
|
||||
|
||||
@@ -281,24 +292,35 @@ fn request_ancestors() {
|
||||
test_harness(|_client, mut sender| {
|
||||
async move {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::Ancestors { hash: THREE, k: 4, response_channel: tx },
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::Ancestors { hash: THREE, k: 4, response_channel: tx },
|
||||
})
|
||||
.await;
|
||||
assert_eq!(rx.await.unwrap().unwrap(), vec![TWO, ONE]);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::Ancestors { hash: TWO, k: 1, response_channel: tx },
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::Ancestors { hash: TWO, k: 1, response_channel: tx },
|
||||
})
|
||||
.await;
|
||||
assert_eq!(rx.await.unwrap().unwrap(), vec![ONE]);
|
||||
|
||||
let (tx, rx) = oneshot::channel();
|
||||
sender.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::Ancestors { hash: ERROR_PATH, k: 2, response_channel: tx },
|
||||
}).await;
|
||||
sender
|
||||
.send(FromOverseer::Communication {
|
||||
msg: ChainApiMessage::Ancestors {
|
||||
hash: ERROR_PATH,
|
||||
k: 2,
|
||||
response_channel: tx,
|
||||
},
|
||||
})
|
||||
.await;
|
||||
assert!(rx.await.unwrap().is_err());
|
||||
|
||||
sender.send(FromOverseer::Signal(OverseerSignal::Conclude)).await;
|
||||
}.boxed()
|
||||
}
|
||||
.boxed()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user