Skip to content

Commit

Permalink
feat: runtime API versioning for monitoring API (#5393)
Browse files Browse the repository at this point in the history
* first implementation of runtime versioning

* use api_version()

* chore: fmt

---------

Co-authored-by: Daniel <daniel@chainflip.io>
  • Loading branch information
2 people authored and kylezs committed Nov 7, 2024
1 parent 83076c4 commit f58608c
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
6 changes: 3 additions & 3 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -136,8 +136,8 @@ pub struct RpcMonitoringData {
pub sol_nonces: SolanaNonces,
pub activating_key_broadcast_ids: ActivateKeysBroadcastIds,
}
impl From<MonitoringData> for RpcMonitoringData {
fn from(monitoring_data: MonitoringData) -> Self {
impl From<MonitoringDataV2> for RpcMonitoringData {
fn from(monitoring_data: MonitoringDataV2) -> Self {
Self {
epoch: monitoring_data.epoch.into(),
pending_redemptions: monitoring_data.pending_redemptions.into(),
Expand Down
14 changes: 12 additions & 2 deletions state-chain/custom-rpc/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -126,8 +129,15 @@ where
&self,
at: Option<state_chain_runtime::Hash>,
) -> RpcResult<RpcMonitoringData> {
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::<dyn Core<Block>>(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,
Expand Down
6 changes: 3 additions & 3 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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(),
Expand Down
55 changes: 52 additions & 3 deletions state-chain/runtime/src/monitoring_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DurableNonceAndAccount>,
pub unavailable: Vec<SolAddress>,
}

#[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<u32>,
pub bitcoin: Option<u32>,
Expand All @@ -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,
Expand All @@ -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<AssetAmount>,
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<MonitoringData> 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;
Expand All @@ -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<AccountId32, sp_core::ConstU32<10>>,
) -> Vec<ValidatorInfo>;
Expand Down

0 comments on commit f58608c

Please sign in to comment.