Skip to content

Commit

Permalink
Add ttl to PvfExecKind
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiEres committed Oct 11, 2024
1 parent 210a3dd commit 8f00e86
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 186 deletions.
19 changes: 15 additions & 4 deletions polkadot/node/core/backing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use polkadot_node_subsystem_util::{
};
use polkadot_parachain_primitives::primitives::IsSystem;
use polkadot_primitives::{
node_features::FeatureIndex, BackedCandidate, CandidateCommitments, CandidateHash,
node_features::FeatureIndex, BackedCandidate, BlockNumber, CandidateCommitments, CandidateHash,
CandidateReceipt, CommittedCandidateReceipt, CoreIndex, CoreState, ExecutorParams, GroupIndex,
GroupRotationInfo, Hash, Id as ParaId, IndexedVec, NodeFeatures, PersistedValidationData,
SessionIndex, SigningContext, ValidationCode, ValidatorId, ValidatorIndex, ValidatorSignature,
Expand Down Expand Up @@ -625,10 +625,18 @@ async fn request_candidate_validation(
candidate_receipt: CandidateReceipt,
pov: Arc<PoV>,
executor_params: ExecutorParams,
mode: ProspectiveParachainsMode,
) -> Result<ValidationResult, Error> {
let (tx, rx) = oneshot::channel();
let is_system = candidate_receipt.descriptor.para_id.is_system();

let ttl = match mode {
ProspectiveParachainsMode::Enabled { allowed_ancestry_len, .. }
if allowed_ancestry_len > 0 =>
Some(validation_data.relay_parent_number + allowed_ancestry_len as BlockNumber),
_ => None,
};

sender
.send_message(CandidateValidationMessage::ValidateFromExhaustive {
validation_data,
Expand All @@ -637,9 +645,9 @@ async fn request_candidate_validation(
pov,
executor_params,
exec_kind: if is_system {
PvfExecKind::BackingSystemParas
PvfExecKind::BackingSystemParas { ttl }
} else {
PvfExecKind::Backing
PvfExecKind::Backing { ttl }
},
response_sender: tx,
})
Expand Down Expand Up @@ -678,6 +686,7 @@ async fn validate_and_make_available(
impl Fn(BackgroundValidationResult) -> ValidatedCandidateCommand + Sync,
>,
core_index: CoreIndex,
mode: ProspectiveParachainsMode,
) -> Result<(), Error> {
let BackgroundValidationParams {
mut sender,
Expand Down Expand Up @@ -754,6 +763,7 @@ async fn validate_and_make_available(
candidate.clone(),
pov.clone(),
executor_params,
mode,
)
.await?
};
Expand Down Expand Up @@ -1812,10 +1822,11 @@ async fn background_validate_and_make_available<Context>(
) -> Result<(), Error> {
let candidate_hash = params.candidate.hash();
let Some(core_index) = rp_state.assigned_core else { return Ok(()) };
let mode = rp_state.prospective_parachains_mode;
if rp_state.awaiting_validation.insert(candidate_hash) {
// spawn background task.
let bg = async move {
if let Err(error) = validate_and_make_available(params, core_index).await {
if let Err(error) = validate_and_make_available(params, core_index, mode).await {
if let Error::BackgroundValidationMpsc(error) = error {
gum::debug!(
target: LOG_TARGET,
Expand Down
20 changes: 10 additions & 10 deletions polkadot/node/core/backing/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ async fn assert_validate_from_exhaustive(
) if validation_data == *assert_pvd &&
validation_code == *assert_validation_code &&
*pov == *assert_pov && &candidate_receipt.descriptor == assert_candidate.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_receipt.commitments_hash == assert_candidate.commitments.hash() =>
{
response_sender.send(Ok(ValidationResult::Valid(
Expand Down Expand Up @@ -651,7 +651,7 @@ fn backing_works(#[case] elastic_scaling_mvp: bool) {
) if validation_data == pvd_ab &&
validation_code == validation_code_ab &&
*pov == pov_ab && &candidate_receipt.descriptor == candidate_a.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_receipt.commitments_hash == candidate_a_commitments_hash =>
{
response_sender.send(Ok(
Expand Down Expand Up @@ -1287,7 +1287,7 @@ fn backing_works_while_validation_ongoing() {
) if validation_data == pvd_abc &&
validation_code == validation_code_abc &&
*pov == pov_abc && &candidate_receipt.descriptor == candidate_a.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_a_commitments_hash == candidate_receipt.commitments_hash =>
{
// we never validate the candidate. our local node
Expand Down Expand Up @@ -1454,7 +1454,7 @@ fn backing_misbehavior_works() {
) if validation_data == pvd_a &&
validation_code == validation_code_a &&
*pov == pov_a && &candidate_receipt.descriptor == candidate_a.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_a_commitments_hash == candidate_receipt.commitments_hash =>
{
response_sender.send(Ok(
Expand Down Expand Up @@ -1621,7 +1621,7 @@ fn backing_dont_second_invalid() {
) if validation_data == pvd_a &&
validation_code == validation_code_a &&
*pov == pov_block_a && &candidate_receipt.descriptor == candidate_a.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_a.commitments.hash() == candidate_receipt.commitments_hash =>
{
response_sender.send(Ok(ValidationResult::Invalid(InvalidCandidate::BadReturn))).unwrap();
Expand Down Expand Up @@ -1661,7 +1661,7 @@ fn backing_dont_second_invalid() {
) if validation_data == pvd_b &&
validation_code == validation_code_b &&
*pov == pov_block_b && &candidate_receipt.descriptor == candidate_b.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_b.commitments.hash() == candidate_receipt.commitments_hash =>
{
response_sender.send(Ok(
Expand Down Expand Up @@ -1788,7 +1788,7 @@ fn backing_second_after_first_fails_works() {
) if validation_data == pvd_a &&
validation_code == validation_code_a &&
*pov == pov_a && &candidate_receipt.descriptor == candidate.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate.commitments.hash() == candidate_receipt.commitments_hash =>
{
response_sender.send(Ok(ValidationResult::Invalid(InvalidCandidate::BadReturn))).unwrap();
Expand Down Expand Up @@ -1932,7 +1932,7 @@ fn backing_works_after_failed_validation() {
) if validation_data == pvd_a &&
validation_code == validation_code_a &&
*pov == pov_a && &candidate_receipt.descriptor == candidate.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate.commitments.hash() == candidate_receipt.commitments_hash =>
{
response_sender.send(Err(ValidationFailed("Internal test error".into()))).unwrap();
Expand Down Expand Up @@ -2211,7 +2211,7 @@ fn retry_works() {
) if validation_data == pvd_a &&
validation_code == validation_code_a &&
*pov == pov_a && &candidate_receipt.descriptor == candidate.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate.commitments.hash() == candidate_receipt.commitments_hash
);
virtual_overseer
Expand Down Expand Up @@ -2753,7 +2753,7 @@ fn validator_ignores_statements_from_disabled_validators() {
) if validation_data == pvd &&
validation_code == expected_validation_code &&
*pov == expected_pov && &candidate_receipt.descriptor == candidate.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate_commitments_hash == candidate_receipt.commitments_hash =>
{
response_sender.send(Ok(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ async fn assert_validate_seconded_candidate(
&validation_code == assert_validation_code &&
&*pov == assert_pov &&
&candidate_receipt.descriptor == candidate.descriptor() &&
exec_kind == PvfExecKind::BackingSystemParas &&
exec_kind.is_backing_system_paras() &&
candidate.commitments.hash() == candidate_receipt.commitments_hash =>
{
response_sender.send(Ok(ValidationResult::Valid(
Expand Down
56 changes: 4 additions & 52 deletions polkadot/node/core/candidate-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,15 @@ use polkadot_node_subsystem::{
overseer, FromOrchestra, OverseerSignal, SpawnedSubsystem, SubsystemError, SubsystemResult,
SubsystemSender,
};
use polkadot_node_subsystem_util::{
self as util,
runtime::{prospective_parachains_mode, ProspectiveParachainsMode},
};
use polkadot_node_subsystem_util::{self as util};
use polkadot_overseer::{ActivatedLeaf, ActiveLeavesUpdate};
use polkadot_parachain_primitives::primitives::ValidationResult as WasmValidationResult;
use polkadot_primitives::{
executor_params::{
DEFAULT_APPROVAL_EXECUTION_TIMEOUT, DEFAULT_BACKING_EXECUTION_TIMEOUT,
DEFAULT_LENIENT_PREPARATION_TIMEOUT, DEFAULT_PRECHECK_PREPARATION_TIMEOUT,
},
AuthorityDiscoveryId, BlockNumber, CandidateCommitments, CandidateDescriptor, CandidateEvent,
AuthorityDiscoveryId, CandidateCommitments, CandidateDescriptor, CandidateEvent,
CandidateReceipt, ExecutorParams, Hash, PersistedValidationData,
PvfExecKind as RuntimePvfExecKind, PvfPrepKind, SessionIndex, ValidationCode,
ValidationCodeHash, ValidatorId,
Expand Down Expand Up @@ -168,13 +165,6 @@ where
response_sender,
} => async move {
let _timer = metrics.time_validate_from_exhaustive();
let ttl = validation_request_ttl(
&mut sender,
candidate_receipt.descriptor.relay_parent,
validation_data.relay_parent_number,
exec_kind,
)
.await;
let res = validate_candidate_exhaustive(
validation_host,
validation_data,
Expand All @@ -183,7 +173,6 @@ where
pov,
executor_params,
exec_kind,
ttl,
&metrics,
)
.await;
Expand Down Expand Up @@ -667,7 +656,6 @@ async fn validate_candidate_exhaustive(
pov: Arc<PoV>,
executor_params: ExecutorParams,
exec_kind: PvfExecKind,
request_ttl: Option<BlockNumber>,
metrics: &Metrics,
) -> Result<ValidationResult, ValidationFailed> {
let _timer = metrics.time_validate_candidate_exhaustive();
Expand Down Expand Up @@ -695,7 +683,7 @@ async fn validate_candidate_exhaustive(
let result = match exec_kind {
// Retry is disabled to reduce the chance of nondeterministic blocks getting backed and
// honest backers getting slashed.
PvfExecKind::Backing | PvfExecKind::BackingSystemParas => {
PvfExecKind::Backing { .. } | PvfExecKind::BackingSystemParas { .. } => {
let prep_timeout = pvf_prep_timeout(&executor_params, PvfPrepKind::Prepare);
let exec_timeout = pvf_exec_timeout(&executor_params, exec_kind.into());
let pvf = PvfPrepData::from_code(
Expand All @@ -709,7 +697,6 @@ async fn validate_candidate_exhaustive(
.validate_candidate(
pvf,
exec_timeout,
request_ttl,
persisted_validation_data.clone(),
pov,
exec_kind.into(),
Expand All @@ -722,7 +709,6 @@ async fn validate_candidate_exhaustive(
.validate_candidate_with_retry(
validation_code.0,
pvf_exec_timeout(&executor_params, exec_kind.into()),
request_ttl,
persisted_validation_data.clone(),
pov,
executor_params,
Expand Down Expand Up @@ -821,7 +807,6 @@ trait ValidationBackend {
&mut self,
pvf: PvfPrepData,
exec_timeout: Duration,
exec_ttl: Option<BlockNumber>,
pvd: Arc<PersistedValidationData>,
pov: Arc<PoV>,
// The priority for the preparation job.
Expand All @@ -842,7 +827,6 @@ trait ValidationBackend {
&mut self,
code: Vec<u8>,
exec_timeout: Duration,
exec_ttl: Option<BlockNumber>,
pvd: Arc<PersistedValidationData>,
pov: Arc<PoV>,
executor_params: ExecutorParams,
Expand All @@ -869,7 +853,6 @@ trait ValidationBackend {
.validate_candidate(
pvf.clone(),
exec_timeout,
exec_ttl,
pvd.clone(),
pov.clone(),
prepare_priority,
Expand Down Expand Up @@ -956,7 +939,6 @@ trait ValidationBackend {
.validate_candidate(
pvf.clone(),
new_timeout,
exec_ttl,
pvd.clone(),
pov.clone(),
prepare_priority,
Expand All @@ -983,7 +965,6 @@ impl ValidationBackend for ValidationHost {
&mut self,
pvf: PvfPrepData,
exec_timeout: Duration,
exec_ttl: Option<BlockNumber>,
pvd: Arc<PersistedValidationData>,
pov: Arc<PoV>,
// The priority for the preparation job.
Expand All @@ -993,7 +974,7 @@ impl ValidationBackend for ValidationHost {
) -> Result<WasmValidationResult, ValidationError> {
let (tx, rx) = oneshot::channel();
if let Err(err) = self
.execute_pvf(pvf, exec_timeout, exec_ttl, pvd, pov, prepare_priority, exec_kind, tx)
.execute_pvf(pvf, exec_timeout, pvd, pov, prepare_priority, exec_kind, tx)
.await
{
return Err(InternalValidationError::HostCommunication(format!(
Expand Down Expand Up @@ -1099,32 +1080,3 @@ fn pvf_exec_timeout(executor_params: &ExecutorParams, kind: RuntimePvfExecKind)
RuntimePvfExecKind::Approval => DEFAULT_APPROVAL_EXECUTION_TIMEOUT,
}
}

async fn validation_request_ttl<S>(
sender: &mut S,
relay_parent: Hash,
relay_parent_number: BlockNumber,
exec_kind: PvfExecKind,
) -> Option<BlockNumber>
where
S: SubsystemSender<RuntimeApiMessage>,
{
if !matches!(exec_kind, PvfExecKind::Backing | PvfExecKind::BackingSystemParas) {
return None;
}

let Ok(mode) = prospective_parachains_mode(sender, relay_parent).await else {
return None;
};

let ttl_in_blocks = match mode {
ProspectiveParachainsMode::Enabled { allowed_ancestry_len, .. } => allowed_ancestry_len,
_ => 1,
} as BlockNumber;

if ttl_in_blocks < 1 {
None
} else {
Some(relay_parent_number + ttl_in_blocks)
}
}
Loading

0 comments on commit 8f00e86

Please sign in to comment.