Skip to content

Commit f27b277

Browse files
Add clippy and rustfmt to CI (paritytech#143)
* Add clippy/rustfmt checks to CI Add clippy check to CI Apply uses correctly Specify runs-on Use checkout specified from check-prettier Try specifying toolchain with clippy as components Apply to correct action Specify nightly without date Use 'rustup component add clippy' instead of toolchain Uses on steps Typo Specify rustfmt and clippy in rust-toolchain file Try only rustfmt Undo Revert rust-toolchain file Add --workspace arg to clippy * rustup clippy in init.sh * Introduce 'install toolchain' step for Clippy * Add rustfmt check Add rustfmt check -- * Update .rustfmt.toml to remove license checking (for now) * Enforce rustfmt rules on codebase * Update Cargo.lock * Resolve clippy complaints * Apply rustfmt * Appease clippy @ node/parachain/src/chain_spec.rs Co-authored-by: Joshy Orndorff <[email protected]> * Prefer unwrap_or_else to unwrap_or * Avoid borrowed-box * Add a pre-commit hook for rustfmt/clippy using rusty-hook Co-authored-by: Joshy Orndorff <[email protected]> Co-authored-by: Joshy Orndorff <[email protected]>
1 parent ac7774e commit f27b277

File tree

25 files changed

+999
-818
lines changed

25 files changed

+999
-818
lines changed

.github/workflows/release.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,36 @@ jobs:
9898
- name: Check with Prettier
9999
run: npx prettier --check --ignore-path .gitignore '**/*.(yml|js|ts|json)'
100100

101+
check-clippy:
102+
name: "Code checks"
103+
runs-on: ubuntu-latest
104+
steps:
105+
- name: Checkout
106+
if: github.event_name == 'pull_request_target'
107+
uses: actions/checkout@v2
108+
with:
109+
fetch-depth: 0
110+
ref: refs/pull/${{ github.event.pull_request.number }}/merge
111+
112+
- name: Checkout
113+
if: github.event_name != 'pull_request_target'
114+
uses: actions/checkout@v2
115+
116+
- name: Install toolchain
117+
uses: actions-rs/toolchain@v1
118+
with:
119+
profile: minimal
120+
toolchain: nightly-2020-10-03
121+
components: clippy, rustfmt
122+
target: wasm32-unknown-unknown
123+
default: true
124+
125+
# - name: Check with Clippy
126+
# run: cargo clippy --workspace --tests -- -D warnings
127+
128+
- name: Format code with rustfmt
129+
run: cargo fmt -- --check
130+
101131
####### Building and Testing binaries #######
102132

103133
build:

.rustfmt.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ reorder_imports = true
55
hard_tabs = true
66
max_width = 100
77

8-
license_template_path = "FILE_TEMPLATE"
8+
# TODO: add template for license checking.
9+
# this will still fail for some licenses where e.g. Parity's license is used
10+
# license_template_path = "FILE_TEMPLATE"

.rusty-hook.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[hooks]
2+
pre-commit = "cargo fmt -- --check && cargo clippy"
3+
4+
[logging]
5+
verbose = true

Cargo.lock

Lines changed: 53 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node/parachain/src/chain_spec.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
use cumulus_primitives::ParaId;
1818
use moonbeam_runtime::{
19-
AccountId, BalancesConfig, GenesisConfig, SudoConfig, SystemConfig,
20-
ParachainInfoConfig, WASM_BINARY, EthereumChainIdConfig, EVMConfig, EthereumConfig
19+
AccountId, BalancesConfig, EVMConfig, EthereumChainIdConfig, EthereumConfig, GenesisConfig,
20+
ParachainInfoConfig, SudoConfig, SystemConfig, WASM_BINARY,
2121
};
2222
use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup};
2323
use sc_service::ChainType;
@@ -40,7 +40,7 @@ pub struct Extensions {
4040

4141
impl Extensions {
4242
/// Try to get the extension from the given `ChainSpec`.
43-
pub fn try_get(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Option<&Self> {
43+
pub fn try_get(chain_spec: &dyn sc_service::ChainSpec) -> Option<&Self> {
4444
sc_chain_spec::get_extension(chain_spec.extensions())
4545
}
4646
}
@@ -53,22 +53,15 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
5353
move || {
5454
testnet_genesis(
5555
AccountId::from_str("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").unwrap(),
56-
vec![
57-
AccountId::from_str("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").unwrap(),
58-
],
56+
vec![AccountId::from_str("6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").unwrap()],
5957
para_id,
6058
1280, //ChainId
6159
)
6260
},
6361
vec![],
6462
None,
6563
None,
66-
Some(
67-
serde_json::from_str(
68-
"{\"tokenDecimals\": 18}"
69-
)
70-
.expect("Provided valid json map")
71-
),
64+
Some(serde_json::from_str("{\"tokenDecimals\": 18}").expect("Provided valid json map")),
7265
Extensions {
7366
relay_chain: "local_testnet".into(),
7467
para_id: para_id.into(),
@@ -97,8 +90,10 @@ fn testnet_genesis(
9790
.collect(),
9891
}),
9992
pallet_sudo: Some(SudoConfig { key: root_key }),
100-
parachain_info: Some(ParachainInfoConfig { parachain_id: para_id }),
101-
pallet_ethereum_chain_id: Some(EthereumChainIdConfig { chain_id: chain_id }),
93+
parachain_info: Some(ParachainInfoConfig {
94+
parachain_id: para_id,
95+
}),
96+
pallet_ethereum_chain_id: Some(EthereumChainIdConfig { chain_id }),
10297
pallet_evm: Some(EVMConfig {
10398
accounts: BTreeMap::new(),
10499
}),

node/parachain/src/cli.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
use std::path::PathBuf;
1818

19-
use sc_cli;
2019
use structopt::StructOpt;
2120

2221
/// Sub-commands supported by the collator.

node/parachain/src/command.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use log::info;
2424
use moonbeam_runtime::Block;
2525
use polkadot_parachain::primitives::AccountIdConversion;
2626
use sc_cli::{
27-
ChainSpec, CliConfiguration, ImportParams, KeystoreParams, NetworkParams, Result,
28-
RuntimeVersion, SharedParams, SubstrateCli, DefaultConfigurationValues,
27+
ChainSpec, CliConfiguration, DefaultConfigurationValues, ImportParams, KeystoreParams,
28+
NetworkParams, Result, RuntimeVersion, SharedParams, SubstrateCli,
2929
};
3030
use sc_service::{
3131
config::{BasePath, PrometheusConfig},
@@ -117,15 +117,15 @@ impl SubstrateCli for RelayChainCli {
117117
}
118118

119119
fn load_spec(&self, id: &str) -> std::result::Result<Box<dyn sc_service::ChainSpec>, String> {
120-
polkadot_cli::Cli::from_iter([RelayChainCli::executable_name().to_string()].iter())
121-
.load_spec(id)
120+
polkadot_cli::Cli::from_iter([RelayChainCli::executable_name()].iter()).load_spec(id)
122121
}
123122

124123
fn native_runtime_version(chain_spec: &Box<dyn ChainSpec>) -> &'static RuntimeVersion {
125124
polkadot_cli::Cli::native_runtime_version(chain_spec)
126125
}
127126
}
128127

128+
#[allow(clippy::borrowed_box)]
129129
fn extract_genesis_wasm(chain_spec: &Box<dyn sc_service::ChainSpec>) -> Result<Vec<u8>> {
130130
let mut storage = chain_spec.build_storage()?;
131131

@@ -254,14 +254,14 @@ pub fn run() -> Result<()> {
254254
// TODO
255255
let key = sp_core::Pair::generate().0;
256256

257-
let extension = chain_spec::Extensions::try_get(&config.chain_spec);
257+
let extension = chain_spec::Extensions::try_get(&*config.chain_spec);
258258
let relay_chain_id = extension.map(|e| e.relay_chain.clone());
259259
let para_id = extension.map(|e| e.para_id);
260260

261261
let polkadot_cli = RelayChainCli::new(
262262
config.base_path.as_ref().map(|x| x.path().join("polkadot")),
263263
relay_chain_id,
264-
[RelayChainCli::executable_name().to_string()]
264+
[RelayChainCli::executable_name()]
265265
.iter()
266266
.chain(cli.relaychain_args.iter()),
267267
);

node/parachain/src/service.rs

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use cumulus_network::build_block_announce_validator;
1818
use cumulus_service::{
1919
prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
2020
};
21+
use frontier_consensus::FrontierBlockImport;
22+
use moonbeam_runtime::{opaque::Block, RuntimeApi};
2123
use polkadot_primitives::v0::CollatorPair;
2224
use sc_executor::native_executor_instance;
2325
pub use sc_executor::NativeExecutor;
@@ -26,8 +28,6 @@ use sp_core::Pair;
2628
use sp_runtime::traits::BlakeTwo256;
2729
use sp_trie::PrefixedMemoryDB;
2830
use std::sync::Arc;
29-
use frontier_consensus::FrontierBlockImport;
30-
use moonbeam_runtime::{RuntimeApi, opaque::Block};
3131
// Our native executor instance.
3232
native_executor_instance!(
3333
pub Executor,
@@ -42,26 +42,17 @@ type FullBackend = TFullBackend<Block>;
4242
///
4343
/// Use this macro if you don't actually need the full service, but just the builder in order to
4444
/// be able to perform chain operations.
45+
#[allow(clippy::type_complexity)]
4546
pub fn new_partial(
4647
config: &Configuration,
4748
) -> Result<
4849
PartialComponents<
4950
FullClient,
5051
FullBackend,
5152
(),
52-
sp_consensus::import_queue::BasicQueue<
53-
Block,
54-
PrefixedMemoryDB<BlakeTwo256>,
55-
>,
56-
sc_transaction_pool::FullPool<
57-
Block,
58-
FullClient,
59-
>,
60-
FrontierBlockImport<
61-
Block,
62-
Arc<FullClient>,
63-
FullClient,
64-
>,
53+
sp_consensus::import_queue::BasicQueue<Block, PrefixedMemoryDB<BlakeTwo256>>,
54+
sc_transaction_pool::FullPool<Block, FullClient>,
55+
FrontierBlockImport<Block, Arc<FullClient>, FullClient>,
6556
>,
6657
sc_service::Error,
6758
> {
@@ -80,18 +71,14 @@ pub fn new_partial(
8071
client.clone(),
8172
);
8273

83-
let frontier_block_import = FrontierBlockImport::new(
84-
client.clone(),
85-
client.clone(),
86-
true
87-
);
74+
let frontier_block_import = FrontierBlockImport::new(client.clone(), client.clone(), true);
8875

8976
let import_queue = cumulus_consensus::import_queue::import_queue(
9077
client.clone(),
9178
frontier_block_import.clone(),
9279
inherent_data_providers.clone(),
9380
&task_manager.spawn_handle(),
94-
registry.clone(),
81+
registry,
9582
)?;
9683

9784
let params = PartialComponents {
@@ -119,7 +106,7 @@ async fn start_node_impl<RB>(
119106
id: polkadot_primitives::v0::Id,
120107
validator: bool,
121108
_rpc_ext_builder: RB,
122-
) -> sc_service::error::Result<(TaskManager,Arc<FullClient>)>
109+
) -> sc_service::error::Result<(TaskManager, Arc<FullClient>)>
123110
where
124111
RB: Fn(
125112
Arc<TFullClient<Block, RuntimeApi, Executor>>,
@@ -163,13 +150,13 @@ where
163150
let block_import = params.other;
164151
let (network, network_status_sinks, system_rpc_tx, start_network) =
165152
sc_service::build_network(sc_service::BuildNetworkParams {
166-
config: &parachain_config,
167-
client: client.clone(),
168-
transaction_pool: transaction_pool.clone(),
169-
spawn_handle: task_manager.spawn_handle(),
170-
import_queue,
171-
on_demand: None,
172-
block_announce_validator_builder: Some(Box::new(|_| block_announce_validator)),
153+
config: &parachain_config,
154+
client: client.clone(),
155+
transaction_pool: transaction_pool.clone(),
156+
spawn_handle: task_manager.spawn_handle(),
157+
import_queue,
158+
on_demand: None,
159+
block_announce_validator_builder: Some(Box::new(|_| block_announce_validator)),
173160
})?;
174161

175162
let is_authority = parachain_config.role.is_authority();
@@ -191,17 +178,14 @@ where
191178
command_sink: None,
192179
};
193180

194-
moonbeam_rpc::create_full(
195-
deps,
196-
subscription_task_executor.clone()
197-
)
181+
moonbeam_rpc::create_full(deps, subscription_task_executor.clone())
198182
})
199183
};
200184

201185
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
202186
on_demand: None,
203187
remote_blockchain: None,
204-
rpc_extensions_builder: rpc_extensions_builder,
188+
rpc_extensions_builder,
205189
client: client.clone(),
206190
transaction_pool: transaction_pool.clone(),
207191
task_manager: &mut task_manager,
@@ -232,7 +216,7 @@ where
232216

233217
let params = StartCollatorParams {
234218
para_id: id,
235-
block_import: block_import,
219+
block_import,
236220
proposer_factory,
237221
inherent_data_providers: params.inherent_data_providers,
238222
block_status: client.clone(),

0 commit comments

Comments
 (0)