Skip to content

Commit

Permalink
Make sure pre_encoded_payloads tests the correct type
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnski committed Nov 6, 2023
1 parent af96dfa commit 67736c4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
11 changes: 0 additions & 11 deletions polkadot/node/core/pvf/common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ pub enum PrepareError {
Kernel(String),
}

/// Pre-encoded length-prefixed `PrepareResult::Err(PrepareError::OutOfMemory)`
pub const OOM_PAYLOAD: &[u8] = b"\x02\x00\x00\x00\x00\x00\x00\x00\x01\x08";

impl PrepareError {
/// Returns whether this is a deterministic error, i.e. one that should trigger reliably. Those
/// errors depend on the PVF itself and the sc-executor/wasmtime logic.
Expand Down Expand Up @@ -174,11 +171,3 @@ impl fmt::Display for InternalValidationError {
}
}
}

#[test]
fn pre_encoded_payloads() {
let oom_enc = PrepareResult::Err(PrepareError::OutOfMemory).encode();
let mut oom_payload = oom_enc.len().to_le_bytes().to_vec();
oom_payload.extend(oom_enc);
assert_eq!(oom_payload, OOM_PAYLOAD);
}
18 changes: 16 additions & 2 deletions polkadot/node/core/pvf/prepare-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use nix::{
use os_pipe::{self, PipeWriter};
use parity_scale_codec::{Decode, Encode};
use polkadot_node_core_pvf_common::{
error::{PrepareError, PrepareResult, OOM_PAYLOAD},
error::{PrepareError, PrepareResult},
executor_intf::create_runtime_from_artifact_bytes,
framed_recv_blocking, framed_send_blocking,
prepare::{MemoryStats, PrepareJobKind, PrepareStats},
Expand Down Expand Up @@ -614,7 +614,7 @@ fn get_total_cpu_usage(rusage: Usage) -> Duration {
/// - `pipe_write`: A `os_pipe::PipeWriter` structure, the writing end of a pipe.
///
/// - `response`: Child process response
fn send_child_response(mut pipe_write: &PipeWriter, response: Result<Response, PrepareError>) -> ! {
fn send_child_response(mut pipe_write: &PipeWriter, response: JobResponse) -> ! {
pipe_write
.write_all(response.encode().as_slice())
.unwrap_or_else(|_| process::exit(libc::EXIT_FAILURE));
Expand All @@ -625,3 +625,17 @@ fn send_child_response(mut pipe_write: &PipeWriter, response: Result<Response, P
fn error_from_errno(context: &'static str, errno: Errno) -> PrepareError {
PrepareError::Kernel(format!("{}: {}: {}", context, errno, io::Error::last_os_error()))
}

type JobResponse = Result<Response, PrepareError>;

/// Pre-encoded length-prefixed `Result::Err(PrepareError::OutOfMemory)`
const OOM_PAYLOAD: &[u8] = b"\x02\x00\x00\x00\x00\x00\x00\x00\x01\x08";

#[test]
fn pre_encoded_payloads() {
// NOTE: This must match the type of `response` in `send_child_response`.
let oom_enc: JobResponse = Result::Err(PrepareError::OutOfMemory).encode();
let mut oom_payload = oom_enc.len().to_le_bytes().to_vec();
oom_payload.extend(oom_enc);
assert_eq!(oom_payload, OOM_PAYLOAD);
}
2 changes: 1 addition & 1 deletion polkadot/node/core/pvf/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl TestHost {
}

#[tokio::test]
async fn terminates_on_timeout() {
async fn execute_job_terminates_on_timeout() {
let host = TestHost::new().await;

let start = std::time::Instant::now();
Expand Down

0 comments on commit 67736c4

Please sign in to comment.