Skip to content

Commit

Permalink
CLI WIP for new fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ebatsell committed Feb 9, 2024
1 parent e2b4eb3 commit cd7f9d6
Showing 1 changed file with 96 additions and 16 deletions.
112 changes: 96 additions & 16 deletions utils/validator-history-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
use std::{path::PathBuf, time::Duration};
use std::{path::PathBuf, thread::sleep, time::Duration};

use anchor_lang::{AccountDeserialize, Discriminator, InstructionData, ToAccountMetas};
use clap::{arg, command, Parser, Subcommand};
use solana_client::{
rpc_client::RpcClient,
rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig},
rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig, RpcSendTransactionConfig},
rpc_filter::{Memcmp, RpcFilterType},
};
use solana_program::instruction::Instruction;
use solana_program::{instruction::Instruction, native_token::lamports_to_sol};

Check failure on line 10 in utils/validator-history-cli/src/main.rs

View workflow job for this annotation

GitHub Actions / udeps

unused import: `native_token::lamports_to_sol`

Check failure on line 10 in utils/validator-history-cli/src/main.rs

View workflow job for this annotation

GitHub Actions / lint

unused import: `native_token::lamports_to_sol`

Check failure on line 10 in utils/validator-history-cli/src/main.rs

View workflow job for this annotation

GitHub Actions / test

unused import: `native_token::lamports_to_sol`
use solana_sdk::{
pubkey::Pubkey, signature::read_keypair_file, signer::Signer, transaction::Transaction,
commitment_config::CommitmentConfig, compute_budget::ComputeBudgetInstruction, pubkey::Pubkey,
signature::read_keypair_file, signer::Signer, transaction::Transaction,
};
use validator_history::{
constants::MAX_ALLOC_BYTES, ClusterHistory, Config, ValidatorHistory, ValidatorHistoryEntry,
constants::MAX_ALLOC_BYTES, ClusterHistory, ClusterHistoryEntry, Config, ValidatorHistory,
ValidatorHistoryEntry,
};

#[derive(Parser)]
Expand All @@ -36,6 +38,7 @@ enum Commands {
InitConfig(InitConfig),
InitClusterHistory(InitClusterHistory),
CrankerStatus(CrankerStatus),
ClusterHistoryStatus,
History(History),
BackfillClusterHistory(BackfillClusterHistory),
}
Expand Down Expand Up @@ -259,6 +262,12 @@ fn formatted_entry(entry: ValidatorHistoryEntry) -> String {
entry.mev_commission.to_string()
};

let mev_earned_str = if entry.mev_earned == ValidatorHistoryEntry::default().mev_earned {
"[NULL]".to_string()
} else {
(entry.mev_earned as f64 / 100.0).to_string()
};

let stake_str = if entry.activated_stake_lamports
== ValidatorHistoryEntry::default().activated_stake_lamports
{
Expand Down Expand Up @@ -317,10 +326,11 @@ fn formatted_entry(entry: ValidatorHistoryEntry) -> String {
};

format!(
"Commission: {}\t| Epoch Credits: {}\t| MEV Commission: {}\t| Stake: {}\t| Rank: {}\t| Superminority: {}\t| IP: {}\t| Client Type: {}\t| Client Version: {}\t| Last Updated: {}",
"Commission: {}\t| Epoch Credits: {}\t| MEV Commission: {}\t| MEV Earned: {}\t| Stake: {}\t| Rank: {}\t| Superminority: {}\t| IP: {}\t| Client Type: {}\t| Client Version: {}\t| Last Updated: {}",
commission_str,
epoch_credits_str,
mev_commission_str,
mev_earned_str,
stake_str,
rank_str,
superminority_str,
Expand Down Expand Up @@ -388,11 +398,14 @@ fn command_cranker_status(args: CrankerStatus, client: RpcClient) {
let mut versions = 0;
let mut types = 0;
let mut mev_comms = 0;
let mut mev_earned = 0;
let mut comms = 0;
let mut epoch_credits = 0;
let mut stakes = 0;
let mut ranks = 0;
let mut superminorities = 0;

Check failure on line 406 in utils/validator-history-cli/src/main.rs

View workflow job for this annotation

GitHub Actions / udeps

variable `superminorities` is assigned to, but never used

Check failure on line 406 in utils/validator-history-cli/src/main.rs

View workflow job for this annotation

GitHub Actions / lint

variable `superminorities` is assigned to, but never used

Check failure on line 406 in utils/validator-history-cli/src/main.rs

View workflow job for this annotation

GitHub Actions / test

variable `superminorities` is assigned to, but never used

let mut missed_mev_earned = 0;
let default = ValidatorHistoryEntry::default();
for validator_history in validator_histories {
match get_entry(validator_history, epoch) {
Expand All @@ -412,6 +425,14 @@ fn command_cranker_status(args: CrankerStatus, client: RpcClient) {
if entry.mev_commission != default.mev_commission {
mev_comms += 1;
}
if entry.mev_earned != default.mev_earned {
mev_earned += 1;
}
if entry.mev_earned == default.mev_earned
&& entry.mev_commission != default.mev_commission
{
missed_mev_earned += 1;
}
if entry.commission != default.commission {
comms += 1;
}
Expand Down Expand Up @@ -449,11 +470,12 @@ fn command_cranker_status(args: CrankerStatus, client: RpcClient) {
println!("Validators with Version:\t{}", versions);
println!("Validators with Client Type:\t{}", types);
println!("Validators with MEV Commission: {}", mev_comms);
println!("Validators with MEV Earned: \t{}", mev_earned);
println!("Validators with Commission:\t{}", comms);
println!("Validators with Epoch Credits:\t{}", epoch_credits);
println!("Validators with Stake:\t\t{}", stakes);
println!("Validators with Rank:\t\t{}", ranks);
println!("Validators with Superminority:\t\t{}", superminorities);
println!("Missed MEV Earned:\t\t{}", missed_mev_earned);
}

fn command_history(args: History, client: RpcClient) {
Expand Down Expand Up @@ -504,27 +526,77 @@ fn command_history(args: History, client: RpcClient) {
}
}

fn command_cluster_history(client: RpcClient) {
let (cluster_history_pda, _) =
Pubkey::find_program_address(&[ClusterHistory::SEED], &validator_history::ID);

let cluster_history_account = client
.get_account(&cluster_history_pda)
.expect("Failed to get cluster history account");
let cluster_history =
ClusterHistory::try_deserialize(&mut cluster_history_account.data.as_slice())
.expect("Failed to deserialize cluster history account");

// let start_epoch = cluster_history
// .history
// .arr
// .iter()
// .filter_map(|entry| {
// if entry.epoch > 0 {
// Some(entry.epoch as u64)
// } else {
// None
// }
// })
// .min()
// .unwrap_or(0);

for entry in cluster_history.history.arr.iter() {
println!(
"Epoch: {} | Total Blocks: {}",
entry.epoch, entry.total_blocks
);

if entry.epoch == ClusterHistoryEntry::default().epoch {
break;
}
}
}

fn command_backfill_cluster_history(args: BackfillClusterHistory, client: RpcClient) {
// Backfill cluster history account for a specific epoch
let keypair = read_keypair_file(args.keypair_path).expect("Failed reading keypair file");
sleep(Duration::from_secs(5));

let mut instructions = vec![];
let (cluster_history_pda, _) =
Pubkey::find_program_address(&[ClusterHistory::SEED], &validator_history::ID);
let (config, _) = Pubkey::find_program_address(&[Config::SEED], &validator_history::ID);
// let (config, _) = Pubkey::find_program_address(&[Config::SEED], &validator_history::ID);
// let cluster_history_account = client
// .get_account(&cluster_history_pda)
// .expect("Failed to get cluster history account");
// let cluster_history =
// ClusterHistory::try_deserialize(&mut cluster_history_account.data.as_slice())
// .expect("Failed to deserialize cluster history account");

// if !cluster_history.history.is_empty()
// && cluster_history.history.last().unwrap().epoch + 1 != args.epoch as u16
// {
// panic!("Cannot set this epoch, you would mess up the ordering");
// }
let max_heap_instruction: Instruction =
ComputeBudgetInstruction::request_heap_frame(148 * 1024);
instructions.push(max_heap_instruction);

instructions.push(Instruction {
program_id: validator_history::ID,
accounts: validator_history::accounts::BackfillTotalBlocks {
accounts: validator_history::accounts::CopyClusterInfo {
cluster_history_account: cluster_history_pda,
config,
slot_history: solana_program::sysvar::slot_history::id(),
signer: keypair.pubkey(),
}
.to_account_metas(None),
data: validator_history::instruction::BackfillTotalBlocks {
epoch: args.epoch,
blocks_in_epoch: args.blocks_in_epoch,
}
.data(),
data: validator_history::instruction::CopyClusterInfo {}.data(),
});

let blockhash = client
Expand All @@ -538,7 +610,14 @@ fn command_backfill_cluster_history(args: BackfillClusterHistory, client: RpcCli
);

let signature = client
.send_and_confirm_transaction_with_spinner(&transaction)
.send_and_confirm_transaction_with_spinner_and_config(
&transaction,
CommitmentConfig::processed(),
RpcSendTransactionConfig {
skip_preflight: true,
..RpcSendTransactionConfig::default()
},
)
.expect("Failed to send transaction");
println!("Signature: {}", signature);
}
Expand All @@ -550,6 +629,7 @@ fn main() {
Commands::InitConfig(args) => command_init_config(args, client),
Commands::CrankerStatus(args) => command_cranker_status(args, client),
Commands::InitClusterHistory(args) => command_init_cluster_history(args, client),
Commands::ClusterHistoryStatus => command_cluster_history(client),
Commands::History(args) => command_history(args, client),
Commands::BackfillClusterHistory(args) => command_backfill_cluster_history(args, client),
};
Expand Down

0 comments on commit cd7f9d6

Please sign in to comment.