referrerpolicy=no-referrer-when-downgrade

polkadot_node_core_pvf/
lib.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17#![warn(missing_docs)]
18
19//! The PVF validation host. Responsible for coordinating preparation and execution of PVFs.
20//!
21//! For more background, refer to the Implementer's Guide: [PVF
22//! Pre-checking](https://paritytech.github.io/polkadot-sdk/book/pvf-prechecking.html), [Candidate
23//! Validation](https://paritytech.github.io/polkadot-sdk/book/node/utility/candidate-validation.html)
24//! and [PVF Host and Workers](https://paritytech.github.io/polkadot-sdk/book/node/utility/pvf-host-and-workers.html).
25//!
26//!
27//! # Entrypoint
28//!
29//! This crate provides a simple API. You first [`start`] the validation host, which gives you the
30//! [handle][`ValidationHost`] and the future you need to poll.
31//!
32//! Then using the handle the client can send three types of requests:
33//!
34//! (a) PVF pre-checking. This takes the `Pvf` code and tries to prepare it (verify and
35//! compile) in order to pre-check its validity.
36//!
37//! (b) PVF execution. This accepts the PVF
38//! [`params`][`polkadot_parachain_primitives::primitives::ValidationParams`]     and the `Pvf`
39//! code, prepares (verifies and compiles) the code, and then executes PVF     with the `params`.
40//!
41//! (c) Heads up. This request allows to signal that the given PVF may be needed soon and that it
42//!     should be prepared for execution.
43//!
44//! The preparation results are cached for some time after they either used or was signaled in heads
45//! up. All requests that depends on preparation of the same PVF are bundled together and will be
46//! executed as soon as the artifact is prepared.
47//!
48//! # Priority
49//!
50//! PVF execution requests can specify the [priority][`Priority`] with which the given request
51//! should be handled. Different priority levels have different effects. This is discussed below.
52//!
53//! Preparation started by a heads up signal always starts with the background priority. If there
54//! is already a request for that PVF preparation under way the priority is inherited. If after
55//! heads up, a new PVF execution request comes in with a higher priority, then the original task's
56//! priority will be adjusted to match the new one if it's larger.
57//!
58//! Priority can never go down, only up.
59//!
60//! # Under the hood
61//!
62//! ## The flow
63//!
64//! Under the hood, the validation host is built using a bunch of communicating processes, not
65//! dissimilar to actors. Each of such "processes" is a future task that contains an event loop that
66//! processes incoming messages, potentially delegating sub-tasks to other "processes".
67//!
68//! Two of these processes are queues. The first one is for preparation jobs and the second one is
69//! for execution. Both of the queues are backed by separate pools of workers of different kind.
70//!
71//! Preparation workers handle preparation requests by prevalidating and instrumenting PVF wasm
72//! code, and then passing it into the compiler, to prepare the artifact.
73//!
74//! ## Artifacts
75//!
76//! An artifact is the final product of preparation. If the preparation succeeded, then the artifact
77//! will contain the compiled code usable for quick execution by a worker later on. If the
78//! preparation failed, then no artifact is created.
79//!
80//! The artifact is saved on disk and is also tracked by an in memory table. This in memory table
81//! doesn't contain the artifact contents though, only a flag for the state of the given artifact
82//! and some associated data. If the artifact failed to process, this also includes the error.
83//!
84//! A pruning task will run at a fixed interval of time. This task will remove all artifacts that
85//! weren't used or received a heads up signal for a while.
86//!
87//! ## Execution
88//!
89//! The execute workers will be fed by the requests from the execution queue, which is basically a
90//! combination of a path to the compiled artifact and the
91//! [`params`][`polkadot_parachain_primitives::primitives::ValidationParams`].
92
93mod artifacts;
94mod error;
95mod execute;
96mod host;
97mod metrics;
98mod prepare;
99mod priority;
100#[cfg(target_os = "linux")]
101mod security;
102mod worker_interface;
103
104#[cfg(feature = "test-utils")]
105pub mod testing;
106
107pub use error::{InvalidCandidate, PossiblyInvalidError, ValidationError};
108pub use host::{
109	start, Config, ValidationHost, EXECUTE_BINARY_NAME, HOST_MESSAGE_QUEUE_SIZE,
110	PREPARE_BINARY_NAME,
111};
112pub use metrics::Metrics;
113pub use priority::Priority;
114pub use worker_interface::{framed_recv, framed_send, JOB_TIMEOUT_WALL_CLOCK_FACTOR};
115
116// Re-export some common types.
117pub use polkadot_node_core_pvf_common::{
118	error::{InternalValidationError, PrepareError},
119	prepare::{PrepareJobKind, PrepareStats},
120	pvf::PvfPrepData,
121	SecurityStatus,
122};
123
124use std::{path::Path, process::Command};
125
126/// The log target for this crate.
127pub const LOG_TARGET: &str = "parachain::pvf";
128
129/// Utility to get the version of a worker, used for version checks.
130///
131/// The worker's existence at the given path must be checked separately.
132pub fn get_worker_version(worker_path: &Path) -> std::io::Result<String> {
133	let worker_version = Command::new(worker_path).args(["--version"]).output()?.stdout;
134	Ok(std::str::from_utf8(&worker_version)
135		.expect("version is printed as a string; qed")
136		.trim()
137		.to_string())
138}
139
140// Trying to run securely and some mandatory errors occurred.
141pub(crate) const SECURE_MODE_ERROR: &'static str =
142	"🚨 Your system cannot securely run a validator. \
143\nRunning validation of malicious PVF code has a higher risk of compromising this machine.";
144// Some errors occurred when running insecurely, or some optional errors occurred when running
145// securely.
146pub(crate) const SECURE_MODE_WARNING: &'static str = "🚨 Some security issues have been detected. \
147\nRunning validation of malicious PVF code has a higher risk of compromising this machine.";
148// Message to be printed only when running securely and mandatory errors occurred.
149pub(crate) const IGNORE_SECURE_MODE_TIP: &'static str =
150"\nYou can ignore this error with the `--insecure-validator-i-know-what-i-do` \
151command line argument if you understand and accept the risks of running insecurely. \
152With this flag, security features are enabled on a best-effort basis, but not mandatory. \
153\nMore information: https://docs.polkadot.com/infrastructure/running-a-validator/operational-tasks/general-management/#secure-your-validator";
154// Only Linux supports security features
155#[cfg(not(target_os = "linux"))]
156pub(crate) const SECURE_LINUX_NOTE: &'static str = "\nSecure mode is enabled only for Linux \
157\nand a full secure mode is enabled only for Linux x86-64.";