Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include polkadot version in artifact path #1828

Merged
merged 5 commits into from
Oct 15, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions polkadot/node/core/pvf/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use crate::host::PrepareResultSender;
use always_assert::always;
use polkadot_node_core_pvf_common::{error::PrepareError, prepare::PrepareStats, pvf::PvfPrepData};
use polkadot_node_primitives::NODE_VERSION;
use polkadot_parachain_primitives::primitives::ValidationCodeHash;
use polkadot_primitives::ExecutorParamsHash;
use std::{
Expand All @@ -75,6 +76,8 @@ pub struct ArtifactId {

impl ArtifactId {
const PREFIX: &'static str = "wasmtime_";
// FIXME or just "v" instead?
const NODE_VERSION_PREFIX: &'static str = "polkadot_v";
eagr marked this conversation as resolved.
Show resolved Hide resolved

/// Creates a new artifact ID with the given hash.
pub fn new(code_hash: ValidationCodeHash, executor_params_hash: ExecutorParamsHash) -> Self {
Expand All @@ -92,8 +95,13 @@ impl ArtifactId {
use polkadot_core_primitives::Hash;
use std::str::FromStr as _;

let file_name = file_name.strip_prefix(Self::PREFIX)?;
let (code_hash_str, executor_params_hash_str) = file_name.split_once('_')?;
let file_name =
file_name.strip_prefix(Self::PREFIX)?.strip_prefix(Self::NODE_VERSION_PREFIX)?;

// [ node version | code hash | param hash ]
let parts: Vec<&str> = file_name.split('_').collect();
let (_node_ver, code_hash_str, executor_params_hash_str) = (parts[0], parts[1], parts[2]);

let code_hash = Hash::from_str(code_hash_str).ok()?.into();
let executor_params_hash =
ExecutorParamsHash::from_hash(Hash::from_str(executor_params_hash_str).ok()?);
Expand All @@ -103,8 +111,14 @@ impl ArtifactId {

/// Returns the expected path to this artifact given the root of the cache.
pub fn path(&self, cache_path: &Path) -> PathBuf {
let file_name =
format!("{}{:#x}_{:#x}", Self::PREFIX, self.code_hash, self.executor_params_hash);
let file_name = format!(
"{}{}{}_{:#x}_{:#x}",
Self::PREFIX,
Self::NODE_VERSION_PREFIX,
NODE_VERSION,
self.code_hash,
self.executor_params_hash
);
cache_path.join(file_name)
}
}
Expand Down Expand Up @@ -253,20 +267,27 @@ impl Artifacts {

#[cfg(test)]
mod tests {
use super::{ArtifactId, Artifacts};
use super::{ArtifactId, Artifacts, NODE_VERSION};
use polkadot_primitives::ExecutorParamsHash;
use sp_core::H256;
use std::{path::Path, str::FromStr};

fn file_name(code_hash: &str, param_hash: &str) -> String {
format!("wasmtime_polkadot_v{}_0x{}_0x{}", NODE_VERSION, code_hash, param_hash)
}

#[test]
fn from_file_name() {
assert!(ArtifactId::from_file_name("").is_none());
assert!(ArtifactId::from_file_name("junk").is_none());

let file_name = file_name(
"0022800000000000000000000000000000000000000000000000000000000000",
"0033900000000000000000000000000000000000000000000000000000000000",
);

assert_eq!(
ArtifactId::from_file_name(
"wasmtime_0x0022800000000000000000000000000000000000000000000000000000000000_0x0033900000000000000000000000000000000000000000000000000000000000"
),
ArtifactId::from_file_name(&file_name),
Some(ArtifactId::new(
hex_literal::hex![
"0022800000000000000000000000000000000000000000000000000000000000"
Expand All @@ -281,16 +302,17 @@ mod tests {

#[test]
fn path() {
let path = Path::new("/test");
let hash =
H256::from_str("1234567890123456789012345678901234567890123456789012345678901234")
.unwrap();
let dir = Path::new("/test");
let hash = "1234567890123456789012345678901234567890123456789012345678901234";
let file_name = file_name(hash, hash);
eagr marked this conversation as resolved.
Show resolved Hide resolved

let hash = H256::from_str(hash).unwrap();

assert_eq!(
ArtifactId::new(hash.into(), ExecutorParamsHash::from_hash(hash)).path(path).to_str(),
Some(
"/test/wasmtime_0x1234567890123456789012345678901234567890123456789012345678901234_0x1234567890123456789012345678901234567890123456789012345678901234"
),
ArtifactId::new(hash.into(), ExecutorParamsHash::from_hash(hash))
.path(dir)
.to_str(),
Some(format!("/test/{}", file_name).as_str()),
);
}

Expand Down
Loading