From f58608c3d125b0ce17354ee98fabbd49654e8617 Mon Sep 17 00:00:00 2001 From: Marcello Date: Thu, 7 Nov 2024 14:21:04 +0100 Subject: [PATCH] feat: runtime API versioning for monitoring API (#5393) * first implementation of runtime versioning * use api_version() * chore: fmt --------- Co-authored-by: Daniel --- state-chain/custom-rpc/src/lib.rs | 6 +-- state-chain/custom-rpc/src/monitoring.rs | 14 +++++- state-chain/runtime/src/lib.rs | 6 +-- state-chain/runtime/src/monitoring_apis.rs | 55 ++++++++++++++++++++-- 4 files changed, 70 insertions(+), 11 deletions(-) diff --git a/state-chain/custom-rpc/src/lib.rs b/state-chain/custom-rpc/src/lib.rs index a5a9a05d22..5297f58d2f 100644 --- a/state-chain/custom-rpc/src/lib.rs +++ b/state-chain/custom-rpc/src/lib.rs @@ -48,7 +48,7 @@ use state_chain_runtime::{ constants::common::TX_FEE_MULTIPLIER, monitoring_apis::{ ActivateKeysBroadcastIds, AuthoritiesInfo, BtcUtxos, EpochState, ExternalChainsBlockHeight, - FeeImbalance, FlipSupply, LastRuntimeUpgradeInfo, MonitoringData, OpenDepositChannels, + FeeImbalance, FlipSupply, LastRuntimeUpgradeInfo, MonitoringDataV2, OpenDepositChannels, PendingBroadcasts, PendingTssCeremonies, RedemptionsInfo, SolanaNonces, }, runtime_apis::{ @@ -136,8 +136,8 @@ pub struct RpcMonitoringData { pub sol_nonces: SolanaNonces, pub activating_key_broadcast_ids: ActivateKeysBroadcastIds, } -impl From for RpcMonitoringData { - fn from(monitoring_data: MonitoringData) -> Self { +impl From for RpcMonitoringData { + fn from(monitoring_data: MonitoringDataV2) -> Self { Self { epoch: monitoring_data.epoch.into(), pending_redemptions: monitoring_data.pending_redemptions.into(), diff --git a/state-chain/custom-rpc/src/monitoring.rs b/state-chain/custom-rpc/src/monitoring.rs index 8973af24f8..c09fbc4bd0 100644 --- a/state-chain/custom-rpc/src/monitoring.rs +++ b/state-chain/custom-rpc/src/monitoring.rs @@ -3,14 +3,17 @@ use crate::{BlockT, CustomRpc, RpcAccountInfoV2, RpcFeeImbalance, RpcMonitoringD use cf_chains::{dot::PolkadotAccountId, sol::SolAddress}; use jsonrpsee::proc_macros::rpc; use sc_client_api::{BlockchainEvents, HeaderBackend}; +use sp_api::{ApiExt, Core}; use sp_core::{bounded_vec::BoundedVec, ConstU32}; use state_chain_runtime::{ + self, chainflip::Offence, monitoring_apis::{ ActivateKeysBroadcastIds, AuthoritiesInfo, BtcUtxos, EpochState, ExternalChainsBlockHeight, LastRuntimeUpgradeInfo, MonitoringRuntimeApi, OpenDepositChannels, PendingBroadcasts, PendingTssCeremonies, RedemptionsInfo, SolanaNonces, }, + Block, }; #[rpc(server, client, namespace = "cf_monitoring")] @@ -126,8 +129,15 @@ where &self, at: Option, ) -> RpcResult { - self.with_runtime_api(at, |api, hash| api.cf_monitoring_data(hash)) - .map(Into::into) + self.with_runtime_api(at, |api, hash| { + if api.api_version::>(hash).unwrap().unwrap() < 2 { + let old_result = api.cf_monitoring_data_before_version_2(hash)?; + Ok(old_result.into()) + } else { + api.cf_monitoring_data(hash) + } + }) + .map(Into::into) } fn cf_accounts_info( &self, diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index da0b4823e3..bd09e91f8d 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -21,7 +21,7 @@ use crate::{ migrations::serialize_solana_broadcast::{NoopUpgrade, SerializeSolanaBroadcastMigration}, monitoring_apis::{ ActivateKeysBroadcastIds, AuthoritiesInfo, BtcUtxos, EpochState, ExternalChainsBlockHeight, - FeeImbalance, FlipSupply, LastRuntimeUpgradeInfo, MonitoringData, OpenDepositChannels, + FeeImbalance, FlipSupply, LastRuntimeUpgradeInfo, MonitoringDataV2, OpenDepositChannels, PendingBroadcasts, PendingTssCeremonies, RedemptionsInfo, SolanaNonces, }, runtime_apis::{ @@ -2296,8 +2296,8 @@ impl_runtime_apis! { fn cf_sol_onchain_key() -> SolAddress{ SolanaBroadcaster::current_on_chain_key().unwrap_or_default() } - fn cf_monitoring_data() -> MonitoringData { - MonitoringData{ + fn cf_monitoring_data() -> MonitoringDataV2 { + MonitoringDataV2{ external_chains_height: Self::cf_external_chains_block_height(), btc_utxos: Self::cf_btc_utxos(), epoch: Self::cf_epoch_state(), diff --git a/state-chain/runtime/src/monitoring_apis.rs b/state-chain/runtime/src/monitoring_apis.rs index 9ee775c74a..018df5ce46 100644 --- a/state-chain/runtime/src/monitoring_apis.rs +++ b/state-chain/runtime/src/monitoring_apis.rs @@ -106,13 +106,17 @@ pub struct FlipSupply { pub offchain_supply: u128, } -#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)] +#[derive( + Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone, Default, +)] pub struct SolanaNonces { pub available: Vec, pub unavailable: Vec, } -#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)] +#[derive( + Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone, Default, +)] pub struct ActivateKeysBroadcastIds { pub ethereum: Option, pub bitcoin: Option, @@ -122,7 +126,7 @@ pub struct ActivateKeysBroadcastIds { } #[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)] -pub struct MonitoringData { +pub struct MonitoringDataV2 { pub external_chains_height: ExternalChainsBlockHeight, pub btc_utxos: BtcUtxos, pub epoch: EpochState, @@ -143,7 +147,50 @@ pub struct MonitoringData { pub activating_key_broadcast_ids: ActivateKeysBroadcastIds, } +#[derive(Serialize, Deserialize, Encode, Decode, Eq, PartialEq, TypeInfo, Debug, Clone)] +pub struct MonitoringData { + pub external_chains_height: ExternalChainsBlockHeight, + pub btc_utxos: BtcUtxos, + pub epoch: EpochState, + pub pending_redemptions: RedemptionsInfo, + pub pending_broadcasts: PendingBroadcasts, + pub pending_tss: PendingTssCeremonies, + pub open_deposit_channels: OpenDepositChannels, + pub fee_imbalance: FeeImbalance, + pub authorities: AuthoritiesInfo, + pub build_version: LastRuntimeUpgradeInfo, + pub suspended_validators: Vec<(Offence, u32)>, + pub pending_swaps: u32, + pub dot_aggkey: PolkadotAccountId, + pub flip_supply: FlipSupply, +} + +impl From for MonitoringDataV2 { + fn from(monitoring_data: MonitoringData) -> Self { + Self { + epoch: monitoring_data.epoch, + pending_redemptions: monitoring_data.pending_redemptions, + fee_imbalance: monitoring_data.fee_imbalance.map(|i| *i), + external_chains_height: monitoring_data.external_chains_height, + btc_utxos: monitoring_data.btc_utxos, + pending_broadcasts: monitoring_data.pending_broadcasts, + pending_tss: monitoring_data.pending_tss, + open_deposit_channels: monitoring_data.open_deposit_channels, + authorities: monitoring_data.authorities, + build_version: monitoring_data.build_version, + suspended_validators: monitoring_data.suspended_validators, + pending_swaps: monitoring_data.pending_swaps, + dot_aggkey: monitoring_data.dot_aggkey, + flip_supply: monitoring_data.flip_supply, + sol_aggkey: Default::default(), + sol_onchain_key: Default::default(), + sol_nonces: Default::default(), + activating_key_broadcast_ids: Default::default(), + } + } +} decl_runtime_apis!( + #[api_version(2)] pub trait MonitoringRuntimeApi { fn cf_authorities() -> AuthoritiesInfo; fn cf_external_chains_block_height() -> ExternalChainsBlockHeight; @@ -162,7 +209,9 @@ decl_runtime_apis!( fn cf_sol_nonces() -> SolanaNonces; fn cf_sol_aggkey() -> SolAddress; fn cf_sol_onchain_key() -> SolAddress; + #[changed_in(2)] fn cf_monitoring_data() -> MonitoringData; + fn cf_monitoring_data() -> MonitoringDataV2; fn cf_accounts_info( accounts: BoundedVec>, ) -> Vec;