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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use super::{
actors::{batcher, resolver, voter},
config::Config,
elector::Config as Elector,
types::{Activity, Context},
};
use crate::{
simplex::{scheme::Scheme, Plan},
CertifiableAutomaton, Relay, Reporter,
};
use commonware_cryptography::Digest;
use commonware_macros::select;
use commonware_p2p::{Blocker, Receiver, Sender};
use commonware_parallel::Strategy;
use commonware_runtime::{
spawn_cell, BufferPooler, Clock, ContextCell, Handle, Metrics, Spawner, Storage,
};
use rand_core::CryptoRngCore;
use tracing::debug;
/// Instance of `simplex` consensus engine.
pub struct Engine<
E: BufferPooler + Clock + CryptoRngCore + Spawner + Storage + Metrics,
S: Scheme<D>,
L: Elector<S>,
B: Blocker<PublicKey = S::PublicKey>,
D: Digest,
A: CertifiableAutomaton<Context = Context<D, S::PublicKey>, Digest = D>,
R: Relay<Digest = D, PublicKey = S::PublicKey, Plan = Plan<S::PublicKey>>,
F: Reporter<Activity = Activity<S, D>>,
T: Strategy,
> {
context: ContextCell<E>,
voter: voter::Actor<E, S, L, B, D, A, R, F>,
voter_mailbox: voter::Mailbox<S, D>,
batcher: batcher::Actor<E, S, B, D, F, R, T>,
batcher_mailbox: batcher::Mailbox<S, D>,
resolver: resolver::Actor<E, S, B, D, T>,
resolver_mailbox: resolver::Mailbox<S, D>,
}
impl<
E: BufferPooler + Clock + CryptoRngCore + Spawner + Storage + Metrics,
S: Scheme<D>,
L: Elector<S>,
B: Blocker<PublicKey = S::PublicKey>,
D: Digest,
A: CertifiableAutomaton<Context = Context<D, S::PublicKey>, Digest = D>,
R: Relay<Digest = D, PublicKey = S::PublicKey, Plan = Plan<S::PublicKey>>,
F: Reporter<Activity = Activity<S, D>>,
T: Strategy,
> Engine<E, S, L, B, D, A, R, F, T>
{
/// Create a new `simplex` consensus engine.
pub fn new(context: E, cfg: Config<S, L, B, D, A, R, F, T>) -> Self {
// Ensure configuration is valid
cfg.assert();
// Create batcher
let (batcher, batcher_mailbox) = batcher::Actor::new(
context.with_label("batcher"),
batcher::Config {
scheme: cfg.scheme.clone(),
blocker: cfg.blocker.clone(),
reporter: cfg.reporter.clone(),
relay: cfg.relay.clone(),
strategy: cfg.strategy.clone(),
epoch: cfg.epoch,
mailbox_size: cfg.mailbox_size,
activity_timeout: cfg.activity_timeout,
skip_timeout: cfg.skip_timeout,
forwarding: cfg.forwarding,
},
);
// Create voter
let (voter, voter_mailbox) = voter::Actor::new(
context.with_label("voter"),
voter::Config {
scheme: cfg.scheme.clone(),
elector: cfg.elector,
blocker: cfg.blocker.clone(),
automaton: cfg.automaton,
relay: cfg.relay,
reporter: cfg.reporter,
partition: cfg.partition,
mailbox_size: cfg.mailbox_size,
epoch: cfg.epoch,
leader_timeout: cfg.leader_timeout,
certification_timeout: cfg.certification_timeout,
timeout_retry: cfg.timeout_retry,
activity_timeout: cfg.activity_timeout,
replay_buffer: cfg.replay_buffer,
write_buffer: cfg.write_buffer,
page_cache: cfg.page_cache,
},
);
// Create resolver
let (resolver, resolver_mailbox) = resolver::Actor::new(
context.with_label("resolver"),
resolver::Config {
blocker: cfg.blocker,
scheme: cfg.scheme,
strategy: cfg.strategy,
mailbox_size: cfg.mailbox_size,
epoch: cfg.epoch,
fetch_concurrent: cfg.fetch_concurrent,
fetch_timeout: cfg.fetch_timeout,
},
);
// Return the engine
Self {
context: ContextCell::new(context),
voter,
voter_mailbox,
batcher,
batcher_mailbox,
resolver,
resolver_mailbox,
}
}
/// Start the `simplex` consensus engine.
///
/// This will also rebuild the state of the engine from provided `Journal`.
///
/// # Network Channels
///
/// The engine requires three separate network channels, each carrying votes or
/// certificates to help drive the consensus engine.
///
/// ## `vote_network`
///
/// Carries **individual votes**:
/// - [`Notarize`](super::types::Notarize): Vote to notarize a proposal
/// - [`Nullify`](super::types::Nullify): Vote to skip a view
/// - [`Finalize`](super::types::Finalize): Vote to finalize a notarized proposal
///
/// These messages are sent to the batcher, which performs batch signature
/// verification before forwarding valid votes to the voter for aggregation.
///
/// ## `certificate_network`
///
/// Carries **certificates**:
/// - [`Notarization`](super::types::Notarization): Proof that a proposal was notarized
/// - [`Nullification`](super::types::Nullification): Proof that a view was skipped
/// - [`Finalization`](super::types::Finalization): Proof that a proposal was finalized
///
/// Certificates are broadcast on this channel as soon as they are constructed
/// from collected votes. We separate this from the `vote_network` to optimistically
/// allow for certificate processing to short-circuit vote processing (if we receive
/// a certificate before processing pending votes, we can skip them).
///
/// ## `resolver_network`
///
/// Used for request-response certificate fetching. When a node needs to
/// catch up on a view it missed (e.g., to verify a proposal's parent), it
/// uses this channel to request certificates from peers. The resolver handles
/// rate limiting, retries, and peer selection for these requests.
pub fn start(
mut self,
vote_network: (
impl Sender<PublicKey = S::PublicKey>,
impl Receiver<PublicKey = S::PublicKey>,
),
certificate_network: (
impl Sender<PublicKey = S::PublicKey>,
impl Receiver<PublicKey = S::PublicKey>,
),
resolver_network: (
impl Sender<PublicKey = S::PublicKey>,
impl Receiver<PublicKey = S::PublicKey>,
),
) -> Handle<()> {
spawn_cell!(
self.context,
self.run(vote_network, certificate_network, resolver_network)
.await
)
}
async fn run(
self,
vote_network: (
impl Sender<PublicKey = S::PublicKey>,
impl Receiver<PublicKey = S::PublicKey>,
),
certificate_network: (
impl Sender<PublicKey = S::PublicKey>,
impl Receiver<PublicKey = S::PublicKey>,
),
resolver_network: (
impl Sender<PublicKey = S::PublicKey>,
impl Receiver<PublicKey = S::PublicKey>,
),
) {
// Start the batcher (receives votes via vote_network, certificates via certificate_network)
// Batcher sends proposals/certificates to voter via voter_mailbox
let (vote_sender, vote_receiver) = vote_network;
let (certificate_sender, certificate_receiver) = certificate_network;
let mut batcher_task = self.batcher.start(
self.voter_mailbox.clone(),
vote_receiver,
certificate_receiver,
);
// Start the resolver (sends certificates to voter via voter_mailbox)
let (resolver_sender, resolver_receiver) = resolver_network;
let mut resolver_task =
self.resolver
.start(self.voter_mailbox, resolver_sender, resolver_receiver);
// Start the voter
let mut voter_task = self.voter.start(
self.batcher_mailbox,
self.resolver_mailbox,
vote_sender,
certificate_sender,
);
// Wait for the resolver or voter to finish
let mut shutdown = self.context.stopped();
select! {
_ = &mut shutdown => {
debug!("context shutdown, stopping engine");
},
_ = &mut voter_task => {
panic!("voter should not finish");
},
_ = &mut batcher_task => {
panic!("batcher should not finish");
},
_ = &mut resolver_task => {
panic!("resolver should not finish");
},
}
}
}