-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathactor.rs
More file actions
373 lines (346 loc) · 13.5 KB
/
actor.rs
File metadata and controls
373 lines (346 loc) · 13.5 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! Consensus engine orchestrator for epoch transitions.
use crate::{
application::{genesis, Block, EpochProvider, Provider},
orchestrator::{ingress::Message, Mailbox},
BLOCKS_PER_EPOCH,
};
use commonware_actor::mailbox;
use commonware_consensus::{
marshal::{core::Mailbox as MarshalMailbox, standard::Standard},
simplex::{self, elector::Config as Elector, scheme, types::Context, Plan},
types::{Epoch, Epocher, FixedEpocher, ViewDelta},
CertifiableAutomaton, Relay,
};
use commonware_cryptography::{
bls12381::primitives::variant::Variant, certificate::Scheme, Digestible, Hasher, Signer,
};
use commonware_macros::select_loop;
use commonware_p2p::{
utils::mux::{Builder, MuxHandle, Muxer},
Blocker, Sender,
};
use commonware_parallel::Strategy;
use commonware_runtime::{
buffer::paged::CacheRef,
spawn_cell,
telemetry::metrics::{Gauge, GaugeExt, MetricsExt as _},
BufferPooler, Clock, ContextCell, Handle, Metrics, Network, Spawner, Storage,
};
use commonware_utils::{vec::NonEmptyVec, NZUsize, NZU16};
use rand_core::CryptoRngCore;
use std::{collections::BTreeMap, marker::PhantomData, num::NonZeroUsize, time::Duration};
use tracing::{debug, info, warn};
/// Returns the digest Simplex should use as the floor for `epoch`.
async fn floor_digest_for_epoch<S, H, C, V>(
epoch: Epoch,
marshal: &MarshalMailbox<S, Standard<Block<H, C, V>>>,
) -> H::Digest
where
S: Scheme,
H: Hasher,
C: Signer,
V: Variant,
{
let Some(previous_epoch) = epoch.previous() else {
return genesis::<H, C, V>().digest();
};
let epocher = FixedEpocher::new(BLOCKS_PER_EPOCH);
let boundary_height = epocher
.last(previous_epoch)
.expect("previous epoch should exist");
marshal
.get_block(boundary_height)
.await
.unwrap_or_else(|| {
panic!("missing boundary block for epoch {epoch} at height {boundary_height}")
})
.digest()
}
/// Configuration for the orchestrator.
pub struct Config<B, V, C, H, A, S, L, T>
where
B: Blocker<PublicKey = C::PublicKey>,
V: Variant,
C: Signer,
H: Hasher,
A: CertifiableAutomaton<Context = Context<H::Digest, C::PublicKey>, Digest = H::Digest>
+ Relay<Digest = H::Digest, PublicKey = C::PublicKey, Plan = Plan<C::PublicKey>>,
S: Scheme,
L: Elector<S>,
T: Strategy,
{
pub oracle: B,
pub application: A,
pub provider: Provider<S, C>,
pub marshal: MarshalMailbox<S, Standard<Block<H, C, V>>>,
pub strategy: T,
pub muxer_size: usize,
pub mailbox_size: NonZeroUsize,
// Partition prefix used for orchestrator metadata persistence
pub partition_prefix: String,
pub _phantom: PhantomData<L>,
}
pub struct Actor<E, B, V, C, H, A, S, L, T>
where
E: BufferPooler + Spawner + Metrics + CryptoRngCore + Clock + Storage + Network,
B: Blocker<PublicKey = C::PublicKey>,
V: Variant,
C: Signer,
H: Hasher,
A: CertifiableAutomaton<Context = Context<H::Digest, C::PublicKey>, Digest = H::Digest>
+ Relay<Digest = H::Digest, PublicKey = C::PublicKey, Plan = Plan<C::PublicKey>>,
S: Scheme,
L: Elector<S>,
T: Strategy,
Provider<S, C>: EpochProvider<Variant = V, PublicKey = C::PublicKey, Scheme = S>,
{
context: ContextCell<E>,
mailbox: mailbox::Receiver<Message<V, C::PublicKey>>,
application: A,
oracle: B,
marshal: MarshalMailbox<S, Standard<Block<H, C, V>>>,
provider: Provider<S, C>,
strategy: T,
muxer_size: usize,
partition_prefix: String,
page_cache_ref: CacheRef,
latest_epoch: Gauge,
_phantom: PhantomData<L>,
}
impl<E, B, V, C, H, A, S, L, T> Actor<E, B, V, C, H, A, S, L, T>
where
E: BufferPooler + Spawner + Metrics + CryptoRngCore + Clock + Storage + Network,
B: Blocker<PublicKey = C::PublicKey>,
V: Variant,
C: Signer,
H: Hasher,
A: CertifiableAutomaton<Context = Context<H::Digest, C::PublicKey>, Digest = H::Digest>
+ Relay<Digest = H::Digest, PublicKey = C::PublicKey, Plan = Plan<C::PublicKey>>,
S: scheme::Scheme<H::Digest, PublicKey = C::PublicKey>,
L: Elector<S>,
T: Strategy,
Provider<S, C>: EpochProvider<Variant = V, PublicKey = C::PublicKey, Scheme = S>,
{
pub fn new(
context: E,
config: Config<B, V, C, H, A, S, L, T>,
) -> (Self, Mailbox<V, C::PublicKey>) {
let (sender, mailbox) = mailbox::new(context.child("mailbox"), config.mailbox_size);
let page_cache_ref = CacheRef::from_pooler(&context, NZU16!(16_384), NZUsize!(10_000));
// Register latest_epoch gauge for Grafana integration
let latest_epoch = context.gauge("latest_epoch", "current epoch");
(
Self {
context: ContextCell::new(context),
mailbox,
application: config.application,
oracle: config.oracle,
marshal: config.marshal,
provider: config.provider,
strategy: config.strategy,
muxer_size: config.muxer_size,
partition_prefix: config.partition_prefix,
page_cache_ref,
latest_epoch,
_phantom: PhantomData,
},
Mailbox::new(sender),
)
}
pub fn start(
mut self,
votes: (
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
),
certificates: (
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
),
resolver: (
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
),
) -> Handle<()> {
spawn_cell!(self.context, self.run(votes, certificates, resolver,))
}
async fn run(
mut self,
(vote_sender, vote_receiver): (
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
),
(certificate_sender, certificate_receiver): (
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
),
(resolver_sender, resolver_receiver): (
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
),
) {
// Start muxers for each physical channel used by consensus
let (mux, mut vote_mux, mut vote_backup) = Muxer::builder(
self.context.child("vote_mux"),
vote_sender,
vote_receiver,
self.muxer_size,
)
.with_backup()
.build();
mux.start();
let (mux, mut certificate_mux) = Muxer::builder(
self.context.child("certificate_mux"),
certificate_sender,
certificate_receiver,
self.muxer_size,
)
.build();
mux.start();
let (mux, mut resolver_mux) = Muxer::new(
self.context.child("resolver_mux"),
resolver_sender,
resolver_receiver,
self.muxer_size,
);
mux.start();
// Wait for instructions to transition epochs.
let epocher = FixedEpocher::new(BLOCKS_PER_EPOCH);
let mut engines: BTreeMap<Epoch, Handle<()>> = BTreeMap::new();
select_loop! {
self.context,
on_stopped => {
debug!("context shutdown, stopping orchestrator");
},
Some((their_epoch, (from, _))) = vote_backup.recv() else {
warn!("vote mux backup channel closed, shutting down orchestrator");
break;
} => {
// If a message is received in an unregistered sub-channel in the vote network,
// ensure we have the boundary finalization.
let their_epoch = Epoch::new(their_epoch);
let Some(our_epoch) = engines.keys().last().copied() else {
debug!(%their_epoch, ?from, "received message from unregistered epoch with no known epochs");
continue;
};
if their_epoch <= our_epoch {
debug!(%their_epoch, %our_epoch, ?from, "received message from past epoch");
continue;
}
// If we're not in the committee of the latest epoch we know about and we observe
// another participant that is ahead of us, ensure we have the boundary finalization.
// We target only the peer who claims to be ahead. If we receive messages from
// multiple peers claiming to be ahead, each call adds them to the target set,
// giving us more peers to try fetching from.
let boundary_height = epocher.last(our_epoch).expect("our epoch should exist");
debug!(
?from,
%their_epoch,
%our_epoch,
%boundary_height,
"received backup message from future epoch, ensuring boundary finalization"
);
self.marshal
.hint_finalized(boundary_height, NonEmptyVec::new(from));
},
Some(transition) = self.mailbox.recv() else {
warn!("mailbox closed, shutting down orchestrator");
break;
} => match transition {
Message::Enter(transition) => {
// If the epoch is already in the map, ignore.
if engines.contains_key(&transition.epoch) {
warn!(epoch = %transition.epoch, "entered existing epoch");
continue;
}
// Register the new signing scheme with the scheme provider.
let scheme = self.provider.scheme_for_epoch(&transition);
assert!(self.provider.register(transition.epoch, scheme.clone()));
// Enter the new epoch.
let handle = self
.enter_epoch(
transition.epoch,
scheme,
&mut vote_mux,
&mut certificate_mux,
&mut resolver_mux,
)
.await;
engines.insert(transition.epoch, handle);
let _ = self.latest_epoch.try_set(transition.epoch.get());
info!(epoch = %transition.epoch, "entered epoch");
}
Message::Exit(epoch) => {
// Remove the engine and abort it.
let Some(handle) = engines.remove(&epoch) else {
warn!(%epoch, "exited non-existent epoch");
continue;
};
handle.abort();
// Unregister the signing scheme for the epoch.
assert!(self.provider.unregister(&epoch));
info!(%epoch, "exited epoch");
}
},
}
}
async fn enter_epoch(
&mut self,
epoch: Epoch,
scheme: S,
vote_mux: &mut MuxHandle<
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
>,
certificate_mux: &mut MuxHandle<
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
>,
resolver_mux: &mut MuxHandle<
impl Sender<PublicKey = C::PublicKey>,
impl commonware_p2p::Receiver<PublicKey = C::PublicKey>,
>,
) -> Handle<()> {
// Start the new engine
let elector = L::default();
let context = self
.context
.child("consensus_engine")
.with_attribute("epoch", epoch);
let floor = simplex::Floor::genesis(
floor_digest_for_epoch::<S, H, C, V>(epoch, &self.marshal).await,
);
let engine = simplex::Engine::new(
context,
simplex::Config {
scheme,
elector,
blocker: self.oracle.clone(),
automaton: self.application.clone(),
relay: self.application.clone(),
reporter: self.marshal.clone(),
partition: format!("{}_consensus_{}", self.partition_prefix, epoch),
mailbox_size: NZUsize!(1024),
epoch,
floor,
replay_buffer: NZUsize!(1024 * 1024),
write_buffer: NZUsize!(1024 * 1024),
leader_timeout: Duration::from_secs(1),
certification_timeout: Duration::from_secs(2),
timeout_retry: Duration::from_secs(10),
fetch_timeout: Duration::from_secs(1),
activity_timeout: ViewDelta::new(256),
skip_timeout: ViewDelta::new(10),
fetch_concurrent: NZUsize!(32),
page_cache: self.page_cache_ref.clone(),
strategy: self.strategy.clone(),
forwarding: simplex::ForwardingPolicy::Disabled,
},
);
// Create epoch-specific subchannels
let vote = vote_mux.register(epoch.get()).await.unwrap();
let certificate = certificate_mux.register(epoch.get()).await.unwrap();
let resolver = resolver_mux.register(epoch.get()).await.unwrap();
engine.start(vote, certificate, resolver)
}
}