Skip to content

Commit

Permalink
Add feature flag to enable v2 assignments
Browse files Browse the repository at this point in the history
Scaffold everything, so that we can enable v2 assignments via a node
feature bit, once all nodes have upgraded to the new protocol.

Implements: #628

Signed-off-by: Alexandru Gheorghe <alexandru.gheorghe@parity.io>
  • Loading branch information
alexggh committed Nov 22, 2023
1 parent 7a32f4b commit da6fd18
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
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
23 changes: 19 additions & 4 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())
.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 @@ -667,6 +681,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
1 change: 1 addition & 0 deletions polkadot/node/core/approval-voting/src/tests.rs
Original file line number Diff line number Diff line change
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
11 changes: 8 additions & 3 deletions 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};

use cache::{RequestResult, RequestResultCache};
use futures::{channel::oneshot, prelude::*, select, stream::FuturesUnordered};
Expand Down Expand Up @@ -173,8 +173,13 @@ where
.cache_para_backing_state((relay_parent, para_id), constraints),
AsyncBackingParams(relay_parent, params) =>
self.requests_cache.cache_async_backing_params(relay_parent, params),
NodeFeatures(session_index, params) =>
self.requests_cache.cache_node_features(session_index, params),
NodeFeatures(session_index, params) => {
let last_set_index = params.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);
}
self.requests_cache.cache_node_features(session_index, params)
},
}
}

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;
/// 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;
}

0 comments on commit da6fd18

Please sign in to comment.