From 626fcd8418e8504c39f6f6445a5d5e69d149db24 Mon Sep 17 00:00:00 2001
From: Al3xGROS <alexandre@e36knots.com>
Date: Fri, 12 Apr 2024 16:06:19 +0200
Subject: [PATCH] fix: delete pending validators

---
 Cargo.lock                                    |  8 ++--
 crates/ash_cli/src/avalanche.rs               | 11 -----
 crates/ash_cli/src/avalanche/subnet.rs        |  1 -
 crates/ash_cli/src/avalanche/validator.rs     | 37 +++++-----------
 crates/ash_sdk/src/avalanche.rs               | 28 ------------
 .../src/avalanche/jsonrpc/platformvm.rs       | 44 +------------------
 6 files changed, 16 insertions(+), 113 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index fd79c74..43d0928 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -161,7 +161,9 @@ dependencies = [
 
 [[package]]
 name = "ash_api"
-version = "0.1.4"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "128e8c7a21a6d998c954ff1ff987121ade10b92ef514b9bf2d1635d2ba51a2f0"
 dependencies = [
  "reqwest",
  "serde",
@@ -173,7 +175,7 @@ dependencies = [
 
 [[package]]
 name = "ash_cli"
-version = "0.4.1"
+version = "0.4.2"
 dependencies = [
  "ash_sdk",
  "async-std",
@@ -202,7 +204,7 @@ dependencies = [
 
 [[package]]
 name = "ash_sdk"
-version = "0.4.1"
+version = "0.4.2"
 dependencies = [
  "ash_api",
  "async-std",
diff --git a/crates/ash_cli/src/avalanche.rs b/crates/ash_cli/src/avalanche.rs
index 11be8dd..1fdedc9 100644
--- a/crates/ash_cli/src/avalanche.rs
+++ b/crates/ash_cli/src/avalanche.rs
@@ -66,17 +66,6 @@ fn update_subnet_validators(
     Ok(())
 }
 
-// Update a Subnet's pending validators
-fn update_subnet_pending_validators(
-    network: &mut AvalancheNetwork,
-    subnet_id: &str,
-) -> Result<(), CliError> {
-    network
-        .update_subnet_pending_validators(parse_id(subnet_id)?)
-        .map_err(|e| CliError::dataerr(format!("Error updating pending validators: {e}")))?;
-    Ok(())
-}
-
 // Parse avalanche subcommand
 pub(crate) fn parse(
     avalanche: AvalancheCommand,
diff --git a/crates/ash_cli/src/avalanche/subnet.rs b/crates/ash_cli/src/avalanche/subnet.rs
index 1e6fb01..b330d67 100644
--- a/crates/ash_cli/src/avalanche/subnet.rs
+++ b/crates/ash_cli/src/avalanche/subnet.rs
@@ -94,7 +94,6 @@ fn info(
     let mut network = load_network(network_name, config)?;
     update_network_subnets(&mut network)?;
     update_subnet_validators(&mut network, id)?;
-    update_subnet_pending_validators(&mut network, id)?;
 
     let subnet = network
         .get_subnet(parse_id(id)?)
diff --git a/crates/ash_cli/src/avalanche/validator.rs b/crates/ash_cli/src/avalanche/validator.rs
index ff96ddb..fca8d87 100644
--- a/crates/ash_cli/src/avalanche/validator.rs
+++ b/crates/ash_cli/src/avalanche/validator.rs
@@ -115,39 +115,22 @@ fn list(
     let subnet;
     let validators;
 
-    let first_line = match pending {
-        true => {
-            update_subnet_pending_validators(&mut network, subnet_id)?;
-            subnet = network
-                .get_subnet(parse_id(subnet_id)?)
-                .map_err(|e| CliError::dataerr(format!("Error listing validators: {e}")))?;
-            validators = subnet.pending_validators.clone();
-            format!(
-                "Found {} pending validators on Subnet '{}':",
-                type_colorize(&subnet.pending_validators.len()),
-                type_colorize(&subnet_id)
-            )
-        }
-        false => {
-            update_subnet_validators(&mut network, subnet_id)?;
-            subnet = network
-                .get_subnet(parse_id(subnet_id)?)
-                .map_err(|e| CliError::dataerr(format!("Error listing validators: {e}")))?;
-            validators = subnet.validators.clone();
-            format!(
-                "Found {} validators on Subnet '{}':",
-                type_colorize(&subnet.validators.len()),
-                type_colorize(&subnet_id)
-            )
-        }
-    };
+    update_subnet_validators(&mut network, subnet_id)?;
+    subnet = network
+        .get_subnet(parse_id(subnet_id)?)
+        .map_err(|e| CliError::dataerr(format!("Error listing validators: {e}")))?;
+    validators = subnet.validators.clone();
+    format!(
+        "Found {} validators on Subnet '{}':",
+        type_colorize(&subnet.validators.len()),
+        type_colorize(&subnet_id)
+    );
 
     if json {
         println!("{}", serde_json::to_string(&validators).unwrap());
         return Ok(());
     }
 
-    println!("{}", first_line);
     for validator in validators.iter() {
         println!(
             "{}",
diff --git a/crates/ash_sdk/src/avalanche.rs b/crates/ash_sdk/src/avalanche.rs
index c13e0c8..c147639 100644
--- a/crates/ash_sdk/src/avalanche.rs
+++ b/crates/ash_sdk/src/avalanche.rs
@@ -295,34 +295,6 @@ impl AvalancheNetwork {
         Ok(())
     }
 
-    /// Update the pending validators of a Subnet by querying an API endpoint
-    pub fn update_subnet_pending_validators(&mut self, subnet_id: Id) -> Result<(), AshError> {
-        let rpc_url = &self.get_pchain()?.rpc_url;
-
-        let validators = platformvm::get_pending_validators(rpc_url, subnet_id)?;
-
-        // Replace the pending validators of the Subnet
-        let mut subnet = self.get_subnet(subnet_id)?.clone();
-
-        subnet.pending_validators = validators;
-
-        // Get the index of the Subnet
-        let subnet_index = self
-            .subnets
-            .iter()
-            .position(|subnet| subnet.id == subnet_id)
-            .ok_or(AvalancheNetworkError::NotFound {
-                network: self.name.clone(),
-                target_type: "Subnet".to_string(),
-                target_value: subnet_id.to_string(),
-            })?;
-
-        // Replace the Subnet
-        self.subnets[subnet_index] = subnet;
-
-        Ok(())
-    }
-
     /// Check if the operation is allowed on the network
     /// If not, return an error
     fn check_operation_allowed(
diff --git a/crates/ash_sdk/src/avalanche/jsonrpc/platformvm.rs b/crates/ash_sdk/src/avalanche/jsonrpc/platformvm.rs
index f30b68a..40c1a89 100644
--- a/crates/ash_sdk/src/avalanche/jsonrpc/platformvm.rs
+++ b/crates/ash_sdk/src/avalanche/jsonrpc/platformvm.rs
@@ -6,7 +6,7 @@
 use crate::avalanche::{
     blockchains::AvalancheBlockchain,
     jsonrpc::{get_json_rpc_req_result, JsonRpcResponse},
-    subnets::{AvalancheSubnet, AvalancheSubnetDelegator, AvalancheSubnetValidator},
+    subnets::{AvalancheSubnet, AvalancheSubnetValidator},
 };
 use crate::{errors::*, impl_json_rpc_response};
 use avalanche_types::{
@@ -51,7 +51,6 @@ impl_json_rpc_response!(
 );
 impl_json_rpc_response!(GetBlockchainsResponse, GetBlockchainsResult);
 impl_json_rpc_response!(GetCurrentValidatorsResponse, GetCurrentValidatorsResult);
-impl_json_rpc_response!(GetPendingValidatorsResponse, GetPendingValidatorsResult);
 
 /// Get the Subnets of the network by querying the P-Chain API
 pub fn get_network_subnets(
@@ -124,47 +123,6 @@ pub fn get_current_validators(
     Ok(current_validators)
 }
 
-/// Get the pending validators of a Subnet by querying the P-Chain API
-pub fn get_pending_validators(
-    rpc_url: &str,
-    subnet_id: Id,
-) -> Result<Vec<AvalancheSubnetValidator>, RpcError> {
-    let pending_validators_result: GetPendingValidatorsResult =
-        get_json_rpc_req_result::<GetPendingValidatorsResponse, GetPendingValidatorsResult>(
-            rpc_url,
-            "platform.getPendingValidators",
-            Some(ureq::json!({ "subnetID": subnet_id.to_string() })),
-        )?;
-
-    let mut pending_validators: Vec<AvalancheSubnetValidator> = pending_validators_result
-        .validators
-        .iter()
-        .map(|validator| AvalancheSubnetValidator::from_api_primary_validator(validator, subnet_id))
-        .collect();
-    let pending_validators_iter = pending_validators.clone();
-
-    // For each pending validator, add related delegators
-    for pending_validator in pending_validators_iter.iter() {
-        let delegators: Vec<AvalancheSubnetDelegator> = pending_validators_result
-            .delegators
-            .iter()
-            .filter(|delegator| delegator.node_id == pending_validator.node_id)
-            .cloned()
-            .map(Into::into)
-            .collect();
-
-        if !delegators.is_empty() {
-            pending_validators
-                .iter_mut()
-                .find(|validator| validator.node_id == pending_validator.node_id)
-                .unwrap()
-                .delegators = Some(delegators);
-        }
-    }
-
-    Ok(pending_validators)
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;