From 96421fb84a9bf4261d2106f75aad6b32f4a7560b Mon Sep 17 00:00:00 2001 From: Trent Nelson Date: Sun, 10 Apr 2022 15:16:59 -0600 Subject: [PATCH] cli: sort option for validators by version (cherry picked from commit 91993d89b0e68f5400bec49e724e1a53bcb928f2) # Conflicts: # Cargo.lock # cli-output/Cargo.toml # programs/bpf/Cargo.lock --- Cargo.lock | 1 + cli-output/Cargo.toml | 1 + cli-output/src/cli_output.rs | 17 +++++++++++++++++ cli/src/cluster_query.rs | 2 ++ programs/bpf/Cargo.lock | 1 + 5 files changed, 22 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 3e38d779177dd2..c7c3d0459e4ece 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4640,6 +4640,7 @@ dependencies = [ "ed25519-dalek", "humantime", "indicatif", + "semver 1.0.6", "serde", "serde_json", "solana-account-decoder", diff --git a/cli-output/Cargo.toml b/cli-output/Cargo.toml index b71dd2eb361964..a92ca18c1f3b26 100644 --- a/cli-output/Cargo.toml +++ b/cli-output/Cargo.toml @@ -17,6 +17,7 @@ clap = "2.33.0" console = "0.15.0" humantime = "2.0.1" indicatif = "0.16.2" +semver = "1.0.6" serde = "1.0.136" serde_json = "1.0.79" solana-account-decoder = { path = "../account-decoder", version = "=1.10.9" } diff --git a/cli-output/src/cli_output.rs b/cli-output/src/cli_output.rs index 606199d4874a72..529ee91ebca61e 100644 --- a/cli-output/src/cli_output.rs +++ b/cli-output/src/cli_output.rs @@ -356,6 +356,7 @@ pub enum CliValidatorsSortOrder { SkipRate, Stake, VoteAccount, + Version, } #[derive(Serialize, Deserialize)] @@ -494,6 +495,22 @@ impl fmt::Display for CliValidators { CliValidatorsSortOrder::Stake => { sorted_validators.sort_by_key(|a| a.activated_stake); } + CliValidatorsSortOrder::Version => { + sorted_validators.sort_by(|a, b| { + use std::cmp::Ordering; + let a_version = semver::Version::parse(a.version.as_str()).ok(); + let b_version = semver::Version::parse(b.version.as_str()).ok(); + match (a_version, b_version) { + (None, None) => a.version.cmp(&b.version), + (None, Some(_)) => Ordering::Less, + (Some(_), None) => Ordering::Greater, + (Some(va), Some(vb)) => match va.cmp(&vb) { + Ordering::Equal => a.activated_stake.cmp(&b.activated_stake), + ordering => ordering, + }, + } + }); + } } if self.validators_reverse_sort { diff --git a/cli/src/cluster_query.rs b/cli/src/cluster_query.rs index 1fb3119a326a55..00e3aab897c964 100644 --- a/cli/src/cluster_query.rs +++ b/cli/src/cluster_query.rs @@ -379,6 +379,7 @@ impl ClusterQuerySubCommands for App<'_, '_> { "root", "skip-rate", "stake", + "version", "vote-account", ]) .default_value("stake") @@ -650,6 +651,7 @@ pub fn parse_show_validators(matches: &ArgMatches<'_>) -> Result CliValidatorsSortOrder::SkipRate, "stake" => CliValidatorsSortOrder::Stake, "vote-account" => CliValidatorsSortOrder::VoteAccount, + "version" => CliValidatorsSortOrder::Version, _ => unreachable!(), }; diff --git a/programs/bpf/Cargo.lock b/programs/bpf/Cargo.lock index 8135585ef17ca0..d3b8a8be394598 100644 --- a/programs/bpf/Cargo.lock +++ b/programs/bpf/Cargo.lock @@ -3317,6 +3317,7 @@ dependencies = [ "console", "humantime", "indicatif", + "semver 1.0.6", "serde", "serde_json", "solana-account-decoder",