de_mls/app/user/plugins/consensus_context.rs
1//! [`ConsensusContext`] — User-level consensus-plugin state.
2//!
3//! Holds the shared storage handle + signer once on the User; mints fresh
4//! per-conversation [`PluginConsensus`] services on demand and tears down a
5//! conversation's scope on leave. Each per-conv service gets its own
6//! private event bus (per upstream "service shape" recommendation).
7
8use hashgraph_like_consensus::storage::ConsensusStorage;
9
10use crate::core::{ConsensusPlugin, PluginConsensus};
11
12pub struct ConsensusContext<P: ConsensusPlugin> {
13 storage: P::ConsensusStorage,
14 signer: P::Signer,
15}
16
17impl<P: ConsensusPlugin> ConsensusContext<P> {
18 /// Build a fresh context. `P::new_storage` creates the shared backing
19 /// once; `signer` is supplied by the integrator (typically wraps the
20 /// user's wallet / account key).
21 pub fn new(signer: P::Signer) -> Self {
22 Self {
23 storage: P::new_storage(),
24 signer,
25 }
26 }
27
28 /// Build a fresh per-conversation `ConsensusService`. Clones the shared
29 /// storage handle so all per-conv services share one underlying
30 /// persistence (scope-keyed); clones the signer; mints a fresh private
31 /// event bus.
32 pub fn build_service(&self) -> PluginConsensus<P> {
33 PluginConsensus::<P>::new_with_components(
34 self.storage.clone(),
35 P::new_event_bus(),
36 self.signer.clone(),
37 10,
38 )
39 }
40
41 /// Drop a conversation's scope from the shared consensus storage.
42 /// Called on conversation leave.
43 pub async fn delete_scope(
44 &self,
45 scope: &P::Scope,
46 ) -> Result<(), hashgraph_like_consensus::error::ConsensusError> {
47 self.storage.delete_scope(scope).await
48 }
49}
50
51impl<P: ConsensusPlugin> Clone for ConsensusContext<P> {
52 fn clone(&self) -> Self {
53 Self {
54 storage: self.storage.clone(),
55 signer: self.signer.clone(),
56 }
57 }
58}