Skip to content

Commit

Permalink
fix: delete pending validators
Browse files Browse the repository at this point in the history
  • Loading branch information
Al3xGROS committed Apr 12, 2024
1 parent 5379363 commit 626fcd8
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 113 deletions.
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions crates/ash_cli/src/avalanche.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion crates/ash_cli/src/avalanche/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?)
Expand Down
37 changes: 10 additions & 27 deletions crates/ash_cli/src/avalanche/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
"{}",
Expand Down
28 changes: 0 additions & 28 deletions crates/ash_sdk/src/avalanche.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
44 changes: 1 addition & 43 deletions crates/ash_sdk/src/avalanche/jsonrpc/platformvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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::*;
Expand Down

0 comments on commit 626fcd8

Please sign in to comment.