Unverified Commit 4a17a2bc authored by Sergey Pepyakin's avatar Sergey Pepyakin Committed by GitHub
Browse files

Runtime API for checking validation outputs (#1842)

* annoying whitespaces

* update guide

Add `CheckValidationOutputs` runtime api and also change the
candidate-validation stuff

* promote ValidationOutputs to global primitives

i.e. move it from node specific primitives to global v1 primitives. This
will be needed when we share it later in the runtime inclusion module

* refactor acceptance checks in the inclusion module

factor out the common code to share it during the block inclusion and
for the forthcoming CheckValidationOutputs runtime api.

Also note that the acceptance criteria was updated to incorporate checks
that exist now in candidate-validation

* plumb the runtime api outside

* extract validation_data from ValidationOutputs

* use runtime-api to check validation outputs

apart from that refactor, update docs and tidy a bit

* Update the maxium code size

This is to fix a test that performs an upgrade.
parent 8f882afd
Pipeline #111715 passed with stages
in 22 minutes and 47 seconds
......@@ -32,11 +32,10 @@ use polkadot_primitives::v1::{
CommittedCandidateReceipt, BackedCandidate, Id as ParaId, ValidatorId,
ValidatorIndex, SigningContext, PoV,
CandidateDescriptor, AvailableData, ValidatorSignature, Hash, CandidateReceipt,
CandidateCommitments, CoreState, CoreIndex, CollatorId,
CandidateCommitments, CoreState, CoreIndex, CollatorId, ValidationOutputs,
};
use polkadot_node_primitives::{
FromTableMisbehavior, Statement, SignedFullStatement, MisbehaviorReport,
ValidationOutputs, ValidationResult,
FromTableMisbehavior, Statement, SignedFullStatement, MisbehaviorReport, ValidationResult,
};
use polkadot_subsystem::{
messages::{
......@@ -287,7 +286,7 @@ impl CandidateBackingJob {
let candidate_hash = candidate.hash();
let statement = match valid {
ValidationResult::Valid(outputs) => {
ValidationResult::Valid(outputs, validation_data) => {
// make PoV available for later distribution. Send data to the availability
// store to keep. Sign and dispatch `valid` statement to network if we
// have not seconded the given candidate.
......@@ -296,6 +295,7 @@ impl CandidateBackingJob {
// the collator, do not make available and report the collator.
let commitments_check = self.make_pov_available(
pov,
validation_data,
outputs,
|commitments| if commitments.hash() == candidate.commitments_hash {
Ok(CommittedCandidateReceipt {
......@@ -510,10 +510,11 @@ impl CandidateBackingJob {
let v = self.request_candidate_validation(descriptor, pov.clone()).await?;
let statement = match v {
ValidationResult::Valid(outputs) => {
ValidationResult::Valid(outputs, validation_data) => {
// If validation produces a new set of commitments, we vote the candidate as invalid.
let commitments_check = self.make_pov_available(
(&*pov).clone(),
validation_data,
outputs,
|commitments| if commitments == expected_commitments {
Ok(())
......@@ -652,12 +653,13 @@ impl CandidateBackingJob {
async fn make_pov_available<T, E>(
&mut self,
pov: PoV,
validation_data: polkadot_primitives::v1::PersistedValidationData,
outputs: ValidationOutputs,
with_commitments: impl FnOnce(CandidateCommitments) -> Result<T, E>,
) -> Result<Result<T, E>, Error> {
let available_data = AvailableData {
pov,
validation_data: outputs.validation_data,
validation_data,
};
let chunks = erasure_coding::obtain_chunks_v1(
......@@ -1147,12 +1149,11 @@ mod tests {
) if pov == pov && &c == candidate.descriptor() => {
tx.send(Ok(
ValidationResult::Valid(ValidationOutputs {
validation_data: test_state.validation_data.persisted,
head_data: expected_head_data.clone(),
upward_messages: Vec::new(),
fees: Default::default(),
new_validation_code: None,
}),
}, test_state.validation_data.persisted),
)).unwrap();
}
);
......@@ -1267,12 +1268,11 @@ mod tests {
) if pov == pov && &c == candidate_a.descriptor() => {
tx.send(Ok(
ValidationResult::Valid(ValidationOutputs {
validation_data: test_state.validation_data.persisted,
head_data: expected_head_data.clone(),
upward_messages: Vec::new(),
fees: Default::default(),
new_validation_code: None,
}),
}, test_state.validation_data.persisted),
)).unwrap();
}
);
......@@ -1406,12 +1406,11 @@ mod tests {
) if pov == pov && &c == candidate_a.descriptor() => {
tx.send(Ok(
ValidationResult::Valid(ValidationOutputs {
validation_data: test_state.validation_data.persisted,
head_data: expected_head_data.clone(),
upward_messages: Vec::new(),
fees: Default::default(),
new_validation_code: None,
}),
}, test_state.validation_data.persisted),
)).unwrap();
}
);
......@@ -1562,12 +1561,11 @@ mod tests {
) if pov == pov && &c == candidate_b.descriptor() => {
tx.send(Ok(
ValidationResult::Valid(ValidationOutputs {
validation_data: test_state.validation_data.persisted,
head_data: expected_head_data.clone(),
upward_messages: Vec::new(),
fees: Default::default(),
new_validation_code: None,
}),
}, test_state.validation_data.persisted),
)).unwrap();
}
);
......
......@@ -343,7 +343,10 @@ async fn candidate_is_valid_inner(
CandidateValidationMessage::ValidateFromChainState(candidate_descriptor, pov, tx),
))
.await?;
Ok(std::matches!(rx.await, Ok(Ok(ValidationResult::Valid(_)))))
Ok(std::matches!(
rx.await,
Ok(Ok(ValidationResult::Valid(_, _)))
))
}
async fn second_candidate(
......@@ -445,8 +448,7 @@ delegated_subsystem!(CandidateSelectionJob((), Metrics) <- ToJob as CandidateSel
mod tests {
use super::*;
use futures::lock::Mutex;
use polkadot_node_primitives::ValidationOutputs;
use polkadot_primitives::v1::{BlockData, HeadData, PersistedValidationData};
use polkadot_primitives::v1::{BlockData, HeadData, PersistedValidationData, ValidationOutputs};
use sp_core::crypto::Public;
fn test_harness<Preconditions, TestBuilder, Test, Postconditions>(
......@@ -478,7 +480,7 @@ mod tests {
postconditions(job, job_result);
}
fn default_validation_outputs() -> ValidationOutputs {
fn default_validation_outputs_and_data() -> (ValidationOutputs, polkadot_primitives::v1::PersistedValidationData) {
let head_data: Vec<u8> = (0..32).rev().cycle().take(256).collect();
let parent_head_data = head_data
.iter()
......@@ -486,17 +488,19 @@ mod tests {
.map(|x| x.saturating_sub(1))
.collect();
ValidationOutputs {
head_data: HeadData(head_data),
validation_data: PersistedValidationData {
(
ValidationOutputs {
head_data: HeadData(head_data),
upward_messages: Vec::new(),
fees: 0,
new_validation_code: None,
},
PersistedValidationData {
parent_head: HeadData(parent_head_data),
block_number: 123,
hrmp_mqc_heads: Vec::new(),
},
upward_messages: Vec::new(),
fees: 0,
new_validation_code: None,
}
)
}
/// when nothing is seconded so far, the collation is fetched and seconded
......@@ -556,8 +560,9 @@ mod tests {
assert_eq!(got_candidate_descriptor, candidate_receipt.descriptor);
assert_eq!(got_pov.as_ref(), &pov);
let (outputs, data) = default_validation_outputs_and_data();
return_sender
.send(Ok(ValidationResult::Valid(default_validation_outputs())))
.send(Ok(ValidationResult::Valid(outputs, data)))
.unwrap();
}
FromJob::Backing(CandidateBackingMessage::Second(
......
......@@ -32,10 +32,10 @@ use polkadot_node_subsystem_util::{
metrics::{self, prometheus},
};
use polkadot_subsystem::errors::RuntimeApiError;
use polkadot_node_primitives::{ValidationResult, ValidationOutputs, InvalidCandidate};
use polkadot_node_primitives::{ValidationResult, InvalidCandidate};
use polkadot_primitives::v1::{
ValidationCode, PoV, CandidateDescriptor, ValidationData, PersistedValidationData,
TransientValidationData, OccupiedCoreAssumption, Hash,
ValidationCode, PoV, CandidateDescriptor, PersistedValidationData,
OccupiedCoreAssumption, Hash, ValidationOutputs,
};
use polkadot_parachain::wasm_executor::{
self, ValidationPool, ExecutionMode, ValidationError,
......@@ -72,7 +72,7 @@ impl Metrics {
fn on_validation_event(&self, event: &Result<ValidationResult, ValidationFailed>) {
if let Some(metrics) = &self.0 {
match event {
Ok(ValidationResult::Valid(_)) => {
Ok(ValidationResult::Valid(_, _)) => {
metrics.validation_requests.with_label_values(&["valid"]).inc();
},
Ok(ValidationResult::Invalid(_)) => {
......@@ -161,7 +161,6 @@ async fn run(
}
CandidateValidationMessage::ValidateFromExhaustive(
persisted_validation_data,
transient_validation_data,
validation_code,
descriptor,
pov,
......@@ -171,7 +170,6 @@ async fn run(
&mut ctx,
execution_mode.clone(),
persisted_validation_data,
transient_validation_data,
validation_code,
descriptor,
pov,
......@@ -214,7 +212,7 @@ async fn runtime_api_request<T>(
#[derive(Debug)]
enum AssumptionCheckOutcome {
Matches(ValidationData, ValidationCode),
Matches(PersistedValidationData, ValidationCode),
DoesNotMatch,
BadRequest,
}
......@@ -229,7 +227,7 @@ async fn check_assumption_validation_data(
let d = runtime_api_request(
ctx,
descriptor.relay_parent,
RuntimeApiRequest::FullValidationData(
RuntimeApiRequest::PersistedValidationData(
descriptor.para_id,
assumption,
tx,
......@@ -245,7 +243,7 @@ async fn check_assumption_validation_data(
}
};
let persisted_validation_data_hash = validation_data.persisted.hash();
let persisted_validation_data_hash = validation_data.hash();
SubsystemResult::Ok(if descriptor.persisted_validation_data_hash == persisted_validation_data_hash {
let (code_tx, code_rx) = oneshot::channel();
......@@ -269,70 +267,100 @@ async fn check_assumption_validation_data(
})
}
async fn spawn_validate_from_chain_state(
async fn find_assumed_validation_data(
ctx: &mut impl SubsystemContext<Message = CandidateValidationMessage>,
execution_mode: ExecutionMode,
descriptor: CandidateDescriptor,
pov: Arc<PoV>,
spawn: impl SpawnNamed + 'static,
) -> SubsystemResult<Result<ValidationResult, ValidationFailed>> {
descriptor: &CandidateDescriptor,
) -> SubsystemResult<AssumptionCheckOutcome> {
// The candidate descriptor has a `persisted_validation_data_hash` which corresponds to
// one of up to two possible values that we can derive from the state of the
// relay-parent. We can fetch these values by getting the persisted validation data
// based on the different `OccupiedCoreAssumption`s.
match check_assumption_validation_data(
ctx,
&descriptor,
const ASSUMPTIONS: &[OccupiedCoreAssumption] = &[
OccupiedCoreAssumption::Included,
).await? {
AssumptionCheckOutcome::Matches(validation_data, validation_code) => {
return spawn_validate_exhaustive(
ctx,
execution_mode,
validation_data.persisted,
Some(validation_data.transient),
validation_code,
descriptor,
pov,
spawn,
).await;
}
AssumptionCheckOutcome::DoesNotMatch => {},
AssumptionCheckOutcome::BadRequest => return Ok(Err(ValidationFailed("Bad request".into()))),
OccupiedCoreAssumption::TimedOut,
// TODO: Why don't we check `Free`? The guide assumes there are only two possible assumptions.
//
// Source that info and leave a comment here.
];
// Consider running these checks in parallel to reduce validation latency.
for assumption in ASSUMPTIONS {
let outcome = check_assumption_validation_data(ctx, descriptor, *assumption).await?;
let () = match outcome {
AssumptionCheckOutcome::Matches(_, _) => return Ok(outcome),
AssumptionCheckOutcome::BadRequest => return Ok(outcome),
AssumptionCheckOutcome::DoesNotMatch => continue,
};
}
match check_assumption_validation_data(
Ok(AssumptionCheckOutcome::DoesNotMatch)
}
async fn spawn_validate_from_chain_state(
ctx: &mut impl SubsystemContext<Message = CandidateValidationMessage>,
execution_mode: ExecutionMode,
descriptor: CandidateDescriptor,
pov: Arc<PoV>,
spawn: impl SpawnNamed + 'static,
) -> SubsystemResult<Result<ValidationResult, ValidationFailed>> {
let (validation_data, validation_code) =
match find_assumed_validation_data(ctx, &descriptor).await? {
AssumptionCheckOutcome::Matches(validation_data, validation_code) => {
(validation_data, validation_code)
}
AssumptionCheckOutcome::DoesNotMatch => {
// If neither the assumption of the occupied core having the para included or the assumption
// of the occupied core timing out are valid, then the persisted_validation_data_hash in the descriptor
// is not based on the relay parent and is thus invalid.
return Ok(Ok(ValidationResult::Invalid(InvalidCandidate::BadParent)));
}
AssumptionCheckOutcome::BadRequest => {
return Ok(Err(ValidationFailed("Assumption Check: Bad request".into())));
}
};
let validation_result = spawn_validate_exhaustive(
ctx,
&descriptor,
OccupiedCoreAssumption::TimedOut,
).await? {
AssumptionCheckOutcome::Matches(validation_data, validation_code) => {
return spawn_validate_exhaustive(
ctx,
execution_mode,
validation_data.persisted,
Some(validation_data.transient),
validation_code,
descriptor,
pov,
spawn,
).await;
execution_mode,
validation_data,
validation_code,
descriptor.clone(),
pov,
spawn,
)
.await;
if let Ok(Ok(ValidationResult::Valid(ref outputs, _))) = validation_result {
let (tx, rx) = oneshot::channel();
match runtime_api_request(
ctx,
descriptor.relay_parent,
RuntimeApiRequest::CheckValidationOutputs(descriptor.para_id, outputs.clone(), tx),
rx,
)
.await?
{
Ok(true) => {}
Ok(false) => {
return Ok(Ok(ValidationResult::Invalid(
InvalidCandidate::InvalidOutputs,
)));
}
Err(_) => {
return Ok(Err(ValidationFailed("Check Validation Outputs: Bad request".into())));
}
}
AssumptionCheckOutcome::DoesNotMatch => {},
AssumptionCheckOutcome::BadRequest => return Ok(Err(ValidationFailed("Bad request".into()))),
}
// If neither the assumption of the occupied core having the para included or the assumption
// of the occupied core timing out are valid, then the persisted_validation_data_hash in the descriptor
// is not based on the relay parent and is thus invalid.
Ok(Ok(ValidationResult::Invalid(InvalidCandidate::BadParent)))
validation_result
}
async fn spawn_validate_exhaustive(
ctx: &mut impl SubsystemContext<Message = CandidateValidationMessage>,
execution_mode: ExecutionMode,
persisted_validation_data: PersistedValidationData,
transient_validation_data: Option<TransientValidationData>,
validation_code: ValidationCode,
descriptor: CandidateDescriptor,
pov: Arc<PoV>,
......@@ -343,7 +371,6 @@ async fn spawn_validate_exhaustive(
let res = validate_candidate_exhaustive::<RealValidationBackend, _>(
execution_mode,
persisted_validation_data,
transient_validation_data,
validation_code,
descriptor,
pov,
......@@ -384,30 +411,6 @@ fn perform_basic_checks(
Ok(())
}
/// Check the result of Wasm execution against the constraints given by the relay-chain.
///
/// Returns `Ok(())` if checks pass, error otherwise.
fn check_wasm_result_against_constraints(
transient_params: &TransientValidationData,
result: &WasmValidationResult,
) -> Result<(), InvalidCandidate> {
if result.head_data.0.len() > transient_params.max_head_data_size as _ {
return Err(InvalidCandidate::HeadDataTooLarge(result.head_data.0.len() as u64))
}
if let Some(ref code) = result.new_validation_code {
if transient_params.code_upgrade_allowed.is_none() {
return Err(InvalidCandidate::CodeUpgradeNotAllowed)
}
if code.0.len() > transient_params.max_code_size as _ {
return Err(InvalidCandidate::NewCodeTooLarge(code.0.len() as u64))
}
}
Ok(())
}
trait ValidationBackend {
type Arg;
......@@ -445,7 +448,6 @@ impl ValidationBackend for RealValidationBackend {
fn validate_candidate_exhaustive<B: ValidationBackend, S: SpawnNamed + 'static>(
backend_arg: B::Arg,
persisted_validation_data: PersistedValidationData,
transient_validation_data: Option<TransientValidationData>,
validation_code: ValidationCode,
descriptor: CandidateDescriptor,
pov: Arc<PoV>,
......@@ -477,25 +479,13 @@ fn validate_candidate_exhaustive<B: ValidationBackend, S: SpawnNamed + 'static>(
Ok(ValidationResult::Invalid(InvalidCandidate::ExecutionError(e.to_string()))),
Err(ValidationError::Internal(e)) => Err(ValidationFailed(e.to_string())),
Ok(res) => {
let post_check_result = if let Some(transient) = transient_validation_data {
check_wasm_result_against_constraints(
&transient,
&res,
)
} else {
Ok(())
let outputs = ValidationOutputs {
head_data: res.head_data,
upward_messages: res.upward_messages,
fees: 0,
new_validation_code: res.new_validation_code,
};
Ok(match post_check_result {
Ok(()) => ValidationResult::Valid(ValidationOutputs {
head_data: res.head_data,
validation_data: persisted_validation_data,
upward_messages: res.upward_messages,
fees: 0,
new_validation_code: res.new_validation_code,
}),
Err(e) => ValidationResult::Invalid(e),
})
Ok(ValidationResult::Valid(outputs, persisted_validation_data))
}
}
}
......@@ -544,10 +534,10 @@ mod tests {
#[test]
fn correctly_checks_included_assumption() {
let validation_data: ValidationData = Default::default();
let validation_data: PersistedValidationData = Default::default();
let validation_code: ValidationCode = vec![1, 2, 3].into();
let persisted_validation_data_hash = validation_data.persisted.hash();
let persisted_validation_data_hash = validation_data.hash();
let relay_parent = [2; 32].into();
let para_id = 5.into();
......@@ -570,7 +560,11 @@ mod tests {
ctx_handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
rp,
RuntimeApiRequest::FullValidationData(p, OccupiedCoreAssumption::Included, tx)
RuntimeApiRequest::PersistedValidationData(
p,
OccupiedCoreAssumption::Included,
tx
),
)) => {
assert_eq!(rp, relay_parent);
assert_eq!(p, para_id);
......@@ -604,10 +598,10 @@ mod tests {
#[test]
fn correctly_checks_timed_out_assumption() {
let validation_data: ValidationData = Default::default();
let validation_data: PersistedValidationData = Default::default();
let validation_code: ValidationCode = vec![1, 2, 3].into();
let persisted_validation_data_hash = validation_data.persisted.hash();
let persisted_validation_data_hash = validation_data.hash();
let relay_parent = [2; 32].into();
let para_id = 5.into();
......@@ -630,7 +624,11 @@ mod tests {
ctx_handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
rp,
RuntimeApiRequest::FullValidationData(p, OccupiedCoreAssumption::TimedOut, tx)
RuntimeApiRequest::PersistedValidationData(
p,
OccupiedCoreAssumption::TimedOut,
tx
),
)) => {
assert_eq!(rp, relay_parent);
assert_eq!(p, para_id);
......@@ -664,8 +662,8 @@ mod tests {
#[test]
fn check_is_bad_request_if_no_validation_data() {
let validation_data: ValidationData = Default::default();
let persisted_validation_data_hash = validation_data.persisted.hash();
let validation_data: PersistedValidationData = Default::default();
let persisted_validation_data_hash = validation_data.hash();
let relay_parent = [2; 32].into();
let para_id = 5.into();
......@@ -688,7 +686,11 @@ mod tests {
ctx_handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
rp,
RuntimeApiRequest::FullValidationData(p, OccupiedCoreAssumption::Included, tx)
RuntimeApiRequest::PersistedValidationData(
p,
OccupiedCoreAssumption::Included,
tx
),
)) => {
assert_eq!(rp, relay_parent);
assert_eq!(p, para_id);
......@@ -706,8 +708,8 @@ mod tests {
#[test]
fn check_is_bad_request_if_no_validation_code() {
let validation_data: ValidationData = Default::default();
let persisted_validation_data_hash = validation_data.persisted.hash();
let validation_data: PersistedValidationData = Default::default();
let persisted_validation_data_hash = validation_data.hash();
let relay_parent = [2; 32].into();
let para_id = 5.into();
......@@ -730,7 +732,11 @@ mod tests {
ctx_handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
rp,
RuntimeApiRequest::FullValidationData(p, OccupiedCoreAssumption::TimedOut, tx)
RuntimeApiRequest::PersistedValidationData(
p,
OccupiedCoreAssumption::TimedOut,
tx
),
)) => {
assert_eq!(rp, relay_parent);
assert_eq!(p, para_id);
......@@ -761,7 +767,7 @@ mod tests {
#[test]
fn check_does_not_match() {
let validation_data: ValidationData = Default::default();
let validation_data: PersistedValidationData = Default::default();
let relay_parent = [2; 32].into();
let para_id = 5.into();
......@@ -784,7 +790,11 @@ mod tests {
ctx_handle.recv().await,
AllMessages::RuntimeApi(RuntimeApiMessage::Request(
rp,
RuntimeApiRequest::FullValidationData(p, OccupiedCoreAssumption::Included, tx)
RuntimeApiRequest::PersistedValidationData(
p,
OccupiedCoreAssumption::Included,
tx
),
)) => {
assert_eq!(rp, relay_parent);
assert_eq!(p, para_id);
......@@ -802,10 +812,7 @@ mod tests {
#[test]
fn candidate_validation_ok_is_ok() {
let mut validation_data: ValidationData = Default::default();
validation_data.transient.max_head_data_size = 1024;
validation_data.transient.max_code_size = 1024;
validation_data.transient.code_upgrade_allowed = Some(20);
let validation_data: PersistedValidationData = Default::default();
let pov = PoV { block_data: BlockData(vec![1; 32]) };
......@@ -822,37 +829,27 @@ mod tests {
processed_downward_messages: 0,
};
assert!(check_wasm_result_against_constraints(
&validation_data.transient,
&validation_result,
).is_ok());
let v = validate_candidate_exhaustive::<MockValidationBackend, _>(