Use PVF code paired with executor params wherever possible (#6742)

This commit is contained in:
s0me0ne-unkn0wn
2023-02-20 13:41:46 +01:00
committed by GitHub
parent bad4afca36
commit b13ba77a3b
11 changed files with 88 additions and 100 deletions
+43 -45
View File
@@ -15,81 +15,79 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use crate::artifacts::ArtifactId;
use parity_scale_codec::{Decode, Encode};
use polkadot_parachain::primitives::ValidationCodeHash;
use polkadot_primitives::vstaging::ExecutorParams;
use sp_core::blake2_256;
use std::{fmt, sync::Arc};
use std::{
cmp::{Eq, PartialEq},
fmt,
sync::Arc,
};
/// A struct that carries code of a parachain validation function and its hash.
/// A struct that carries code of a parachain validation function, its hash, and a corresponding
/// set of executor parameters.
///
/// Should be cheap to clone.
#[derive(Clone)]
pub struct Pvf {
#[derive(Clone, Encode, Decode)]
pub struct PvfWithExecutorParams {
pub(crate) code: Arc<Vec<u8>>,
pub(crate) code_hash: ValidationCodeHash,
}
impl fmt::Debug for Pvf {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Pvf {{ code, code_hash: {:?} }}", self.code_hash)
}
}
impl Pvf {
/// Returns an instance of the PVF out of the given PVF code.
pub fn from_code(code: Vec<u8>) -> Self {
let code = Arc::new(code);
let code_hash = blake2_256(&code).into();
Self { code, code_hash }
}
/// Creates a new PVF which artifact id can be uniquely identified by the given number.
#[cfg(test)]
pub(crate) fn from_discriminator(num: u32) -> Self {
let descriminator_buf = num.to_le_bytes().to_vec();
Pvf::from_code(descriminator_buf)
}
}
/// Coupling PVF code with executor params
#[derive(Debug, Clone)]
pub struct PvfWithExecutorParams {
pvf: Pvf,
executor_params: Arc<ExecutorParams>,
pub(crate) executor_params: Arc<ExecutorParams>,
}
impl PvfWithExecutorParams {
/// Creates a new PVF-ExecutorParams pair structure
pub fn new(pvf: Pvf, executor_params: ExecutorParams) -> Self {
Self { pvf, executor_params: Arc::new(executor_params) }
/// Returns an instance of the PVF out of the given PVF code and executor params.
pub fn from_code(code: Vec<u8>, executor_params: ExecutorParams) -> Self {
let code = Arc::new(code);
let code_hash = blake2_256(&code).into();
let executor_params = Arc::new(executor_params);
Self { code, code_hash, executor_params }
}
/// Returns artifact ID that corresponds to the PVF with given executor params
pub(crate) fn as_artifact_id(&self) -> ArtifactId {
ArtifactId::new(self.pvf.code_hash, self.executor_params.hash())
ArtifactId::new(self.code_hash, self.executor_params.hash())
}
/// Returns validation code hash for the PVF
pub(crate) fn code_hash(&self) -> ValidationCodeHash {
self.pvf.code_hash
self.code_hash
}
/// Returns PVF code
pub(crate) fn code(&self) -> Arc<Vec<u8>> {
self.pvf.code.clone()
self.code.clone()
}
/// Returns executor params
pub(crate) fn executor_params(&self) -> ExecutorParams {
(*self.executor_params).clone()
pub(crate) fn executor_params(&self) -> Arc<ExecutorParams> {
self.executor_params.clone()
}
/// Creates a structure for tests
#[cfg(test)]
pub(crate) fn from_discriminator(num: u32) -> Self {
Self {
pvf: Pvf::from_discriminator(num),
executor_params: Arc::new(ExecutorParams::default()),
}
let descriminator_buf = num.to_le_bytes().to_vec();
Self::from_code(descriminator_buf, ExecutorParams::default())
}
}
impl fmt::Debug for PvfWithExecutorParams {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Pvf {{ code, code_hash: {:?}, executor_params: {:?} }}",
self.code_hash, self.executor_params
)
}
}
impl PartialEq for PvfWithExecutorParams {
fn eq(&self, other: &Self) -> bool {
self.code_hash == other.code_hash &&
self.executor_params.hash() == other.executor_params.hash()
}
}
impl Eq for PvfWithExecutorParams {}