Skip to content

Commit

Permalink
cli command to verify if it is safe to update
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellorigotti committed Oct 10, 2023
1 parent 6c8f435 commit 185a72e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/bin/chainflip-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ async fn run_cli() -> Result<()> {
VanityName { name } => {
api.operator_api().set_vanity_name(name).await?;
},
CheckUpdate {} => {
get_update_state(api.query_api()).await?
}
ForceRotation {} => {
api.governance_api().force_rotation().await?;
},
Expand Down Expand Up @@ -291,6 +294,17 @@ async fn get_bound_executor_address(api: QueryApi) -> Result<()> {
Ok(())
}

async fn get_update_state(api: QueryApi) -> Result<()> {
let can_update = api.get_update_state(None, None).await?;
if can_update {
println!("You can safely update your node/engine");
} else {
println!("A rotation is in progress and you are an Authority, please wait till the end of the rotation to update your node/engine");
}

Ok(())
}

fn confirm_submit() -> bool {
use std::{io, io::*};

Expand Down
2 changes: 2 additions & 0 deletions api/bin/chainflip-cli/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ pub enum CliCommand {
#[clap(help = "Name in UTF-8 (max length 64)")]
name: String,
},
#[clap(about = "Check if it is safe to update your node/engine")]
CheckUpdate {},
#[clap(
// This is only useful for testing. No need to show to the end user.
hide = true,
Expand Down
20 changes: 20 additions & 0 deletions api/lib/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use chainflip_engine::state_chain_observer::client::{
chain_api::ChainApi, storage_api::StorageApi,
};
use pallet_cf_ingress_egress::DepositChannelDetails;
use pallet_cf_validator::RotationPhase;
use serde::Deserialize;
use state_chain_runtime::PalletInstanceAlias;
use std::{collections::BTreeMap, sync::Arc};
Expand Down Expand Up @@ -143,4 +144,23 @@ impl QueryApi {
)
.await?)
}

pub async fn get_update_state(&self, block_hash: Option<state_chain_runtime::Hash>, account_id: Option<state_chain_runtime::AccountId>) -> Result<bool, anyhow::Error> {
let block_hash =
block_hash.unwrap_or_else(|| self.state_chain_client.latest_finalized_hash());
let account_id = account_id.unwrap_or_else(|| self.state_chain_client.account_id());

if self.state_chain_client.storage_value::<pallet_cf_validator::CurrentRotationPhase<state_chain_runtime::Runtime>>(block_hash).await? == RotationPhase::Idle{
return Ok(true);
}

let current_validators = self.state_chain_client.storage_value::<pallet_cf_validator::CurrentAuthorities<state_chain_runtime::Runtime>>(block_hash).await?;
for validator in current_validators.iter() {
if account_id == *validator {
return Ok(false);
}
}

Ok(true)
}
}

0 comments on commit 185a72e

Please sign in to comment.