From ff0e2f21d857e6a1c0f41b9bf79a8ebd1180c9e5 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Mon, 1 Apr 2024 16:22:55 +0300 Subject: [PATCH 01/14] Rename release related workflow to release.yml --- .github/workflows/{crunch-ci.yml => release.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{crunch-ci.yml => release.yml} (99%) diff --git a/.github/workflows/crunch-ci.yml b/.github/workflows/release.yml similarity index 99% rename from .github/workflows/crunch-ci.yml rename to .github/workflows/release.yml index 8325744..a0c930e 100644 --- a/.github/workflows/crunch-ci.yml +++ b/.github/workflows/release.yml @@ -1,4 +1,4 @@ -name: CI +name: Release on: workflow_dispatch: From 3a7b9930f7158931c19adac28a58d2dcceec4fba Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Mon, 1 Apr 2024 16:29:43 +0300 Subject: [PATCH 02/14] Copy over create_release.yml from upstream again where their tests are because previous commits didn't leave this file alone but rather made lots of changes into it and then subsequently removed it. --- .github/workflows/create_release.yml | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 .github/workflows/create_release.yml diff --git a/.github/workflows/create_release.yml b/.github/workflows/create_release.yml new file mode 100644 index 0000000..e33f056 --- /dev/null +++ b/.github/workflows/create_release.yml @@ -0,0 +1,83 @@ +on: + push: + # Sequence of patterns matched against refs/tags + tags: + - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + +name: Rust CI - Create Release + +jobs: + check: + name: Create Release + runs-on: ubuntu-20.04 + steps: + - uses: actions/checkout@v2 + + - name: Install Rust latest stable + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rustfmt, clippy + + - name: Run cargo test + uses: actions-rs/cargo@v1 + env: + CRUNCH_CONFIG_FILENAME: .env.example + with: + command: test + + - name: Run cargo build + uses: actions-rs/cargo@v1 + env: + CRUNCH_CONFIG_FILENAME: .env.example + with: + command: build + args: --release + + - name: Generate SHA-256 hash file + run: | + cd ./target/release + sha256sum crunch > crunch.sha256 + + - name: Get Rustc version + id: get_rustc + run: echo ::set-output name=rustc::$(rustc -V) + + - name: Get Tag version + id: get_tag + run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} + + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ steps.get_tag.outputs.tag }} + release_name: Crunch ${{ steps.get_tag.outputs.tag }} + body: "Note: This release was built using `${{ steps.get_rustc.outputs.rustc }}`" + draft: true + prerelease: false + + - name: Upload crunch binary + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./target/release/crunch + asset_name: crunch + asset_content_type: application/octet-stream + + - name: Upload crunch sha256 + uses: actions/upload-release-asset@v1.0.1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: ./target/release/crunch.sha256 + asset_name: crunch.sha256 + asset_content_type: text/plain + \ No newline at end of file From 458572c344934e0135500a5b251b9ff993a26d25 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Mon, 1 Apr 2024 16:30:48 +0300 Subject: [PATCH 03/14] Rename upstream create_release.yml into ci.yml will add subsequent changes here to make tests trigger on each PR however this is a good starting point --- .github/workflows/{create_release.yml => ci.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{create_release.yml => ci.yml} (98%) diff --git a/.github/workflows/create_release.yml b/.github/workflows/ci.yml similarity index 98% rename from .github/workflows/create_release.yml rename to .github/workflows/ci.yml index e33f056..f0a212a 100644 --- a/.github/workflows/create_release.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ on: tags: - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 -name: Rust CI - Create Release +name: CI jobs: check: From d2c8cc522ca707f32bac10cb00d9eb21f91f7dd2 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Mon, 1 Apr 2024 16:34:40 +0300 Subject: [PATCH 04/14] Enable minimal testing on PRs - cargo check - cargo clippy - cargo fmt - cargo test --- .github/workflows/ci.yml | 120 ++++++++++++++++++++------------------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f0a212a..42ca6a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,83 +1,85 @@ on: push: - # Sequence of patterns matched against refs/tags - tags: - - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + branches: [main, dev] + pull_request: name: CI +permissions: read-all jobs: - check: - name: Create Release - runs-on: ubuntu-20.04 + rustfmt: + runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - - name: Install Rust latest stable + - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - profile: minimal - toolchain: stable - override: true - components: rustfmt, clippy + toolchain: stable + profile: minimal + override: true + components: rustfmt - - name: Run cargo test + - name: Check formatting uses: actions-rs/cargo@v1 - env: - CRUNCH_CONFIG_FILENAME: .env.example with: - command: test + command: fmt + args: -- --check - - name: Run cargo build - uses: actions-rs/cargo@v1 - env: - CRUNCH_CONFIG_FILENAME: .env.example + clippy: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 with: - command: build - args: --release + toolchain: stable + profile: minimal + override: true + components: clippy + - uses: Swatinem/rust-cache@v2 - - name: Generate SHA-256 hash file - run: | - cd ./target/release - sha256sum crunch > crunch.sha256 + - name: Run Clippy + uses: actions-rs/cargo@v1 + with: + command: clippy + args: --all-targets --all-features - - name: Get Rustc version - id: get_rustc - run: echo ::set-output name=rustc::$(rustc -V) + check: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 - - name: Get Tag version - id: get_tag - run: echo ::set-output name=tag::${GITHUB_REF#refs/*/} - - - name: Create release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 with: - tag_name: ${{ steps.get_tag.outputs.tag }} - release_name: Crunch ${{ steps.get_tag.outputs.tag }} - body: "Note: This release was built using `${{ steps.get_rustc.outputs.rustc }}`" - draft: true - prerelease: false + toolchain: stable + target: wasm32-unknown-unknown + profile: minimal + override: true + - uses: Swatinem/rust-cache@v2 - - name: Upload crunch binary - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Check Build + run: | + cargo check --release + + unit-test: + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v4 + + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./target/release/crunch - asset_name: crunch - asset_content_type: application/octet-stream + toolchain: stable + profile: minimal + override: true + - uses: Swatinem/rust-cache@v2 - - name: Upload crunch sha256 - uses: actions/upload-release-asset@v1.0.1 + - name: Run cargo test + uses: actions-rs/cargo@v1 env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + CRUNCH_CONFIG_FILENAME: .env.example with: - upload_url: ${{ steps.create_release.outputs.upload_url }} - asset_path: ./target/release/crunch.sha256 - asset_name: crunch.sha256 - asset_content_type: text/plain - \ No newline at end of file + command: test From 80e8f195058461257436ae4d1c5fb0485013709c Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 12:47:26 +0300 Subject: [PATCH 05/14] Reformat with cargo fmt --- src/crunch.rs | 53 +++++++++++++++------ src/main.rs | 9 ++-- src/pools.rs | 2 +- src/report.rs | 6 +-- src/runtimes/creditcoin.rs | 92 ++++++++++++++++++++++++++---------- src/runtimes/kusama.rs | 97 +++++++++++++++++++++++++++----------- src/runtimes/polkadot.rs | 97 +++++++++++++++++++++++++++----------- src/runtimes/westend.rs | 97 +++++++++++++++++++++++++++----------- src/stats.rs | 6 +-- 9 files changed, 327 insertions(+), 132 deletions(-) diff --git a/src/crunch.rs b/src/crunch.rs index 1f5eab2..97c85f0 100644 --- a/src/crunch.rs +++ b/src/crunch.rs @@ -19,32 +19,55 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. use crate::{ - config::{Config, CONFIG}, + config::{ + Config, + CONFIG, + }, errors::CrunchError, runtimes::{ - creditcoin, kusama, polkadot, - support::{ChainPrefix, ChainTokenSymbol, SupportedRuntime}, + creditcoin, + kusama, + polkadot, + support::{ + ChainPrefix, + ChainTokenSymbol, + SupportedRuntime, + }, westend, }, }; use async_std::task; -use log::{debug, error, info, warn}; +use log::{ + debug, + error, + info, + warn, +}; use rand::Rng; use regex::Regex; use serde::Deserialize; use std::{ convert::TryInto, - io::{prelude::*, BufReader}, + io::{ + prelude::*, + BufReader, + }, net::TcpListener, result::Result, - thread, time, + thread, + time, }; use subxt::{ - ext::sp_core::{crypto, sr25519, Pair as PairT}, + ext::sp_core::{ + crypto, + sr25519, + Pair as PairT, + }, storage::StorageKey, utils::AccountId32, - OnlineClient, PolkadotConfig, + OnlineClient, + PolkadotConfig, }; pub type ValidatorIndex = Option; @@ -124,7 +147,7 @@ pub async fn create_or_await_substrate_node_client( chain, config.substrate_ws_url, name, version ); - break (client, SupportedRuntime::from(chain_token_symbol)); + break (client, SupportedRuntime::from(chain_token_symbol)) } Err(e) => { error!("{}", e); @@ -229,7 +252,7 @@ fn spawn_and_restart_subscription_on_error() { let sleep_min = u32::pow(config.error_interval, n); thread::sleep(time::Duration::from_secs((60 * sleep_min).into())); n += 1; - continue; + continue } } thread::sleep(time::Duration::from_secs(1)); @@ -257,7 +280,7 @@ fn spawn_and_restart_crunch_flakes_on_error() { } thread::sleep(time::Duration::from_secs((60 * sleep_min).into())); n += 1; - continue; + continue }; thread::sleep(time::Duration::from_secs(config.interval)); } @@ -289,7 +312,7 @@ fn healthcheck() -> async_std::task::JoinHandle<()> { } }); - return h; + return h } fn spawn_crunch_view() { let crunch_task = task::spawn(async { @@ -310,12 +333,12 @@ pub async fn try_fetch_stashes_from_remote_url( ) -> Result>, CrunchError> { let config = CONFIG.clone(); if config.stashes_url.len() == 0 { - return Ok(None); + return Ok(None) } let response = reqwest::get(&config.stashes_url).await?.text().await?; let v: Vec = response.trim().split('\n').map(|s| s.to_string()).collect(); if v.is_empty() { - return Ok(None); + return Ok(None) } info!("{} stashes loaded from {}", v.len(), config.stashes_url); Ok(Some(v)) @@ -336,7 +359,7 @@ pub async fn try_fetch_onet_data( ) -> Result, CrunchError> { let config = CONFIG.clone(); if !config.onet_api_enabled { - return Ok(None); + return Ok(None) } let endpoint = if config.onet_api_url != "" { diff --git a/src/main.rs b/src/main.rs index 404671d..b5b6999 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,7 +27,10 @@ mod report; mod runtimes; mod stats; -use crate::{config::CONFIG, crunch::Crunch}; +use crate::{ + config::CONFIG, + crunch::Crunch, +}; use log::info; use std::env; @@ -48,10 +51,10 @@ fn main() { ); if config.only_view { - return Crunch::view(); + return Crunch::view() } if config.is_mode_era { - return Crunch::subscribe(); + return Crunch::subscribe() } info!("flaking"); Crunch::flakes() diff --git a/src/pools.rs b/src/pools.rs index 2dbcf67..4b19de9 100644 --- a/src/pools.rs +++ b/src/pools.rs @@ -61,7 +61,7 @@ pub fn nomination_pool_account(account_type: AccountType, pool_id: u32) -> Accou let buffer_hex = buffer.encode_hex::(); // NOTE: subxt::utils::AccountId32 currently doesn't support from hex conversion let acc = subxt::ext::sp_runtime::AccountId32::from_str(&buffer_hex).unwrap(); - return AccountId32::from_str(&acc.to_string()).unwrap(); + return AccountId32::from_str(&acc.to_string()).unwrap() } #[test] diff --git a/src/report.rs b/src/report.rs index 799fea3..50722d5 100644 --- a/src/report.rs +++ b/src/report.rs @@ -277,7 +277,7 @@ impl From for Report { report.add_raw_text(format!("⚠️ {} ⚠️", warning.clone())); warn!("{}", warning); } - continue; + continue } report.add_text(format!( @@ -502,7 +502,7 @@ fn trend(a: f64, b: f64) -> String { fn performance(a: f64, b: f64, out: String) -> Option { if a > b { - return Some(out); + return Some(out) } None } @@ -607,7 +607,7 @@ fn random_index(len: usize) -> usize { fn context() -> String { let config = CONFIG.clone(); if config.is_boring { - return String::from("rewards"); + return String::from("rewards") } format!("{} flakes", Random::Words) } diff --git a/src/runtimes/creditcoin.rs b/src/runtimes/creditcoin.rs index edf3eb0..f7dfcf0 100644 --- a/src/runtimes/creditcoin.rs +++ b/src/runtimes/creditcoin.rs @@ -1,34 +1,64 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, - try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, + get_account_id_from_storage_key, + get_from_seed, + random_wait, + try_fetch_onet_data, + try_fetch_stashes_from_remote_url, + Crunch, + NominatorsAmount, + ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{nomination_pool_account, AccountType}, + pools::{ + nomination_pool_account, + AccountType, + }, report::{ - Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, - RawData, Report, Signer, Validator, Validators, + Batch, + EraIndex, + Network, + NominationPoolsSummary, + Payout, + PayoutSummary, + Points, + RawData, + Report, + Signer, + Validator, + Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{debug, info, warn}; +use log::{ + debug, + info, + warn, +}; use std::{ cmp, - convert::{TryFrom, TryInto}, + convert::{ + TryFrom, + TryInto, + }, fs, result::Result, str::FromStr, - thread, time, + thread, + time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{sr25519, Pair as PairT}, + sp_core::{ + sr25519, + Pair as PairT, + }, }, tx::PairSigner, utils::AccountId32, @@ -44,12 +74,22 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{BondExtra, ClaimPermission}, + pallet_nomination_pools::{ + BondExtra, + ClaimPermission, + }, + }, + staking::events::{ + EraPaid, + PayoutStarted, + Rewarded, }, - staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, + BatchCompleted, + BatchCompletedWithErrors, + BatchInterrupted, + ItemCompleted, ItemFailed, }, }; @@ -466,7 +506,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()); + return Err(dispatch_error.into()) } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -642,7 +682,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue; + continue } }; debug!("controller {:?}", controller); @@ -685,7 +725,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = @@ -720,13 +760,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0); + return Ok(0) } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth); + return Ok(era_index - history_depth) } } @@ -819,7 +859,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await; + .await } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -991,7 +1031,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1057,7 +1097,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None); + return Ok(None) } let api = crunch.client().clone(); @@ -1120,11 +1160,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None); + return Ok(None) } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await; + return try_fetch_pool_operators_for_compound(&crunch).await } let api = crunch.client().clone(); @@ -1191,7 +1231,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None); + return Ok(None) } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1253,7 +1293,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None); + return Ok(None) } if config.pool_all_nominees_payout_enabled { @@ -1268,7 +1308,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)); + return Ok(Some(all)) } // Note: by default only active nominees (stashes) are triggered diff --git a/src/runtimes/kusama.rs b/src/runtimes/kusama.rs index 29133ba..4179ee3 100644 --- a/src/runtimes/kusama.rs +++ b/src/runtimes/kusama.rs @@ -22,37 +22,70 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, - try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, + get_account_id_from_storage_key, + get_from_seed, + random_wait, + try_fetch_onet_data, + try_fetch_stashes_from_remote_url, + Crunch, + NominatorsAmount, + ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{nomination_pool_account, AccountType}, + pools::{ + nomination_pool_account, + AccountType, + }, report::{ - Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, - RawData, Report, Signer, Validator, Validators, + Batch, + EraIndex, + Network, + NominationPoolsSummary, + Payout, + PayoutSummary, + Points, + RawData, + Report, + Signer, + Validator, + Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{debug, info, warn}; +use log::{ + debug, + info, + warn, +}; use std::{ cmp, - convert::{TryFrom, TryInto}, + convert::{ + TryFrom, + TryInto, + }, fs, result::Result, str::FromStr, - thread, time, + thread, + time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{sr25519, Pair as PairT}, + sp_core::{ + sr25519, + Pair as PairT, + }, }, tx::PairSigner, - utils::{AccountId32, MultiAddress}, + utils::{ + AccountId32, + MultiAddress, + }, PolkadotConfig, }; @@ -65,12 +98,22 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{BondExtra, ClaimPermission}, + pallet_nomination_pools::{ + BondExtra, + ClaimPermission, + }, + }, + staking::events::{ + EraPaid, + PayoutStarted, + Rewarded, }, - staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, + BatchCompleted, + BatchCompletedWithErrors, + BatchInterrupted, + ItemCompleted, ItemFailed, }, }; @@ -484,7 +527,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()); + return Err(dispatch_error.into()) } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -660,7 +703,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue; + continue } }; debug!("controller {:?}", controller); @@ -703,7 +746,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = @@ -738,13 +781,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0); + return Ok(0) } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth); + return Ok(era_index - history_depth) } } @@ -837,7 +880,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await; + .await } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -1004,7 +1047,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1070,7 +1113,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None); + return Ok(None) } let api = crunch.client().clone(); @@ -1133,11 +1176,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None); + return Ok(None) } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await; + return try_fetch_pool_operators_for_compound(&crunch).await } let api = crunch.client().clone(); @@ -1204,7 +1247,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None); + return Ok(None) } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1266,7 +1309,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None); + return Ok(None) } if config.pool_all_nominees_payout_enabled { @@ -1281,7 +1324,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)); + return Ok(Some(all)) } // Note: by default only active nominees (stashes) are triggered diff --git a/src/runtimes/polkadot.rs b/src/runtimes/polkadot.rs index 1ce2bae..31715b8 100644 --- a/src/runtimes/polkadot.rs +++ b/src/runtimes/polkadot.rs @@ -22,37 +22,70 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, - try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, + get_account_id_from_storage_key, + get_from_seed, + random_wait, + try_fetch_onet_data, + try_fetch_stashes_from_remote_url, + Crunch, + NominatorsAmount, + ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{nomination_pool_account, AccountType}, + pools::{ + nomination_pool_account, + AccountType, + }, report::{ - Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, - RawData, Report, Signer, Validator, Validators, + Batch, + EraIndex, + Network, + NominationPoolsSummary, + Payout, + PayoutSummary, + Points, + RawData, + Report, + Signer, + Validator, + Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{debug, info, warn}; +use log::{ + debug, + info, + warn, +}; use std::{ cmp, - convert::{TryFrom, TryInto}, + convert::{ + TryFrom, + TryInto, + }, fs, result::Result, str::FromStr, - thread, time, + thread, + time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{sr25519, Pair as PairT}, + sp_core::{ + sr25519, + Pair as PairT, + }, }, tx::PairSigner, - utils::{AccountId32, MultiAddress}, + utils::{ + AccountId32, + MultiAddress, + }, PolkadotConfig, }; @@ -65,12 +98,22 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{BondExtra, ClaimPermission}, + pallet_nomination_pools::{ + BondExtra, + ClaimPermission, + }, + }, + staking::events::{ + EraPaid, + PayoutStarted, + Rewarded, }, - staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, + BatchCompleted, + BatchCompletedWithErrors, + BatchInterrupted, + ItemCompleted, ItemFailed, }, }; @@ -475,7 +518,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()); + return Err(dispatch_error.into()) } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -651,7 +694,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue; + continue } }; debug!("controller {:?}", controller); @@ -694,7 +737,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = @@ -729,13 +772,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0); + return Ok(0) } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth); + return Ok(era_index - history_depth) } } @@ -828,7 +871,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await; + .await } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -995,7 +1038,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1061,7 +1104,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None); + return Ok(None) } let api = crunch.client().clone(); @@ -1124,11 +1167,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None); + return Ok(None) } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await; + return try_fetch_pool_operators_for_compound(&crunch).await } let api = crunch.client().clone(); @@ -1195,7 +1238,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None); + return Ok(None) } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1257,7 +1300,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None); + return Ok(None) } if config.pool_all_nominees_payout_enabled { @@ -1272,7 +1315,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)); + return Ok(Some(all)) } // Note: by default only active nominees (stashes) are triggered diff --git a/src/runtimes/westend.rs b/src/runtimes/westend.rs index 04a5d86..d681428 100644 --- a/src/runtimes/westend.rs +++ b/src/runtimes/westend.rs @@ -22,37 +22,70 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, - try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, + get_account_id_from_storage_key, + get_from_seed, + random_wait, + try_fetch_onet_data, + try_fetch_stashes_from_remote_url, + Crunch, + NominatorsAmount, + ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{nomination_pool_account, AccountType}, + pools::{ + nomination_pool_account, + AccountType, + }, report::{ - Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, - RawData, Report, Signer, Validator, Validators, + Batch, + EraIndex, + Network, + NominationPoolsSummary, + Payout, + PayoutSummary, + Points, + RawData, + Report, + Signer, + Validator, + Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{debug, info, warn}; +use log::{ + debug, + info, + warn, +}; use std::{ cmp, - convert::{TryFrom, TryInto}, + convert::{ + TryFrom, + TryInto, + }, fs, result::Result, str::FromStr, - thread, time, + thread, + time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{sr25519, Pair as PairT}, + sp_core::{ + sr25519, + Pair as PairT, + }, }, tx::PairSigner, - utils::{AccountId32, MultiAddress}, + utils::{ + AccountId32, + MultiAddress, + }, PolkadotConfig, }; @@ -65,12 +98,22 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{BondExtra, ClaimPermission}, + pallet_nomination_pools::{ + BondExtra, + ClaimPermission, + }, + }, + staking::events::{ + EraPaid, + PayoutStarted, + Rewarded, }, - staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, + BatchCompleted, + BatchCompletedWithErrors, + BatchInterrupted, + ItemCompleted, ItemFailed, }, }; @@ -484,7 +527,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()); + return Err(dispatch_error.into()) } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -660,7 +703,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue; + continue } }; debug!("controller {:?}", controller); @@ -703,7 +746,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = @@ -738,13 +781,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0); + return Ok(0) } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth); + return Ok(era_index - history_depth) } } @@ -837,7 +880,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await; + .await } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -1004,7 +1047,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue; + continue } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1070,7 +1113,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None); + return Ok(None) } let api = crunch.client().clone(); @@ -1133,11 +1176,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None); + return Ok(None) } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await; + return try_fetch_pool_operators_for_compound(&crunch).await } let api = crunch.client().clone(); @@ -1204,7 +1247,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None); + return Ok(None) } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1266,7 +1309,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None); + return Ok(None) } if config.pool_all_nominees_payout_enabled { @@ -1281,7 +1324,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)); + return Ok(Some(all)) } // Note: by default only active nominees (stashes) are triggered diff --git a/src/stats.rs b/src/stats.rs index a14e6c7..90b4fd6 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -21,7 +21,7 @@ pub fn mean(list: &Vec) -> f64 { if list.len() == 0 { - return 0.0; + return 0.0 } let sum: f64 = list.iter().sum(); sum / (list.len() as f64) @@ -36,7 +36,7 @@ pub fn standard_deviation(list: &Vec) -> f64 { pub fn median(list: &mut Vec) -> u32 { if list.len() == 0 { - return 0; + return 0 } list.sort(); let mid = list.len() / 2; @@ -69,7 +69,7 @@ pub fn confidence_interval(list: &Vec, z: f64) -> (f64, f64) { // https://www.statisticshowto.com/statistics-basics/find-outliers/ pub fn iqr_interval(list: &mut Vec) -> (f64, f64) { if list.len() == 0 { - return (0.0, 0.0); + return (0.0, 0.0) } list.sort(); let q1 = median(&mut (&list[..&list.len() / 2]).into()); From 39fc293906bbb101ce400d8151597614b4036700 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 12:56:10 +0300 Subject: [PATCH 06/14] Stricter checks in CI --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 42ca6a5..5da87ee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,7 +44,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: clippy - args: --all-targets --all-features + args: --all-targets --all-features -- -D warnings check: runs-on: ubuntu-22.04 @@ -62,7 +62,7 @@ jobs: - name: Check Build run: | - cargo check --release + RUSTFLAGS="-D warnings" cargo check --release unit-test: runs-on: ubuntu-22.04 From 496e209e99b1c9d8aa54a9014df05a9fc54c5645 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 12:57:12 +0300 Subject: [PATCH 07/14] Remove unused imports --- src/runtimes/kusama.rs | 1 - src/runtimes/polkadot.rs | 2 -- src/runtimes/westend.rs | 1 - 3 files changed, 4 deletions(-) diff --git a/src/runtimes/kusama.rs b/src/runtimes/kusama.rs index 4179ee3..1e62303 100644 --- a/src/runtimes/kusama.rs +++ b/src/runtimes/kusama.rs @@ -46,7 +46,6 @@ use crate::{ PayoutSummary, Points, RawData, - Report, Signer, Validator, Validators, diff --git a/src/runtimes/polkadot.rs b/src/runtimes/polkadot.rs index 31715b8..4ec9267 100644 --- a/src/runtimes/polkadot.rs +++ b/src/runtimes/polkadot.rs @@ -45,8 +45,6 @@ use crate::{ Payout, PayoutSummary, Points, - RawData, - Report, Signer, Validator, Validators, diff --git a/src/runtimes/westend.rs b/src/runtimes/westend.rs index d681428..7a1f433 100644 --- a/src/runtimes/westend.rs +++ b/src/runtimes/westend.rs @@ -46,7 +46,6 @@ use crate::{ PayoutSummary, Points, RawData, - Report, Signer, Validator, Validators, From 7e5e40ce8e0216b3c5966aabcdde85f8a727ec78 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 13:03:46 +0300 Subject: [PATCH 08/14] Silence warnings for unused variables for the upstream code just prefix them with underscore to minimize differences, for creditcoin/ just remove them --- src/crunch.rs | 6 +++--- src/runtimes/creditcoin.rs | 2 +- src/runtimes/kusama.rs | 2 +- src/runtimes/polkadot.rs | 4 ++-- src/runtimes/westend.rs | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/crunch.rs b/src/crunch.rs index 97c85f0..a076cf7 100644 --- a/src/crunch.rs +++ b/src/crunch.rs @@ -260,7 +260,7 @@ fn spawn_and_restart_subscription_on_error() { } }); - let h = healthcheck(); + healthcheck(); task::block_on(t); } @@ -286,7 +286,7 @@ fn spawn_and_restart_crunch_flakes_on_error() { } }); - let h = healthcheck(); + healthcheck(); task::block_on(t); } @@ -302,7 +302,7 @@ fn healthcheck() -> async_std::task::JoinHandle<()> { // we need to read the full request before we respond or we get a 'connection reset by peer error' let buf_reader = BufReader::new(&mut stream); - let http_request: Vec<_> = buf_reader + let _http_request: Vec<_> = buf_reader .lines() .map(|result| result.unwrap()) .take_while(|line| !line.is_empty()) diff --git a/src/runtimes/creditcoin.rs b/src/runtimes/creditcoin.rs index f7dfcf0..29a2492 100644 --- a/src/runtimes/creditcoin.rs +++ b/src/runtimes/creditcoin.rs @@ -234,7 +234,7 @@ pub async fn try_crunch(crunch: &Crunch) -> Result<(), CrunchError> { pools_summary, }; - let report = Report::from(data); + let _report = Report::from(data); Ok(()) } diff --git a/src/runtimes/kusama.rs b/src/runtimes/kusama.rs index 1e62303..f5320e5 100644 --- a/src/runtimes/kusama.rs +++ b/src/runtimes/kusama.rs @@ -249,7 +249,7 @@ pub async fn try_crunch(crunch: &Crunch) -> Result<(), CrunchError> { }; debug!("network {:?}", network); - let data = RawData { + let _data = RawData { network, signer, validators, diff --git a/src/runtimes/polkadot.rs b/src/runtimes/polkadot.rs index 4ec9267..9188cc5 100644 --- a/src/runtimes/polkadot.rs +++ b/src/runtimes/polkadot.rs @@ -191,11 +191,11 @@ pub async fn try_crunch(crunch: &Crunch) -> Result<(), CrunchError> { } // Try run payouts in batches - let (mut validators, payout_summary) = + let (mut validators, _payout_summary) = try_run_batch_payouts(&crunch, &seed_account_signer).await?; // Try run members in batches - let pools_summary = try_run_batch_pool_members(&crunch, &seed_account_signer).await?; + let _pools_summary = try_run_batch_pool_members(&crunch, &seed_account_signer).await?; // Get Network name let chain_name = api.rpc().system_chain().await?; diff --git a/src/runtimes/westend.rs b/src/runtimes/westend.rs index 7a1f433..383b533 100644 --- a/src/runtimes/westend.rs +++ b/src/runtimes/westend.rs @@ -249,7 +249,7 @@ pub async fn try_crunch(crunch: &Crunch) -> Result<(), CrunchError> { }; debug!("network {:?}", network); - let data = RawData { + let _data = RawData { network, signer, validators, From 9b50a70b58dbf7e937d25120cdddbc5482c0f36b Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 13:27:10 +0300 Subject: [PATCH 09/14] Remove unnecessary mut --- src/runtimes/creditcoin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtimes/creditcoin.rs b/src/runtimes/creditcoin.rs index 29a2492..e12717e 100644 --- a/src/runtimes/creditcoin.rs +++ b/src/runtimes/creditcoin.rs @@ -658,7 +658,7 @@ async fn collect_validators_data( debug!("active_validators {:?}", active_validators); let mut validators: Validators = Vec::new(); - let mut stashes = get_stashes(&crunch).await?; + let stashes = get_stashes(&crunch).await?; for (_i, stash_str) in stashes.iter().enumerate() { let stash = AccountId32::from_str(stash_str).map_err(|e| { From 258dda226bbe51bd731124340b19c962c6d2dd61 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 13:42:20 +0300 Subject: [PATCH 10/14] Allow 2 unused methods from upstream implementation --- src/report.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/report.rs b/src/report.rs index 50722d5..db1676c 100644 --- a/src/report.rs +++ b/src/report.rs @@ -164,10 +164,12 @@ impl Report { self.add_raw_text("".into()); } + #[allow(dead_code)] pub fn message(&self) -> String { self.body.join("\n") } + #[allow(dead_code)] pub fn formatted_message(&self) -> String { self.body.join("
") } From 5087bcb278bc2824dfd0d06072217a539856984d Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 13:46:10 +0300 Subject: [PATCH 11/14] Add rust-toolchain.toml and use the same rustc version in CI because fmt and clippy appear to be slightly different from version to version and we'd like to have consistency regardless of how or where this is built --- .github/workflows/ci.yml | 28 ++++++++++++++++++++++++---- rust-toolchain.toml | 4 ++++ 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 rust-toolchain.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5da87ee..b8bfa46 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,10 +12,15 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Configure rustc version + run: | + RUSTC_VERSION=$(grep channel rust-toolchain.toml | tail -n1 | tr -d " " | cut -f2 -d'"') + echo "RUSTC_VERSION=$RUSTC_VERSION" >> "$GITHUB_ENV" + - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: ${{ env.RUSTC_VERSION }} profile: minimal override: true components: rustfmt @@ -31,10 +36,15 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Configure rustc version + run: | + RUSTC_VERSION=$(grep channel rust-toolchain.toml | tail -n1 | tr -d " " | cut -f2 -d'"') + echo "RUSTC_VERSION=$RUSTC_VERSION" >> "$GITHUB_ENV" + - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: ${{ env.RUSTC_VERSION }} profile: minimal override: true components: clippy @@ -51,10 +61,15 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Configure rustc version + run: | + RUSTC_VERSION=$(grep channel rust-toolchain.toml | tail -n1 | tr -d " " | cut -f2 -d'"') + echo "RUSTC_VERSION=$RUSTC_VERSION" >> "$GITHUB_ENV" + - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: ${{ env.RUSTC_VERSION }} target: wasm32-unknown-unknown profile: minimal override: true @@ -69,10 +84,15 @@ jobs: steps: - uses: actions/checkout@v4 + - name: Configure rustc version + run: | + RUSTC_VERSION=$(grep channel rust-toolchain.toml | tail -n1 | tr -d " " | cut -f2 -d'"') + echo "RUSTC_VERSION=$RUSTC_VERSION" >> "$GITHUB_ENV" + - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: ${{ env.RUSTC_VERSION }} profile: minimal override: true - uses: Swatinem/rust-cache@v2 diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..f4883c8 --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,4 @@ +[toolchain] +channel = "1.77.0" # rustc 1.77.0 (aedd173a2 2024-03-17) +components = ["cargo", "clippy", "rustc", "rustfmt", "rust-src"] +profile = "minimal" From 9a538e3de38ca1d354422a15dd45ccd7f871dc50 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 13:48:10 +0300 Subject: [PATCH 12/14] Reformat again with rustc 1.77.0 --- src/config.rs | 11 +---- src/crunch.rs | 53 ++++++--------------- src/errors.rs | 10 +--- src/main.rs | 9 ++-- src/pools.rs | 2 +- src/report.rs | 31 ++++-------- src/runtimes/creditcoin.rs | 92 ++++++++++------------------------- src/runtimes/kusama.rs | 96 +++++++++++-------------------------- src/runtimes/polkadot.rs | 98 +++++++++++--------------------------- src/runtimes/westend.rs | 96 +++++++++++-------------------------- src/stats.rs | 6 +-- 11 files changed, 145 insertions(+), 359 deletions(-) diff --git a/src/config.rs b/src/config.rs index f97c06b..4ab662b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,17 +30,10 @@ // // Set Config struct into a CONFIG lazy_static to avoid multiple processing. // -use clap::{ - App, - Arg, - SubCommand, -}; +use clap::{App, Arg, SubCommand}; use dotenv; use lazy_static::lazy_static; -use log::{ - info, - warn, -}; +use log::{info, warn}; use serde::Deserialize; use std::env; diff --git a/src/crunch.rs b/src/crunch.rs index a076cf7..c986197 100644 --- a/src/crunch.rs +++ b/src/crunch.rs @@ -19,55 +19,32 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. use crate::{ - config::{ - Config, - CONFIG, - }, + config::{Config, CONFIG}, errors::CrunchError, runtimes::{ - creditcoin, - kusama, - polkadot, - support::{ - ChainPrefix, - ChainTokenSymbol, - SupportedRuntime, - }, + creditcoin, kusama, polkadot, + support::{ChainPrefix, ChainTokenSymbol, SupportedRuntime}, westend, }, }; use async_std::task; -use log::{ - debug, - error, - info, - warn, -}; +use log::{debug, error, info, warn}; use rand::Rng; use regex::Regex; use serde::Deserialize; use std::{ convert::TryInto, - io::{ - prelude::*, - BufReader, - }, + io::{prelude::*, BufReader}, net::TcpListener, result::Result, - thread, - time, + thread, time, }; use subxt::{ - ext::sp_core::{ - crypto, - sr25519, - Pair as PairT, - }, + ext::sp_core::{crypto, sr25519, Pair as PairT}, storage::StorageKey, utils::AccountId32, - OnlineClient, - PolkadotConfig, + OnlineClient, PolkadotConfig, }; pub type ValidatorIndex = Option; @@ -147,7 +124,7 @@ pub async fn create_or_await_substrate_node_client( chain, config.substrate_ws_url, name, version ); - break (client, SupportedRuntime::from(chain_token_symbol)) + break (client, SupportedRuntime::from(chain_token_symbol)); } Err(e) => { error!("{}", e); @@ -252,7 +229,7 @@ fn spawn_and_restart_subscription_on_error() { let sleep_min = u32::pow(config.error_interval, n); thread::sleep(time::Duration::from_secs((60 * sleep_min).into())); n += 1; - continue + continue; } } thread::sleep(time::Duration::from_secs(1)); @@ -280,7 +257,7 @@ fn spawn_and_restart_crunch_flakes_on_error() { } thread::sleep(time::Duration::from_secs((60 * sleep_min).into())); n += 1; - continue + continue; }; thread::sleep(time::Duration::from_secs(config.interval)); } @@ -312,7 +289,7 @@ fn healthcheck() -> async_std::task::JoinHandle<()> { } }); - return h + return h; } fn spawn_crunch_view() { let crunch_task = task::spawn(async { @@ -333,12 +310,12 @@ pub async fn try_fetch_stashes_from_remote_url( ) -> Result>, CrunchError> { let config = CONFIG.clone(); if config.stashes_url.len() == 0 { - return Ok(None) + return Ok(None); } let response = reqwest::get(&config.stashes_url).await?.text().await?; let v: Vec = response.trim().split('\n').map(|s| s.to_string()).collect(); if v.is_empty() { - return Ok(None) + return Ok(None); } info!("{} stashes loaded from {}", v.len(), config.stashes_url); Ok(Some(v)) @@ -359,7 +336,7 @@ pub async fn try_fetch_onet_data( ) -> Result, CrunchError> { let config = CONFIG.clone(); if !config.onet_api_enabled { - return Ok(None) + return Ok(None); } let endpoint = if config.onet_api_url != "" { diff --git a/src/errors.rs b/src/errors.rs index 8e148a8..180a6a8 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -21,14 +21,8 @@ use codec; use reqwest; -use std::{ - str::Utf8Error, - string::String, -}; -use subxt::error::{ - DispatchError, - MetadataError, -}; +use std::{str::Utf8Error, string::String}; +use subxt::error::{DispatchError, MetadataError}; use thiserror::Error; /// Crunch specific error messages diff --git a/src/main.rs b/src/main.rs index b5b6999..404671d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,10 +27,7 @@ mod report; mod runtimes; mod stats; -use crate::{ - config::CONFIG, - crunch::Crunch, -}; +use crate::{config::CONFIG, crunch::Crunch}; use log::info; use std::env; @@ -51,10 +48,10 @@ fn main() { ); if config.only_view { - return Crunch::view() + return Crunch::view(); } if config.is_mode_era { - return Crunch::subscribe() + return Crunch::subscribe(); } info!("flaking"); Crunch::flakes() diff --git a/src/pools.rs b/src/pools.rs index 4b19de9..2dbcf67 100644 --- a/src/pools.rs +++ b/src/pools.rs @@ -61,7 +61,7 @@ pub fn nomination_pool_account(account_type: AccountType, pool_id: u32) -> Accou let buffer_hex = buffer.encode_hex::(); // NOTE: subxt::utils::AccountId32 currently doesn't support from hex conversion let acc = subxt::ext::sp_runtime::AccountId32::from_str(&buffer_hex).unwrap(); - return AccountId32::from_str(&acc.to_string()).unwrap() + return AccountId32::from_str(&acc.to_string()).unwrap(); } #[test] diff --git a/src/report.rs b/src/report.rs index db1676c..6eb72c3 100644 --- a/src/report.rs +++ b/src/report.rs @@ -18,19 +18,10 @@ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -use crate::{ - config::CONFIG, - crunch::OnetData, -}; -use log::{ - info, - warn, -}; +use crate::{config::CONFIG, crunch::OnetData}; +use log::{info, warn}; use rand::Rng; -use subxt::{ - ext::sp_core::H256, - utils::AccountId32, -}; +use subxt::{ext::sp_core::H256, utils::AccountId32}; pub type EraIndex = u32; @@ -279,7 +270,7 @@ impl From for Report { report.add_raw_text(format!("⚠️ {} ⚠️", warning.clone())); warn!("{}", warning); } - continue + continue; } report.add_text(format!( @@ -504,7 +495,7 @@ fn trend(a: f64, b: f64) -> String { fn performance(a: f64, b: f64, out: String) -> Option { if a > b { - return Some(out) + return Some(out); } None } @@ -512,12 +503,10 @@ fn performance(a: f64, b: f64, out: String) -> Option { fn good_performance(value: u32, higher_limit: f64, outlier_limit: f64) -> String { match performance(value.into(), outlier_limit, "🤑 🤯 🚀".into()) { Some(p) => p, - None => { - match performance(value.into(), higher_limit, "😊 🔥".into()) { - Some(p) => p, - None => String::from(""), - } - } + None => match performance(value.into(), higher_limit, "😊 🔥".into()) { + Some(p) => p, + None => String::from(""), + }, } } @@ -609,7 +598,7 @@ fn random_index(len: usize) -> usize { fn context() -> String { let config = CONFIG.clone(); if config.is_boring { - return String::from("rewards") + return String::from("rewards"); } format!("{} flakes", Random::Words) } diff --git a/src/runtimes/creditcoin.rs b/src/runtimes/creditcoin.rs index e12717e..0bc659f 100644 --- a/src/runtimes/creditcoin.rs +++ b/src/runtimes/creditcoin.rs @@ -1,64 +1,34 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, - get_from_seed, - random_wait, - try_fetch_onet_data, - try_fetch_stashes_from_remote_url, - Crunch, - NominatorsAmount, - ValidatorAmount, + get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, + try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{ - nomination_pool_account, - AccountType, - }, + pools::{nomination_pool_account, AccountType}, report::{ - Batch, - EraIndex, - Network, - NominationPoolsSummary, - Payout, - PayoutSummary, - Points, - RawData, - Report, - Signer, - Validator, - Validators, + Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, + RawData, Report, Signer, Validator, Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{ - debug, - info, - warn, -}; +use log::{debug, info, warn}; use std::{ cmp, - convert::{ - TryFrom, - TryInto, - }, + convert::{TryFrom, TryInto}, fs, result::Result, str::FromStr, - thread, - time, + thread, time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{ - sr25519, - Pair as PairT, - }, + sp_core::{sr25519, Pair as PairT}, }, tx::PairSigner, utils::AccountId32, @@ -74,22 +44,12 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{ - BondExtra, - ClaimPermission, - }, - }, - staking::events::{ - EraPaid, - PayoutStarted, - Rewarded, + pallet_nomination_pools::{BondExtra, ClaimPermission}, }, + staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, - BatchCompletedWithErrors, - BatchInterrupted, - ItemCompleted, + BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, ItemFailed, }, }; @@ -506,7 +466,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()) + return Err(dispatch_error.into()); } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -682,7 +642,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue + continue; } }; debug!("controller {:?}", controller); @@ -725,7 +685,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = @@ -760,13 +720,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0) + return Ok(0); } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth) + return Ok(era_index - history_depth); } } @@ -859,7 +819,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await + .await; } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -1031,7 +991,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1097,7 +1057,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None) + return Ok(None); } let api = crunch.client().clone(); @@ -1160,11 +1120,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None) + return Ok(None); } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await + return try_fetch_pool_operators_for_compound(&crunch).await; } let api = crunch.client().clone(); @@ -1231,7 +1191,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None) + return Ok(None); } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1293,7 +1253,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None) + return Ok(None); } if config.pool_all_nominees_payout_enabled { @@ -1308,7 +1268,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)) + return Ok(Some(all)); } // Note: by default only active nominees (stashes) are triggered diff --git a/src/runtimes/kusama.rs b/src/runtimes/kusama.rs index f5320e5..a5fe94f 100644 --- a/src/runtimes/kusama.rs +++ b/src/runtimes/kusama.rs @@ -22,69 +22,37 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, - get_from_seed, - random_wait, - try_fetch_onet_data, - try_fetch_stashes_from_remote_url, - Crunch, - NominatorsAmount, - ValidatorAmount, + get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, + try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{ - nomination_pool_account, - AccountType, - }, + pools::{nomination_pool_account, AccountType}, report::{ - Batch, - EraIndex, - Network, - NominationPoolsSummary, - Payout, - PayoutSummary, - Points, - RawData, - Signer, - Validator, - Validators, + Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, + RawData, Signer, Validator, Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{ - debug, - info, - warn, -}; +use log::{debug, info, warn}; use std::{ cmp, - convert::{ - TryFrom, - TryInto, - }, + convert::{TryFrom, TryInto}, fs, result::Result, str::FromStr, - thread, - time, + thread, time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{ - sr25519, - Pair as PairT, - }, + sp_core::{sr25519, Pair as PairT}, }, tx::PairSigner, - utils::{ - AccountId32, - MultiAddress, - }, + utils::{AccountId32, MultiAddress}, PolkadotConfig, }; @@ -97,22 +65,12 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{ - BondExtra, - ClaimPermission, - }, - }, - staking::events::{ - EraPaid, - PayoutStarted, - Rewarded, + pallet_nomination_pools::{BondExtra, ClaimPermission}, }, + staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, - BatchCompletedWithErrors, - BatchInterrupted, - ItemCompleted, + BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, ItemFailed, }, }; @@ -526,7 +484,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()) + return Err(dispatch_error.into()); } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -702,7 +660,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue + continue; } }; debug!("controller {:?}", controller); @@ -745,7 +703,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = @@ -780,13 +738,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0) + return Ok(0); } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth) + return Ok(era_index - history_depth); } } @@ -879,7 +837,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await + .await; } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -1046,7 +1004,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1112,7 +1070,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None) + return Ok(None); } let api = crunch.client().clone(); @@ -1175,11 +1133,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None) + return Ok(None); } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await + return try_fetch_pool_operators_for_compound(&crunch).await; } let api = crunch.client().clone(); @@ -1246,7 +1204,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None) + return Ok(None); } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1308,7 +1266,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None) + return Ok(None); } if config.pool_all_nominees_payout_enabled { @@ -1323,7 +1281,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)) + return Ok(Some(all)); } // Note: by default only active nominees (stashes) are triggered diff --git a/src/runtimes/polkadot.rs b/src/runtimes/polkadot.rs index 9188cc5..410d5b1 100644 --- a/src/runtimes/polkadot.rs +++ b/src/runtimes/polkadot.rs @@ -22,68 +22,37 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, - get_from_seed, - random_wait, - try_fetch_onet_data, - try_fetch_stashes_from_remote_url, - Crunch, - NominatorsAmount, - ValidatorAmount, + get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, + try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{ - nomination_pool_account, - AccountType, - }, + pools::{nomination_pool_account, AccountType}, report::{ - Batch, - EraIndex, - Network, - NominationPoolsSummary, - Payout, - PayoutSummary, - Points, - Signer, - Validator, - Validators, + Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, + Signer, Validator, Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{ - debug, - info, - warn, -}; +use log::{debug, info, warn}; use std::{ cmp, - convert::{ - TryFrom, - TryInto, - }, + convert::{TryFrom, TryInto}, fs, result::Result, str::FromStr, - thread, - time, + thread, time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{ - sr25519, - Pair as PairT, - }, + sp_core::{sr25519, Pair as PairT}, }, tx::PairSigner, - utils::{ - AccountId32, - MultiAddress, - }, + utils::{AccountId32, MultiAddress}, PolkadotConfig, }; @@ -96,22 +65,12 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{ - BondExtra, - ClaimPermission, - }, - }, - staking::events::{ - EraPaid, - PayoutStarted, - Rewarded, + pallet_nomination_pools::{BondExtra, ClaimPermission}, }, + staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, - BatchCompletedWithErrors, - BatchInterrupted, - ItemCompleted, + BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, ItemFailed, }, }; @@ -195,7 +154,8 @@ pub async fn try_crunch(crunch: &Crunch) -> Result<(), CrunchError> { try_run_batch_payouts(&crunch, &seed_account_signer).await?; // Try run members in batches - let _pools_summary = try_run_batch_pool_members(&crunch, &seed_account_signer).await?; + let _pools_summary = + try_run_batch_pool_members(&crunch, &seed_account_signer).await?; // Get Network name let chain_name = api.rpc().system_chain().await?; @@ -516,7 +476,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()) + return Err(dispatch_error.into()); } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -692,7 +652,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue + continue; } }; debug!("controller {:?}", controller); @@ -735,7 +695,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = @@ -770,13 +730,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0) + return Ok(0); } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth) + return Ok(era_index - history_depth); } } @@ -869,7 +829,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await + .await; } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -1036,7 +996,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1102,7 +1062,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None) + return Ok(None); } let api = crunch.client().clone(); @@ -1165,11 +1125,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None) + return Ok(None); } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await + return try_fetch_pool_operators_for_compound(&crunch).await; } let api = crunch.client().clone(); @@ -1236,7 +1196,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None) + return Ok(None); } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1298,7 +1258,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None) + return Ok(None); } if config.pool_all_nominees_payout_enabled { @@ -1313,7 +1273,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)) + return Ok(Some(all)); } // Note: by default only active nominees (stashes) are triggered diff --git a/src/runtimes/westend.rs b/src/runtimes/westend.rs index 383b533..569f6ae 100644 --- a/src/runtimes/westend.rs +++ b/src/runtimes/westend.rs @@ -22,69 +22,37 @@ use crate::{ config::CONFIG, crunch::{ - get_account_id_from_storage_key, - get_from_seed, - random_wait, - try_fetch_onet_data, - try_fetch_stashes_from_remote_url, - Crunch, - NominatorsAmount, - ValidatorAmount, + get_account_id_from_storage_key, get_from_seed, random_wait, try_fetch_onet_data, + try_fetch_stashes_from_remote_url, Crunch, NominatorsAmount, ValidatorAmount, ValidatorIndex, }, errors::CrunchError, - pools::{ - nomination_pool_account, - AccountType, - }, + pools::{nomination_pool_account, AccountType}, report::{ - Batch, - EraIndex, - Network, - NominationPoolsSummary, - Payout, - PayoutSummary, - Points, - RawData, - Signer, - Validator, - Validators, + Batch, EraIndex, Network, NominationPoolsSummary, Payout, PayoutSummary, Points, + RawData, Signer, Validator, Validators, }, stats, }; use async_recursion::async_recursion; use futures::StreamExt; -use log::{ - debug, - info, - warn, -}; +use log::{debug, info, warn}; use std::{ cmp, - convert::{ - TryFrom, - TryInto, - }, + convert::{TryFrom, TryInto}, fs, result::Result, str::FromStr, - thread, - time, + thread, time, }; use subxt::{ error::DispatchError, ext::{ codec::Encode, - sp_core::{ - sr25519, - Pair as PairT, - }, + sp_core::{sr25519, Pair as PairT}, }, tx::PairSigner, - utils::{ - AccountId32, - MultiAddress, - }, + utils::{AccountId32, MultiAddress}, PolkadotConfig, }; @@ -97,22 +65,12 @@ mod node_runtime {} use node_runtime::{ runtime_types::{ bounded_collections::bounded_vec::BoundedVec, - pallet_nomination_pools::{ - BondExtra, - ClaimPermission, - }, - }, - staking::events::{ - EraPaid, - PayoutStarted, - Rewarded, + pallet_nomination_pools::{BondExtra, ClaimPermission}, }, + staking::events::{EraPaid, PayoutStarted, Rewarded}, system::events::ExtrinsicFailed, utility::events::{ - BatchCompleted, - BatchCompletedWithErrors, - BatchInterrupted, - ItemCompleted, + BatchCompleted, BatchCompletedWithErrors, BatchInterrupted, ItemCompleted, ItemFailed, }, }; @@ -526,7 +484,7 @@ pub async fn try_run_batch_payouts( event.field_bytes(), api.metadata(), )?; - return Err(dispatch_error.into()) + return Err(dispatch_error.into()); } else if let Some(ev) = event.as_event::()? { // https://polkadot.js.org/docs/substrate/events#payoutstartedu32-accountid32 // PayoutStarted(u32, AccountId32) @@ -702,7 +660,7 @@ async fn collect_validators_data( stash )]; validators.push(v); - continue + continue; } }; debug!("controller {:?}", controller); @@ -745,7 +703,7 @@ async fn collect_validators_data( v.is_previous_era_already_claimed = true; } v.claimed.push(e); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = @@ -780,13 +738,13 @@ async fn get_era_index_start( let history_depth: u32 = api.constants().at(&history_depth_addr)?; if era_index < cmp::min(config.maximum_history_eras, history_depth) { - return Ok(0) + return Ok(0); } else if config.is_short { - return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)) + return Ok(era_index - cmp::min(config.maximum_history_eras, history_depth)); } else { // Note: If crunch is running in verbose mode, ignore MAXIMUM_ERAS // since we still want to show information about inclusion and eras crunched for all history_depth - return Ok(era_index - history_depth) + return Ok(era_index - history_depth); } } @@ -879,7 +837,7 @@ async fn get_display_name( &parent_account, Some(sub_account_name.to_string()), ) - .await + .await; } else { let s = &stash.to_string(); Ok(format!("{}...{}", &s[..6], &s[s.len() - 6..])) @@ -1046,7 +1004,7 @@ pub async fn inspect(crunch: &Crunch) -> Result<(), CrunchError> { // If reward was already claimed skip it if claimed_rewards.contains(&era_index) { claimed.push(era_index); - continue + continue; } // Verify if stash was active in set let eras_stakers_addr = node_runtime::storage() @@ -1112,7 +1070,7 @@ pub async fn try_fetch_pool_operators_for_compound( let config = CONFIG.clone(); if config.pool_ids.len() == 0 && !config.pool_only_operator_compound_enabled { - return Ok(None) + return Ok(None); } let api = crunch.client().clone(); @@ -1175,11 +1133,11 @@ pub async fn try_fetch_pool_members_for_compound( && !config.pool_only_operator_compound_enabled && !config.pool_members_compound_enabled { - return Ok(None) + return Ok(None); } if config.pool_only_operator_compound_enabled { - return try_fetch_pool_operators_for_compound(&crunch).await + return try_fetch_pool_operators_for_compound(&crunch).await; } let api = crunch.client().clone(); @@ -1246,7 +1204,7 @@ pub async fn try_fetch_stashes_from_pool_ids( || (!config.pool_active_nominees_payout_enabled && !config.pool_all_nominees_payout_enabled) { - return Ok(None) + return Ok(None); } let active_era_addr = node_runtime::storage().staking().active_era(); @@ -1308,7 +1266,7 @@ pub async fn try_fetch_stashes_from_pool_ids( } } if all.is_empty() && active.is_empty() { - return Ok(None) + return Ok(None); } if config.pool_all_nominees_payout_enabled { @@ -1323,7 +1281,7 @@ pub async fn try_fetch_stashes_from_pool_ids( .join(",") ); - return Ok(Some(all)) + return Ok(Some(all)); } // Note: by default only active nominees (stashes) are triggered diff --git a/src/stats.rs b/src/stats.rs index 90b4fd6..a14e6c7 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -21,7 +21,7 @@ pub fn mean(list: &Vec) -> f64 { if list.len() == 0 { - return 0.0 + return 0.0; } let sum: f64 = list.iter().sum(); sum / (list.len() as f64) @@ -36,7 +36,7 @@ pub fn standard_deviation(list: &Vec) -> f64 { pub fn median(list: &mut Vec) -> u32 { if list.len() == 0 { - return 0 + return 0; } list.sort(); let mid = list.len() / 2; @@ -69,7 +69,7 @@ pub fn confidence_interval(list: &Vec, z: f64) -> (f64, f64) { // https://www.statisticshowto.com/statistics-basics/find-outliers/ pub fn iqr_interval(list: &mut Vec) -> (f64, f64) { if list.len() == 0 { - return (0.0, 0.0) + return (0.0, 0.0); } list.sort(); let q1 = median(&mut (&list[..&list.len() / 2]).into()); From 9ba640d46eb5f233b49f9d032887fc906ef34926 Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 14:09:09 +0300 Subject: [PATCH 13/14] Apply suggestions from clippy --- src/config.rs | 5 ++--- src/crunch.rs | 19 ++++++++----------- src/errors.rs | 2 -- src/pools.rs | 2 +- src/report.rs | 30 +++++++++++++++--------------- src/stats.rs | 16 +++++++++------- 6 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4ab662b..63d5c06 100644 --- a/src/config.rs +++ b/src/config.rs @@ -31,7 +31,6 @@ // Set Config struct into a CONFIG lazy_static to avoid multiple processing. // use clap::{App, Arg, SubCommand}; -use dotenv; use lazy_static::lazy_static; use log::{info, warn}; use serde::Deserialize; @@ -482,12 +481,12 @@ fn get_config() -> Config { // Try to load configuration from file first let config_path = matches.value_of("config-path").unwrap_or(".env"); - match dotenv::from_filename(&config_path).ok() { + match dotenv::from_filename(config_path).ok() { Some(_) => info!("Loading configuration from {} file", &config_path), None => { let config_path = env::var("CRUNCH_CONFIG_FILENAME").unwrap_or(".env".to_string()); - if let Some(_) = dotenv::from_filename(&config_path).ok() { + if dotenv::from_filename(&config_path).is_ok() { info!("Loading configuration from {} file", &config_path); } } diff --git a/src/crunch.rs b/src/crunch.rs index c986197..35176ca 100644 --- a/src/crunch.rs +++ b/src/crunch.rs @@ -139,7 +139,7 @@ pub async fn create_or_await_substrate_node_client( pub fn get_from_seed(seed: &str, pass: Option<&str>) -> sr25519::Pair { // Use regex to remove control characters let re = Regex::new(r"[\x00-\x1F]").unwrap(); - let clean_seed = re.replace_all(&seed.trim(), ""); + let clean_seed = re.replace_all(seed.trim(), ""); sr25519::Pair::from_string(&clean_seed, pass) .expect("constructed from known-good static value; qed") } @@ -250,10 +250,8 @@ fn spawn_and_restart_crunch_flakes_on_error() { let c: Crunch = Crunch::new().await; if let Err(e) = c.try_run_batch().await { let sleep_min = u32::pow(config.error_interval, n); - match e { - _ => { - error!("{}", e); - } + { + error!("{}", e); } thread::sleep(time::Duration::from_secs((60 * sleep_min).into())); n += 1; @@ -269,7 +267,7 @@ fn spawn_and_restart_crunch_flakes_on_error() { } fn healthcheck() -> async_std::task::JoinHandle<()> { - let h = task::spawn(async { + task::spawn(async { let listener = TcpListener::bind("127.0.0.1:9999").unwrap(); let response = "HTTP/1.1 200 OK\r\n\r\n".as_bytes(); @@ -287,10 +285,9 @@ fn healthcheck() -> async_std::task::JoinHandle<()> { stream.write_all(response).unwrap(); } - }); - - return h; + }) } + fn spawn_crunch_view() { let crunch_task = task::spawn(async { let c: Crunch = Crunch::new().await; @@ -309,7 +306,7 @@ pub fn random_wait(max: u64) -> u64 { pub async fn try_fetch_stashes_from_remote_url( ) -> Result>, CrunchError> { let config = CONFIG.clone(); - if config.stashes_url.len() == 0 { + if config.stashes_url.is_empty() { return Ok(None); } let response = reqwest::get(&config.stashes_url).await?.text().await?; @@ -339,7 +336,7 @@ pub async fn try_fetch_onet_data( return Ok(None); } - let endpoint = if config.onet_api_url != "" { + let endpoint = if !config.onet_api_url.is_empty() { config.onet_api_url } else { format!("https://{}-onet-api-beta.turboflakes.io", chain_name) diff --git a/src/errors.rs b/src/errors.rs index 180a6a8..d9c41ab 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -19,8 +19,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -use codec; -use reqwest; use std::{str::Utf8Error, string::String}; use subxt::error::{DispatchError, MetadataError}; use thiserror::Error; diff --git a/src/pools.rs b/src/pools.rs index 2dbcf67..f51b8f2 100644 --- a/src/pools.rs +++ b/src/pools.rs @@ -61,7 +61,7 @@ pub fn nomination_pool_account(account_type: AccountType, pool_id: u32) -> Accou let buffer_hex = buffer.encode_hex::(); // NOTE: subxt::utils::AccountId32 currently doesn't support from hex conversion let acc = subxt::ext::sp_runtime::AccountId32::from_str(&buffer_hex).unwrap(); - return AccountId32::from_str(&acc.to_string()).unwrap(); + AccountId32::from_str(&acc.to_string()).unwrap() } #[test] diff --git a/src/report.rs b/src/report.rs index 6eb72c3..b89284d 100644 --- a/src/report.rs +++ b/src/report.rs @@ -189,7 +189,7 @@ impl From for Report { * 100.0, ) } else { - format!("") + String::new() }; let summary_already_desc = if data @@ -204,7 +204,7 @@ impl From for Report { .total_validators_previous_era_already_claimed, ) } else { - format!("") + String::new() }; let summary_next_desc = if data.payout_summary.next_minimum_expected > 0 { @@ -260,12 +260,12 @@ impl From for Report { report.add_raw_text(format!( "{} {}", is_active_desc, - data.network.name.to_lowercase().trim().replace(" ", ""), + data.network.name.to_lowercase().trim().replace(' ', ""), validator.stash, validator.name, )); // Show validator warnings - if validator.warnings.len() > 0 { + if !validator.warnings.is_empty() { for warning in validator.warnings { report.add_raw_text(format!("⚠️ {} ⚠️", warning.clone())); warn!("{}", warning); @@ -279,7 +279,7 @@ impl From for Report { )); // Check if there are no payouts - if validator.payouts.len() == 0 { + if validator.payouts.is_empty() { if validator.is_active { report.add_text(format!( "🥣 Looking forward for next crunch {} {}", @@ -303,7 +303,7 @@ impl From for Report { / 10f64.powi(data.network.token_decimals.into()), data.network.token_symbol, good_performance( - payout.points.validator.into(), + payout.points.validator, payout.points.ci99_9_interval.1, payout.points.outlier_limits.1 ) @@ -356,14 +356,14 @@ impl From for Report { ({}) ✨", payout.era_index, payout.block_number, - data.network.name.to_lowercase().trim().replace(" ", ""), + data.network.name.to_lowercase().trim().replace(' ', ""), payout.extrinsic, - payout.extrinsic.to_string() + payout.extrinsic )); } // Check if there are still eras left to claim - if validator.unclaimed.len() > 0 { + if !validator.unclaimed.is_empty() { let symbols = number_to_symbols(validator.unclaimed.len(), "⚡", 84); report.add_text(format!( "{} There are still {} eras left with {} to crunch {}", @@ -395,7 +395,7 @@ impl From for Report { )); // Claimed - if validator.claimed.len() > 0 { + if !validator.claimed.is_empty() { let claimed_percentage = (validator.claimed.len() as f32 / (validator.claimed.len() + validator.unclaimed.len()) as f32) * 100.0; @@ -437,9 +437,9 @@ impl From for Report { "💯 Batch finalized at block #{} ({}) ✨", batch.block_number, - data.network.name.to_lowercase().trim().replace(" ", ""), + data.network.name.to_lowercase().trim().replace(' ', ""), batch.extrinsic, - batch.extrinsic.to_string() + batch.extrinsic )); } } else { @@ -476,9 +476,9 @@ impl From for Report { fn number_to_symbols(n: usize, symbol: &str, max: usize) -> String { let cap: usize = match n { - n if n < (max / 4) as usize => 1, - n if n < (max / 2) as usize => 2, - n if n < max - (max / 4) as usize => 3, + n if n < (max / 4) => 1, + n if n < (max / 2) => 2, + n if n < max - (max / 4) => 3, _ => 4, }; let v = vec![""; cap + 1]; diff --git a/src/stats.rs b/src/stats.rs index a14e6c7..b806229 100644 --- a/src/stats.rs +++ b/src/stats.rs @@ -19,8 +19,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +#[allow(clippy::ptr_arg)] pub fn mean(list: &Vec) -> f64 { - if list.len() == 0 { + if list.is_empty() { return 0.0; } let sum: f64 = list.iter().sum(); @@ -29,13 +30,13 @@ pub fn mean(list: &Vec) -> f64 { pub fn standard_deviation(list: &Vec) -> f64 { let m = mean(list); - let mut variance: Vec = - list.iter().map(|&score| (score - m).powf(2.0)).collect(); - mean(&mut variance).sqrt() + let variance: Vec = list.iter().map(|&score| (score - m).powf(2.0)).collect(); + mean(&variance).sqrt() } +#[allow(clippy::ptr_arg)] pub fn median(list: &mut Vec) -> u32 { - if list.len() == 0 { + if list.is_empty() { return 0; } list.sort(); @@ -67,13 +68,14 @@ pub fn confidence_interval(list: &Vec, z: f64) -> (f64, f64) { } // Find outliers by Interquartile Range(IQR) // https://www.statisticshowto.com/statistics-basics/find-outliers/ +#[allow(clippy::ptr_arg)] pub fn iqr_interval(list: &mut Vec) -> (f64, f64) { - if list.len() == 0 { + if list.is_empty() { return (0.0, 0.0); } list.sort(); let q1 = median(&mut (&list[..&list.len() / 2]).into()); - let q3 = median(&mut (&list[&list.len() - (&list.len() / 2)..]).into()); + let q3 = median(&mut (&list[list.len() - (&list.len() / 2)..]).into()); let iqr = q3 - q1; ( (q1 as f64) - (iqr as f64 * 1.5), From c3c72b09ef98cdad2c6da2fccb0fa17b9b1859fc Mon Sep 17 00:00:00 2001 From: Alex Todorov Date: Thu, 4 Apr 2024 15:13:14 +0300 Subject: [PATCH 14/14] Adjust CRUNCH_CONFIG_FILENAME to point to an existing file because .env.example has been removed in https://github.com/gluwa/crunch/commit/ca51756f500b28694d5fafdd3748a27673977819 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b8bfa46..1375622 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -100,6 +100,6 @@ jobs: - name: Run cargo test uses: actions-rs/cargo@v1 env: - CRUNCH_CONFIG_FILENAME: .env.example + CRUNCH_CONFIG_FILENAME: environments/cc3/testnet/.env with: command: test