Skip to content

Commit

Permalink
updated command to return general informations
Browse files Browse the repository at this point in the history
  • Loading branch information
marcellorigotti committed Oct 13, 2023
1 parent e93ef29 commit 773b9a1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions api/bin/chainflip-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,11 @@ async fn get_bound_executor_address(api: QueryApi) -> Result<()> {

async fn pre_update_check(api: QueryApi) -> Result<()> {
let can_update = api.pre_update_check(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");

println!("Your node is an authority: {}", can_update.is_authority);
println!("A rotation is occurring: {}", can_update.rotation);
if let Some(blocks) = can_update.next_block_in {
println!("Your validator will produce a block in {} blocks", blocks);
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion api/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ pallet-cf-validator = { path = "../../state-chain/pallets/cf-validator" }
state-chain-runtime = { path = "../../state-chain/runtime" }
frame-system = { git = 'https://github.com/chainflip-io/substrate.git', tag = 'chainflip-monthly-2023-08+2' }
cf-amm = { path = "../../state-chain/amm" }
frame-support = { git = "https://github.com/chainflip-io/substrate.git", tag = "chainflip-monthly-2023-08+2", default-features = false }

# Substrate key types
sp-consensus-aura = { git = 'https://github.com/chainflip-io/substrate.git', tag = 'chainflip-monthly-2023-08+2' }
sp-core = { git = 'https://github.com/chainflip-io/substrate.git', tag = 'chainflip-monthly-2023-08+2' }
sp-consensus-grandpa = { git = 'https://github.com/chainflip-io/substrate.git', tag = 'chainflip-monthly-2023-08+2' }
codec = { package = "parity-scale-codec", version = "3.6.1" }
codec = { package = "parity-scale-codec", version = "3.6.1" }
49 changes: 43 additions & 6 deletions api/lib/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use cf_primitives::{chains::assets::any, AssetAmount};
use chainflip_engine::state_chain_observer::client::{
chain_api::ChainApi, storage_api::StorageApi,
};
use codec::Decode;
use frame_support::sp_runtime::DigestItem;
use pallet_cf_ingress_egress::DepositChannelDetails;
use pallet_cf_validator::RotationPhase;
use serde::Deserialize;
use sp_consensus_aura::{Slot, AURA_ENGINE_ID};
use state_chain_runtime::PalletInstanceAlias;
use std::{collections::BTreeMap, sync::Arc};
use std::{collections::BTreeMap, ops::Deref, sync::Arc};
use tracing::log;
use utilities::task_scope;

Expand All @@ -19,6 +22,12 @@ pub struct SwapChannelInfo<C: Chain> {
destination_asset: any::Asset,
}

pub struct PreUpdate {
pub rotation: bool,
pub is_authority: bool,
pub next_block_in: Option<usize>,
}

pub struct QueryApi {
pub(crate) state_chain_client: Arc<StateChainClient>,
}
Expand Down Expand Up @@ -149,19 +158,21 @@ impl QueryApi {
&self,
block_hash: Option<state_chain_runtime::Hash>,
account_id: Option<state_chain_runtime::AccountId>,
) -> Result<bool, anyhow::Error> {
) -> Result<PreUpdate, 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());

let mut result = PreUpdate { rotation: false, is_authority: false, next_block_in: None };

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

let current_validators = self
Expand All @@ -172,9 +183,35 @@ impl QueryApi {
.await?;

if current_validators.contains(&account_id) {
return Ok(false)
result.is_authority = true;
} else {
return Ok(result)
}

let header = self.state_chain_client.base_rpc_client.block_header(block_hash).await?;

let slot: usize =
*extract_slot_from_digest_item(&header.digest.logs[0]).unwrap().deref() as usize;

let validator_len = current_validators.len();
let current_relative_slot = slot % validator_len;
let index = current_validators.iter().position(|account| account == &account_id).unwrap();
if index >= current_relative_slot {
result.next_block_in = Some(index - current_relative_slot);
} else {
result.next_block_in = Some(validator_len - 1 - current_relative_slot + index);
}

Ok(true)
Ok(result)
}
}

fn extract_slot_from_digest_item(item: &DigestItem) -> Option<Slot> {
item.as_pre_runtime().and_then(|(id, mut data)| {
if id == AURA_ENGINE_ID {
Slot::decode(&mut data).ok()
} else {
None
}
})
}

0 comments on commit 773b9a1

Please sign in to comment.