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

Add feature flag to enable v2 assignments #2444

Merged
merged 14 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 3 additions & 1 deletion polkadot/node/core/approval-voting/src/criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ pub(crate) trait AssignmentCriteria {
relay_vrf_story: RelayVRFStory,
config: &Config,
leaving_cores: Vec<(CandidateHash, CoreIndex, GroupIndex)>,
enable_v2_assignments: bool,
) -> HashMap<CoreIndex, OurAssignment>;

fn check_assignment_cert(
Expand All @@ -282,8 +283,9 @@ impl AssignmentCriteria for RealAssignmentCriteria {
relay_vrf_story: RelayVRFStory,
config: &Config,
leaving_cores: Vec<(CandidateHash, CoreIndex, GroupIndex)>,
enable_v2_assignments: bool,
) -> HashMap<CoreIndex, OurAssignment> {
compute_assignments(keystore, relay_vrf_story, config, leaving_cores, false)
compute_assignments(keystore, relay_vrf_story, config, leaving_cores, enable_v2_assignments)
}

fn check_assignment_cert(
Expand Down
62 changes: 57 additions & 5 deletions polkadot/node/core/approval-voting/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ use polkadot_node_subsystem::{
},
overseer, RuntimeApiError, SubsystemError, SubsystemResult,
};
use polkadot_node_subsystem_util::{determine_new_blocks, runtime::RuntimeInfo};
use polkadot_node_subsystem_util::{
determine_new_blocks,
runtime::{request_node_features, RuntimeInfo},
};
use polkadot_primitives::{
BlockNumber, CandidateEvent, CandidateHash, CandidateReceipt, ConsensusLog, CoreIndex,
GroupIndex, Hash, Header, SessionIndex,
vstaging::node_features, BlockNumber, CandidateEvent, CandidateHash, CandidateReceipt,
ConsensusLog, CoreIndex, GroupIndex, Hash, Header, SessionIndex,
};
use sc_keystore::LocalKeystore;
use sp_consensus_slots::Slot;
Expand Down Expand Up @@ -217,7 +220,17 @@ async fn imported_block_info<Context>(
let session_info = get_session_info(env.runtime_info, ctx.sender(), block_hash, session_index)
.await
.ok_or(ImportedBlockInfoError::SessionInfoUnavailable)?;

let enable_v2_assignments = request_node_features(block_hash, session_index, ctx.sender())
alexggh marked this conversation as resolved.
Show resolved Hide resolved
.await
.ok()
.flatten()
.map_or(false, |node_features| {
*node_features
.get(node_features::ENABLE_ASSIGNMENTS_V2 as usize)
.as_deref()
.unwrap_or(&false)
});
gum::debug!(target: LOG_TARGET, ?enable_v2_assignments, "V2 assignments");
let (assignments, slot, relay_vrf_story) = {
let unsafe_vrf = approval_types::v1::babe_unsafe_vrf_info(&block_header);

Expand All @@ -239,6 +252,7 @@ async fn imported_block_info<Context>(
.iter()
.map(|(c_hash, _, core, group)| (*c_hash, *core, *group))
.collect(),
enable_v2_assignments,
);

(assignments, slot, relay_vrf)
Expand Down Expand Up @@ -603,7 +617,8 @@ pub(crate) mod tests {
use polkadot_node_subsystem_test_helpers::make_subsystem_context;
use polkadot_node_subsystem_util::database::Database;
use polkadot_primitives::{
ExecutorParams, Id as ParaId, IndexedVec, SessionInfo, ValidatorId, ValidatorIndex,
vstaging::NodeFeatures, ExecutorParams, Id as ParaId, IndexedVec, SessionInfo, ValidatorId,
ValidatorIndex,
};
pub(crate) use sp_consensus_babe::{
digests::{CompatibleDigestItem, PreDigest, SecondaryVRFPreDigest},
Expand Down Expand Up @@ -667,6 +682,7 @@ pub(crate) mod tests {
polkadot_primitives::CoreIndex,
polkadot_primitives::GroupIndex,
)>,
_enable_assignments_v2: bool,
) -> HashMap<polkadot_primitives::CoreIndex, criteria::OurAssignment> {
HashMap::new()
}
Expand Down Expand Up @@ -856,6 +872,15 @@ pub(crate) mod tests {
si_tx.send(Ok(Some(ExecutorParams::default()))).unwrap();
}
);

assert_matches!(
handle.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(_, RuntimeApiRequest::NodeFeatures(_, si_tx), )
) => {
si_tx.send(Ok(NodeFeatures::EMPTY)).unwrap();
alexggh marked this conversation as resolved.
Show resolved Hide resolved
}
);
});

futures::executor::block_on(futures::future::join(test_fut, aux_fut));
Expand Down Expand Up @@ -987,6 +1012,15 @@ pub(crate) mod tests {
si_tx.send(Ok(Some(ExecutorParams::default()))).unwrap();
}
);

assert_matches!(
handle.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(_, RuntimeApiRequest::NodeFeatures(_, si_tx), )
) => {
si_tx.send(Ok(NodeFeatures::EMPTY)).unwrap();
}
);
});

futures::executor::block_on(futures::future::join(test_fut, aux_fut));
Expand Down Expand Up @@ -1221,6 +1255,15 @@ pub(crate) mod tests {
si_tx.send(Ok(Some(ExecutorParams::default()))).unwrap();
}
);

assert_matches!(
handle.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(_, RuntimeApiRequest::NodeFeatures(_, si_tx), )
) => {
si_tx.send(Ok(NodeFeatures::EMPTY)).unwrap();
}
);
});

futures::executor::block_on(futures::future::join(test_fut, aux_fut));
Expand Down Expand Up @@ -1438,6 +1481,15 @@ pub(crate) mod tests {
}
);

assert_matches!(
handle.recv().await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(_, RuntimeApiRequest::NodeFeatures(_, si_tx), )
) => {
si_tx.send(Ok(NodeFeatures::EMPTY)).unwrap();
}
);

assert_matches!(
handle.recv().await,
AllMessages::ApprovalDistribution(ApprovalDistributionMessage::NewBlocks(
Expand Down
14 changes: 12 additions & 2 deletions polkadot/node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use polkadot_node_subsystem_test_helpers as test_helpers;
use polkadot_node_subsystem_util::TimeoutExt;
use polkadot_overseer::HeadSupportsParachains;
use polkadot_primitives::{
CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, Header, Id as ParaId, IndexedVec,
ValidationCode, ValidatorSignature,
vstaging::NodeFeatures, CandidateCommitments, CandidateEvent, CoreIndex, GroupIndex, Header,
Id as ParaId, IndexedVec, ValidationCode, ValidatorSignature,
};
use std::time::Duration;

Expand Down Expand Up @@ -243,6 +243,7 @@ where
polkadot_primitives::CoreIndex,
polkadot_primitives::GroupIndex,
)>,
_enable_assignments_v2: bool,
) -> HashMap<polkadot_primitives::CoreIndex, criteria::OurAssignment> {
self.0()
}
Expand Down Expand Up @@ -999,6 +1000,15 @@ async fn import_block(
);
}

assert_matches!(
overseer_recv(overseer).await,
AllMessages::RuntimeApi(
RuntimeApiMessage::Request(_, RuntimeApiRequest::NodeFeatures(_, si_tx), )
) => {
si_tx.send(Ok(NodeFeatures::EMPTY)).unwrap();
}
);

assert_matches!(
overseer_recv(overseer).await,
AllMessages::ApprovalDistribution(
Expand Down
29 changes: 28 additions & 1 deletion polkadot/node/core/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use polkadot_node_subsystem::{
overseer, FromOrchestra, OverseerSignal, SpawnedSubsystem, SubsystemError, SubsystemResult,
};
use polkadot_node_subsystem_types::RuntimeApiSubsystemClient;
use polkadot_primitives::Hash;
use polkadot_primitives::{vstaging::node_features::FIRST_UNASSIGNED, Hash, SessionIndex};

use cache::{RequestResult, RequestResultCache};
use futures::{channel::oneshot, prelude::*, select, stream::FuturesUnordered};
Expand Down Expand Up @@ -327,6 +327,24 @@ where
}
}

// Checks that all set features in runtime are known by the current version of the node, if not
// it throws an warning for users to update their version.
async fn maybe_check_node_features(
should_check_node_features: Option<(Hash, SessionIndex, Arc<Client>, Metrics)>,
) {
if let Some((block_hash, session_index, client, metrics)) = should_check_node_features {
let (tx, _rx) = oneshot::channel();
let request = Request::NodeFeatures(session_index, tx);
let result = make_runtime_api_request(client, metrics, block_hash, request).await;
alexggh marked this conversation as resolved.
Show resolved Hide resolved
if let Some(RequestResult::NodeFeatures(_, features)) = result {
let last_set_index = features.iter_ones().last().unwrap_or_default();
if last_set_index >= FIRST_UNASSIGNED as usize {
gum::warn!(target: LOG_TARGET, "Runtime requires feature bit {} that node doesn't support, please upgrade node version", last_set_index);
}
}
};
}

/// Spawn a runtime API request.
fn spawn_request(&mut self, relay_parent: Hash, request: Request) {
let client = self.client.clone();
Expand All @@ -339,9 +357,18 @@ where
None => return,
};

// Every-time we fetch a new session info from the runtime, let's check the node features as
// well to make sure things are compatible.
let should_check_node_features = match request {
alexggh marked this conversation as resolved.
Show resolved Hide resolved
Request::SessionInfo(session_index, _) =>
Some((relay_parent, session_index, self.client.clone(), self.metrics.clone())),
_ => None,
};

let request = async move {
let result = make_runtime_api_request(client, metrics, relay_parent, request).await;
let _ = sender.send(result);
Self::maybe_check_node_features(should_check_node_features).await;
alexggh marked this conversation as resolved.
Show resolved Hide resolved
}
.boxed();

Expand Down
11 changes: 11 additions & 0 deletions polkadot/primitives/src/vstaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ use bitvec::vec::BitVec;

/// Bit indices in the `HostConfiguration.node_features` that correspond to different node features.
pub type NodeFeatures = BitVec<u8, bitvec::order::Lsb0>;

/// Module containing feature-specific bit indices into the `NodeFeatures` bitvec.
pub mod node_features {
/// Tells if tranch0 assignments could be sent in a single certificate.
/// Reserved for: https://github.com/paritytech/polkadot-sdk/issues/628
pub const ENABLE_ASSIGNMENTS_V2: u8 = 0;
alexggh marked this conversation as resolved.
Show resolved Hide resolved
/// First unassigned feature bit.
/// Every time a new feature flag is assigned it should take this value.
/// and this should be incremented.
pub const FIRST_UNASSIGNED: u8 = 1;
alexggh marked this conversation as resolved.
Show resolved Hide resolved
}
Loading