Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: runtime API versioning for monitoring API #5393

Merged
merged 3 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
25 changes: 15 additions & 10 deletions state-chain/custom-rpc/src/monitoring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ 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::{
chainflip::Offence,
monitoring_apis::{
ActivateKeysBroadcastIds, AuthoritiesInfo, BtcUtxos, EpochState, ExternalChainsBlockHeight,
LastRuntimeUpgradeInfo, MonitoringRuntimeApi, OpenDepositChannels, PendingBroadcasts,
PendingTssCeremonies, RedemptionsInfo, SolanaNonces,
},
};
use state_chain_runtime::{self, Block, chainflip::Offence, monitoring_apis::{
ActivateKeysBroadcastIds, AuthoritiesInfo, BtcUtxos, EpochState, ExternalChainsBlockHeight,
LastRuntimeUpgradeInfo, MonitoringRuntimeApi, OpenDepositChannels, PendingBroadcasts,
PendingTssCeremonies, RedemptionsInfo, SolanaNonces,
}};

#[rpc(server, client, namespace = "cf_monitoring")]
pub trait MonitoringApi {
Expand Down Expand Up @@ -126,8 +124,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 @@ -20,7 +20,7 @@ use crate::{
},
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 @@ -2272,8 +2272,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
Loading