Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a563e25
make pruning explicit
eagr Oct 17, 2023
f31d354
preserve cache unless stale
eagr Oct 17, 2023
195bbce
barely working
eagr Oct 17, 2023
99aa012
use ArtifactId::from_file_name()
eagr Oct 17, 2023
17973ef
ignore non-unicode file names
eagr Oct 17, 2023
e81b456
generalize concat_const!()
eagr Oct 18, 2023
851a77a
per advices
eagr Oct 18, 2023
a3b0fcf
break on IO error
eagr Oct 18, 2023
b323c28
make pruning sound
eagr Oct 18, 2023
a2808f8
log more events
eagr Oct 18, 2023
0657d41
refactor
eagr Oct 20, 2023
0384ae7
doc
eagr Oct 20, 2023
4b1bb0a
Refactor indentation
mrcnski Oct 21, 2023
a8bcce4
refactor
eagr Oct 21, 2023
50b7ccc
checksum poc
eagr Oct 22, 2023
b6c1a07
Revert "checksum poc"
eagr Oct 30, 2023
2ad5262
redo checksum p1
eagr Oct 30, 2023
3723806
p2
eagr Oct 30, 2023
e44c451
remove corrupted cache
eagr Oct 31, 2023
6c19164
diversify results
eagr Nov 1, 2023
dad5285
fix tests
eagr Nov 1, 2023
1ede201
fix pruning
eagr Nov 1, 2023
2d51b52
fix message serialization
eagr Nov 1, 2023
36a33e8
clean up
eagr Nov 1, 2023
69f6a44
retire path_prefix()
eagr Nov 2, 2023
d3254f5
improve test
eagr Nov 3, 2023
f4d22fa
Merge branch 'master' into preserve-art
mrcnski Nov 12, 2023
39448ff
Fix test
mrcnski Nov 12, 2023
3a2c1cd
cargo fmt
mrcnski Nov 12, 2023
a02cb06
as per advices
eagr Nov 13, 2023
53e4557
tag artifact with runtime version
eagr Nov 13, 2023
d4f3083
fix tests
eagr Nov 13, 2023
9d2142e
Merge branch 'master' into preserve-art
mrcnski Nov 14, 2023
931aae1
upstream build fn to substrate
eagr Nov 15, 2023
5de4e8e
glitch
eagr Nov 15, 2023
a0b71c5
wrong attribution
eagr Nov 15, 2023
c85adfe
as per suggestions
eagr Nov 17, 2023
b897f1c
glitch
eagr Nov 17, 2023
89ada31
prevent `cargo tree` from accessing network
eagr Nov 17, 2023
868426a
glitch
eagr Nov 17, 2023
652bec7
Merge branch 'master' into preserve-art
eagr Nov 19, 2023
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
Prev Previous commit
Next Next commit
checksum poc
  • Loading branch information
eagr committed Oct 22, 2023
commit 50b7ccc4b20947188585ce67a716a7de6e79c42a
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions polkadot/node/core/pvf/common/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct PrepareStats {
pub cpu_time_elapsed: std::time::Duration,
/// The observed memory statistics for the preparation job.
pub memory_stats: MemoryStats,
/// checksum
pub checksum: [u8; 32],
}

/// Helper struct to contain all the memory stats, including `MemoryAllocationStats` and, if
Expand Down
1 change: 1 addition & 0 deletions polkadot/node/core/pvf/prepare-worker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition.workspace = true
license.workspace = true

[dependencies]
blake3 = "1.5"
cfg-if = "1.0"
futures = "0.3.21"
gum = { package = "tracing-gum", path = "../../../gum" }
Expand Down
5 changes: 4 additions & 1 deletion polkadot/node/core/pvf/prepare-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ pub fn worker_entrypoint(
);
tokio::fs::write(&temp_artifact_dest, &artifact).await?;

Ok(PrepareStats { cpu_time_elapsed, memory_stats })
let mut gen_hash = blake3::hash(artifact.as_ref());
let checksum = gen_hash.finalize();

Ok(PrepareStats { cpu_time_elapsed, memory_stats, checksum })
},
}
},
Expand Down
46 changes: 42 additions & 4 deletions polkadot/node/core/pvf/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,59 @@ const RUNTIME_PREFIX: &str = "wasmtime_";
const NODE_PREFIX: &str = "polkadot_v";
const ARTIFACT_PREFIX: &str = concat_const!(RUNTIME_PREFIX, NODE_PREFIX, NODE_VERSION);

pub type ChecksumInner = [u8; 32];

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Checksum(ChecksumInner);

impl Checksum {
pub fn new() -> Self {
Self([0; 32])
}

pub fn update(&mut self, src: &ChecksumInner) {
self.0.copy_from_slice(src);
}
}

impl std::fmt::LowerHex for Checksum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for byte in self.0 {
write!(f, "{}", byte)?;
}
Ok(())
}
}

/// Identifier of an artifact. Encodes a code hash of the PVF and a hash of executor parameter set.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ArtifactId {
pub(crate) code_hash: ValidationCodeHash,
pub(crate) executor_params_hash: ExecutorParamsHash,
pub(crate) checksum: Checksum,
}

impl ArtifactId {
/// Creates a new artifact ID with the given hash.
pub fn new(code_hash: ValidationCodeHash, executor_params_hash: ExecutorParamsHash) -> Self {
Self { code_hash, executor_params_hash }
Self { code_hash, executor_params_hash, checksum: Checksum::new() }
}

/// Returns an artifact ID that corresponds to the PVF with given executor params.
pub fn from_pvf_prep_data(pvf: &PvfPrepData) -> Self {
Self::new(pvf.code_hash(), pvf.executor_params().hash())
}

pub fn update_checksum(&mut self, src: &ChecksumInner) {
self.checksum.update(src);
}

/// Returns the expected path to this artifact given the root of the cache.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should update the docstring.

pub fn path(&self, cache_path: &Path) -> PathBuf {
let file_name =
format!("{}_{:#x}_{:#x}", ARTIFACT_PREFIX, self.code_hash, self.executor_params_hash);
let file_name = format!(
"{}_{:#x}_{:#x}_{:#x}",
ARTIFACT_PREFIX, self.code_hash, self.executor_params_hash, self.checksum
);
cache_path.join(file_name)
}

Expand All @@ -153,7 +184,7 @@ impl ArtifactId {
let executor_params_hash =
ExecutorParamsHash::from_hash(Hash::from_str(executor_params_hash_str).ok()?);

Some(Self { code_hash, executor_params_hash })
Some(Self::new(code_hash, executor_params_hash))
}
}

Expand Down Expand Up @@ -371,6 +402,13 @@ impl Artifacts {
.is_none());
}

pub(crate) fn remove_artifact(
&mut self,
artifact_id: &ArtifactId,
) -> Option<(ArtifactId, ArtifactState)> {
self.inner.remove_entry(&artifact_id)
}

/// Remove and retrieve the artifacts from the table that are older than the supplied
/// Time-To-Live.
pub fn prune(&mut self, artifact_ttl: Duration) -> Vec<ArtifactId> {
Expand Down
15 changes: 10 additions & 5 deletions polkadot/node/core/pvf/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,9 +757,14 @@ async fn handle_prepare_done(
.await?;
}

*state = match result {
Ok(prepare_stats) =>
ArtifactState::Prepared { last_time_needed: SystemTime::now(), prepare_stats },
match result {
Ok(prepare_stats) => match artifacts.remove_artifact(&artifact_id) {
Some((mut id, _)) => {
id.update_checksum(&prepare_stats.checksum);
artifacts.insert_prepared(artifact_id, SystemTime::now(), prepare_stats);
},
None => unreachable!(),
},
Err(error) => {
let last_time_failed = SystemTime::now();
let num_failures = *num_failures + 1;
Expand All @@ -772,9 +777,9 @@ async fn handle_prepare_done(
"artifact preparation failed: {}",
error
);
ArtifactState::FailedToProcess { last_time_failed, num_failures, error }
*state = ArtifactState::FailedToProcess { last_time_failed, num_failures, error };
},
};
}

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion polkadot/node/core/pvf/src/prepare/worker_intf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ async fn handle_response(
artifact_path: PathBuf,
preparation_timeout: Duration,
) -> Outcome {
let PrepareStats { cpu_time_elapsed, memory_stats } = match result.clone() {
let PrepareStats { cpu_time_elapsed, memory_stats, .. } = match result.clone() {
Ok(result) => result,
// Timed out on the child. This should already be logged by the child.
Err(PrepareError::TimedOut) => return Outcome::TimedOut,
Expand Down