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
use super::Variant;
use crate::{
marshal::{
ancestry::{AncestorStream, BlockProvider},
Identifier,
},
simplex::types::{Activity, Finalization, Notarization},
types::{Height, Round},
Reporter,
};
use commonware_cryptography::{certificate::Scheme, Digestible};
use commonware_utils::{
channel::{fallible::AsyncFallibleExt, mpsc, oneshot},
vec::NonEmptyVec,
};
/// Messages sent to the marshal [Actor](super::Actor).
///
/// These messages are sent from the consensus engine and other parts of the
/// system to drive the state of the marshal.
pub(crate) enum Message<S: Scheme, V: Variant> {
/// A request to retrieve the `(height, digest)` of a block by its identifier.
/// The block must be finalized; returns `None` if the block is not finalized.
GetInfo {
/// The identifier of the block to get the information of.
identifier: Identifier<<V::Block as Digestible>::Digest>,
/// A channel to send the retrieved `(height, digest)`.
response: oneshot::Sender<Option<(Height, <V::Block as Digestible>::Digest)>>,
},
/// A request to retrieve a block by its identifier.
///
/// Requesting by [Identifier::Height] or [Identifier::Latest] will only return finalized
/// blocks, whereas requesting by [Identifier::Digest] may return non-finalized
/// or even unverified blocks.
GetBlock {
/// The identifier of the block to retrieve.
identifier: Identifier<<V::Block as Digestible>::Digest>,
/// A channel to send the retrieved block.
response: oneshot::Sender<Option<V::Block>>,
},
/// A request to retrieve a finalization by height.
GetFinalization {
/// The height of the finalization to retrieve.
height: Height,
/// A channel to send the retrieved finalization.
response: oneshot::Sender<Option<Finalization<S, V::Commitment>>>,
},
/// A hint that a finalized block may be available at a given height.
///
/// This triggers a network fetch if the finalization is not available locally.
/// This is fire-and-forget: the finalization will be stored in marshal and
/// delivered via the normal finalization flow when available.
///
/// The height must be covered by both the epocher and the provider. If the
/// epocher cannot map the height to an epoch, or the provider cannot supply
/// a scheme for that epoch, the hint is silently dropped.
///
/// Targets are required because this is typically called when a peer claims to
/// be ahead. If a target returns invalid data, the resolver will block them.
/// Sending this message multiple times with different targets adds to the
/// target set.
HintFinalized {
/// The height of the finalization to fetch.
height: Height,
/// Target peers to fetch from. Added to any existing targets for this height.
targets: NonEmptyVec<S::PublicKey>,
},
/// A request to subscribe to a block by its digest.
SubscribeByDigest {
/// The round in which the block was notarized. This is an optimization
/// to help locate the block.
round: Option<Round>,
/// The digest of the block to retrieve.
digest: <V::Block as Digestible>::Digest,
/// A channel to send the retrieved block.
response: oneshot::Sender<V::Block>,
},
/// A request to subscribe to a block by its commitment.
SubscribeByCommitment {
/// The round in which the block was notarized. This is an optimization
/// to help locate the block.
round: Option<Round>,
/// The commitment of the block to retrieve.
commitment: V::Commitment,
/// A channel to send the retrieved block.
response: oneshot::Sender<V::Block>,
},
/// A request to broadcast a proposed block to peers.
Proposed {
/// The round in which the block was proposed.
round: Round,
/// The block to broadcast.
block: V::Block,
},
/// A request to forward a block to a set of peers.
Forward {
/// The round in which the block was proposed.
round: Round,
/// The commitment of the block to forward.
commitment: V::Commitment,
/// The peers to forward the block to.
peers: Vec<S::PublicKey>,
},
/// A notification that a block has been verified by the application.
Verified {
/// The round in which the block was verified.
round: Round,
/// The verified block.
block: V::Block,
},
/// Sets the sync starting point (advances if higher than current).
///
/// Marshal will sync and deliver blocks starting at `floor + 1`. Data below
/// the floor is pruned.
///
/// To prune data without affecting the sync starting point (say at some trailing depth
/// from tip), use [Message::Prune] instead.
///
/// The default floor is 0.
SetFloor {
/// The candidate floor height.
height: Height,
},
/// Prunes finalized blocks and certificates below the given height.
///
/// Unlike [Message::SetFloor], this does not affect the sync starting point.
/// The height must be at or below the current floor (last processed height),
/// otherwise the prune request is ignored.
Prune {
/// The minimum height to keep (blocks below this are pruned).
height: Height,
},
/// A notarization from the consensus engine.
Notarization {
/// The notarization.
notarization: Notarization<S, V::Commitment>,
},
/// A finalization from the consensus engine.
Finalization {
/// The finalization.
finalization: Finalization<S, V::Commitment>,
},
}
/// A mailbox for sending messages to the marshal [Actor](super::Actor).
#[derive(Clone)]
pub struct Mailbox<S: Scheme, V: Variant> {
sender: mpsc::Sender<Message<S, V>>,
}
impl<S: Scheme, V: Variant> Mailbox<S, V> {
/// Creates a new mailbox.
pub(crate) const fn new(sender: mpsc::Sender<Message<S, V>>) -> Self {
Self { sender }
}
/// A request to retrieve the information about the highest finalized block.
pub async fn get_info(
&self,
identifier: impl Into<Identifier<<V::Block as Digestible>::Digest>>,
) -> Option<(Height, <V::Block as Digestible>::Digest)> {
let identifier = identifier.into();
self.sender
.request(|response| Message::GetInfo {
identifier,
response,
})
.await
.flatten()
}
/// A best-effort attempt to retrieve a given block from local
/// storage. It is not an indication to go fetch the block from the network.
pub async fn get_block(
&self,
identifier: impl Into<Identifier<<V::Block as Digestible>::Digest>>,
) -> Option<V::Block> {
let identifier = identifier.into();
self.sender
.request(|response| Message::GetBlock {
identifier,
response,
})
.await
.flatten()
}
/// A best-effort attempt to retrieve a given [Finalization] from local
/// storage. It is not an indication to go fetch the [Finalization] from the network.
pub async fn get_finalization(&self, height: Height) -> Option<Finalization<S, V::Commitment>> {
self.sender
.request(|response| Message::GetFinalization { height, response })
.await
.flatten()
}
/// Hints that a finalized block may be available at the given height.
///
/// This method will request the finalization from the network via the resolver
/// if it is not available locally.
///
/// Targets are required because this is typically called when a peer claims to be
/// ahead. By targeting only those peers, we limit who we ask. If a target returns
/// invalid data, they will be blocked by the resolver. If targets don't respond
/// or return "no data", they effectively rate-limit themselves.
///
/// Calling this multiple times for the same height with different targets will
/// add to the target set if there is an ongoing fetch, allowing more peers to be tried.
///
/// This is fire-and-forget: the finalization will be stored in marshal and delivered
/// via the normal finalization flow when available.
///
/// The height must be covered by both the epocher and the provider. If the
/// epocher cannot map the height to an epoch, or the provider cannot supply
/// a scheme for that epoch, the hint is silently dropped.
pub async fn hint_finalized(&self, height: Height, targets: NonEmptyVec<S::PublicKey>) {
self.sender
.send_lossy(Message::HintFinalized { height, targets })
.await;
}
/// Subscribe to a block by its digest.
///
/// If the block is found available locally, the block will be returned immediately.
///
/// If the block is not available locally, the request will be registered and the caller will
/// be notified when the block is available. If the block is not finalized, it's possible that
/// it may never become available.
///
/// The oneshot receiver should be dropped to cancel the subscription.
pub async fn subscribe_by_digest(
&self,
round: Option<Round>,
digest: <V::Block as Digestible>::Digest,
) -> oneshot::Receiver<V::Block> {
let (tx, rx) = oneshot::channel();
self.sender
.send_lossy(Message::SubscribeByDigest {
round,
digest,
response: tx,
})
.await;
rx
}
/// Subscribe to a block by its commitment.
///
/// If the block is found available locally, the block will be returned immediately.
///
/// If the block is not available locally, the request will be registered and the caller will
/// be notified when the block is available. If the block is not finalized, it's possible that
/// it may never become available.
///
/// The oneshot receiver should be dropped to cancel the subscription.
pub async fn subscribe_by_commitment(
&self,
round: Option<Round>,
commitment: V::Commitment,
) -> oneshot::Receiver<V::Block> {
let (tx, rx) = oneshot::channel();
self.sender
.send_lossy(Message::SubscribeByCommitment {
round,
commitment,
response: tx,
})
.await;
rx
}
/// Returns an [AncestorStream] over the ancestry of a given block, leading up to genesis.
///
/// If the starting block is not found, `None` is returned.
pub async fn ancestry(
&self,
(start_round, start_digest): (Option<Round>, <V::Block as Digestible>::Digest),
) -> Option<AncestorStream<Self, V::ApplicationBlock>> {
self.subscribe_by_digest(start_round, start_digest)
.await
.await
.ok()
.map(|block| AncestorStream::new(self.clone(), [V::into_inner(block)]))
}
/// Requests that a proposed block is sent to peers.
pub async fn proposed(&self, round: Round, block: V::Block) {
self.sender
.send_lossy(Message::Proposed { round, block })
.await;
}
/// Notifies the actor that a block has been verified.
pub async fn verified(&self, round: Round, block: V::Block) {
self.sender
.send_lossy(Message::Verified { round, block })
.await;
}
/// Sets the sync starting point (advances if higher than current).
///
/// Marshal will sync and deliver blocks starting at `floor + 1`. Data below
/// the floor is pruned.
///
/// To prune data without affecting the sync starting point (say at some trailing depth
/// from tip), use [Self::prune] instead.
///
/// The default floor is 0.
pub async fn set_floor(&self, height: Height) {
self.sender.send_lossy(Message::SetFloor { height }).await;
}
/// Prunes finalized blocks and certificates below the given height.
///
/// Unlike [Self::set_floor], this does not affect the sync starting point.
/// The height must be at or below the current floor (last processed height),
/// otherwise the prune request is ignored.
///
/// A `prune` request for a height above marshal's current floor is dropped.
pub async fn prune(&self, height: Height) {
self.sender.send_lossy(Message::Prune { height }).await;
}
/// Forward a block to a set of peers.
pub async fn forward(&self, round: Round, commitment: V::Commitment, peers: Vec<S::PublicKey>) {
self.sender
.send_lossy(Message::Forward {
round,
commitment,
peers,
})
.await;
}
}
impl<S: Scheme, V: Variant> BlockProvider for Mailbox<S, V> {
type Block = V::ApplicationBlock;
async fn fetch_block(self, digest: <V::Block as Digestible>::Digest) -> Option<Self::Block> {
let subscription = self.subscribe_by_digest(None, digest).await;
subscription.await.ok().map(V::into_inner)
}
}
impl<S: Scheme, V: Variant> Reporter for Mailbox<S, V> {
type Activity = Activity<S, V::Commitment>;
async fn report(&mut self, activity: Self::Activity) {
let message = match activity {
Activity::Notarization(notarization) => Message::Notarization { notarization },
Activity::Finalization(finalization) => Message::Finalization { finalization },
_ => {
// Ignore other activity types
return;
}
};
self.sender.send_lossy(message).await;
}
}