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
+94 -96
View File
@@ -19,15 +19,20 @@
//! Substrate chain configurations.
#![warn(missing_docs)]
use std::{borrow::Cow, fs::File, path::PathBuf, sync::Arc, collections::HashMap};
use serde::{Serialize, Deserialize};
use sp_core::{storage::{StorageKey, StorageData, ChildInfo, Storage, StorageChild}, Bytes};
use sp_runtime::BuildStorage;
use serde_json as json;
use crate::{RuntimeGenesis, ChainType, extension::GetExtension, Properties};
use crate::{extension::GetExtension, ChainType, Properties, RuntimeGenesis};
use sc_network::config::MultiaddrWithPeerId;
use sc_telemetry::TelemetryEndpoints;
use sp_runtime::traits::{Block as BlockT, NumberFor};
use serde::{Deserialize, Serialize};
use serde_json as json;
use sp_core::{
storage::{ChildInfo, Storage, StorageChild, StorageData, StorageKey},
Bytes,
};
use sp_runtime::{
traits::{Block as BlockT, NumberFor},
BuildStorage,
};
use std::{borrow::Cow, collections::HashMap, fs::File, path::PathBuf, sync::Arc};
enum GenesisSource<G> {
File(PathBuf),
@@ -56,8 +61,8 @@ impl<G: RuntimeGenesis> GenesisSource<G> {
match self {
Self::File(path) => {
let file = File::open(path)
.map_err(|e| format!("Error opening spec file: {}", e))?;
let file =
File::open(path).map_err(|e| format!("Error opening spec file: {}", e))?;
let genesis: GenesisContainer<G> = json::from_reader(file)
.map_err(|e| format!("Error parsing spec file: {}", e))?;
Ok(genesis.genesis)
@@ -69,22 +74,25 @@ impl<G: RuntimeGenesis> GenesisSource<G> {
},
Self::Factory(f) => Ok(Genesis::Runtime(f())),
Self::Storage(storage) => {
let top = storage.top
let top = storage
.top
.iter()
.map(|(k, v)| (StorageKey(k.clone()), StorageData(v.clone())))
.collect();
let children_default = storage.children_default
let children_default = storage
.children_default
.iter()
.map(|(k, child)|
(
StorageKey(k.clone()),
child.data
.map(|(k, child)| {
(
StorageKey(k.clone()),
child
.data
.iter()
.map(|(k, v)| (StorageKey(k.clone()), StorageData(v.clone())))
.collect()
)
)
.collect(),
)
})
.collect();
Ok(Genesis::Raw(RawGenesis { top, children_default }))
@@ -99,24 +107,24 @@ impl<G: RuntimeGenesis, E> BuildStorage for ChainSpec<G, E> {
Genesis::Runtime(gc) => gc.build_storage(),
Genesis::Raw(RawGenesis { top: map, children_default: children_map }) => Ok(Storage {
top: map.into_iter().map(|(k, v)| (k.0, v.0)).collect(),
children_default: children_map.into_iter().map(|(storage_key, child_content)| {
let child_info = ChildInfo::new_default(storage_key.0.as_slice());
(
storage_key.0,
StorageChild {
data: child_content.into_iter().map(|(k, v)| (k.0, v.0)).collect(),
child_info,
},
)
}).collect(),
children_default: children_map
.into_iter()
.map(|(storage_key, child_content)| {
let child_info = ChildInfo::new_default(storage_key.0.as_slice());
(
storage_key.0,
StorageChild {
data: child_content.into_iter().map(|(k, v)| (k.0, v.0)).collect(),
child_info,
},
)
})
.collect(),
}),
}
}
fn assimilate_storage(
&self,
_: &mut Storage,
) -> Result<(), String> {
fn assimilate_storage(&self, _: &mut Storage) -> Result<(), String> {
Err("`assimilate_storage` not implemented for `ChainSpec`.".into())
}
}
@@ -181,10 +189,7 @@ pub struct ChainSpec<G, E = NoExtension> {
impl<G, E: Clone> Clone for ChainSpec<G, E> {
fn clone(&self) -> Self {
ChainSpec {
client_spec: self.client_spec.clone(),
genesis: self.genesis.clone(),
}
ChainSpec { client_spec: self.client_spec.clone(), genesis: self.genesis.clone() }
}
}
@@ -258,10 +263,7 @@ impl<G, E> ChainSpec<G, E> {
code_substitutes: HashMap::new(),
};
ChainSpec {
client_spec,
genesis: GenesisSource::Factory(Arc::new(constructor)),
}
ChainSpec { client_spec, genesis: GenesisSource::Factory(Arc::new(constructor)) }
}
/// Type of the chain.
@@ -281,22 +283,15 @@ impl<G, E: serde::de::DeserializeOwned> ChainSpec<G, E> {
let json = json.into();
let client_spec = json::from_slice(json.as_ref())
.map_err(|e| format!("Error parsing spec file: {}", e))?;
Ok(ChainSpec {
client_spec,
genesis: GenesisSource::Binary(json),
})
Ok(ChainSpec { client_spec, genesis: GenesisSource::Binary(json) })
}
/// Parse json file into a `ChainSpec`
pub fn from_json_file(path: PathBuf) -> Result<Self, String> {
let file = File::open(&path)
.map_err(|e| format!("Error opening spec file: {}", e))?;
let client_spec = json::from_reader(file)
.map_err(|e| format!("Error parsing spec file: {}", e))?;
Ok(ChainSpec {
client_spec,
genesis: GenesisSource::File(path),
})
let file = File::open(&path).map_err(|e| format!("Error opening spec file: {}", e))?;
let client_spec =
json::from_reader(file).map_err(|e| format!("Error parsing spec file: {}", e))?;
Ok(ChainSpec { client_spec, genesis: GenesisSource::File(path) })
}
}
@@ -312,33 +307,34 @@ impl<G: RuntimeGenesis, E: serde::Serialize + Clone + 'static> ChainSpec<G, E> {
let genesis = match (raw, self.genesis.resolve()?) {
(true, Genesis::Runtime(g)) => {
let storage = g.build_storage()?;
let top = storage.top.into_iter()
.map(|(k, v)| (StorageKey(k), StorageData(v)))
.collect();
let children_default = storage.children_default.into_iter()
.map(|(sk, child)| (
StorageKey(sk),
child.data.into_iter()
.map(|(k, v)| (StorageKey(k), StorageData(v)))
.collect(),
))
let top =
storage.top.into_iter().map(|(k, v)| (StorageKey(k), StorageData(v))).collect();
let children_default = storage
.children_default
.into_iter()
.map(|(sk, child)| {
(
StorageKey(sk),
child
.data
.into_iter()
.map(|(k, v)| (StorageKey(k), StorageData(v)))
.collect(),
)
})
.collect();
Genesis::Raw(RawGenesis { top, children_default })
},
(_, genesis) => genesis,
};
Ok(JsonContainer {
client_spec: self.client_spec.clone(),
genesis,
})
Ok(JsonContainer { client_spec: self.client_spec.clone(), genesis })
}
/// Dump to json string.
pub fn as_json(&self, raw: bool) -> Result<String, String> {
let container = self.json_container(raw)?;
json::to_string_pretty(&container)
.map_err(|e| format!("Error generating spec json: {}", e))
json::to_string_pretty(&container).map_err(|e| format!("Error generating spec json: {}", e))
}
}
@@ -404,7 +400,11 @@ where
}
fn code_substitutes(&self) -> std::collections::HashMap<String, Vec<u8>> {
self.client_spec.code_substitutes.iter().map(|(h, c)| (h.clone(), c.0.clone())).collect()
self.client_spec
.code_substitutes
.iter()
.map(|(h, c)| (h.clone(), c.0.clone()))
.collect()
}
}
@@ -417,7 +417,8 @@ pub struct LightSyncState<Block: BlockT> {
/// The babe weight of the finalized block.
pub babe_finalized_block_weight: sp_consensus_babe::BabeBlockWeight,
/// The authority set for grandpa.
pub grandpa_authority_set: sc_finality_grandpa::AuthoritySet<<Block as BlockT>::Hash, NumberFor<Block>>,
pub grandpa_authority_set:
sc_finality_grandpa::AuthoritySet<<Block as BlockT>::Hash, NumberFor<Block>>,
}
impl<Block: BlockT> LightSyncState<Block> {
@@ -427,25 +428,25 @@ impl<Block: BlockT> LightSyncState<Block> {
SerializableLightSyncState {
finalized_block_header: StorageData(self.finalized_block_header.encode()),
babe_epoch_changes:
StorageData(self.babe_epoch_changes.encode()),
babe_finalized_block_weight:
self.babe_finalized_block_weight,
grandpa_authority_set:
StorageData(self.grandpa_authority_set.encode()),
babe_epoch_changes: StorageData(self.babe_epoch_changes.encode()),
babe_finalized_block_weight: self.babe_finalized_block_weight,
grandpa_authority_set: StorageData(self.grandpa_authority_set.encode()),
}
}
/// Convert from a `SerializableLightSyncState`.
pub fn from_serializable(serialized: &SerializableLightSyncState) -> Result<Self, codec::Error> {
pub fn from_serializable(
serialized: &SerializableLightSyncState,
) -> Result<Self, codec::Error> {
Ok(Self {
finalized_block_header: codec::Decode::decode(&mut &serialized.finalized_block_header.0[..])?,
babe_epoch_changes:
codec::Decode::decode(&mut &serialized.babe_epoch_changes.0[..])?,
babe_finalized_block_weight:
serialized.babe_finalized_block_weight,
grandpa_authority_set:
codec::Decode::decode(&mut &serialized.grandpa_authority_set.0[..])?,
finalized_block_header: codec::Decode::decode(
&mut &serialized.finalized_block_header.0[..],
)?,
babe_epoch_changes: codec::Decode::decode(&mut &serialized.babe_epoch_changes.0[..])?,
babe_finalized_block_weight: serialized.babe_finalized_block_weight,
grandpa_authority_set: codec::Decode::decode(
&mut &serialized.grandpa_authority_set.0[..],
)?,
})
}
}
@@ -469,12 +470,9 @@ mod tests {
struct Genesis(HashMap<String, String>);
impl BuildStorage for Genesis {
fn assimilate_storage(
&self,
storage: &mut Storage,
) -> Result<(), String> {
fn assimilate_storage(&self, storage: &mut Storage) -> Result<(), String> {
storage.top.extend(
self.0.iter().map(|(a, b)| (a.clone().into_bytes(), b.clone().into_bytes()))
self.0.iter().map(|(a, b)| (a.clone().into_bytes(), b.clone().into_bytes())),
);
Ok(())
}
@@ -485,11 +483,10 @@ mod tests {
#[test]
fn should_deserialize_example_chain_spec() {
let spec1 = TestSpec::from_json_bytes(Cow::Owned(
include_bytes!("../res/chain_spec.json").to_vec()
)).unwrap();
let spec2 = TestSpec::from_json_file(
PathBuf::from("./res/chain_spec.json")
).unwrap();
include_bytes!("../res/chain_spec.json").to_vec(),
))
.unwrap();
let spec2 = TestSpec::from_json_file(PathBuf::from("./res/chain_spec.json")).unwrap();
assert_eq!(spec1.as_json(false), spec2.as_json(false));
assert_eq!(spec2.chain_type(), ChainType::Live)
@@ -506,8 +503,9 @@ mod tests {
#[test]
fn should_deserialize_chain_spec_with_extensions() {
let spec = TestSpec2::from_json_bytes(Cow::Owned(
include_bytes!("../res/chain_spec2.json").to_vec()
)).unwrap();
include_bytes!("../res/chain_spec2.json").to_vec(),
))
.unwrap();
assert_eq!(spec.extensions().my_property, "Test Extension");
}