-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathengine.rs
More file actions
78 lines (68 loc) · 2.99 KB
/
engine.rs
File metadata and controls
78 lines (68 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Engine definition trait and supporting types.
use super::tracker::FinalizationUpdate;
use commonware_cryptography::PublicKey;
use commonware_p2p::simulated::{self, Oracle};
use commonware_runtime::{deterministic, Handle, Quota};
use commonware_utils::channel::mpsc;
use std::future::Future;
/// A registered p2p channel pair (sender, receiver).
pub type ChannelPair<P> = (
simulated::Sender<P, deterministic::Context>,
simulated::Receiver<P>,
);
/// Arguments passed to [`EngineDefinition::init`].
pub struct InitContext<'a, P: PublicKey> {
/// Labeled runtime context for this validator.
pub context: deterministic::Context,
/// Index of this validator in the participant list.
pub index: usize,
/// This validator's public key.
pub public_key: &'a P,
/// Network oracle for peer management.
pub oracle: &'a Oracle<P, deterministic::Context>,
/// Registered p2p channel pairs (same order as `channels()`).
pub channels: Vec<ChannelPair<P>>,
/// All participants in the simulation.
pub participants: &'a [P],
/// Channel for reporting finalization events to the harness.
pub monitor: mpsc::Sender<FinalizationUpdate<P>>,
}
/// Defines how to construct and start one validator's service stack.
///
/// The harness calls these methods for each validator in the simulation.
/// The lifecycle is:
/// 1. `channels()` -- declare which p2p channels are needed.
/// 2. `init()` -- construct the engine (actors, archives, mailboxes).
/// 3. `start()` -- start all actors, return a joinable handle.
///
/// On restart after a crash, `init()` and `start()` are called again
/// with the same validator identity but a fresh runtime context (storage
/// state is preserved by the deterministic runtime).
pub trait EngineDefinition: Clone + Send + 'static {
/// The public key type used by this engine.
type PublicKey: PublicKey;
/// The constructed engine, passed from `init` to `start`.
type Engine: Send + 'static;
/// Per-validator state inspectable by property checkers.
type State: Send + Sync + 'static;
/// The participants for this simulation.
///
/// Called once by the harness to determine the validator set. The engine
/// is responsible for generating keys and any associated state (signing
/// schemes, databases, etc.) during construction.
fn participants(&self) -> Vec<Self::PublicKey>;
/// Which p2p channels to register for each validator.
///
/// Returns `(channel_id, quota)` pairs. The harness registers each
/// on the simulated oracle and passes sender/receiver pairs to
/// `init` in the same order.
fn channels(&self) -> Vec<(u64, Quota)>;
/// Construct the engine for a single validator.
fn init(
&self,
ctx: InitContext<'_, Self::PublicKey>,
) -> impl Future<Output = (Self::Engine, Self::State)> + Send;
/// Start all actors in the engine. Returns a handle the harness
/// can join on (or abort on crash).
fn start(engine: Self::Engine) -> Handle<()>;
}