Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
remove explicit version runtime API call
Browse files Browse the repository at this point in the history
this is not needed, as the RuntimeAPISubsystem already takes care
of versioning and will return NotSupported if the version is not
right.
  • Loading branch information
alindima committed Aug 23, 2023
1 parent fe51ef8 commit c846d6d
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 70 deletions.
11 changes: 6 additions & 5 deletions node/core/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ use statement_table::{
},
Config as TableConfig, Context as TableContextTrait, Table,
};
use util::{request_min_backing_votes, runtime::RuntimeInfo};
use util::runtime::{request_min_backing_votes, RuntimeInfo};

mod error;

Expand Down Expand Up @@ -999,10 +999,11 @@ async fn construct_per_relay_parent_state<Context>(
let session_index =
try_runtime_api!(runtime_info.get_session_index_for_child(ctx.sender(), parent).await);

let minimum_backing_votes = request_min_backing_votes(parent, ctx.sender(), |sender| {
runtime_info.get_min_backing_votes(sender, session_index, parent)
})
.await?;
let minimum_backing_votes =
request_min_backing_votes(parent, ctx.sender(), |parent, sender| {
runtime_info.get_min_backing_votes(sender, session_index, parent)
})
.await?;

let (validators, groups, cores) = futures::try_join!(
request_validators(parent, ctx.sender()).await,
Expand Down
16 changes: 8 additions & 8 deletions node/core/backing/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,14 @@ async fn test_startup(virtual_overseer: &mut VirtualOverseer, test_state: &TestS
);

// Check that subsystem job issues a request for the runtime API version.
assert_matches!(
virtual_overseer.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(parent, RuntimeApiRequest::Version(tx))
) if parent == test_state.relay_parent => {
tx.send(Ok(RuntimeApiRequest::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT)).unwrap();
}
);
// assert_matches!(
// virtual_overseer.recv().await,
// AllMessages::RuntimeApi(
// RuntimeApiMessage::Request(parent, RuntimeApiRequest::Version(tx))
// ) if parent == test_state.relay_parent => {
// tx.send(Ok(RuntimeApiRequest::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT)).unwrap();
// }
// );

// Check if subsystem job issues a request for the minimum backing votes.
// This may or may not happen, depending if the minimum backing votes is already cached in the
Expand Down
10 changes: 0 additions & 10 deletions node/core/backing/src/tests/prospective_parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,16 +153,6 @@ async fn activate_leaf(
}
);

// Check that subsystem job issues a request for the runtime API version.
assert_matches!(
virtual_overseer.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(parent, RuntimeApiRequest::Version(tx))
) if parent == hash => {
tx.send(Ok(RuntimeApiRequest::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT)).unwrap();
}
);

// Check if subsystem job issues a request for the minimum backing votes.
// This may or may not happen, depending if the minimum backing votes is already cached in
// the `RuntimeInfo`.
Expand Down
17 changes: 10 additions & 7 deletions node/network/statement-distribution/src/vstaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ use polkadot_node_subsystem_types::messages::RuntimeApiRequest;
use polkadot_node_subsystem_util::{
backing_implicit_view::View as ImplicitView,
reputation::ReputationAggregator,
request_from_runtime, request_min_backing_votes,
runtime::{recv_runtime, ProspectiveParachainsMode},
request_from_runtime,
runtime::{recv_runtime, request_min_backing_votes, ProspectiveParachainsMode},
};
use polkadot_primitives::vstaging::{
AuthorityDiscoveryId, CandidateHash, CompactStatement, CoreIndex, CoreState, GroupIndex,
Expand Down Expand Up @@ -507,17 +507,20 @@ pub(crate) async fn handle_active_leaves_update<Context>(
Some(s) => s,
};

let minimum_backing_votes =
request_min_backing_votes(new_relay_parent, ctx.sender(), |sender| async {
let minimum_backing_votes = request_min_backing_votes(
new_relay_parent,
ctx.sender(),
|parent, sender| async move {
recv_runtime(
request_from_runtime(new_relay_parent, sender, |tx| {
request_from_runtime(parent, sender, |tx| {
RuntimeApiRequest::MinimumBackingVotes(tx)
})
.await,
)
.await
})
.await?;
},
)
.await?;

state.per_session.insert(
session_index,
Expand Down
8 changes: 0 additions & 8 deletions node/network/statement-distribution/src/vstaging/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,6 @@ async fn handle_leaf_activation(
}
);

assert_matches!(
virtual_overseer.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(parent, RuntimeApiRequest::Version(tx))) if parent == *hash => {
tx.send(Ok(RuntimeApiRequest::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT)).unwrap();
}
);

assert_matches!(
virtual_overseer.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
Expand Down
32 changes: 1 addition & 31 deletions node/subsystem-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ pub use overseer::{

pub use polkadot_node_metrics::{metrics, Metronome};

use futures::{
channel::{mpsc, oneshot},
Future,
};
use futures::channel::{mpsc, oneshot};
use parity_scale_codec::Encode;

use polkadot_primitives::{
Expand Down Expand Up @@ -99,8 +96,6 @@ mod tests;
pub const JOB_GRACEFUL_STOP_DURATION: Duration = Duration::from_secs(1);
/// Capacity of channels to and from individual jobs
pub const JOB_CHANNEL_CAPACITY: usize = 64;
/// Used prior to runtime API version 6.
const LEGACY_MIN_BACKING_VOTES: u32 = 2;

/// Utility errors
#[derive(Debug, Error)]
Expand Down Expand Up @@ -235,31 +230,6 @@ specialize_requests! {
fn request_staging_async_backing_params() -> vstaging_primitives::AsyncBackingParams; StagingAsyncBackingParams;
}

/// Request the min backing votes value.
/// Prior to runtime API version 6, just return a hardcoded constant.
pub async fn request_min_backing_votes<'a, S, Func, Fut>(
parent: Hash,
sender: &'a mut S,
get_min_backing_votes: Func,
) -> Result<u32, runtime::Error>
where
S: overseer::SubsystemSender<RuntimeApiMessage>,
Func: FnOnce(&'a mut S) -> Fut,
Fut: Future<Output = Result<u32, runtime::Error>>,
{
let runtime_api_version = request_runtime_api_version(parent, sender)
.await
.await
.map_err(runtime::Error::RuntimeRequestCanceled)?
.map_err(runtime::Error::RuntimeRequest)?;

if runtime_api_version >= RuntimeApiRequest::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT {
get_min_backing_votes(sender).await
} else {
Ok(LEGACY_MIN_BACKING_VOTES)
}
}

/// Requests executor parameters from the runtime effective at given relay-parent. First obtains
/// session index at the relay-parent, relying on the fact that it should be cached by the runtime
/// API caching layer even if the block itself has already been pruned. Then requests executor
Expand Down
33 changes: 32 additions & 1 deletion node/subsystem-util/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

//! Convenient interface to runtime information.

use std::num::NonZeroUsize;
use std::{future::Future, num::NonZeroUsize};

use lru::LruCache;

Expand Down Expand Up @@ -52,6 +52,9 @@ pub use error::{recv_runtime, Error, FatalError, JfyiError};

const LOG_TARGET: &'static str = "parachain::runtime-info";

/// Used prior to runtime API version 6.
const LEGACY_MIN_BACKING_VOTES: u32 = 2;

/// Configuration for construction a `RuntimeInfo`.
pub struct Config {
/// Needed for retrieval of `ValidatorInfo`
Expand Down Expand Up @@ -490,3 +493,31 @@ where
})
}
}

/// Request the min backing votes value.
/// Prior to runtime API version 6, just return a hardcoded constant.
pub async fn request_min_backing_votes<'a, S, Func, Fut>(
parent: Hash,
sender: &'a mut S,
get_min_backing_votes: Func,
) -> Result<u32>
where
S: overseer::SubsystemSender<RuntimeApiMessage>,
Func: FnOnce(Hash, &'a mut S) -> Fut,
Fut: Future<Output = Result<u32>>,
{
let min_backing_votes_res = get_min_backing_votes(parent, sender).await;

if let Err(Error::RuntimeRequest(RuntimeApiError::NotSupported { .. })) = min_backing_votes_res
{
gum::trace!(
target: LOG_TARGET,
?parent,
"Querying the backing threshold from the runtime is not supported by the current Runtime API",
);

Ok(LEGACY_MIN_BACKING_VOTES)
} else {
min_backing_votes_res
}
}

0 comments on commit c846d6d

Please sign in to comment.