Skip to main content

de_mls/consensus/
plugin.rs

1//! Consensus backend "plugin" contract.
2//!
3//! [`ConsensusPlugin`] pins the concrete types used by the consensus library:
4//! - **`Scope`**: the per-conversation partition key
5//! - **storage**: proposal/vote persistence
6//! - **event bus**: delivery of consensus outcomes
7//! - **signer**: vote authentication (must match across peers)
8//!
9//! The app layer constructs a per-conversation [`ConsensusServiceFor<C>`] using
10//! these associated types and factories.
11//!
12//! Reference implementation: [`crate::defaults::DefaultConsensusPlugin`].
13
14use hashgraph_like_consensus::{
15    events::ConsensusEventBus, scope::ConsensusScope, service::ConsensusService,
16    signing::ConsensusSignatureScheme, storage::ConsensusStorage, types::ConsensusEvent,
17};
18
19/// Pull-side contract for a [`ConsensusEventBus`] receiver: non-blocking.
20///
21/// Returns `None` when the queue is empty.
22///
23/// Each `Conversation` holds one receiver and drains it from
24/// `tick_deadlines`.
25pub trait SyncConsensusReceiver<Scope: ConsensusScope>: Send {
26    fn try_recv(&mut self) -> Option<(Scope, ConsensusEvent)>;
27}
28
29/// User-level consensus backend bundle.
30///
31/// This trait only selects the concrete types the consensus library runs with:
32/// scope key, storage, event bus, and signer. The app layer decides how to
33/// instantiate and share those pieces across conversations, then builds a
34/// per-conversation service via [`ConsensusServiceFor`].
35pub trait ConsensusPlugin {
36    /// Conversation identifier type used as a consensus scope key.
37    type Scope: ConsensusScope + From<String>;
38
39    /// Proposal/vote persistence.
40    type ConsensusStorage: ConsensusStorage<Self::Scope>;
41
42    /// Consensus-outcome delivery (event bus).
43    ///
44    /// The receiver must implement [`SyncConsensusReceiver`] so the app can
45    /// drain it from a tick loop.
46    type EventBus: ConsensusEventBus<Self::Scope, Receiver: SyncConsensusReceiver<Self::Scope>>;
47
48    /// Signature scheme for authenticating votes.
49    ///
50    /// All peers on a network must agree on this.
51    type Signer: ConsensusSignatureScheme + Clone + 'static;
52
53    /// Build storage.
54    fn new_storage() -> Self::ConsensusStorage;
55
56    /// Build an event bus.
57    ///
58    /// Most app wiring uses one bus per conversation.
59    fn new_event_bus() -> Self::EventBus;
60}
61
62/// Concrete consensus service derived from a [`ConsensusPlugin`]'s
63/// associated types.
64pub type ConsensusServiceFor<C> = ConsensusService<
65    <C as ConsensusPlugin>::Scope,
66    <C as ConsensusPlugin>::ConsensusStorage,
67    <C as ConsensusPlugin>::EventBus,
68    <C as ConsensusPlugin>::Signer,
69>;