Skip to content

Commit

Permalink
check binaries > item_storage in the rpc_call
Browse files Browse the repository at this point in the history
To avoid requesting the storage item over and over on the engine side
  • Loading branch information
marcellorigotti committed Oct 11, 2023
1 parent 57dfe43 commit cb625f4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
51 changes: 18 additions & 33 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StateChainClient>,
state_chain_stream: &impl StateChainStreamApi,
) -> anyhow::Result<()> {
let recorded_version = state_chain_client
.storage_map_entry::<pallet_cf_validator::NodeVersion<state_chain_runtime::Runtime>>(
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(())
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions state-chain/custom-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ pub trait CustomApi {
#[method(name = "current_compatibility_version")]
fn cf_current_compatibility_version(&self) -> RpcResult<SemVer>;
#[method(name = "current_node_version")]
fn cf_current_node_version(&self) -> RpcResult<SemVer>;
fn cf_current_node_version(
&self,
account_id: state_chain_runtime::AccountId,
) -> RpcResult<Option<SemVer>>;
#[method(name = "min_swap_amount")]
fn cf_min_swap_amount(&self, asset: Asset) -> RpcResult<AssetAmount>;
#[subscription(name = "subscribe_pool_price", item = Price)]
Expand Down Expand Up @@ -778,10 +781,13 @@ where
.map_err(to_rpc_error)
}

fn cf_current_node_version(&self) -> RpcResult<SemVer> {
fn cf_current_node_version(
&self,
account_id: state_chain_runtime::AccountId,
) -> RpcResult<Option<SemVer>> {
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)
}

Expand Down
11 changes: 9 additions & 2 deletions state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SemVer> {
let version = SemVer {
major: env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap(),
minor: env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap(),
patch: env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap(),
};

let item_version = Validator::get_node_version(&account_id);
if version > item_version {
Some(version)
} else {
None
}
}
fn cf_epoch_duration() -> u32 {
Expand Down
2 changes: 1 addition & 1 deletion state-chain/runtime/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SemVer>;
fn cf_epoch_duration() -> u32;
fn cf_current_epoch_started_at() -> u32;
fn cf_authority_emission_per_block() -> u128;
Expand Down

0 comments on commit cb625f4

Please sign in to comment.