Skip to main content

de_mls/core/
consensus_plugin.rs

1//! [`ConsensusPlugin`] — user-level bundle of consensus-library backends
2//! (scope key, proposal/vote storage, event bus). One plug-in per `User`
3//! today; per-conversation consensus is on the roadmap and will expand
4//! this surface. The concrete consensus service type is derived via
5//! [`PluginConsensus<P>`] — the voting algorithm itself is fixed.
6//!
7//! Reference impl: [`crate::defaults::DefaultConsensusPlugin`].
8
9use hashgraph_like_consensus::{
10    events::ConsensusEventBus, scope::ConsensusScope, service::ConsensusService,
11    signing::ConsensusSignatureScheme, storage::ConsensusStorage,
12};
13
14/// User-level consensus backend bundle. Carries the four types the
15/// `hashgraph_like_consensus` service is parameterised by: the scope key
16/// (one consensus partition per conversation), proposal/vote storage,
17/// outcome event bus, and the signature scheme used to authenticate votes.
18/// The concrete service is materialised via [`PluginConsensus`].
19pub trait ConsensusPlugin: 'static {
20    /// Conversation-identifier type used as consensus scope (default: `String`).
21    type Scope: ConsensusScope + From<String> + 'static;
22
23    /// Proposal/vote persistence (default: `InMemoryConsensusStorage<String>`).
24    type ConsensusStorage: ConsensusStorage<Self::Scope> + 'static;
25
26    /// Consensus-outcome delivery (default: `BroadcastEventBus<String>`).
27    type EventBus: ConsensusEventBus<Self::Scope> + 'static;
28
29    /// Signature scheme for authenticating votes (default:
30    /// [`hashgraph_like_consensus::signing::EthereumConsensusSigner`]).
31    /// All peers on a network must agree.
32    type Signer: ConsensusSignatureScheme + Clone + 'static;
33
34    /// Build a fresh storage handle. Called once at `User` init; the handle
35    /// is cloned per conversation so all per-conv `ConsensusService` instances
36    /// share one underlying persistence (see upstream "per-scope service
37    /// composition" pattern).
38    fn new_storage() -> Self::ConsensusStorage;
39
40    /// Build a fresh event bus for one conversation. Each per-conv
41    /// `ConsensusService` owns its own bus; subscribers automatically see
42    /// only that conversation's events.
43    fn new_event_bus() -> Self::EventBus;
44}
45
46/// Concrete consensus service derived from a [`ConsensusPlugin`]'s
47/// associated types.
48pub type PluginConsensus<P> = ConsensusService<
49    <P as ConsensusPlugin>::Scope,
50    <P as ConsensusPlugin>::ConsensusStorage,
51    <P as ConsensusPlugin>::EventBus,
52    <P as ConsensusPlugin>::Signer,
53>;