Skip to content

Commit

Permalink
fix: handle breaking change in monitoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Oct 7, 2024
1 parent 2813495 commit 2b352ba
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions state-chain/custom-rpc/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
use cf_chains::{dot::PolkadotAccountId, sol::SolAddress};
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use sc_client_api::{BlockchainEvents, HeaderBackend};
use serde::{Deserialize, Serialize};
use sp_core::{bounded_vec::BoundedVec, ConstU32};
use state_chain_runtime::{
chainflip::Offence,
Expand All @@ -14,6 +15,25 @@ use state_chain_runtime::{
},
};

// Temporary struct to hold the deprecated blocks_per_epoch field.
// Can be deleted after v1.7 is released (meaning: after the version is bumped to 1.8).
#[derive(Serialize, Deserialize)]
pub struct RpcEpochState {
#[deprecated(
since = "1.8.0",
note = "This field is deprecated and will be removed in v1.8. Use blocks_per_epoch instead."
)]
blocks_per_epoch: u32,
#[serde(flatten)]
epoch_state: EpochState,
}

impl From<EpochState> for RpcEpochState {
fn from(epoch_state: EpochState) -> Self {
Self { blocks_per_epoch: epoch_state.epoch_duration, epoch_state }
}
}

#[rpc(server, client, namespace = "cf_monitoring")]
pub trait MonitoringApi {
#[method(name = "authorities")]
Expand All @@ -33,7 +53,7 @@ pub trait MonitoringApi {
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<Vec<(Offence, u32)>>;
#[method(name = "epoch_state")]
fn cf_epoch_state(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<EpochState>;
fn cf_epoch_state(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<RpcEpochState>;
#[method(name = "redemptions")]
fn cf_redemptions(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<RedemptionsInfo>;
#[method(name = "pending_broadcasts")]
Expand Down Expand Up @@ -86,13 +106,14 @@ pub trait MonitoringApi {
}

macro_rules! pass_through {
($( $name:ident -> $result_type:ty ),+) => {
($( $name:ident -> $result_type:ty $([with: $map_fn:expr])? ),+) => {

$(
fn $name(&self, at: Option<state_chain_runtime::Hash>) -> RpcResult<$result_type> {
self.client
.runtime_api()
.$name(self.unwrap_or_best(at))
$( .map($map_fn) )?
.map_err(to_rpc_error)
}
)+
Expand All @@ -116,7 +137,7 @@ where
cf_btc_utxos -> BtcUtxos,
cf_dot_aggkey -> PolkadotAccountId,
cf_suspended_validators -> Vec<(Offence, u32)>,
cf_epoch_state -> EpochState,
cf_epoch_state -> RpcEpochState [with: RpcEpochState::from],
cf_redemptions -> RedemptionsInfo,
cf_pending_broadcasts_count -> PendingBroadcasts,
cf_pending_tss_ceremonies_count -> PendingTssCeremonies,
Expand Down

0 comments on commit 2b352ba

Please sign in to comment.