From fc37ab1d783bb5bcd343e6d1ea971927d88177d2 Mon Sep 17 00:00:00 2001 From: Coach Chuck Date: Wed, 15 May 2024 15:07:36 -0500 Subject: [PATCH] linted --- .../src/entries/gossip_entry.rs | 10 +-- .../src/entries/mev_commission_entry.rs | 6 +- keepers/validator-keeper/src/lib.rs | 10 +-- keepers/validator-keeper/src/main.rs | 65 ++++++++++--------- .../src/operations/cluster_history.rs | 13 ++-- .../src/operations/gossip_upload.rs | 19 ++---- .../src/operations/metrics_emit.rs | 2 +- .../src/operations/mev_commission.rs | 9 +-- .../src/operations/mev_earned.rs | 4 +- .../src/operations/stake_upload.rs | 13 ++-- .../src/operations/vote_account.rs | 19 ++---- .../src/state/keeper_state.rs | 52 ++++++++------- .../src/state/update_state.rs | 14 ++-- 13 files changed, 110 insertions(+), 126 deletions(-) diff --git a/keepers/validator-keeper/src/entries/gossip_entry.rs b/keepers/validator-keeper/src/entries/gossip_entry.rs index f1fe8117..58ffcfab 100644 --- a/keepers/validator-keeper/src/entries/gossip_entry.rs +++ b/keepers/validator-keeper/src/entries/gossip_entry.rs @@ -33,14 +33,14 @@ impl GossipEntry { let validator_history_account = derive_validator_history_address(vote_account, program_id); let config = derive_validator_history_config_address(program_id); Self { - vote_account: vote_account.clone(), + vote_account: *vote_account, validator_history_account, config, - signature: signature.clone(), + signature: *signature, message: message.to_vec(), - program_id: program_id.clone(), - identity: identity.clone(), - signer: signer.clone(), + program_id: *program_id, + identity: *identity, + signer: *signer, } } } diff --git a/keepers/validator-keeper/src/entries/mev_commission_entry.rs b/keepers/validator-keeper/src/entries/mev_commission_entry.rs index b54c0231..ea885834 100644 --- a/keepers/validator-keeper/src/entries/mev_commission_entry.rs +++ b/keepers/validator-keeper/src/entries/mev_commission_entry.rs @@ -33,12 +33,12 @@ impl ValidatorMevCommissionEntry { let config = derive_validator_history_config_address(program_id); Self { - vote_account: vote_account.clone(), + vote_account: *vote_account, tip_distribution_account, validator_history_account, config, - program_id: program_id.clone(), - signer: signer.clone(), + program_id: *program_id, + signer: *signer, epoch, } } diff --git a/keepers/validator-keeper/src/lib.rs b/keepers/validator-keeper/src/lib.rs index 88f0557b..6341467e 100644 --- a/keepers/validator-keeper/src/lib.rs +++ b/keepers/validator-keeper/src/lib.rs @@ -257,10 +257,10 @@ pub fn get_create_validator_history_instructions( let config_account = derive_validator_history_config_address(program_id); let mut ixs = vec![Instruction { - program_id: program_id.clone(), + program_id: *program_id, accounts: validator_history::accounts::InitializeValidatorHistoryAccount { validator_history_account, - vote_account: vote_account.clone(), + vote_account: *vote_account, system_program: solana_program::system_program::id(), signer: signer.pubkey(), } @@ -271,10 +271,10 @@ pub fn get_create_validator_history_instructions( let num_reallocs = (ValidatorHistory::SIZE - MAX_ALLOC_BYTES) / MAX_ALLOC_BYTES + 1; ixs.extend(vec![ Instruction { - program_id: program_id.clone(), + program_id: *program_id, accounts: validator_history::accounts::ReallocValidatorHistoryAccount { - validator_history_account: validator_history_account, - vote_account: vote_account.clone(), + validator_history_account, + vote_account: *vote_account, config: config_account, system_program: solana_program::system_program::id(), signer: signer.pubkey(), diff --git a/keepers/validator-keeper/src/main.rs b/keepers/validator-keeper/src/main.rs index 386efb14..9259a024 100644 --- a/keepers/validator-keeper/src/main.rs +++ b/keepers/validator-keeper/src/main.rs @@ -70,7 +70,7 @@ struct Args { cluster: Cluster, } -fn should_update(tick: u64, intervals: &Vec) -> bool { +fn should_update(tick: u64, intervals: &[u64]) -> bool { intervals.iter().any(|interval| tick % interval == 0) } @@ -87,7 +87,7 @@ async fn sleep_and_tick(tick: &mut u64) { advance_tick(tick); } -async fn run_loop( +struct RunLoopConfig { client: Arc, keypair: Arc, program_id: Pubkey, @@ -96,7 +96,19 @@ async fn run_loop( gossip_entrypoint: Option, validator_history_interval: u64, metrics_interval: u64, -) { +} + +async fn run_loop(config: RunLoopConfig) { + let RunLoopConfig { + client, + keypair, + program_id, + tip_distribution_program_id, + oracle_authority_keypair, + gossip_entrypoint, + validator_history_interval, + metrics_interval, + } = config; let intervals = vec![validator_history_interval, metrics_interval]; // Stateful data @@ -214,7 +226,7 @@ async fn run_loop( keeper_state.set_runs_and_errors_for_epoch( operations::stake_upload::fire_and_emit( &client, - &oracle_authority_keypair, + oracle_authority_keypair, &program_id, &keeper_state, ) @@ -229,7 +241,7 @@ async fn run_loop( keeper_state.set_runs_and_errors_for_epoch( operations::gossip_upload::fire_and_emit( &client, - &oracle_authority_keypair, + oracle_authority_keypair, &program_id, &gossip_entrypoint, &keeper_state, @@ -266,39 +278,32 @@ async fn main() { let keypair = Arc::new(read_keypair_file(args.keypair).expect("Failed reading keypair file")); - let oracle_authority_keypair = { - if let Some(oracle_authority_keypair) = args.oracle_authority_keypair { - Some(Arc::new( + let oracle_authority_keypair = args + .oracle_authority_keypair + .map(|oracle_authority_keypair| { + Arc::new( read_keypair_file(oracle_authority_keypair) .expect("Failed reading stake keypair file"), - )) - } else { - None - } - }; - - let gossip_entrypoint = { - if let Some(gossip_entrypoint) = args.gossip_entrypoint { - Some( - solana_net_utils::parse_host_port(&gossip_entrypoint) - .expect("Failed to parse host and port from gossip entrypoint"), ) - } else { - None - } - }; + }); + + let gossip_entrypoint = args.gossip_entrypoint.map(|gossip_entrypoint| { + solana_net_utils::parse_host_port(&gossip_entrypoint) + .expect("Failed to parse host and port from gossip entrypoint") + }); info!("Starting validator history keeper..."); - run_loop( + let config = RunLoopConfig { client, keypair, - args.program_id, - args.tip_distribution_program_id, + program_id: args.program_id, + tip_distribution_program_id: args.tip_distribution_program_id, oracle_authority_keypair, gossip_entrypoint, - args.validator_history_interval, - args.metrics_interval, - ) - .await; + validator_history_interval: args.validator_history_interval, + metrics_interval: args.metrics_interval, + }; + + run_loop(config).await; } diff --git a/keepers/validator-keeper/src/operations/cluster_history.rs b/keepers/validator-keeper/src/operations/cluster_history.rs index 928edc10..3046a2f1 100644 --- a/keepers/validator-keeper/src/operations/cluster_history.rs +++ b/keepers/validator-keeper/src/operations/cluster_history.rs @@ -22,17 +22,14 @@ use std::sync::Arc; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::ClusterHistory; + KeeperOperations::ClusterHistory } fn _should_run(epoch_info: &EpochInfo, runs_for_epoch: u64) -> bool { // Run at 0.1%, 50% and 90% completion of epoch - let should_run = (epoch_info.slot_index > epoch_info.slots_in_epoch / 1000 - && runs_for_epoch < 1) + (epoch_info.slot_index > epoch_info.slots_in_epoch / 1000 && runs_for_epoch < 1) || (epoch_info.slot_index > epoch_info.slots_in_epoch / 2 && runs_for_epoch < 2) - || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3); - - should_run + || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3) } async fn _process( @@ -65,7 +62,7 @@ pub async fn fire_and_emit( let (mut runs_for_epoch, mut errors_for_epoch) = keeper_state.copy_runs_and_errors_for_epoch(operation.clone()); - let should_run = _should_run(&epoch_info, runs_for_epoch.clone()); + let should_run = _should_run(epoch_info, runs_for_epoch); let mut stats = SubmitStats::default(); if should_run { @@ -117,7 +114,7 @@ pub fn get_update_cluster_info_instructions( accounts: validator_history::accounts::CopyClusterInfo { cluster_history_account, slot_history: solana_program::sysvar::slot_history::id(), - signer: keypair.clone(), + signer: *keypair, } .to_account_metas(None), data: validator_history::instruction::CopyClusterInfo {}.data(), diff --git a/keepers/validator-keeper/src/operations/gossip_upload.rs b/keepers/validator-keeper/src/operations/gossip_upload.rs index c46f0f06..e703f8a2 100644 --- a/keepers/validator-keeper/src/operations/gossip_upload.rs +++ b/keepers/validator-keeper/src/operations/gossip_upload.rs @@ -32,16 +32,14 @@ use validator_history::ValidatorHistoryEntry; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::GossipUpload; + KeeperOperations::GossipUpload } fn _should_run(epoch_info: &EpochInfo, runs_for_epoch: u64) -> bool { // Run at 0%, 50% and 90% completion of epoch - let should_run = runs_for_epoch < 1 + runs_for_epoch < 1 || (epoch_info.slot_index > epoch_info.slots_in_epoch / 2 && runs_for_epoch < 2) - || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3); - - should_run + || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3) } async fn _process( @@ -255,13 +253,8 @@ pub async fn upload_gossip_values( gossip_port, ); let exit: Arc = Arc::new(AtomicBool::new(false)); - let (_gossip_service, cluster_info) = start_spy_server( - entrypoint.clone(), - gossip_port, - spy_socket_addr, - &keypair, - &exit, - ); + let (_gossip_service, cluster_info) = + start_spy_server(*entrypoint, gossip_port, spy_socket_addr, keypair, &exit); // Wait for all active validators to be received sleep(Duration::from_secs(150)).await; @@ -284,7 +277,7 @@ pub async fn upload_gossip_values( validator_history_account, &crds, *program_id, - &keypair, + keypair, ) }) .flatten() diff --git a/keepers/validator-keeper/src/operations/metrics_emit.rs b/keepers/validator-keeper/src/operations/metrics_emit.rs index 4f11f764..9accbb51 100644 --- a/keepers/validator-keeper/src/operations/metrics_emit.rs +++ b/keepers/validator-keeper/src/operations/metrics_emit.rs @@ -11,7 +11,7 @@ use validator_history::ValidatorHistoryEntry; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::EmitMetrics; + KeeperOperations::EmitMetrics } fn _should_run() -> bool { diff --git a/keepers/validator-keeper/src/operations/mev_commission.rs b/keepers/validator-keeper/src/operations/mev_commission.rs index cda1349e..2606e1fe 100644 --- a/keepers/validator-keeper/src/operations/mev_commission.rs +++ b/keepers/validator-keeper/src/operations/mev_commission.rs @@ -21,7 +21,7 @@ use validator_history::ValidatorHistoryEntry; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::MevCommission; + KeeperOperations::MevCommission } fn _should_run() -> bool { @@ -120,15 +120,12 @@ pub async fn update_mev_commission( let existing_entries = current_epoch_tip_distribution_map .iter() - .filter_map(|(pubkey, account)| match account { - Some(_) => Some(pubkey.clone()), - None => None, - }) + .filter_map(|(pubkey, account)| account.as_ref().map(|_| *pubkey)) .collect::>(); let entries_to_update = existing_entries .into_iter() - .filter(|entry| !mev_commission_uploaded(&validator_history_map, entry, epoch_info.epoch)) + .filter(|entry| !mev_commission_uploaded(validator_history_map, entry, epoch_info.epoch)) .collect::>(); let update_instructions = entries_to_update diff --git a/keepers/validator-keeper/src/operations/mev_earned.rs b/keepers/validator-keeper/src/operations/mev_earned.rs index 6723d0d1..3d83e470 100644 --- a/keepers/validator-keeper/src/operations/mev_earned.rs +++ b/keepers/validator-keeper/src/operations/mev_earned.rs @@ -23,7 +23,7 @@ use validator_history::ValidatorHistoryEntry; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::MevEarned; + KeeperOperations::MevEarned } fn _should_run() -> bool { @@ -129,7 +129,7 @@ pub async fn update_mev_earned( let mut data: &[u8] = &account_data.data; let tda = TipDistributionAccount::try_deserialize(&mut data).ok()?; if tda.merkle_root.is_some() { - Some(address.clone()) + Some(*address) } else { None } diff --git a/keepers/validator-keeper/src/operations/stake_upload.rs b/keepers/validator-keeper/src/operations/stake_upload.rs index 336f88b6..e98cb181 100644 --- a/keepers/validator-keeper/src/operations/stake_upload.rs +++ b/keepers/validator-keeper/src/operations/stake_upload.rs @@ -22,17 +22,14 @@ use validator_history::{ValidatorHistory, ValidatorHistoryEntry}; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::StakeUpload; + KeeperOperations::StakeUpload } fn _should_run(epoch_info: &EpochInfo, runs_for_epoch: u64) -> bool { // Run at 0.1%, 50% and 90% completion of epoch - let should_run = (epoch_info.slot_index > epoch_info.slots_in_epoch / 1000 - && runs_for_epoch < 1) + (epoch_info.slot_index > epoch_info.slots_in_epoch / 1000 && runs_for_epoch < 1) || (epoch_info.slot_index > epoch_info.slots_in_epoch / 2 && runs_for_epoch < 2) - || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3); - - should_run + || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3) } async fn _process( @@ -136,7 +133,7 @@ pub async fn update_stake_history( let rank = stake_rank_map[&vote_account.vote_pubkey.clone()]; let is_superminority = rank <= superminority_threshold; - if stake_entry_uploaded(&validator_history_map, vote_account, epoch_info.epoch) { + if stake_entry_uploaded(validator_history_map, vote_account, epoch_info.epoch) { return None; } @@ -191,7 +188,7 @@ Calculates ordering of validators by stake, assigning a 0..N rank (validator 0 h and returns the index at which all validators before are in the superminority. 0-indexed. */ fn get_stake_rank_map_and_superminority_count( - vote_accounts: &Vec<&RpcVoteAccountInfo>, + vote_accounts: &[&RpcVoteAccountInfo], ) -> (HashMap, u32) { let mut stake_vec = vote_accounts .iter() diff --git a/keepers/validator-keeper/src/operations/vote_account.rs b/keepers/validator-keeper/src/operations/vote_account.rs index bc8a0bbe..1f40ac22 100644 --- a/keepers/validator-keeper/src/operations/vote_account.rs +++ b/keepers/validator-keeper/src/operations/vote_account.rs @@ -22,17 +22,14 @@ use validator_history::ValidatorHistoryEntry; use super::keeper_operations::KeeperOperations; fn _get_operation() -> KeeperOperations { - return KeeperOperations::VoteAccount; + KeeperOperations::VoteAccount } fn _should_run(epoch_info: &EpochInfo, runs_for_epoch: u64) -> bool { // Run at 10%, 50% and 90% completion of epoch - let should_run = (epoch_info.slot_index > epoch_info.slots_in_epoch / 1000 - && runs_for_epoch < 1) + (epoch_info.slot_index > epoch_info.slots_in_epoch / 1000 && runs_for_epoch < 1) || (epoch_info.slot_index > epoch_info.slots_in_epoch / 2 && runs_for_epoch < 2) - || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3); - - should_run + || (epoch_info.slot_index > epoch_info.slots_in_epoch * 9 / 10 && runs_for_epoch < 3) } async fn _process( @@ -65,7 +62,7 @@ pub async fn fire_and_emit( let (mut runs_for_epoch, mut errors_for_epoch) = keeper_state.copy_runs_and_errors_for_epoch(operation.clone()); - let should_run = _should_run(epoch_info, runs_for_epoch.clone()); + let should_run = _should_run(epoch_info, runs_for_epoch); let mut stats = SubmitStats::default(); if should_run { @@ -97,11 +94,7 @@ pub async fn fire_and_emit( }; } - _emit( - &stats, - runs_for_epoch.clone() as i64, - errors_for_epoch.clone() as i64, - ); + _emit(&stats, runs_for_epoch as i64, errors_for_epoch as i64); (operation, runs_for_epoch, errors_for_epoch) } @@ -124,7 +117,7 @@ pub async fn update_vote_accounts( vote_accounts_to_update.retain(|vote_account| { !closed_vote_accounts.contains(vote_account) && !vote_account_uploaded_recently( - &validator_history_map, + validator_history_map, vote_account, epoch_info.epoch, epoch_info.absolute_slot, diff --git a/keepers/validator-keeper/src/state/keeper_state.rs b/keepers/validator-keeper/src/state/keeper_state.rs index 24e65bce..5a47cd4f 100644 --- a/keepers/validator-keeper/src/state/keeper_state.rs +++ b/keepers/validator-keeper/src/state/keeper_state.rs @@ -29,26 +29,7 @@ pub struct KeeperState { } impl KeeperState { pub fn new() -> Self { - Self { - epoch_info: EpochInfo { - epoch: 0, - slot_index: 0, - slots_in_epoch: 0, - absolute_slot: 0, - block_height: 0, - transaction_count: None, - }, - runs_for_epoch: [0; KeeperOperations::LEN], - errors_for_epoch: [0; KeeperOperations::LEN], - vote_account_map: HashMap::new(), - validator_history_map: HashMap::new(), - all_history_vote_account_map: HashMap::new(), - all_get_vote_account_map: HashMap::new(), - previous_epoch_tip_distribution_map: HashMap::new(), - current_epoch_tip_distribution_map: HashMap::new(), - cluster_history: ClusterHistory::zeroed(), - keeper_balance: 0, - } + Self::default() } pub fn increment_update_run_for_epoch(&mut self, operation: KeeperOperations) { @@ -63,10 +44,7 @@ impl KeeperState { pub fn copy_runs_and_errors_for_epoch(&self, operation: KeeperOperations) -> (u64, u64) { let index = operation as usize; - ( - self.runs_for_epoch[index].clone(), - self.errors_for_epoch[index].clone(), - ) + (self.runs_for_epoch[index], self.errors_for_epoch[index]) } pub fn set_runs_and_errors_for_epoch( @@ -81,7 +59,6 @@ impl KeeperState { pub fn get_history_pubkeys(&self, program_id: &Pubkey) -> HashSet { self.all_history_vote_account_map .keys() - .into_iter() .map(|vote_account| derive_validator_history_address(vote_account, program_id)) .collect() } @@ -115,6 +92,31 @@ impl KeeperState { } } +impl Default for KeeperState { + fn default() -> Self { + Self { + epoch_info: EpochInfo { + epoch: 0, + slot_index: 0, + slots_in_epoch: 0, + absolute_slot: 0, + block_height: 0, + transaction_count: None, + }, + runs_for_epoch: [0; KeeperOperations::LEN], + errors_for_epoch: [0; KeeperOperations::LEN], + vote_account_map: HashMap::new(), + validator_history_map: HashMap::new(), + all_history_vote_account_map: HashMap::new(), + all_get_vote_account_map: HashMap::new(), + previous_epoch_tip_distribution_map: HashMap::new(), + current_epoch_tip_distribution_map: HashMap::new(), + cluster_history: ClusterHistory::zeroed(), + keeper_balance: 0, + } + } +} + impl std::fmt::Debug for KeeperState { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { f.debug_struct("KeeperState") diff --git a/keepers/validator-keeper/src/state/update_state.rs b/keepers/validator-keeper/src/state/update_state.rs index 6c2aeae6..30b7f9c9 100644 --- a/keepers/validator-keeper/src/state/update_state.rs +++ b/keepers/validator-keeper/src/state/update_state.rs @@ -93,7 +93,7 @@ pub async fn create_missing_accounts( keeper_state: &KeeperState, ) -> Result<(), Box> { // Create Missing Accounts - create_missing_validator_history_accounts(client, keypair, program_id, &keeper_state).await?; + create_missing_validator_history_accounts(client, keypair, program_id, keeper_state).await?; Ok(()) } @@ -182,7 +182,7 @@ async fn get_cluster_history( client: &Arc, program_id: &Pubkey, ) -> Result> { - let cluster_history_address = derive_cluster_history_address(&program_id); + let cluster_history_address = derive_cluster_history_address(program_id); let cluster_history_account = client.get_account(&cluster_history_address).await?; let cluster_history = ClusterHistory::try_deserialize(&mut cluster_history_account.data.as_slice())?; @@ -195,12 +195,12 @@ async fn get_validator_history_map( program_id: &Pubkey, ) -> Result, Box> { let validator_histories = - get_validator_history_accounts_with_retry(&client, program_id.clone()).await?; + get_validator_history_accounts_with_retry(client, *program_id).await?; let validator_history_map = HashMap::from_iter( validator_histories .iter() - .map(|vote_history| (vote_history.vote_account, vote_history.clone())), + .map(|vote_history| (vote_history.vote_account, *vote_history)), ); Ok(validator_history_map) @@ -271,12 +271,12 @@ async fn get_tip_distribution_accounts( .collect::>(); let tip_distribution_accounts = - get_multiple_accounts_batched(&tip_distribution_addresses, &client).await?; + get_multiple_accounts_batched(&tip_distribution_addresses, client).await?; let result = vote_accounts .into_iter() .zip(tip_distribution_accounts) - .map(|(vote_pubkey, account)| (vote_pubkey.clone(), account)) // Dereference vote_pubkey here + .map(|(vote_pubkey, account)| (*vote_pubkey, account)) // Dereference vote_pubkey here .collect::>>(); Ok(result) @@ -298,7 +298,7 @@ async fn create_missing_validator_history_accounts( .map(|vote_pubkey| derive_validator_history_address(vote_pubkey, program_id)) .collect::>(); - let history_accounts = get_multiple_accounts_batched(&all_history_addresses, client).await?; + let history_accounts = get_multiple_accounts_batched(all_history_addresses, client).await?; assert!(vote_accounts.len() == history_accounts.len());