This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add PVF module documentation #6293
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0149532
Add PVF module documentation
mrcnski 82beedc
Update node/core/pvf/src/lib.rs
mrcnski 79e3196
Clarify meaning of PVF acronym
mrcnski c8f7b53
Move PVF doc to implementer's guide
mrcnski 55c1183
Clean up implementer's guide a bit
mrcnski 8b1f3b7
Add page for PVF types
mrcnski 175504e
pvf: Better separation between crate docs and implementer's guide
mrcnski e90c68c
ci: Add "prevalidating" to the dictionary
mrcnski 4c0042b
ig: Remove types/chain.md
mrcnski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# PVF | ||
|
||
The `pvf` module is responsible for handling preparation and execution subtasks | ||
for PVF code blobs. | ||
|
||
## Entrypoint | ||
|
||
This crate provides a simple API. You first `start` the validation host, which | ||
gives you the [handle][ValidationHost] and the future you need to poll. | ||
|
||
Then using the handle the client can send three types of requests: | ||
|
||
(a) PVF pre-checking. This takes the PVF [code][Pvf] and tries to prepare it | ||
(verify and compile) in order to pre-check its validity. | ||
|
||
(b) PVF execution. This accepts the PVF [`params`][ValidationParams] and the PVF | ||
[code][Pvf], prepares (verifies and compiles) the code, and then executes PVF | ||
with the `params`. | ||
|
||
(c) Heads up. This request allows to signal that the given PVF may be needed | ||
soon and that it should be prepared for execution. | ||
|
||
The preparation results are cached for some time after they either used or was | ||
signaled in heads up. All requests that depends on preparation of the same PVF | ||
are bundled together and will be executed as soon as the artifact is prepared. | ||
|
||
## Priority | ||
|
||
PVF execution requests can specify the [priority][Priority] with which the | ||
given request should be handled. Different priority levels have different | ||
effects. This is discussed below. | ||
|
||
Preparation started by a heads up signal always starts with the background | ||
priority. If there is already a request for that PVF preparation under way the | ||
priority is inherited. If after heads up, a new PVF execution request comes in | ||
with a higher priority, then the original task's priority will be adjusted to | ||
match the new one if it's larger. | ||
|
||
Priority can never go down, only up. | ||
|
||
## Mitigating disputes | ||
|
||
### Retrying execution requests | ||
|
||
If the execution request fails during **preparation**, we will retry if it is | ||
possible that the preparation error was transient (i.e. it was of type | ||
`PrepareError::Panic`, `PrepareError::TimedOut`, or | ||
`PrepareError::DidNotMakeIt`). We will only retry preparation if another | ||
requests comes in after 15 minutes, to ensure any potential transient conditions | ||
had time to be resolved. We will retry up to 5 times. See | ||
`can_retry_prepare_after_failure`. | ||
|
||
If the actual **execution** of the artifact fails, we will retry once if it was | ||
an `InvalidCandidate::AmbiguousWorkerDeath` error, after a 1 second delay to | ||
allow any potential transient conditions to clear. This occurs outside this | ||
module, in the Candidate Validation subsystem. | ||
|
||
### Preparation timeouts | ||
|
||
We use a timeout for preparation to limit the amount of time it can take. As the | ||
time for preparation can vary depending on the machine and load on the machine, | ||
this can potentially lead to disputes where some validators are able to execute | ||
a PVF and others aren't. | ||
|
||
One mitigation we have in place is a more lenient timeout for preparation during | ||
execution than during pre-checking. The rationale is that the PVF has already | ||
passed pre-checking, so we know it should be valid, and we allow it to take | ||
longer than expected as this is likely due to an issue with the machine and not | ||
the PVF. | ||
|
||
## Under the hood | ||
|
||
### The flow | ||
|
||
Under the hood, the validation host is built using a bunch of communicating | ||
processes, not dissimilar to actors. Each of such "processes" is a future task | ||
that contains an event loop that processes incoming messages, potentially | ||
delegating sub-tasks to other "processes". | ||
|
||
Two of these processes are queues. The first one is for preparation jobs and the | ||
second one is for execution. Both of the queues are backed by separate pools of | ||
workers of different kind. | ||
|
||
Preparation workers handle preparation requests by prevalidating and | ||
instrumenting PVF wasm code, and then passing it into the compiler, to prepare | ||
the artifact. | ||
|
||
### Artifacts | ||
|
||
An artifact is the final product of preparation. If the preparation succeeded, | ||
then the artifact will contain the compiled code usable for quick execution by a | ||
worker later on. | ||
|
||
If the preparation failed, then the worker will still write the artifact with | ||
the error message. We save the artifact with the error so that we don't try to | ||
prepare the artifacts that are broken repeatedly. | ||
|
||
The artifact is saved on disk and is also tracked by an in memory table. This in | ||
memory table doesn't contain the artifact contents though, only a flag that the | ||
given artifact is compiled. | ||
|
||
A pruning task will run at a fixed interval of time. This task will remove all | ||
artifacts that weren't used or received a heads up signal for a while. | ||
|
||
### Execution | ||
|
||
The execute workers will be fed by the requests from the execution queue, which | ||
is basically a combination of a path to the compiled artifact and the | ||
[`params`][ValidationParams]. | ||
|
||
[ValidationHost]: ../../types/pvf.md#validationhost | ||
[Pvf]: ../../types/pvf.md#pvf | ||
[ValidationParams]: ../../types/candidate.md#validationparams | ||
[Priority]: ../../types/pvf.md#priority |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
Types pertaining to the relay-chain - events, structures, etc. | ||
|
||
TODO: These no longer exist. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can go ahead and drop it then |
||
|
||
## Block Import Event | ||
|
||
```rust | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# PVF Types | ||
|
||
## `ValidationHost` | ||
|
||
```rust | ||
/// A handle to the async process serving the validation host requests. | ||
pub struct ValidationHost { | ||
to_host_tx: mpsc::Sender<ToHost>, | ||
} | ||
``` | ||
|
||
## `Pvf` | ||
|
||
```rust | ||
/// A struct that carries code of a parachain validation function and its hash. | ||
pub struct Pvf { | ||
pub(crate) code: Arc<Vec<u8>>, | ||
pub(crate) code_hash: ValidationCodeHash, | ||
} | ||
``` | ||
|
||
## `Priority` | ||
|
||
```rust | ||
/// A priority assigned to execution of a PVF. | ||
pub enum Priority { | ||
/// Normal priority for things that do not require immediate response, but still need to be | ||
/// done pretty quick. | ||
/// | ||
/// Approvals and disputes fall into this category. | ||
Normal, | ||
/// This priority is used for requests that are required to be processed as soon as possible. | ||
/// | ||
/// For example, backing is on a critical path and requires execution as soon as possible. | ||
Critical, | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we really go with 1 second? Andronik suggested 3 seconds back then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh oops, I saw that this was resolved so I didn't make the change. I should have read more closely, because it sounds like we do want to go with 3 seconds. I can raise a separate PR, LMK.