#![doc(
html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
html_favicon_url = "https://commonware.xyz/favicon.ico"
)]
use commonware_codec::Codec;
use commonware_cryptography::{Committable, Digestible};
pub mod aggregation;
pub mod ordered_broadcast;
pub mod simplex;
pub mod types;
use types::{Epoch, Height, View};
pub trait Epochable {
fn epoch(&self) -> Epoch;
}
pub trait Heightable {
fn height(&self) -> Height;
}
pub trait Viewable {
fn view(&self) -> View;
}
pub trait Block: Heightable + Codec + Digestible + Committable + Send + Sync + 'static {
fn parent(&self) -> Self::Commitment;
}
cfg_if::cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
use commonware_cryptography::Digest;
use commonware_utils::channels::fallible::OneshotExt;
use futures::channel::{oneshot, mpsc};
use std::future::Future;
use commonware_runtime::{Spawner, Metrics, Clock};
use rand::Rng;
use crate::marshal::ingress::mailbox::AncestorStream;
use commonware_cryptography::certificate::Scheme;
pub mod application;
pub mod marshal;
mod reporter;
pub use reporter::*;
const LATENCY: [f64; 36] = [
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, 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, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
];
pub trait Automaton: Clone + Send + 'static {
type Context;
type Digest: Digest;
fn genesis(&mut self, epoch: Epoch) -> impl Future<Output = Self::Digest> + Send;
fn propose(
&mut self,
context: Self::Context,
) -> impl Future<Output = oneshot::Receiver<Self::Digest>> + Send;
fn verify(
&mut self,
context: Self::Context,
payload: Self::Digest,
) -> impl Future<Output = oneshot::Receiver<bool>> + Send;
}
pub trait CertifiableAutomaton: Automaton {
fn certify(
&mut self,
_payload: Self::Digest,
) -> impl Future<Output = oneshot::Receiver<bool>> + Send {
#[allow(clippy::async_yields_async)]
async move {
let (sender, receiver) = oneshot::channel();
sender.send_lossy(true);
receiver
}
}
}
pub trait Application<E>: Clone + Send + 'static
where
E: Rng + Spawner + Metrics + Clock
{
type SigningScheme: Scheme;
type Context: Epochable;
type Block: Block;
fn genesis(&mut self) -> impl Future<Output = Self::Block> + Send;
fn propose(
&mut self,
context: (E, Self::Context),
ancestry: AncestorStream<Self::SigningScheme, Self::Block>,
) -> impl Future<Output = Option<Self::Block>> + Send;
}
pub trait VerifyingApplication<E>: Application<E>
where
E: Rng + Spawner + Metrics + Clock
{
fn verify(
&mut self,
context: (E, Self::Context),
ancestry: AncestorStream<Self::SigningScheme, Self::Block>,
) -> impl Future<Output = bool> + Send;
}
pub trait Relay: Clone + Send + 'static {
type Digest: Digest;
fn broadcast(&mut self, payload: Self::Digest) -> impl Future<Output = ()> + Send;
}
pub trait Reporter: Clone + Send + 'static {
type Activity;
fn report(&mut self, activity: Self::Activity) -> impl Future<Output = ()> + Send;
}
pub trait Monitor: Clone + Send + 'static {
type Index;
fn subscribe(&mut self) -> impl Future<Output = (Self::Index, mpsc::Receiver<Self::Index>)> + Send;
}
}
}