use crate::prepare::PrepareJobKind;
use codec::{Decode, Encode};
use polkadot_parachain_primitives::primitives::ValidationCodeHash;
use polkadot_primitives::ExecutorParams;
use std::{fmt, sync::Arc, time::Duration};
#[derive(Clone, Encode, Decode)]
pub struct PvfPrepData {
maybe_compressed_code: Arc<Vec<u8>>,
code_hash: ValidationCodeHash,
executor_params: Arc<ExecutorParams>,
prep_timeout: Duration,
prep_kind: PrepareJobKind,
}
impl PvfPrepData {
pub fn from_code(
code: Vec<u8>,
executor_params: ExecutorParams,
prep_timeout: Duration,
prep_kind: PrepareJobKind,
) -> Self {
let maybe_compressed_code = Arc::new(code);
let code_hash = sp_crypto_hashing::blake2_256(&maybe_compressed_code).into();
let executor_params = Arc::new(executor_params);
Self { maybe_compressed_code, code_hash, executor_params, prep_timeout, prep_kind }
}
pub fn code_hash(&self) -> ValidationCodeHash {
self.code_hash
}
pub fn maybe_compressed_code(&self) -> Arc<Vec<u8>> {
self.maybe_compressed_code.clone()
}
pub fn executor_params(&self) -> Arc<ExecutorParams> {
self.executor_params.clone()
}
pub fn prep_timeout(&self) -> Duration {
self.prep_timeout
}
pub fn prep_kind(&self) -> PrepareJobKind {
self.prep_kind
}
#[cfg(feature = "test-utils")]
pub fn from_discriminator_and_timeout(num: u32, timeout: Duration) -> Self {
let discriminator_buf = num.to_le_bytes().to_vec();
Self::from_code(
discriminator_buf,
ExecutorParams::default(),
timeout,
PrepareJobKind::Compilation,
)
}
#[cfg(feature = "test-utils")]
pub fn from_discriminator(num: u32) -> Self {
Self::from_discriminator_and_timeout(num, crate::tests::TEST_PREPARATION_TIMEOUT)
}
#[cfg(feature = "test-utils")]
pub fn from_discriminator_precheck(num: u32) -> Self {
let mut pvf =
Self::from_discriminator_and_timeout(num, crate::tests::TEST_PREPARATION_TIMEOUT);
pvf.prep_kind = PrepareJobKind::Prechecking;
pvf
}
}
impl fmt::Debug for PvfPrepData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Pvf {{ code: [...], code_hash: {:?}, executor_params: {:?}, prep_timeout: {:?} }}",
self.code_hash, self.executor_params, self.prep_timeout
)
}
}
impl PartialEq for PvfPrepData {
fn eq(&self, other: &Self) -> bool {
self.code_hash == other.code_hash &&
self.executor_params.hash() == other.executor_params.hash()
}
}
impl Eq for PvfPrepData {}