Skip to content

Commit

Permalink
chore(gh): static analysis (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey-N-Chernyshov authored Feb 15, 2024
1 parent 82260be commit 474c332
Show file tree
Hide file tree
Showing 16 changed files with 160 additions and 135 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/static_analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Cargo fmt & clippy

on:
pull_request:
push:
branches: [master, develop]

env:
CARGO_TERM_COLOR: always

jobs:
static_analysis:
runs-on: ubuntu-latest

steps:
# v4.1.1
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
with:
submodules: 'true'

# v2.1.0
- uses: arduino/setup-protoc@a8b67ba40b37d35169e222f3bb352603327985b6

# v2.7.1
- uses: Swatinem/rust-cache@3cf7f8cc28d1b4e7d01e3783be10a97d55d483c8

# v2.20.16
- name: cargo install
uses: taiki-e/install-action@ac89944b5b150d78567ab6c02badfbe48b0b55aa
with:
tool: clippy-sarif, sarif-fmt

- name: cargo fmt
run: cargo fmt --check

- name: cargo clippy
env:
SKIP_WASM_BUILD: 1
run: cargo clippy --all-targets --features runtime-benchmarks
--message-format=json -- -D warnings | clippy-sarif | tee results.sarif | sarif-fmt
shell: bash

- name: Upload SARIF file
if: always()
# v2.24.2
uses: github/codeql-action/upload-sarif@49abf0ba24d0b7953cb586944e918a0b92074c80
with:
sarif_file: results.sarif
13 changes: 4 additions & 9 deletions pallets/beefy-light-client/src/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ impl ValidatorSet {

pub fn proofs(&self) -> Vec<Vec<H256>> {
(0..self.addresses.len())
.into_iter()
.map(|i| self.validator_pubkey_proof(i))
.collect()
}
Expand All @@ -132,10 +131,7 @@ impl ValidatorSet {
}

pub fn root(&self) -> H256 {
let root = beefy_merkle_tree::merkle_root::<sp_runtime::traits::Keccak256, _>(
self.addresses.clone(),
);
root
beefy_merkle_tree::merkle_root::<sp_runtime::traits::Keccak256, _>(self.addresses.clone())
}

fn authority_set(&self) -> sp_beefy::mmr::BeefyAuthoritySet<H256> {
Expand Down Expand Up @@ -235,11 +231,10 @@ enum MMRNode {

impl MMRNode {
fn hash(&self) -> H256 {
let hash = match self {
match self {
MMRNode::Leaf(leaf) => sp_runtime::traits::Keccak256::hash_of(leaf),
MMRNode::Hash(hash) => *hash,
};
hash
}
}
}

Expand Down Expand Up @@ -315,7 +310,7 @@ pub fn generate_fixture(validators: usize, tree_size: u32) -> AnyResult<Fixture>
sp_beefy::known_payloads::MMR_ROOT_ID,
mmr_root.encode(),
),
block_number: tree_size as u32,
block_number: tree_size,
validator_set_id: validator_set.id,
};
let signed_commitment = validator_set.sign_commitment(&mut rng, commitment, None);
Expand Down
7 changes: 3 additions & 4 deletions pallets/beefy-light-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ fn validator_proof(
public_key_merkle_proofs.push(fixture.validator_set_proofs[i].clone());
}
}
let validator_proof = bridge_common::beefy_types::ValidatorProof {
ValidatorProof {
signatures: proof_signatures,
positions,
public_keys,
public_key_merkle_proofs: public_key_merkle_proofs,
public_key_merkle_proofs,
validator_claims_bitfield: initial_bitfield,
};
validator_proof
}
}

#[test_case(3, 5; "3 validators, 5 leaves")]
Expand Down
29 changes: 13 additions & 16 deletions pallets/data-signer/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ fn it_works_finish_add_peer() {
assert_ok!(DataSigner::register_network(
RuntimeOrigin::root(),
network_id,
peers.clone(),
peers,
));

let new_peer = test_signer().public();
Expand Down Expand Up @@ -533,7 +533,7 @@ fn it_fails_add_peer_no_pending_update() {
assert_ok!(DataSigner::register_network(
RuntimeOrigin::root(),
network_id,
peers.clone(),
peers,
));

let new_peer = test_signer().public();
Expand Down Expand Up @@ -569,7 +569,7 @@ fn it_works_validate_unsigned() {
assert_eq!(
<DataSigner as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call.into(),
&call,
),
TransactionValidity::Ok(
ValidTransaction::with_tag_prefix("DataSignerApprove")
Expand All @@ -594,7 +594,7 @@ fn it_fails_validate_unsigned_no_network() {
assert_ok!(DataSigner::register_network(
RuntimeOrigin::root(),
network_id,
peers.clone(),
peers,
));

let different_network_id = bridge_types::GenericNetworkId::Sub(SubNetworkId::Kusama);
Expand All @@ -611,7 +611,7 @@ fn it_fails_validate_unsigned_no_network() {
assert_eq!(
<DataSigner as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call.into(),
&call,
),
InvalidTransaction::BadSigner.into()
);
Expand All @@ -628,7 +628,7 @@ fn it_fails_validate_unsigned_bad_proof() {
assert_ok!(DataSigner::register_network(
RuntimeOrigin::root(),
network_id,
peers.clone(),
peers,
));

let data = [1u8; 32];
Expand All @@ -643,7 +643,7 @@ fn it_fails_validate_unsigned_bad_proof() {
assert_eq!(
<DataSigner as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call.into(),
&call,
),
InvalidTransaction::BadProof.into()
);
Expand All @@ -660,7 +660,7 @@ fn it_fails_validate_unsigned_bad_signer() {
assert_ok!(DataSigner::register_network(
RuntimeOrigin::root(),
network_id,
peers.clone(),
peers,
));

let data = [1u8; 32];
Expand All @@ -675,7 +675,7 @@ fn it_fails_validate_unsigned_bad_signer() {
assert_eq!(
<DataSigner as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call.into(),
&call,
),
InvalidTransaction::BadSigner.into()
);
Expand All @@ -692,7 +692,7 @@ fn it_fails_validate_unsigned_transaction_stale() {
assert_ok!(DataSigner::register_network(
RuntimeOrigin::root(),
network_id,
peers.clone(),
peers,
));

let data = [1u8; 32];
Expand All @@ -716,7 +716,7 @@ fn it_fails_validate_unsigned_transaction_stale() {
assert_eq!(
<DataSigner as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::InBlock,
&call.into(),
&call,
),
InvalidTransaction::Stale.into()
);
Expand All @@ -736,15 +736,12 @@ fn it_fails_validate_unsigned_invalid_call() {
peers.clone(),
));

let call = Call::register_network {
network_id,
peers: peers.clone(),
};
let call = Call::register_network { network_id, peers };

assert_eq!(
<DataSigner as sp_runtime::traits::ValidateUnsigned>::validate_unsigned(
TransactionSource::External,
&call.into(),
&call,
),
InvalidTransaction::Call.into()
);
Expand Down
10 changes: 5 additions & 5 deletions pallets/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ impl frame_system::Config for Test {
}

pub struct CallFilter;
impl frame_support::traits::Contains<RuntimeCall> for CallFilter {
impl Contains<RuntimeCall> for CallFilter {
fn contains(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::System(frame_system::pallet::Call::<Test>::remark { .. }) => true,
_ => false,
}
matches!(
call,
RuntimeCall::System(frame_system::pallet::Call::<Test>::remark { .. })
)
}
}

Expand Down
10 changes: 5 additions & 5 deletions pallets/multisig-verifier/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ fn it_works_delete_peer() {
().into()
);

let key = peers.last().unwrap().clone();
let key = *peers.last().unwrap();

assert_ok!(
TrustedVerifier::remove_peer(RuntimeOrigin::signed(alice::<Test>()), key,),
Expand All @@ -189,7 +189,7 @@ fn it_works_delete_peer() {
#[test]
fn it_fails_delete_peer_not_initialized() {
new_test_ext().execute_with(|| {
let key = test_peers().last().unwrap().clone();
let key = *test_peers().last().unwrap();

assert_noop!(
TrustedVerifier::remove_peer(RuntimeOrigin::signed(alice::<Test>()), key,),
Expand Down Expand Up @@ -230,7 +230,7 @@ fn it_works_verify_signatures() {
fn it_fails_verify_dublicated_signatures() {
new_test_ext().execute_with(|| {
let pairs = test_pairs();
let peers: Vec<ecdsa::Public> = pairs.clone().into_iter().map(|x| x.public()).collect();
let peers: Vec<ecdsa::Public> = pairs.into_iter().map(|x| x.public()).collect();
assert_ok!(
TrustedVerifier::initialize(
RuntimeOrigin::root(),
Expand Down Expand Up @@ -266,7 +266,7 @@ fn it_fails_verify_dublicated_signatures() {
fn it_fails_verify_not_enough_signatures() {
new_test_ext().execute_with(|| {
let pairs = test_pairs();
let peers: Vec<ecdsa::Public> = pairs.clone().into_iter().map(|x| x.public()).collect();
let peers: Vec<ecdsa::Public> = pairs.into_iter().map(|x| x.public()).collect();
assert_ok!(
TrustedVerifier::initialize(
RuntimeOrigin::root(),
Expand Down Expand Up @@ -301,7 +301,7 @@ fn it_fails_verify_not_enough_signatures() {
fn it_fails_verify_invalid_signature() {
new_test_ext().execute_with(|| {
let pairs = test_pairs();
let peers: Vec<ecdsa::Public> = pairs.clone().into_iter().map(|x| x.public()).collect();
let peers: Vec<ecdsa::Public> = pairs.into_iter().map(|x| x.public()).collect();
assert_ok!(
TrustedVerifier::initialize(
RuntimeOrigin::root(),
Expand Down
2 changes: 1 addition & 1 deletion pallets/parachain-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ pub mod pallet {
.ok_or(Error::<T>::UnknownPrecision)?;

let (amount, sidechain_amount) =
T::BalancePrecisionConverter::to_sidechain(&asset_id, precision, amount.clone())
T::BalancePrecisionConverter::to_sidechain(&asset_id, precision, amount)
.ok_or(Error::<T>::WrongAmount)?;

ensure!(sidechain_amount > 0, Error::<T>::WrongAmount);
Expand Down
18 changes: 9 additions & 9 deletions pallets/parachain-app/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ type Block = frame_system::mocking::MockBlock<Test>;
Deserialize,
)]
pub enum AssetId {
XOR,
ETH,
DAI,
Xor,
Eth,
Dai,
Custom(u8),
}

Expand Down Expand Up @@ -187,7 +187,7 @@ impl currencies::Config for Test {
type WeightInfo = ();
}
parameter_types! {
pub const GetBaseAssetId: AssetId = AssetId::XOR;
pub const GetBaseAssetId: AssetId = AssetId::Xor;
pub GetTeamReservesAccountId: AccountId = AccountId32::from([0; 32]);
pub GetFeesAccountId: AccountId = AccountId32::from([1; 32]);
pub GetTreasuryAccountId: AccountId = AccountId32::from([2; 32]);
Expand All @@ -213,7 +213,7 @@ parameter_types! {
}

parameter_types! {
pub const FeeCurrency: AssetId = AssetId::XOR;
pub const FeeCurrency: AssetId = AssetId::Xor;
pub const ThisNetworkId: GenericNetworkId = GenericNetworkId::Sub(SubNetworkId::Mainnet);
}

Expand Down Expand Up @@ -257,7 +257,7 @@ impl BridgeAssetRegistry<AccountId, AssetId> for AssetRegistryImpl {
_symbol: Self::AssetSymbol,
) -> Result<AssetId, sp_runtime::DispatchError> {
match name.as_str() {
"XOR" => Ok(AssetId::XOR),
"XOR" => Ok(AssetId::Xor),
"KSM" => Ok(AssetId::Custom(1)),
_ => Ok(AssetId::Custom(0)),
}
Expand All @@ -272,7 +272,7 @@ impl BridgeAssetRegistry<AccountId, AssetId> for AssetRegistryImpl {

fn get_raw_info(asset_id: AssetId) -> bridge_types::types::RawAssetInfo {
match asset_id {
AssetId::XOR => bridge_types::types::RawAssetInfo {
AssetId::Xor => bridge_types::types::RawAssetInfo {
name: "XOR".to_owned().into(),
symbol: "XOR".to_owned().into(),
precision: 18,
Expand Down Expand Up @@ -376,7 +376,7 @@ pub fn new_tester() -> sp_io::TestExternalities {
ParachainApp::register_thischain_asset(
Origin::<Test>::Root.into(),
SubNetworkId::Kusama,
AssetId::XOR,
AssetId::Xor,
sidechain_asset,
allowed_parachains.clone(),
minimal_xcm_amount,
Expand All @@ -402,7 +402,7 @@ pub fn new_tester() -> sp_io::TestExternalities {
.into();
ParachainApp::finalize_asset_registration(
origin_kusama.clone(),
AssetId::XOR,
AssetId::Xor,
AssetKind::Thischain,
)
.expect("XOR registration finalization failed");
Expand Down
Loading

0 comments on commit 474c332

Please sign in to comment.