Skip to content

Commit fe26e9e

Browse files
[pvf-worker] Refactor execute request handling (#8908)
# Description Fixes #8886 PVF execution worker communication was organized into a single ExecuteRequest struct. This should improve performance: one encode/decode operation instead of four. Also, no more chance of ordering mistakes. ## Integration This is an internal refactoring of the PVF execution worker. Downstream projects will not need any code changes. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent b046a67 commit fe26e9e

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

polkadot/node/core/pvf/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ thiserror = { workspace = true }
2121

2222
codec = { features = ["derive"], workspace = true }
2323

24+
polkadot-node-primitives = { workspace = true, default-features = true }
2425
polkadot-parachain-primitives = { workspace = true, default-features = true }
2526
polkadot-primitives = { workspace = true, default-features = true }
2627

polkadot/node/core/pvf/common/src/execute.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

17-
use crate::error::InternalValidationError;
17+
use crate::{error::InternalValidationError, ArtifactChecksum};
1818
use codec::{Decode, Encode};
19+
use polkadot_node_primitives::PoV;
1920
use polkadot_parachain_primitives::primitives::ValidationResult;
20-
use polkadot_primitives::ExecutorParams;
21+
use polkadot_primitives::{ExecutorParams, PersistedValidationData};
2122
use std::time::Duration;
2223

2324
/// The payload of the one-time handshake that is done when a worker process is created. Carries
@@ -28,6 +29,19 @@ pub struct Handshake {
2829
pub executor_params: ExecutorParams,
2930
}
3031

32+
/// A request to execute a PVF
33+
#[derive(Encode, Decode)]
34+
pub struct ExecuteRequest {
35+
/// Persisted validation data.
36+
pub pvd: PersistedValidationData,
37+
/// Proof-of-validity.
38+
pub pov: PoV,
39+
/// Execution timeout.
40+
pub execution_timeout: Duration,
41+
/// Checksum of the artifact to execute.
42+
pub artifact_checksum: ArtifactChecksum,
43+
}
44+
3145
/// The response from the execution worker.
3246
#[derive(Debug, Encode, Decode)]
3347
pub struct WorkerResponse {

polkadot/node/core/pvf/execute-worker/src/lib.rs

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ use nix::{
4141
use polkadot_node_core_pvf_common::{
4242
compute_checksum,
4343
error::InternalValidationError,
44-
execute::{Handshake, JobError, JobResponse, JobResult, WorkerError, WorkerResponse},
44+
execute::{
45+
ExecuteRequest, Handshake, JobError, JobResponse, JobResult, WorkerError, WorkerResponse,
46+
},
4547
executor_interface::params_to_wasmtime_semantics,
4648
framed_recv_blocking, framed_send_blocking,
4749
worker::{
@@ -91,40 +93,15 @@ fn recv_execute_handshake(stream: &mut UnixStream) -> io::Result<Handshake> {
9193
fn recv_request(
9294
stream: &mut UnixStream,
9395
) -> io::Result<(PersistedValidationData, PoV, Duration, ArtifactChecksum)> {
94-
let pvd = framed_recv_blocking(stream)?;
95-
let pvd = PersistedValidationData::decode(&mut &pvd[..]).map_err(|_| {
96-
io::Error::new(
97-
io::ErrorKind::Other,
98-
"execute pvf recv_request: failed to decode persisted validation data".to_string(),
99-
)
100-
})?;
101-
102-
let pov = framed_recv_blocking(stream)?;
103-
let pov = PoV::decode(&mut &pov[..]).map_err(|_| {
96+
let request_bytes = framed_recv_blocking(stream)?;
97+
let request = ExecuteRequest::decode(&mut &request_bytes[..]).map_err(|_| {
10498
io::Error::new(
10599
io::ErrorKind::Other,
106-
"execute pvf recv_request: failed to decode PoV".to_string(),
100+
"execute pvf recv_request: failed to decode ExecuteRequest".to_string(),
107101
)
108102
})?;
109103

110-
let execution_timeout = framed_recv_blocking(stream)?;
111-
let execution_timeout = Duration::decode(&mut &execution_timeout[..]).map_err(|_| {
112-
io::Error::new(
113-
io::ErrorKind::Other,
114-
"execute pvf recv_request: failed to decode duration".to_string(),
115-
)
116-
})?;
117-
118-
let artifact_checksum = framed_recv_blocking(stream)?;
119-
let artifact_checksum =
120-
ArtifactChecksum::decode(&mut &artifact_checksum[..]).map_err(|_| {
121-
io::Error::new(
122-
io::ErrorKind::Other,
123-
"execute pvf recv_request: failed to decode artifact checksum".to_string(),
124-
)
125-
})?;
126-
127-
Ok((pvd, pov, execution_timeout, artifact_checksum))
104+
Ok((request.pvd, request.pov, request.execution_timeout, request.artifact_checksum))
128105
}
129106

130107
/// Sends an error to the host and returns the original error wrapped in `io::Error`.

polkadot/node/core/pvf/src/execute/worker_interface.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,13 @@ async fn send_request(
295295
execution_timeout: Duration,
296296
artifact_checksum: ArtifactChecksum,
297297
) -> io::Result<()> {
298-
framed_send(stream, &pvd.encode()).await?;
299-
framed_send(stream, &pov.encode()).await?;
300-
framed_send(stream, &execution_timeout.encode()).await?;
301-
framed_send(stream, &artifact_checksum.encode()).await
298+
let request = polkadot_node_core_pvf_common::execute::ExecuteRequest {
299+
pvd: (*pvd).clone(),
300+
pov: (*pov).clone(),
301+
execution_timeout,
302+
artifact_checksum,
303+
};
304+
framed_send(stream, &request.encode()).await
302305
}
303306

304307
async fn recv_result(stream: &mut UnixStream) -> io::Result<Result<WorkerResponse, WorkerError>> {

prdoc/pr_8908.prdoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
title: '[pvf-worker] Refactor execute request handling'
2+
doc:
3+
- audience: Node Dev
4+
description: |-
5+
PVF execution worker communication was organized into a single ExecuteRequest struct. This should improve performance: one encode/decode operation instead of four. Also, no more chance of ordering mistakes.
6+
7+
crates:
8+
- name: polkadot-node-core-pvf-common
9+
bump: minor
10+
- name: polkadot-node-core-pvf-execute-worker
11+
bump: patch
12+
- name: polkadot-node-core-pvf
13+
bump: patch

0 commit comments

Comments
 (0)