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

Commit

Permalink
introduce api versioning for min_backing votes
Browse files Browse the repository at this point in the history
also enable it for rococo/versi for testing
  • Loading branch information
alindima committed Aug 22, 2023
1 parent 8c9a8d8 commit 09c9f4e
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 39 deletions.
20 changes: 16 additions & 4 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::runtime::RuntimeInfo;
use util::{request_runtime_api_version, runtime::RuntimeInfo};

mod error;

Expand All @@ -127,6 +127,9 @@ mod tests;

const LOG_TARGET: &str = "parachain::candidate-backing";

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

/// PoV data to validate.
enum PoVData {
/// Already available (from candidate selection).
Expand Down Expand Up @@ -998,9 +1001,19 @@ 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 runtime_api_version = try_runtime_api!(request_runtime_api_version(parent, ctx.sender())
.await
.await
.map_err(Error::RuntimeApiUnavailable)?);

let minimum_backing_votes =
runtime_info.get_min_backing_votes(ctx.sender(), session_index, parent).await;
// TODO: if this does not exist, fall back to the hardcoded 2 value.
if runtime_api_version >= RuntimeApiRequest::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT {
try_runtime_api!(
runtime_info.get_min_backing_votes(ctx.sender(), session_index, parent).await
)
} else {
LEGACY_MIN_BACKING_VOTES
};

let (validators, groups, cores) = futures::try_join!(
request_validators(parent, ctx.sender()).await,
Expand All @@ -1015,7 +1028,6 @@ async fn construct_per_relay_parent_state<Context>(
let validators: Vec<_> = try_runtime_api!(validators);
let (validator_groups, group_rotation_info) = try_runtime_api!(groups);
let cores = try_runtime_api!(cores);
let minimum_backing_votes = try_runtime_api!(minimum_backing_votes);

let signing_context = SigningContext { parent_hash: parent, session_index };
let validator =
Expand Down
10 changes: 10 additions & 0 deletions node/core/backing/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ 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();
}
);

// 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
10 changes: 10 additions & 0 deletions node/core/backing/src/tests/prospective_parachains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ 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
9 changes: 6 additions & 3 deletions node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,6 @@ where

Request::Authorities(sender) => query!(Authorities, authorities(), ver = 1, sender),
Request::Validators(sender) => query!(Validators, validators(), ver = 1, sender),
Request::MinimumBackingVotes(sender) =>
query!(MinimumBackingVotes, minimum_backing_votes(), ver = 1, sender),

Request::ValidatorGroups(sender) => {
query!(ValidatorGroups, validator_groups(), ver = 1, sender)
},
Expand Down Expand Up @@ -559,6 +556,12 @@ where
ver = Request::SUBMIT_REPORT_DISPUTE_LOST_RUNTIME_REQUIREMENT,
sender
),
Request::MinimumBackingVotes(sender) => query!(
MinimumBackingVotes,
minimum_backing_votes(),
ver = Request::MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT,
sender
),

Request::StagingParaBackingState(para, sender) => {
query!(
Expand Down
2 changes: 2 additions & 0 deletions node/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ pub fn maybe_compress_pov(pov: PoV) -> PoV {
/// How many votes we need to consider a candidate backed.
///
/// WARNING: This has to be kept in sync with the runtime check in the inclusion module.
/// The min threshold has been added to the HostConfiguration and after the runtime API for querying
/// it, `minimum_backing_votes`, is moved from vstaging to production, we can remove this.
pub fn minimum_votes(n_validators: usize) -> usize {
std::cmp::min(2, n_validators)
}
4 changes: 0 additions & 4 deletions node/service/src/fake_runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ sp_api::impl_runtime_apis! {
unimplemented!()
}

fn minimum_backing_votes() -> u32 {
unimplemented!()
}

fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
unimplemented!()
}
Expand Down
3 changes: 3 additions & 0 deletions node/subsystem-types/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,9 @@ impl RuntimeApiRequest {
/// `SubmitReportDisputeLost`
pub const SUBMIT_REPORT_DISPUTE_LOST_RUNTIME_REQUIREMENT: u32 = 5;

/// `MinimumBackingVotes`
pub const MINIMUM_BACKING_VOTES_RUNTIME_REQUIREMENT: u32 = 6;

/// Minimum version for backing state, required for async backing.
///
/// 99 for now, should be adjusted to VSTAGING/actual runtime version once released.
Expand Down
5 changes: 4 additions & 1 deletion primitives/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,11 @@ sp_api::decl_runtime_apis! {
key_ownership_proof: vstaging::slashing::OpaqueKeyOwnershipProof,
) -> Option<()>;

/***** Staging *****/

/// Get the minimum number of backing votes for a parachain candidate.
/// TODO: bump api version here?
/// This is a staging method! Do not use on production runtimes!
#[api_version(6)]
fn minimum_backing_votes() -> u32;

/***** Asynchronous backing *****/
Expand Down
4 changes: 0 additions & 4 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1894,10 +1894,6 @@ sp_api::impl_runtime_apis! {
parachains_runtime_api_impl::validators::<Runtime>()
}

fn minimum_backing_votes() -> u32 {
parachains_runtime_api_impl::minimum_backing_votes::<Runtime>()
}

fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
parachains_runtime_api_impl::validator_groups::<Runtime>()
}
Expand Down
6 changes: 0 additions & 6 deletions runtime/parachains/src/runtime_api_impl/v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ pub fn validators<T: initializer::Config>() -> Vec<ValidatorId> {
<shared::Pallet<T>>::active_validator_keys()
}

/// Implementation of the `minimum_backing_votes` function of the runtime API.
pub fn minimum_backing_votes<T: initializer::Config>() -> u32 {
let config = <configuration::Pallet<T>>::config();
config.minimum_backing_votes
}

/// Implementation for the `validator_groups` function of the runtime API.
pub fn validator_groups<T: initializer::Config>(
) -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumberFor<T>>) {
Expand Down
6 changes: 6 additions & 0 deletions runtime/parachains/src/runtime_api_impl/vstaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,9 @@ pub fn backing_state<T: initializer::Config>(
pub fn async_backing_params<T: configuration::Config>() -> AsyncBackingParams {
<configuration::Pallet<T>>::config().async_backing_params
}

/// Implementation of the `minimum_backing_votes` function of the runtime API.
pub fn minimum_backing_votes<T: initializer::Config>() -> u32 {
let config = <configuration::Pallet<T>>::config();
config.minimum_backing_votes
}
4 changes: 0 additions & 4 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1697,10 +1697,6 @@ sp_api::impl_runtime_apis! {
parachains_runtime_api_impl::validators::<Runtime>()
}

fn minimum_backing_votes() -> u32 {
parachains_runtime_api_impl::minimum_backing_votes::<Runtime>()
}

fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
parachains_runtime_api_impl::validator_groups::<Runtime>()
}
Expand Down
13 changes: 8 additions & 5 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ use runtime_parachains::{
inclusion::{AggregateMessageOrigin, UmpQueueId},
initializer as parachains_initializer, origin as parachains_origin, paras as parachains_paras,
paras_inherent as parachains_paras_inherent,
runtime_api_impl::v5 as parachains_runtime_api_impl,
runtime_api_impl::{
v5 as parachains_runtime_api_impl, vstaging as parachains_staging_runtime_api_impl,
},
scheduler as parachains_scheduler, session_info as parachains_session_info,
shared as parachains_shared,
};
Expand Down Expand Up @@ -1715,15 +1717,12 @@ sp_api::impl_runtime_apis! {
}
}

#[api_version(6)]
impl primitives::runtime_api::ParachainHost<Block, Hash, BlockNumber> for Runtime {
fn validators() -> Vec<ValidatorId> {
parachains_runtime_api_impl::validators::<Runtime>()
}

fn minimum_backing_votes() -> u32 {
parachains_runtime_api_impl::minimum_backing_votes::<Runtime>()
}

fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
parachains_runtime_api_impl::validator_groups::<Runtime>()
}
Expand Down Expand Up @@ -1849,6 +1848,10 @@ sp_api::impl_runtime_apis! {
key_ownership_proof,
)
}

fn minimum_backing_votes() -> u32 {
parachains_staging_runtime_api_impl::minimum_backing_votes::<Runtime>()
}
}

#[api_version(3)]
Expand Down
4 changes: 0 additions & 4 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,10 +826,6 @@ sp_api::impl_runtime_apis! {
runtime_impl::validators::<Runtime>()
}

fn minimum_backing_votes() -> u32 {
runtime_impl::minimum_backing_votes::<Runtime>()
}

fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
runtime_impl::validator_groups::<Runtime>()
}
Expand Down
4 changes: 0 additions & 4 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1567,10 +1567,6 @@ sp_api::impl_runtime_apis! {
parachains_runtime_api_impl::validators::<Runtime>()
}

fn minimum_backing_votes() -> u32 {
parachains_runtime_api_impl::minimum_backing_votes::<Runtime>()
}

fn validator_groups() -> (Vec<Vec<ValidatorIndex>>, GroupRotationInfo<BlockNumber>) {
parachains_runtime_api_impl::validator_groups::<Runtime>()
}
Expand Down

0 comments on commit 09c9f4e

Please sign in to comment.