referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_pvf_common/
pvf.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17use 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/// A struct that carries the exhaustive set of data to prepare an artifact out of plain
24/// Wasm binary
25///
26/// Should be cheap to clone.
27#[derive(Clone, Encode, Decode)]
28pub struct PvfPrepData {
29	/// Wasm code (maybe compressed)
30	maybe_compressed_code: Arc<Vec<u8>>,
31	/// Maximum uncompressed code size.
32	validation_code_bomb_limit: u32,
33	/// Wasm code hash.
34	code_hash: ValidationCodeHash,
35	/// Executor environment parameters for the session for which artifact is prepared
36	executor_params: Arc<ExecutorParams>,
37	/// Preparation timeout
38	prep_timeout: Duration,
39	/// The kind of preparation job.
40	prep_kind: PrepareJobKind,
41}
42
43impl PvfPrepData {
44	/// Returns an instance of the PVF out of the given PVF code and executor params.
45	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	/// Returns validation code hash
66	pub fn code_hash(&self) -> ValidationCodeHash {
67		self.code_hash
68	}
69
70	/// Returns PVF code blob
71	pub fn maybe_compressed_code(&self) -> Arc<Vec<u8>> {
72		self.maybe_compressed_code.clone()
73	}
74
75	/// Returns executor params
76	pub fn executor_params(&self) -> Arc<ExecutorParams> {
77		self.executor_params.clone()
78	}
79
80	/// Returns preparation timeout.
81	pub fn prep_timeout(&self) -> Duration {
82		self.prep_timeout
83	}
84
85	/// Returns preparation kind.
86	pub fn prep_kind(&self) -> PrepareJobKind {
87		self.prep_kind
88	}
89
90	/// Returns validation code bomb limit.
91	pub fn validation_code_bomb_limit(&self) -> u32 {
92		self.validation_code_bomb_limit
93	}
94
95	/// Creates a structure for tests.
96	#[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	/// Creates a structure for tests.
109	#[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	/// Creates a structure for tests.
115	#[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 {}