polkadot_node_core_pvf_common/
pvf.rs1use crate::prepare::PrepareJobKind;
18use codec::{Decode, Encode};
19use polkadot_parachain_primitives::primitives::ValidationCodeHash;
20use polkadot_primitives::ExecutorParams;
21use std::{fmt, sync::Arc, time::Duration};
22
23#[derive(Clone, Encode, Decode)]
28pub struct PvfPrepData {
29	maybe_compressed_code: Arc<Vec<u8>>,
31	validation_code_bomb_limit: u32,
33	code_hash: ValidationCodeHash,
35	executor_params: Arc<ExecutorParams>,
37	prep_timeout: Duration,
39	prep_kind: PrepareJobKind,
41}
42
43impl PvfPrepData {
44	pub fn from_code(
46		code: Vec<u8>,
47		executor_params: ExecutorParams,
48		prep_timeout: Duration,
49		prep_kind: PrepareJobKind,
50		validation_code_bomb_limit: u32,
51	) -> Self {
52		let maybe_compressed_code = Arc::new(code);
53		let code_hash = sp_crypto_hashing::blake2_256(&maybe_compressed_code).into();
54		let executor_params = Arc::new(executor_params);
55		Self {
56			maybe_compressed_code,
57			code_hash,
58			executor_params,
59			prep_timeout,
60			prep_kind,
61			validation_code_bomb_limit,
62		}
63	}
64
65	pub fn code_hash(&self) -> ValidationCodeHash {
67		self.code_hash
68	}
69
70	pub fn maybe_compressed_code(&self) -> Arc<Vec<u8>> {
72		self.maybe_compressed_code.clone()
73	}
74
75	pub fn executor_params(&self) -> Arc<ExecutorParams> {
77		self.executor_params.clone()
78	}
79
80	pub fn prep_timeout(&self) -> Duration {
82		self.prep_timeout
83	}
84
85	pub fn prep_kind(&self) -> PrepareJobKind {
87		self.prep_kind
88	}
89
90	pub fn validation_code_bomb_limit(&self) -> u32 {
92		self.validation_code_bomb_limit
93	}
94
95	#[cfg(feature = "test-utils")]
97	pub fn from_discriminator_and_timeout(num: u32, timeout: Duration) -> Self {
98		let discriminator_buf = num.to_le_bytes().to_vec();
99		Self::from_code(
100			discriminator_buf,
101			ExecutorParams::default(),
102			timeout,
103			PrepareJobKind::Compilation,
104			30 * 1024 * 1024,
105		)
106	}
107
108	#[cfg(feature = "test-utils")]
110	pub fn from_discriminator(num: u32) -> Self {
111		Self::from_discriminator_and_timeout(num, crate::tests::TEST_PREPARATION_TIMEOUT)
112	}
113
114	#[cfg(feature = "test-utils")]
116	pub fn from_discriminator_precheck(num: u32) -> Self {
117		let mut pvf =
118			Self::from_discriminator_and_timeout(num, crate::tests::TEST_PREPARATION_TIMEOUT);
119		pvf.prep_kind = PrepareJobKind::Prechecking;
120		pvf
121	}
122}
123
124impl fmt::Debug for PvfPrepData {
125	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126		write!(
127			f,
128			"Pvf {{ code: [...], code_hash: {:?}, executor_params: {:?}, prep_timeout: {:?} }}",
129			self.code_hash, self.executor_params, self.prep_timeout
130		)
131	}
132}
133
134impl PartialEq for PvfPrepData {
135	fn eq(&self, other: &Self) -> bool {
136		self.code_hash == other.code_hash &&
137			self.executor_params.hash() == other.executor_params.hash()
138	}
139}
140
141impl Eq for PvfPrepData {}