From cb625f47d0f83a3f6d0ffde40667c3684b4892de Mon Sep 17 00:00:00 2001 From: marcellorigotti Date: Wed, 11 Oct 2023 17:02:00 +0200 Subject: [PATCH] check binaries > item_storage in the rpc_call To avoid requesting the storage item over and over on the engine side --- engine/src/main.rs | 51 +++++++++---------------- state-chain/custom-rpc/src/lib.rs | 12 ++++-- state-chain/runtime/src/lib.rs | 11 +++++- state-chain/runtime/src/runtime_apis.rs | 2 +- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/engine/src/main.rs b/engine/src/main.rs index 01e8e745117..7e5bfa9540e 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -78,33 +78,18 @@ async fn ensure_cfe_version_record_up_to_date( async fn ensure_node_version_record_up_to_date( current_node_version: SemVer, state_chain_client: &Arc, - state_chain_stream: &impl StateChainStreamApi, ) -> anyhow::Result<()> { - let recorded_version = state_chain_client - .storage_map_entry::>( - state_chain_stream.cache().block_hash, - &state_chain_client.account_id(), - ) - .await?; - - // Note that around Node upgrade period, the less recent version might still be running (and - // can even be *the* "active" instance), so it is important that it doesn't downgrade the - // version record: - if current_node_version.is_more_recent_than(recorded_version) { - info!( - "Updating Node version record from {:?} to {:?}", - recorded_version, current_node_version - ); - - state_chain_client - .finalize_signed_extrinsic(pallet_cf_validator::Call::node_version { - new_version: current_node_version, - }) - .await - .until_finalized() - .await - .context("Failed to submit version to state chain")?; - } + // This function is called only if the custom_rpc call returned a new version + info!("Updating Node version record to {:?}", current_node_version); + + state_chain_client + .finalize_signed_extrinsic(pallet_cf_validator::Call::node_version { + new_version: current_node_version, + }) + .await + .until_finalized() + .await + .context("Failed to submit version to state chain")?; Ok(()) } @@ -142,11 +127,11 @@ async fn main() -> anyhow::Result<()> { true, ) .await?; - + let account_id = state_chain_client.account_id(); ensure_cfe_version_record_up_to_date(&state_chain_client, &state_chain_stream).await?; // Use Option so we can take it out later without cloning (while inside a loop) - let mut stream_container = Some(state_chain_stream.clone()); + let mut stream_container = Some(state_chain_stream); let mut poll_interval = make_periodic_tick(std::time::Duration::from_secs(6), true); loop { @@ -160,12 +145,12 @@ async fn main() -> anyhow::Result<()> { let compatible = CFE_VERSION.is_compatible_with(runtime_compatibility_version); - let node_version: SemVer = ws_rpc_client - .request("cf_current_node_version", Vec::<()>::new()) + if let Some(node_version) = ws_rpc_client + .request("cf_current_node_version", vec![&account_id]) .await - .unwrap(); - ensure_node_version_record_up_to_date(node_version, &state_chain_client, &state_chain_stream).await?; - + .unwrap() { + ensure_node_version_record_up_to_date(node_version, &state_chain_client).await?; + } match cfe_status { CfeStatus::Active(_) => if !compatible { diff --git a/state-chain/custom-rpc/src/lib.rs b/state-chain/custom-rpc/src/lib.rs index 27a6f3c66fb..ec6077a9c05 100644 --- a/state-chain/custom-rpc/src/lib.rs +++ b/state-chain/custom-rpc/src/lib.rs @@ -327,7 +327,10 @@ pub trait CustomApi { #[method(name = "current_compatibility_version")] fn cf_current_compatibility_version(&self) -> RpcResult; #[method(name = "current_node_version")] - fn cf_current_node_version(&self) -> RpcResult; + fn cf_current_node_version( + &self, + account_id: state_chain_runtime::AccountId, + ) -> RpcResult>; #[method(name = "min_swap_amount")] fn cf_min_swap_amount(&self, asset: Asset) -> RpcResult; #[subscription(name = "subscribe_pool_price", item = Price)] @@ -778,10 +781,13 @@ where .map_err(to_rpc_error) } - fn cf_current_node_version(&self) -> RpcResult { + fn cf_current_node_version( + &self, + account_id: state_chain_runtime::AccountId, + ) -> RpcResult> { self.client .runtime_api() - .cf_current_node_version(self.unwrap_or_best(None)) + .cf_current_node_version(self.unwrap_or_best(None), account_id) .map_err(to_rpc_error) } diff --git a/state-chain/runtime/src/lib.rs b/state-chain/runtime/src/lib.rs index 61698ebad29..c2a1d25e961 100644 --- a/state-chain/runtime/src/lib.rs +++ b/state-chain/runtime/src/lib.rs @@ -889,11 +889,18 @@ impl_runtime_apis! { use cf_traits::CompatibleCfeVersions; Environment::current_compatibility_version() } - fn cf_current_node_version() -> SemVer { - SemVer { + fn cf_current_node_version(account_id: AccountId) -> Option { + let version = SemVer { major: env!("CARGO_PKG_VERSION_MAJOR").parse::().unwrap(), minor: env!("CARGO_PKG_VERSION_MINOR").parse::().unwrap(), patch: env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(), + }; + + let item_version = Validator::get_node_version(&account_id); + if version > item_version { + Some(version) + } else { + None } } fn cf_epoch_duration() -> u32 { diff --git a/state-chain/runtime/src/runtime_apis.rs b/state-chain/runtime/src/runtime_apis.rs index dc3ed7f8ba0..6ceede3ceff 100644 --- a/state-chain/runtime/src/runtime_apis.rs +++ b/state-chain/runtime/src/runtime_apis.rs @@ -95,7 +95,7 @@ decl_runtime_apis!( fn cf_min_funding() -> u128; fn cf_current_epoch() -> u32; fn cf_current_compatibility_version() -> SemVer; - fn cf_current_node_version() -> SemVer; + fn cf_current_node_version(account_id: AccountId32) -> Option; fn cf_epoch_duration() -> u32; fn cf_current_epoch_started_at() -> u32; fn cf_authority_emission_per_block() -> u128;