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
@@ -19,7 +19,7 @@ use crate::BenchmarkCmd;
use codec::{Decode, Encode};
use frame_benchmarking::{Analysis, BenchmarkBatch, BenchmarkSelector};
use frame_support::traits::StorageInfo;
use sc_cli::{SharedParams, CliConfiguration, ExecutionStrategy, Result};
use sc_cli::{CliConfiguration, ExecutionStrategy, Result, SharedParams};
use sc_client_db::BenchmarkingState;
use sc_executor::NativeExecutor;
use sc_service::{Configuration, NativeExecutionDispatch};
@@ -49,11 +49,15 @@ impl BenchmarkCmd {
}
if let Some(header_file) = &self.header {
if !header_file.is_file() { return Err("Header file is invalid!".into()) };
if !header_file.is_file() {
return Err("Header file is invalid!".into())
};
}
if let Some(handlebars_template_file) = &self.template {
if !handlebars_template_file.is_file() { return Err("Handlebars template file is invalid!".into()) };
if !handlebars_template_file.is_file() {
return Err("Handlebars template file is invalid!".into())
};
}
let spec = config.chain_spec;
@@ -93,7 +97,8 @@ impl BenchmarkCmd {
self.repeat,
!self.no_verify,
self.extra,
).encode(),
)
.encode(),
extensions,
&sp_state_machine::backend::BackendRuntimeCode::new(&state).runtime_code()?,
sp_core::testing::TaskExecutor::new(),
@@ -126,20 +131,25 @@ impl BenchmarkCmd {
);
// Skip raw data + analysis if there are no results
if batch.results.is_empty() { continue }
if batch.results.is_empty() {
continue
}
if self.raw_data {
// Print the table header
batch.results[0].components.iter().for_each(|param| print!("{:?},", param.0));
batch.results[0]
.components
.iter()
.for_each(|param| print!("{:?},", param.0));
print!("extrinsic_time_ns,storage_root_time_ns,reads,repeat_reads,writes,repeat_writes,proof_size_bytes\n");
// Print the values
batch.results.iter().for_each(|result| {
let parameters = &result.components;
parameters.iter().for_each(|param| print!("{:?},", param.1));
// Print extrinsic time and storage root time
print!("{:?},{:?},{:?},{:?},{:?},{:?},{:?}\n",
print!(
"{:?},{:?},{:?},{:?},{:?},{:?},{:?}\n",
result.extrinsic_time,
result.storage_root_time,
result.reads,
@@ -156,25 +166,39 @@ impl BenchmarkCmd {
// Conduct analysis.
if !self.no_median_slopes {
println!("Median Slopes Analysis\n========");
if let Some(analysis) = Analysis::median_slopes(&batch.results, BenchmarkSelector::ExtrinsicTime) {
if let Some(analysis) = Analysis::median_slopes(
&batch.results,
BenchmarkSelector::ExtrinsicTime,
) {
println!("-- Extrinsic Time --\n{}", analysis);
}
if let Some(analysis) = Analysis::median_slopes(&batch.results, BenchmarkSelector::Reads) {
if let Some(analysis) =
Analysis::median_slopes(&batch.results, BenchmarkSelector::Reads)
{
println!("Reads = {:?}", analysis);
}
if let Some(analysis) = Analysis::median_slopes(&batch.results, BenchmarkSelector::Writes) {
if let Some(analysis) =
Analysis::median_slopes(&batch.results, BenchmarkSelector::Writes)
{
println!("Writes = {:?}", analysis);
}
}
if !self.no_min_squares {
println!("Min Squares Analysis\n========");
if let Some(analysis) = Analysis::min_squares_iqr(&batch.results, BenchmarkSelector::ExtrinsicTime) {
if let Some(analysis) = Analysis::min_squares_iqr(
&batch.results,
BenchmarkSelector::ExtrinsicTime,
) {
println!("-- Extrinsic Time --\n{}", analysis);
}
if let Some(analysis) = Analysis::min_squares_iqr(&batch.results, BenchmarkSelector::Reads) {
if let Some(analysis) =
Analysis::min_squares_iqr(&batch.results, BenchmarkSelector::Reads)
{
println!("Reads = {:?}", analysis);
}
if let Some(analysis) = Analysis::min_squares_iqr(&batch.results, BenchmarkSelector::Writes) {
if let Some(analysis) =
Analysis::min_squares_iqr(&batch.results, BenchmarkSelector::Writes)
{
println!("Writes = {:?}", analysis);
}
}
@@ -17,21 +17,23 @@
// Outputs benchmark results to Rust files that can be ingested by the runtime.
use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::PathBuf;
use core::convert::TryInto;
use std::{
collections::{HashMap, HashSet},
fs,
path::PathBuf,
};
use serde::Serialize;
use inflector::Inflector;
use serde::Serialize;
use crate::BenchmarkCmd;
use frame_benchmarking::{
BenchmarkBatch, BenchmarkSelector, Analysis, AnalysisChoice, RegressionModel, BenchmarkResults,
Analysis, AnalysisChoice, BenchmarkBatch, BenchmarkResults, BenchmarkSelector, RegressionModel,
};
use frame_support::traits::StorageInfo;
use sp_core::hexdisplay::HexDisplay;
use sp_runtime::traits::Zero;
use frame_support::traits::StorageInfo;
const VERSION: &'static str = env!("CARGO_PKG_VERSION");
const TEMPLATE: &str = include_str!("./template.hbs");
@@ -117,7 +119,9 @@ fn map_results(
analysis_choice: &AnalysisChoice,
) -> Result<HashMap<(String, String), Vec<BenchmarkData>>, std::io::Error> {
// Skip if batches is empty.
if batches.is_empty() { return Err(io_error("empty batches")) }
if batches.is_empty() {
return Err(io_error("empty batches"))
}
let mut all_benchmarks = HashMap::new();
let mut pallet_benchmarks = Vec::new();
@@ -125,7 +129,9 @@ fn map_results(
let mut batches_iter = batches.iter().peekable();
while let Some(batch) = batches_iter.next() {
// Skip if there are no results
if batch.results.is_empty() { continue }
if batch.results.is_empty() {
continue
}
let pallet_string = String::from_utf8(batch.pallet.clone()).unwrap();
let instance_string = String::from_utf8(batch.instance.clone()).unwrap();
@@ -150,13 +156,11 @@ fn map_results(
}
// Get an iterator of errors from a model. If the model is `None` all errors are zero.
fn extract_errors(model: &Option<RegressionModel>) -> impl Iterator<Item=u128> + '_ {
fn extract_errors(model: &Option<RegressionModel>) -> impl Iterator<Item = u128> + '_ {
let mut errors = model.as_ref().map(|m| m.se.regressor_values.iter());
std::iter::from_fn(move || {
match &mut errors {
Some(model) => model.next().map(|val| *val as u128),
_ => Some(0),
}
std::iter::from_fn(move || match &mut errors {
Some(model) => model.next().map(|val| *val as u128),
_ => Some(0),
})
}
@@ -189,12 +193,16 @@ fn get_benchmark_data(
let mut used_reads = Vec::new();
let mut used_writes = Vec::new();
extrinsic_time.slopes.into_iter()
extrinsic_time
.slopes
.into_iter()
.zip(extrinsic_time.names.iter())
.zip(extract_errors(&extrinsic_time.model))
.for_each(|((slope, name), error)| {
if !slope.is_zero() {
if !used_components.contains(&name) { used_components.push(name); }
if !used_components.contains(&name) {
used_components.push(name);
}
used_extrinsic_time.push(ComponentSlope {
name: name.clone(),
slope: slope.saturating_mul(1000),
@@ -202,35 +210,36 @@ fn get_benchmark_data(
});
}
});
reads.slopes.into_iter()
reads
.slopes
.into_iter()
.zip(reads.names.iter())
.zip(extract_errors(&reads.model))
.for_each(|((slope, name), error)| {
if !slope.is_zero() {
if !used_components.contains(&name) { used_components.push(name); }
used_reads.push(ComponentSlope {
name: name.clone(),
slope,
error,
});
if !used_components.contains(&name) {
used_components.push(name);
}
used_reads.push(ComponentSlope { name: name.clone(), slope, error });
}
});
writes.slopes.into_iter()
writes
.slopes
.into_iter()
.zip(writes.names.iter())
.zip(extract_errors(&writes.model))
.for_each(|((slope, name), error)| {
if !slope.is_zero() {
if !used_components.contains(&name) { used_components.push(name); }
used_writes.push(ComponentSlope {
name: name.clone(),
slope,
error,
});
if !used_components.contains(&name) {
used_components.push(name);
}
used_writes.push(ComponentSlope { name: name.clone(), slope, error });
}
});
// This puts a marker on any component which is entirely unused in the weight formula.
let components = batch.results[0].components
let components = batch.results[0]
.components
.iter()
.map(|(name, _)| -> Component {
let name_string = name.to_string();
@@ -264,12 +273,8 @@ pub fn write_results(
) -> Result<(), std::io::Error> {
// Use custom template if provided.
let template: String = match &cmd.template {
Some(template_file) => {
fs::read_to_string(template_file)?
},
None => {
TEMPLATE.to_string()
},
Some(template_file) => fs::read_to_string(template_file)?,
None => TEMPLATE.to_string(),
};
// Use header if provided
@@ -288,9 +293,8 @@ pub fn write_results(
let args = std::env::args().collect::<Vec<String>>();
// Which analysis function should be used when outputting benchmarks
let analysis_choice: AnalysisChoice = cmd.output_analysis.clone()
.try_into()
.map_err(|e| io_error(e))?;
let analysis_choice: AnalysisChoice =
cmd.output_analysis.clone().try_into().map_err(|e| io_error(e))?;
// Capture individual args
let cmd_data = CmdData {
@@ -341,7 +345,8 @@ pub fn write_results(
};
let mut output_file = fs::File::create(file_path)?;
handlebars.render_template_to_write(&template, &hbs_data, &mut output_file)
handlebars
.render_template_to_write(&template, &hbs_data, &mut output_file)
.map_err(|e| io_error(&e.to_string()))?;
}
Ok(())
@@ -355,7 +360,9 @@ fn add_storage_comments(
results: &[BenchmarkResults],
storage_info: &[StorageInfo],
) {
let storage_info_map = storage_info.iter().map(|info| (info.prefix.clone(), info))
let storage_info_map = storage_info
.iter()
.map(|info| (info.prefix.clone(), info))
.collect::<HashMap<_, _>>();
// This tracks the keys we already identified, so we only generate a single comment.
let mut identified = HashSet::<Vec<u8>>::new();
@@ -363,12 +370,14 @@ fn add_storage_comments(
for result in results.clone() {
for (key, reads, writes, whitelisted) in &result.keys {
// skip keys which are whitelisted
if *whitelisted { continue; }
if *whitelisted {
continue
}
let prefix_length = key.len().min(32);
let prefix = key[0..prefix_length].to_vec();
if identified.contains(&prefix) {
// skip adding comments for keys we already identified
continue;
continue
} else {
// track newly identified keys
identified.insert(prefix.clone());
@@ -377,8 +386,10 @@ fn add_storage_comments(
Some(key_info) => {
let comment = format!(
"Storage: {} {} (r:{} w:{})",
String::from_utf8(key_info.pallet_name.clone()).expect("encoded from string"),
String::from_utf8(key_info.storage_name.clone()).expect("encoded from string"),
String::from_utf8(key_info.pallet_name.clone())
.expect("encoded from string"),
String::from_utf8(key_info.storage_name.clone())
.expect("encoded from string"),
reads,
writes,
);
@@ -392,7 +403,7 @@ fn add_storage_comments(
writes,
);
comments.push(comment)
}
},
}
}
}
@@ -400,7 +411,8 @@ fn add_storage_comments(
// Add an underscore after every 3rd character, i.e. a separator for large numbers.
fn underscore<Number>(i: Number) -> String
where Number: std::string::ToString
where
Number: std::string::ToString,
{
let mut s = String::new();
let i_str = i.to_string();
@@ -420,11 +432,12 @@ fn underscore<Number>(i: Number) -> String
struct UnderscoreHelper;
impl handlebars::HelperDef for UnderscoreHelper {
fn call<'reg: 'rc, 'rc>(
&self, h: &handlebars::Helper,
&self,
h: &handlebars::Helper,
_: &handlebars::Handlebars,
_: &handlebars::Context,
_rc: &mut handlebars::RenderContext,
out: &mut dyn handlebars::Output
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
use handlebars::JsonRender;
let param = h.param(0).unwrap();
@@ -439,17 +452,20 @@ impl handlebars::HelperDef for UnderscoreHelper {
struct JoinHelper;
impl handlebars::HelperDef for JoinHelper {
fn call<'reg: 'rc, 'rc>(
&self, h: &handlebars::Helper,
&self,
h: &handlebars::Helper,
_: &handlebars::Handlebars,
_: &handlebars::Context,
_rc: &mut handlebars::RenderContext,
out: &mut dyn handlebars::Output
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
use handlebars::JsonRender;
let param = h.param(0).unwrap();
let value = param.value();
let joined = if value.is_array() {
value.as_array().unwrap()
value
.as_array()
.unwrap()
.iter()
.map(|v| v.render())
.collect::<Vec<String>>()
@@ -465,9 +481,9 @@ impl handlebars::HelperDef for JoinHelper {
// u128 does not serialize well into JSON for `handlebars`, so we represent it as a string.
fn string_serialize<S>(x: &u128, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
S: serde::Serializer,
{
s.serialize_str(&x.to_string())
s.serialize_str(&x.to_string())
}
#[cfg(test)]
@@ -475,22 +491,26 @@ mod test {
use super::*;
use frame_benchmarking::{BenchmarkBatch, BenchmarkParameter, BenchmarkResults};
fn test_data(pallet: &[u8], benchmark: &[u8], param: BenchmarkParameter, base: u32, slope: u32) -> BenchmarkBatch {
fn test_data(
pallet: &[u8],
benchmark: &[u8],
param: BenchmarkParameter,
base: u32,
slope: u32,
) -> BenchmarkBatch {
let mut results = Vec::new();
for i in 0 .. 5 {
results.push(
BenchmarkResults {
components: vec![(param, i), (BenchmarkParameter::z, 0)],
extrinsic_time: (base + slope * i).into(),
storage_root_time: (base + slope * i).into(),
reads: (base + slope * i).into(),
repeat_reads: 0,
writes: (base + slope * i).into(),
repeat_writes: 0,
proof_size: 0,
keys: vec![],
}
)
for i in 0..5 {
results.push(BenchmarkResults {
components: vec![(param, i), (BenchmarkParameter::z, 0)],
extrinsic_time: (base + slope * i).into(),
storage_root_time: (base + slope * i).into(),
reads: (base + slope * i).into(),
repeat_reads: 0,
writes: (base + slope * i).into(),
repeat_writes: 0,
proof_size: 0,
keys: vec![],
})
}
return BenchmarkBatch {
@@ -506,37 +526,25 @@ mod test {
benchmark.components,
vec![
Component { name: component.to_string(), is_used: true },
Component { name: "z".to_string(), is_used: false},
Component { name: "z".to_string(), is_used: false },
],
);
// Weights multiplied by 1,000
assert_eq!(benchmark.base_weight, base * 1_000);
assert_eq!(
benchmark.component_weight,
vec![ComponentSlope {
name: component.to_string(),
slope: slope * 1_000,
error: 0,
}]
vec![ComponentSlope { name: component.to_string(), slope: slope * 1_000, error: 0 }]
);
// DB Reads/Writes are untouched
assert_eq!(benchmark.base_reads, base);
assert_eq!(
benchmark.component_reads,
vec![ComponentSlope {
name: component.to_string(),
slope,
error: 0,
}]
vec![ComponentSlope { name: component.to_string(), slope, error: 0 }]
);
assert_eq!(benchmark.base_writes, base);
assert_eq!(
benchmark.component_writes,
vec![ComponentSlope {
name: component.to_string(),
slope,
error: 0,
}]
vec![ComponentSlope { name: component.to_string(), slope, error: 0 }]
);
}
@@ -550,23 +558,24 @@ mod test {
],
&[],
&AnalysisChoice::default(),
).unwrap();
)
.unwrap();
let first_benchmark = &mapped_results.get(
&("first_pallet".to_string(), "instance".to_string())
).unwrap()[0];
let first_benchmark = &mapped_results
.get(&("first_pallet".to_string(), "instance".to_string()))
.unwrap()[0];
assert_eq!(first_benchmark.name, "first_benchmark");
check_data(first_benchmark, "a", 10, 3);
let second_benchmark = &mapped_results.get(
&("first_pallet".to_string(), "instance".to_string())
).unwrap()[1];
let second_benchmark = &mapped_results
.get(&("first_pallet".to_string(), "instance".to_string()))
.unwrap()[1];
assert_eq!(second_benchmark.name, "second_benchmark");
check_data(second_benchmark, "b", 9, 2);
let second_pallet_benchmark = &mapped_results.get(
&("second_pallet".to_string(), "instance".to_string())
).unwrap()[0];
let second_pallet_benchmark = &mapped_results
.get(&("second_pallet".to_string(), "instance".to_string()))
.unwrap()[0];
assert_eq!(second_pallet_benchmark.name, "first_benchmark");
check_data(second_pallet_benchmark, "c", 3, 4);
}
@@ -20,4 +20,3 @@
mod pallet_id;
pub use pallet_id::PalletIdCmd;
@@ -17,22 +17,19 @@
//! Implementation of the `palletid` subcommand
use sc_cli::{
Error, utils::print_from_uri, CryptoSchemeFlag,
OutputTypeFlag, KeystoreParams, with_crypto_scheme,
};
use sp_runtime::traits::AccountIdConversion;
use sp_core::crypto::{Ss58Codec, Ss58AddressFormat};
use std::convert::{TryInto, TryFrom};
use structopt::StructOpt;
use frame_support::PalletId;
use sc_cli::{
utils::print_from_uri, with_crypto_scheme, CryptoSchemeFlag, Error, KeystoreParams,
OutputTypeFlag,
};
use sp_core::crypto::{Ss58AddressFormat, Ss58Codec};
use sp_runtime::traits::AccountIdConversion;
use std::convert::{TryFrom, TryInto};
use structopt::StructOpt;
/// The `palletid` command
#[derive(Debug, StructOpt)]
#[structopt(
name = "palletid",
about = "Inspect a module ID address"
)]
#[structopt(name = "palletid", about = "Inspect a module ID address")]
pub struct PalletIdCmd {
/// The module ID used to derive the account
id: String,
@@ -63,18 +60,18 @@ pub struct PalletIdCmd {
impl PalletIdCmd {
/// runs the command
pub fn run<R>(&self) -> Result<(), Error>
where
R: frame_system::Config,
R::AccountId: Ss58Codec,
where
R: frame_system::Config,
R::AccountId: Ss58Codec,
{
if self.id.len() != 8 {
Err("a module id must be a string of 8 characters")?
}
let password = self.keystore_params.read_password()?;
let id_fixed_array: [u8; 8] = self.id.as_bytes()
.try_into()
.map_err(|_| "Cannot convert argument to palletid: argument should be 8-character string")?;
let id_fixed_array: [u8; 8] = self.id.as_bytes().try_into().map_err(|_| {
"Cannot convert argument to palletid: argument should be 8-character string"
})?;
let account_id: R::AccountId = PalletId(id_fixed_array).into_account();
@@ -91,4 +88,3 @@ impl PalletIdCmd {
Ok(())
}
}
@@ -20,22 +20,20 @@
//! An equivalent of `sp_io::TestExternalities` that can load its state from a remote substrate
//! based chain, or a local state snapshot file.
use codec::{Decode, Encode};
use jsonrpsee_ws_client::{types::v2::params::JsonRpcParams, WsClient, WsClientBuilder};
use log::*;
use sp_core::{
hashing::twox_128,
hexdisplay::HexDisplay,
storage::{StorageData, StorageKey},
};
pub use sp_io::TestExternalities;
use sp_runtime::traits::Block as BlockT;
use std::{
fs,
path::{Path, PathBuf},
};
use log::*;
use sp_core::hashing::twox_128;
pub use sp_io::TestExternalities;
use sp_core::{
hexdisplay::HexDisplay,
storage::{StorageKey, StorageData},
};
use codec::{Encode, Decode};
use sp_runtime::traits::Block as BlockT;
use jsonrpsee_ws_client::{
WsClientBuilder, WsClient, types::v2::params::JsonRpcParams,
};
pub mod rpc_api;
@@ -122,7 +120,10 @@ pub struct OnlineConfig<B: BlockT> {
impl<B: BlockT> OnlineConfig<B> {
/// Return rpc (ws) client.
fn rpc_client(&self) -> &WsClient {
self.transport.client.as_ref().expect("ws client must have been initialized by now; qed.")
self.transport
.client
.as_ref()
.expect("ws client must have been initialized by now; qed.")
}
}
@@ -137,7 +138,6 @@ impl<B: BlockT> Default for OnlineConfig<B> {
}
}
/// Configuration of the state snapshot.
#[derive(Clone)]
pub struct SnapshotConfig {
@@ -208,10 +208,12 @@ impl<B: BlockT> Builder<B> {
maybe_at: Option<B::Hash>,
) -> Result<StorageData, &'static str> {
trace!(target: LOG_TARGET, "rpc: get_storage");
RpcApi::<B>::get_storage(self.as_online().rpc_client(), key, maybe_at).await.map_err(|e| {
error!("Error = {:?}", e);
"rpc get_storage failed."
})
RpcApi::<B>::get_storage(self.as_online().rpc_client(), key, maybe_at)
.await
.map_err(|e| {
error!("Error = {:?}", e);
"rpc get_storage failed."
})
}
/// Get the latest finalized head.
async fn rpc_get_head(&self) -> Result<B::Hash, &'static str> {
@@ -249,7 +251,7 @@ impl<B: BlockT> Builder<B> {
if page_len < PAGE as usize {
debug!(target: LOG_TARGET, "last page received: {}", page_len);
break all_keys;
break all_keys
} else {
let new_last_key =
all_keys.last().expect("all_keys is populated; has .last(); qed");
@@ -290,21 +292,22 @@ impl<B: BlockT> Builder<B> {
.map(|key| {
(
"state_getStorage",
JsonRpcParams::Array(
vec![
to_value(key).expect("json serialization will work; qed."),
to_value(at).expect("json serialization will work; qed."),
]
),
JsonRpcParams::Array(vec![
to_value(key).expect("json serialization will work; qed."),
to_value(at).expect("json serialization will work; qed."),
]),
)
})
.collect::<Vec<_>>();
let values = client.batch_request::<Option<StorageData>>(batch)
.await
.map_err(|e| {
log::error!(target: LOG_TARGET, "failed to execute batch: {:?}. Error: {:?}", chunk_keys, e);
"batch failed."
})?;
let values = client.batch_request::<Option<StorageData>>(batch).await.map_err(|e| {
log::error!(
target: LOG_TARGET,
"failed to execute batch: {:?}. Error: {:?}",
chunk_keys,
e
);
"batch failed."
})?;
assert_eq!(chunk_keys.len(), values.len());
for (idx, key) in chunk_keys.into_iter().enumerate() {
let maybe_value = values[idx].clone();
@@ -428,7 +431,7 @@ impl<B: BlockT> Builder<B> {
self.save_state_snapshot(&kp, &c.path)?;
}
kp
}
},
};
info!(
@@ -497,7 +500,7 @@ impl<B: BlockT> Builder<B> {
#[cfg(test)]
mod test_prelude {
pub(crate) use super::*;
pub(crate) use sp_runtime::testing::{H256 as Hash, Block as RawBlock, ExtrinsicWrapper};
pub(crate) use sp_runtime::testing::{Block as RawBlock, ExtrinsicWrapper, H256 as Hash};
pub(crate) type Block = RawBlock<ExtrinsicWrapper<Hash>>;
@@ -551,7 +554,11 @@ mod remote_tests {
init_logger();
Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
modules: vec!["Proxy".to_owned(), "Multisig".to_owned(), "PhragmenElection".to_owned()],
modules: vec![
"Proxy".to_owned(),
"Multisig".to_owned(),
"PhragmenElection".to_owned(),
],
..Default::default()
}))
.build()
@@ -18,14 +18,13 @@
//! WS RPC API for one off RPC calls to a substrate node.
// TODO: Consolidate one off RPC calls https://github.com/paritytech/substrate/issues/8988
use sp_runtime::{generic::SignedBlock, traits::{Block as BlockT, Header as HeaderT}};
use jsonrpsee_ws_client::{
WsClientBuilder,
WsClient,
types::{
v2::params::JsonRpcParams,
traits::Client
},
types::{traits::Client, v2::params::JsonRpcParams},
WsClient, WsClientBuilder,
};
use sp_runtime::{
generic::SignedBlock,
traits::{Block as BlockT, Header as HeaderT},
};
/// Get the header of the block identified by `at`
@@ -38,7 +37,8 @@ where
let params = vec![hash_to_json::<Block>(at)?];
let client = build_client(from).await?;
client.request::<Block::Header>("chain_getHeader", JsonRpcParams::Array(params))
client
.request::<Block::Header>("chain_getHeader", JsonRpcParams::Array(params))
.await
.map_err(|e| format!("chain_getHeader request failed: {:?}", e))
}
@@ -51,7 +51,8 @@ where
{
let client = build_client(from).await?;
client.request::<Block::Hash>("chain_getFinalizedHead", JsonRpcParams::NoParams)
client
.request::<Block::Hash>("chain_getFinalizedHead", JsonRpcParams::NoParams)
.await
.map_err(|e| format!("chain_getFinalizedHead request failed: {:?}", e))
}
@@ -81,7 +82,7 @@ fn hash_to_json<Block: BlockT>(hash: Block::Hash) -> Result<serde_json::Value, S
/// Build a website client that connects to `from`.
async fn build_client<S: AsRef<str>>(from: S) -> Result<WsClient, String> {
WsClientBuilder::default()
WsClientBuilder::default()
.max_request_body_size(u32::MAX)
.build(from.as_ref())
.await
+8 -19
View File
@@ -20,16 +20,14 @@
#![warn(missing_docs)]
use codec::{DecodeAll, FullCodec, FullEncode};
use core::marker::PhantomData;
use frame_support::storage::generator::{StorageDoubleMap, StorageMap, StorageValue};
use futures::compat::Future01CompatExt;
use jsonrpc_client_transports::RpcError;
use codec::{DecodeAll, FullCodec, FullEncode};
use serde::{de::DeserializeOwned, Serialize};
use frame_support::storage::generator::{
StorageDoubleMap, StorageMap, StorageValue
};
use sp_storage::{StorageData, StorageKey};
use sc_rpc_api::state::StateClient;
use serde::{de::DeserializeOwned, Serialize};
use sp_storage::{StorageData, StorageKey};
/// A typed query on chain state usable from an RPC client.
///
@@ -54,7 +52,7 @@ use sc_rpc_api::state::StateClient;
/// # struct TestRuntime;
/// #
/// # decl_module! {
/// # pub struct Module<T: Config> for enum Call where origin: T::Origin {}
/// # pub struct Module<T: Config> for enum Call where origin: T::Origin {}
/// # }
/// #
/// pub type Loc = (i64, i64, i64);
@@ -98,18 +96,12 @@ pub struct StorageQuery<V> {
impl<V: FullCodec> StorageQuery<V> {
/// Create a storage query for a StorageValue.
pub fn value<St: StorageValue<V>>() -> Self {
Self {
key: StorageKey(St::storage_value_final_key().to_vec()),
_spook: PhantomData,
}
Self { key: StorageKey(St::storage_value_final_key().to_vec()), _spook: PhantomData }
}
/// Create a storage query for a value in a StorageMap.
pub fn map<St: StorageMap<K, V>, K: FullEncode>(key: K) -> Self {
Self {
key: StorageKey(St::storage_map_final_key(key)),
_spook: PhantomData,
}
Self { key: StorageKey(St::storage_map_final_key(key)), _spook: PhantomData }
}
/// Create a storage query for a value in a StorageDoubleMap.
@@ -117,10 +109,7 @@ impl<V: FullCodec> StorageQuery<V> {
key1: K1,
key2: K2,
) -> Self {
Self {
key: StorageKey(St::storage_double_map_final_key(key1, key2)),
_spook: PhantomData,
}
Self { key: StorageKey(St::storage_double_map_final_key(key1, key2)), _spook: PhantomData }
}
/// Send this query over RPC, await the typed result.
+71 -93
View File
@@ -20,28 +20,22 @@
use std::sync::Arc;
use codec::{self, Codec, Decode, Encode};
use sc_client_api::light::{future_header, RemoteBlockchain, Fetcher, RemoteCallRequest};
use futures::future::{ready, TryFutureExt};
use jsonrpc_core::{
futures::future::{self as rpc_future, result, Future},
Error as RpcError, ErrorCode,
futures::future::{self as rpc_future,result, Future},
};
use jsonrpc_derive::rpc;
use futures::future::{ready, TryFutureExt};
use sp_blockchain::{
HeaderBackend,
Error as ClientError
};
use sp_runtime::{
generic::BlockId,
traits,
};
use sp_core::{hexdisplay::HexDisplay, Bytes};
use sc_transaction_pool_api::{TransactionPool, InPoolTransaction};
use sp_block_builder::BlockBuilder;
use sc_client_api::light::{future_header, Fetcher, RemoteBlockchain, RemoteCallRequest};
use sc_rpc_api::DenyUnsafe;
use sc_transaction_pool_api::{InPoolTransaction, TransactionPool};
use sp_block_builder::BlockBuilder;
use sp_blockchain::{Error as ClientError, HeaderBackend};
use sp_core::{hexdisplay::HexDisplay, Bytes};
use sp_runtime::{generic::BlockId, traits};
pub use frame_system_rpc_runtime_api::AccountNonceApi;
pub use self::gen_client::Client as SystemClient;
pub use frame_system_rpc_runtime_api::AccountNonceApi;
/// Future that resolves to account nonce.
pub type FutureResult<T> = Box<dyn Future<Item = T, Error = RpcError> + Send>;
@@ -89,13 +83,8 @@ pub struct FullSystem<P: TransactionPool, C, B> {
impl<P: TransactionPool, C, B> FullSystem<P, C, B> {
/// Create new `FullSystem` given client and transaction pool.
pub fn new(client: Arc<C>, pool: Arc<P>, deny_unsafe: DenyUnsafe,) -> Self {
FullSystem {
client,
pool,
deny_unsafe,
_marker: Default::default(),
}
pub fn new(client: Arc<C>, pool: Arc<P>, deny_unsafe: DenyUnsafe) -> Self {
FullSystem { client, pool, deny_unsafe, _marker: Default::default() }
}
}
@@ -130,35 +119,37 @@ where
Box::new(result(get_nonce()))
}
fn dry_run(&self, extrinsic: Bytes, at: Option<<Block as traits::Block>::Hash>) -> FutureResult<Bytes> {
fn dry_run(
&self,
extrinsic: Bytes,
at: Option<<Block as traits::Block>::Hash>,
) -> FutureResult<Bytes> {
if let Err(err) = self.deny_unsafe.check_if_safe() {
return Box::new(rpc_future::err(err.into()));
return Box::new(rpc_future::err(err.into()))
}
let dry_run = || {
let api = self.client.runtime_api();
let at = BlockId::<Block>::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash
));
self.client.info().best_hash));
let uxt: <Block as traits::Block>::Extrinsic = Decode::decode(&mut &*extrinsic).map_err(|e| RpcError {
code: ErrorCode::ServerError(Error::DecodeError.into()),
message: "Unable to dry run extrinsic.".into(),
data: Some(format!("{:?}", e).into()),
})?;
let result = api.apply_extrinsic(&at, uxt)
let uxt: <Block as traits::Block>::Extrinsic = Decode::decode(&mut &*extrinsic)
.map_err(|e| RpcError {
code: ErrorCode::ServerError(Error::RuntimeError.into()),
code: ErrorCode::ServerError(Error::DecodeError.into()),
message: "Unable to dry run extrinsic.".into(),
data: Some(format!("{:?}", e).into()),
})?;
let result = api.apply_extrinsic(&at, uxt).map_err(|e| RpcError {
code: ErrorCode::ServerError(Error::RuntimeError.into()),
message: "Unable to dry run extrinsic.".into(),
data: Some(format!("{:?}", e).into()),
})?;
Ok(Encode::encode(&result).into())
};
Box::new(result(dry_run()))
}
}
@@ -179,12 +170,7 @@ impl<P: TransactionPool, C, F, Block> LightSystem<P, C, F, Block> {
fetcher: Arc<F>,
pool: Arc<P>,
) -> Self {
LightSystem {
client,
remote_blockchain,
fetcher,
pool,
}
LightSystem { client, remote_blockchain, fetcher, pool }
}
}
@@ -205,21 +191,27 @@ where
let future_best_header = future_header(&*self.remote_blockchain, &*self.fetcher, best_id);
let fetcher = self.fetcher.clone();
let call_data = account.encode();
let future_best_header = future_best_header
.and_then(move |maybe_best_header| ready(
maybe_best_header.ok_or_else(|| { ClientError::UnknownBlock(format!("{}", best_hash)) })
));
let future_nonce = future_best_header.and_then(move |best_header|
fetcher.remote_call(RemoteCallRequest {
block: best_hash,
header: best_header,
method: "AccountNonceApi_account_nonce".into(),
call_data,
retry_count: None,
let future_best_header = future_best_header.and_then(move |maybe_best_header| {
ready(
maybe_best_header
.ok_or_else(|| ClientError::UnknownBlock(format!("{}", best_hash))),
)
});
let future_nonce = future_best_header
.and_then(move |best_header| {
fetcher.remote_call(RemoteCallRequest {
block: best_hash,
header: best_header,
method: "AccountNonceApi_account_nonce".into(),
call_data,
retry_count: None,
})
})
).compat();
let future_nonce = future_nonce.and_then(|nonce| Decode::decode(&mut &nonce[..])
.map_err(|e| ClientError::CallResultDecode("Cannot decode account nonce", e)));
.compat();
let future_nonce = future_nonce.and_then(|nonce| {
Decode::decode(&mut &nonce[..])
.map_err(|e| ClientError::CallResultDecode("Cannot decode account nonce", e))
});
let future_nonce = future_nonce.map_err(|e| RpcError {
code: ErrorCode::ServerError(Error::RuntimeError.into()),
message: "Unable to query nonce.".into(),
@@ -232,7 +224,11 @@ where
Box::new(future_nonce)
}
fn dry_run(&self, _extrinsic: Bytes, _at: Option<<Block as traits::Block>::Hash>) -> FutureResult<Bytes> {
fn dry_run(
&self,
_extrinsic: Bytes,
_at: Option<<Block as traits::Block>::Hash>,
) -> FutureResult<Bytes> {
Box::new(result(Err(RpcError {
code: ErrorCode::MethodNotFound,
message: "Unable to dry run extrinsic.".into(),
@@ -243,11 +239,8 @@ where
/// Adjust account nonce from state, so that tx with the nonce will be
/// placed after all ready txpool transactions.
fn adjust_nonce<P, AccountId, Index>(
pool: &P,
account: AccountId,
nonce: Index,
) -> Index where
fn adjust_nonce<P, AccountId, Index>(pool: &P, account: AccountId, nonce: Index) -> Index
where
P: TransactionPool,
AccountId: Clone + std::fmt::Display + Encode,
Index: Clone + std::fmt::Display + Encode + traits::AtLeast32Bit + 'static,
@@ -285,9 +278,12 @@ mod tests {
use super::*;
use futures::executor::block_on;
use substrate_test_runtime_client::{runtime::Transfer, AccountKeyring};
use sc_transaction_pool::BasicPool;
use sp_runtime::{ApplyExtrinsicResult, transaction_validity::{TransactionValidityError, InvalidTransaction}};
use sp_runtime::{
transaction_validity::{InvalidTransaction, TransactionValidityError},
ApplyExtrinsicResult,
};
use substrate_test_runtime_client::{runtime::Transfer, AccountKeyring};
#[test]
fn should_return_next_nonce_for_some_account() {
@@ -296,13 +292,8 @@ mod tests {
// given
let client = Arc::new(substrate_test_runtime_client::new());
let spawner = sp_core::testing::TaskExecutor::new();
let pool = BasicPool::new_full(
Default::default(),
true.into(),
None,
spawner,
client.clone(),
);
let pool =
BasicPool::new_full(Default::default(), true.into(), None, spawner, client.clone());
let source = sp_runtime::transaction_validity::TransactionSource::External;
let new_transaction = |nonce: u64| {
@@ -336,13 +327,8 @@ mod tests {
// given
let client = Arc::new(substrate_test_runtime_client::new());
let spawner = sp_core::testing::TaskExecutor::new();
let pool = BasicPool::new_full(
Default::default(),
true.into(),
None,
spawner,
client.clone(),
);
let pool =
BasicPool::new_full(Default::default(), true.into(), None, spawner, client.clone());
let accounts = FullSystem::new(client, pool, DenyUnsafe::Yes);
@@ -360,13 +346,8 @@ mod tests {
// given
let client = Arc::new(substrate_test_runtime_client::new());
let spawner = sp_core::testing::TaskExecutor::new();
let pool = BasicPool::new_full(
Default::default(),
true.into(),
None,
spawner,
client.clone(),
);
let pool =
BasicPool::new_full(Default::default(), true.into(), None, spawner, client.clone());
let accounts = FullSystem::new(client, pool, DenyUnsafe::No);
@@ -375,7 +356,8 @@ mod tests {
to: AccountKeyring::Bob.into(),
amount: 5,
nonce: 0,
}.into_signed_tx();
}
.into_signed_tx();
// when
let res = accounts.dry_run(tx.encode().into(), None);
@@ -393,13 +375,8 @@ mod tests {
// given
let client = Arc::new(substrate_test_runtime_client::new());
let spawner = sp_core::testing::TaskExecutor::new();
let pool = BasicPool::new_full(
Default::default(),
true.into(),
None,
spawner,
client.clone(),
);
let pool =
BasicPool::new_full(Default::default(), true.into(), None, spawner, client.clone());
let accounts = FullSystem::new(client, pool, DenyUnsafe::No);
@@ -408,7 +385,8 @@ mod tests {
to: AccountKeyring::Bob.into(),
amount: 5,
nonce: 100,
}.into_signed_tx();
}
.into_signed_tx();
// when
let res = accounts.dry_run(tx.encode().into(), None);
@@ -18,24 +18,23 @@
//! `Structopt`-ready structs for `try-runtime`.
use parity_scale_codec::{Decode, Encode};
use std::{fmt::Debug, path::PathBuf, str::FromStr, sync::Arc};
use sc_service::Configuration;
use remote_externalities::{rpc_api, Builder, Mode, OfflineConfig, OnlineConfig, SnapshotConfig};
use sc_chain_spec::ChainSpec;
use sc_cli::{CliConfiguration, ExecutionStrategy, WasmExecutionMethod};
use sc_executor::NativeExecutor;
use sc_service::NativeExecutionDispatch;
use sc_chain_spec::ChainSpec;
use sp_state_machine::StateMachine;
use sp_runtime::traits::{Block as BlockT, NumberFor, Header as HeaderT};
use sc_service::{Configuration, NativeExecutionDispatch};
use sp_core::{
offchain::{
OffchainWorkerExt, OffchainDbExt, TransactionPoolExt,
testing::{TestOffchainExt, TestTransactionPoolExt},
},
storage::{StorageData, StorageKey, well_known_keys},
hashing::twox_128,
offchain::{
testing::{TestOffchainExt, TestTransactionPoolExt},
OffchainDbExt, OffchainWorkerExt, TransactionPoolExt,
},
storage::{well_known_keys, StorageData, StorageKey},
};
use sp_keystore::{KeystoreExt, testing::KeyStore};
use remote_externalities::{Builder, Mode, SnapshotConfig, OfflineConfig, OnlineConfig, rpc_api};
use sp_keystore::{testing::KeyStore, KeystoreExt};
use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
use sp_state_machine::StateMachine;
use std::{fmt::Debug, path::PathBuf, str::FromStr, sync::Arc};
mod parse;
@@ -170,7 +169,7 @@ pub enum State {
/// The modules to scrape. If empty, entire chain state will be scraped.
#[structopt(short, long, require_delimiter = true)]
modules: Option<Vec<String>>,
}
},
}
async fn on_runtime_upgrade<Block, ExecDispatch>(
@@ -192,36 +191,31 @@ where
let mut changes = Default::default();
let max_runtime_instances = config.max_runtime_instances;
let executor = NativeExecutor::<ExecDispatch>::new(
wasm_method.into(),
heap_pages,
max_runtime_instances,
);
let executor =
NativeExecutor::<ExecDispatch>::new(wasm_method.into(), heap_pages, max_runtime_instances);
let ext = {
let builder = match command.state {
State::Snap { snapshot_path } => {
State::Snap { snapshot_path } =>
Builder::<Block>::new().mode(Mode::Offline(OfflineConfig {
state_snapshot: SnapshotConfig::new(snapshot_path),
}))
},
State::Live {
snapshot_path,
modules
} => Builder::<Block>::new().mode(Mode::Online(OnlineConfig {
transport: shared.url.to_owned().into(),
state_snapshot: snapshot_path.as_ref().map(SnapshotConfig::new),
modules: modules.to_owned().unwrap_or_default(),
at: Some(shared.block_at::<Block>()?),
..Default::default()
})),
})),
State::Live { snapshot_path, modules } =>
Builder::<Block>::new().mode(Mode::Online(OnlineConfig {
transport: shared.url.to_owned().into(),
state_snapshot: snapshot_path.as_ref().map(SnapshotConfig::new),
modules: modules.to_owned().unwrap_or_default(),
at: Some(shared.block_at::<Block>()?),
..Default::default()
})),
};
let (code_key, code) = extract_code(config.chain_spec)?;
builder
.inject_key_value(&[(code_key, code)])
.inject_hashed_key(&[twox_128(b"System"), twox_128(b"LastRuntimeUpgrade")].concat())
.build().await?
.build()
.await?
};
let encoded_result = StateMachine::<_, _, NumberFor<Block>, _>::new(
@@ -232,8 +226,7 @@ where
"TryRuntime_on_runtime_upgrade",
&[],
ext.extensions,
&sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend)
.runtime_code()?,
&sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend).runtime_code()?,
sp_core::testing::TaskExecutor::new(),
)
.execute(execution.into())
@@ -271,35 +264,28 @@ where
let mut changes = Default::default();
let max_runtime_instances = config.max_runtime_instances;
let executor = NativeExecutor::<ExecDispatch>::new(
wasm_method.into(),
heap_pages,
max_runtime_instances,
);
let executor =
NativeExecutor::<ExecDispatch>::new(wasm_method.into(), heap_pages, max_runtime_instances);
let mode = match command.state {
State::Live {
snapshot_path,
modules
} => {
let at = shared.block_at::<Block>()?;
let online_config = OnlineConfig {
transport: shared.url.to_owned().into(),
state_snapshot: snapshot_path.as_ref().map(SnapshotConfig::new),
modules: modules.to_owned().unwrap_or_default(),
at: Some(at),
..Default::default()
};
State::Live { snapshot_path, modules } => {
let at = shared.block_at::<Block>()?;
let online_config = OnlineConfig {
transport: shared.url.to_owned().into(),
state_snapshot: snapshot_path.as_ref().map(SnapshotConfig::new),
modules: modules.to_owned().unwrap_or_default(),
at: Some(at),
..Default::default()
};
Mode::Online(online_config)
},
State::Snap { snapshot_path } => {
let mode = Mode::Offline(OfflineConfig {
state_snapshot: SnapshotConfig::new(snapshot_path),
});
Mode::Online(online_config)
},
State::Snap { snapshot_path } => {
let mode =
Mode::Offline(OfflineConfig { state_snapshot: SnapshotConfig::new(snapshot_path) });
mode
}
mode
},
};
let builder = Builder::<Block>::new()
.mode(mode)
@@ -308,10 +294,7 @@ where
let (code_key, code) = extract_code(config.chain_spec)?;
builder.inject_key_value(&[(code_key, code)]).build().await?
} else {
builder
.inject_hashed_key(well_known_keys::CODE)
.build()
.await?
builder.inject_hashed_key(well_known_keys::CODE).build().await?
};
let (offchain, _offchain_state) = TestOffchainExt::new();
@@ -332,8 +315,7 @@ where
"OffchainWorkerApi_offchain_worker",
header.encode().as_ref(),
ext.extensions,
&sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend)
.runtime_code()?,
&sp_state_machine::backend::BackendRuntimeCode::new(&ext.backend).runtime_code()?,
sp_core::testing::TaskExecutor::new(),
)
.execute(execution.into())
@@ -363,20 +345,16 @@ where
let mut changes = Default::default();
let max_runtime_instances = config.max_runtime_instances;
let executor = NativeExecutor::<ExecDispatch>::new(
wasm_method.into(),
heap_pages,
max_runtime_instances,
);
let executor =
NativeExecutor::<ExecDispatch>::new(wasm_method.into(), heap_pages, max_runtime_instances);
let block_hash = shared.block_at::<Block>()?;
let block: Block = rpc_api::get_block::<Block, _>(shared.url.clone(), block_hash).await?;
let mode = match command.state {
State::Snap { snapshot_path } => {
let mode = Mode::Offline(OfflineConfig {
state_snapshot: SnapshotConfig::new(snapshot_path),
});
let mode =
Mode::Offline(OfflineConfig { state_snapshot: SnapshotConfig::new(snapshot_path) });
mode
},
@@ -392,7 +370,7 @@ where
});
mode
}
},
};
let ext = {
@@ -403,10 +381,7 @@ where
let (code_key, code) = extract_code(config.chain_spec)?;
builder.inject_key_value(&[(code_key, code)]).build().await?
} else {
builder
.inject_hashed_key(well_known_keys::CODE)
.build()
.await?
builder.inject_hashed_key(well_known_keys::CODE).build().await?
};
// register externality extensions in order to provide host interface for OCW to the
@@ -459,15 +434,14 @@ impl TryRuntimeCmd {
ExecDispatch: NativeExecutionDispatch + 'static,
{
match &self.command {
Command::OnRuntimeUpgrade(ref cmd) => {
on_runtime_upgrade::<Block, ExecDispatch>(self.shared.clone(), cmd.clone(), config).await
}
Command::OffchainWorker(cmd) => {
offchain_worker::<Block, ExecDispatch>(self.shared.clone(), cmd.clone(), config).await
}
Command::ExecuteBlock(cmd) => {
execute_block::<Block, ExecDispatch>(self.shared.clone(), cmd.clone(), config).await
}
Command::OnRuntimeUpgrade(ref cmd) =>
on_runtime_upgrade::<Block, ExecDispatch>(self.shared.clone(), cmd.clone(), config)
.await,
Command::OffchainWorker(cmd) =>
offchain_worker::<Block, ExecDispatch>(self.shared.clone(), cmd.clone(), config)
.await,
Command::ExecuteBlock(cmd) =>
execute_block::<Block, ExecDispatch>(self.shared.clone(), cmd.clone(), config).await,
}
}
}
@@ -18,11 +18,8 @@
//! Utils for parsing user input
pub(crate) fn hash(block_hash: &str) -> Result<String, String> {
let (block_hash, offset) = if block_hash.starts_with("0x") {
(&block_hash[2..], 2)
} else {
(block_hash, 0)
};
let (block_hash, offset) =
if block_hash.starts_with("0x") { (&block_hash[2..], 2) } else { (block_hash, 0) };
if let Some(pos) = block_hash.chars().position(|c| !c.is_ascii_hexdigit()) {
Err(format!(