commonware_consensus/lib.rs
1//! Order opaque messages in a Byzantine environment.
2//!
3//! # Status
4//!
5//! Stability varies by primitive. See [README](https://github.com/commonwarexyz/monorepo#stability) for details.
6
7#![doc(
8 html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
9 html_favicon_url = "https://commonware.xyz/favicon.ico"
10)]
11
12use commonware_macros::stability_scope;
13
14stability_scope!(BETA {
15 use commonware_codec::{Codec, Encode};
16 use commonware_cryptography::Digestible;
17
18 pub mod simplex;
19
20 pub mod types;
21 use types::{Epoch, Height, View, Round};
22
23 /// Epochable is a trait that provides access to the epoch number.
24 /// Any consensus message or object that is associated with a specific epoch should implement this.
25 pub trait Epochable {
26 /// Returns the epoch associated with this object.
27 fn epoch(&self) -> Epoch;
28 }
29
30 /// Heightable is a trait that provides access to the height.
31 /// Any consensus message or object that is associated with a specific height should implement this.
32 pub trait Heightable {
33 /// Returns the height associated with this object.
34 fn height(&self) -> Height;
35 }
36
37 /// Viewable is a trait that provides access to the view (round) number.
38 /// Any consensus message or object that is associated with a specific view should implement this.
39 pub trait Viewable {
40 /// Returns the view associated with this object.
41 fn view(&self) -> View;
42 }
43
44 /// Roundable is a trait that provides access to the [`Round`] number.
45 /// Any consensus message or object that implements [`Epochable`] and [`Viewable`] automatically
46 /// implements this trait.
47 pub trait Roundable: Epochable + Viewable {
48 /// Returns the round associated with this object, derived from its epoch and view.
49 fn round(&self) -> Round {
50 Round::new(self.epoch(), self.view())
51 }
52 }
53
54 impl<T: Epochable + Viewable> Roundable for T {}
55
56 /// Block is the interface for a block in the blockchain.
57 ///
58 /// Blocks are used to track the progress of the consensus engine.
59 pub trait Block: Heightable + Codec + Digestible + Send + Sync + 'static {
60 /// Get the parent block's digest.
61 fn parent(&self) -> Self::Digest;
62 }
63
64 /// CertifiableBlock extends [Block] with consensus context information.
65 ///
66 /// This trait is required for blocks used with deferred verification in [CertifiableAutomaton].
67 /// It allows the verification context to be derived directly from the block when a validator
68 /// needs to participate in certification but never verified the block locally (necessary for liveness).
69 pub trait CertifiableBlock: Block {
70 /// The consensus context type stored in this block.
71 type Context: Clone + Encode;
72
73 /// Get the consensus context that was used when this block was proposed.
74 fn context(&self) -> Self::Context;
75 }
76});
77stability_scope!(BETA, cfg(not(target_arch = "wasm32")) {
78 use commonware_actor::Feedback;
79 use commonware_cryptography::{Digest, PublicKey};
80 use commonware_utils::channel::{fallible::OneshotExt, mpsc, oneshot};
81 use std::future::Future;
82
83 pub mod marshal;
84
85 mod reporter;
86 pub use reporter::*;
87
88 /// Histogram buckets for measuring consensus latency.
89 const LATENCY: [f64; 36] = [
90 0.05, 0.1, 0.125, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26,
91 0.27, 0.28, 0.29, 0.3, 0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4, 0.45,
92 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
93 ];
94
95 /// Automaton is the interface responsible for driving the consensus forward by proposing new payloads
96 /// and verifying payloads proposed by other participants.
97 pub trait Automaton: Clone + Send + 'static {
98 /// Context is metadata provided by the consensus engine associated with a given payload.
99 ///
100 /// This often includes things like the proposer, view number, the height, or the epoch.
101 type Context;
102
103 /// Hash of an arbitrary payload.
104 type Digest: Digest;
105
106 /// Generate a new payload for the given context.
107 ///
108 /// If it is possible to generate a payload, the Digest should be returned over the provided
109 /// channel. If it is not possible to generate a payload, the channel can be dropped. If construction
110 /// takes too long, the consensus engine may drop the provided proposal.
111 ///
112 /// Returning a payload from `propose` commits the local proposer to verifying
113 /// the same `(context, payload)`.
114 ///
115 /// For [`CertifiableAutomaton`] implementations, returning a payload from
116 /// `propose` also commits the local proposer to certifying that same
117 /// `(round, payload)` if it later becomes notarized.
118 fn propose(
119 &mut self,
120 context: Self::Context,
121 ) -> impl Future<Output = oneshot::Receiver<Self::Digest>> + Send;
122
123 /// Verify the payload is valid.
124 ///
125 /// This request is single-shot for the given `(context, payload)`. Once the returned
126 /// channel resolves or closes, consensus treats verification as concluded and will not
127 /// retry the same request.
128 ///
129 /// Implementations should therefore keep the request pending while the verdict may still
130 /// change. Return `false` only when the payload is permanently invalid for this context.
131 /// For example, temporary conditions such as time skew, missing dependencies, or data
132 /// that may arrive later should not conclude verification with `false`.
133 ///
134 /// Closing the channel is also terminal for this request and should be reserved for cases
135 /// where verification cannot ever produce a verdict anymore (for example, shutdown), not
136 /// for temporary inability to decide.
137 fn verify(
138 &mut self,
139 context: Self::Context,
140 payload: Self::Digest,
141 ) -> impl Future<Output = oneshot::Receiver<bool>> + Send;
142 }
143
144 /// CertifiableAutomaton extends [Automaton] with the ability to certify payloads before finalization.
145 ///
146 /// This trait is required by consensus implementations (like Simplex) that support a certification
147 /// phase between notarization and finalization. Applications that do not need custom certification
148 /// logic can use the default implementation which always certifies.
149 pub trait CertifiableAutomaton: Automaton {
150 /// Determine whether a verified payload is safe to commit.
151 ///
152 /// The round parameter identifies which consensus round is being certified, allowing
153 /// applications to associate certification with the correct verification context. The
154 /// same payload may appear in multiple rounds, so implementations must key any state
155 /// on `(round, payload)` rather than `payload` alone.
156 ///
157 /// Like [`Automaton::verify`], payloads produced by [`Automaton::propose`] are certifiable-by-construction.
158 /// Also like [`Automaton::verify`], certification is single-shot for the given
159 /// `(round, payload)`. Once the returned channel resolves or closes, consensus treats
160 /// certification as concluded and will not retry the same request.
161 ///
162 /// Implementations should therefore keep the request pending while the verdict may still
163 /// change. Return `false` only when the payload is permanently uncertifiable for that
164 /// round. Temporary conditions such as waiting for more data should not conclude
165 /// certification with `false`.
166 ///
167 /// Closing the channel is also terminal for this request and should be reserved for cases
168 /// where certification can no longer produce a verdict (for example, shutdown), not for temporary
169 /// inability to decide.
170 ///
171 /// # Determinism Requirement
172 ///
173 /// The decision returned by `certify` must be deterministic and consistent across
174 /// all honest participants to ensure liveness.
175 fn certify(
176 &mut self,
177 _round: Round,
178 _payload: Self::Digest,
179 ) -> impl Future<Output = oneshot::Receiver<bool>> + Send {
180 #[allow(clippy::async_yields_async)]
181 async move {
182 let (sender, receiver) = oneshot::channel();
183 sender.send_lossy(true);
184 receiver
185 }
186 }
187 }
188
189 /// Relay is the interface responsible for broadcasting payloads to the network.
190 ///
191 /// The consensus engine is only aware of a payload's digest, not its contents. It is up
192 /// to the relay to efficiently broadcast the full payload to other participants.
193 pub trait Relay: Clone + Send + 'static {
194 /// Hash of an arbitrary payload.
195 type Digest: Digest;
196
197 /// Identity key of a network participant.
198 type PublicKey: PublicKey;
199
200 /// Directive for how a payload should be broadcast.
201 ///
202 /// Consensus mechanisms that need broadcast control (e.g. distinguishing
203 /// initial broadcast from rebroadcasts) define a custom enum here. Mechanisms that
204 /// treat every broadcast identically can set this to `()`.
205 type Plan: Send;
206
207 /// Broadcast a payload according to the given plan.
208 fn broadcast(&mut self, payload: Self::Digest, plan: Self::Plan) -> Feedback;
209 }
210
211 /// Reporter is the interface responsible for reporting activity to some external actor.
212 pub trait Reporter: Clone + Send + 'static {
213 /// Activity is specified by the underlying consensus implementation and can be interpreted if desired.
214 ///
215 /// Examples of activity would be "vote", "finalize", or "fault". Various consensus implementations may
216 /// want to reward (or penalize) participation in different ways and in different places. For example,
217 /// validators could be required to send multiple types of messages (i.e. vote and finalize) and rewarding
218 /// both equally may better align incentives with desired behavior.
219 type Activity;
220
221 /// Report some activity observed by the consensus implementation.
222 fn report(&mut self, activity: Self::Activity) -> Feedback;
223 }
224
225 /// Monitor is the interface an external actor can use to observe the progress of a consensus implementation.
226 ///
227 /// Monitor is used to implement mechanisms that share the same set of active participants as consensus and/or
228 /// perform some activity that requires some synchronization with the progress of consensus.
229 ///
230 /// Monitor can be implemented using [crate::Reporter] to avoid introducing complexity
231 /// into any particular consensus implementation.
232 pub trait Monitor: Clone + Send + 'static {
233 /// Index is the type used to indicate the in-progress consensus decision.
234 type Index;
235
236 /// Create a channel that will receive updates when the latest index (also provided) changes.
237 fn subscribe(
238 &mut self,
239 ) -> impl Future<Output = (Self::Index, mpsc::Receiver<Self::Index>)> + Send;
240 }
241});
242stability_scope!(ALPHA {
243 pub mod aggregation;
244 pub mod ordered_broadcast;
245});
246stability_scope!(ALPHA, cfg(not(target_arch = "wasm32")) {
247 use commonware_cryptography::certificate::Scheme;
248 use crate::marshal::ancestry::Ancestry;
249 use commonware_runtime::{Clock, Metrics, Spawner};
250 use rand::Rng;
251
252 /// Application is a minimal interface for standard implementations that operate over a stream
253 /// of epoched blocks.
254 pub trait Application<E>: Clone + Send + 'static
255 where
256 E: Rng + Spawner + Metrics + Clock,
257 {
258 /// The signing scheme used by the application.
259 type SigningScheme: Scheme;
260
261 /// Context is metadata provided by the consensus engine associated with a given payload.
262 ///
263 /// This often includes things like the proposer, view number, the height, or the epoch.
264 type Context: Epochable;
265
266 /// The block type produced by the application's builder.
267 type Block: Block;
268
269 /// Build a new block on top of the provided parent ancestry. If the build job fails,
270 /// or the proposer's slot should be skipped, the implementor should return [None].
271 ///
272 /// This future may be cancelled before it completes. Implementations must be
273 /// cancellation-safe.
274 fn propose(
275 &mut self,
276 context: (E, Self::Context),
277 ancestry: impl Ancestry<Self::Block>,
278 ) -> impl Future<Output = Option<Self::Block>> + Send;
279
280 /// Verify a block produced by the application's proposer, relative to its ancestry.
281 ///
282 /// This future should not resolve until the implementation can produce a stable verdict.
283 /// Return `false` only when the block is permanently invalid for the supplied context and
284 /// ancestry. If validity may still change as additional information becomes available,
285 /// continue waiting instead of returning `false`.
286 ///
287 /// In other words, to abstain from voting, do not resolve this future yet. Keep it
288 /// pending until the implementation can either prove the block valid, prove it invalid,
289 /// or the consensus engine cancels the request. Abstaining is not represented by a
290 /// special return value.
291 ///
292 /// This future may be cancelled before it completes. Implementations must be
293 /// cancellation-safe.
294 fn verify(
295 &mut self,
296 context: (E, Self::Context),
297 ancestry: impl Ancestry<Self::Block>,
298 ) -> impl Future<Output = bool> + Send;
299 }
300});