de-mls 2.1.0

Decentralized MLS — end-to-end encrypted group messaging with consensus-based membership management over gossipsub-like networks
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Consensus integration for DE-MLS group operations.
//!
//! This module bridges MLS group operations with the consensus voting layer.
//! It provides functions for creating proposals, casting votes, and handling
//! consensus outcomes.
//!
//! # Overview
//!
//! When a membership change is requested (join/remove), it goes through consensus:
//!
//! ```text
//! 1. Steward receives key package → ProcessResult::GetUpdateRequest
//! 2. dispatch_result() returns DispatchAction::StartVoting(request)
//! 3. App calls start_voting() → creates proposal, notifies UI
//! 4. Users vote via cast_vote() → votes sent as MLS messages
//! 5. Consensus service emits ConsensusEvent
//! 6. App calls handle_consensus_event() → updates proposal state
//! 7. Steward calls create_batch_proposals() → applies approved changes
//! ```
//!
//! # Key Functions
//!
//! ## Voting Workflow
//! - [`start_voting`] - Create a proposal and start voting
//! - [`cast_vote`] - Cast a vote on a proposal
//! - [`handle_consensus_event`] - Process consensus outcomes
//!
//! ## Message Forwarding
//! - [`forward_incoming_proposal`] - Forward received proposals to consensus
//! - [`forward_incoming_vote`] - Forward received votes to consensus
//!
//! ## Result Dispatching
//! - [`dispatch_result`] - Route [`ProcessResult`] to appropriate handlers
//!
//! # DispatchAction
//!
//! After calling [`dispatch_result`], handle the returned [`DispatchAction`]:
//!
//! - `Done` - Nothing more needed
//! - `StartVoting(request)` - Spawn a background task to run voting
//! - `GroupUpdated` - MLS state changed, update your state machine
//! - `LeaveGroup` - User was removed, clean up group state
//! - `JoinedGroup` - User joined successfully, transition to Working state

use alloy::signers::Signer;
use openmls_rust_crypto::MemoryStorage;
use prost::Message;
use std::time::Duration;
use tracing::info;

use hashgraph_like_consensus::{
    api::ConsensusServiceAPI,
    protos::consensus::v1::{Proposal, Vote},
    session::ConsensusConfig,
    types::{ConsensusEvent, CreateProposalRequest},
};

use crate::core::{
    CoreError, DeMlsProvider, GroupEventHandler, GroupHandle, ProcessResult, build_message,
};
use crate::mls_crypto::{DeMlsStorage, MlsService};
use crate::protos::de_mls::messages::v1::{
    AppMessage, ConversationMessage, GroupUpdateRequest, VotePayload,
};

/// Action returned by [`dispatch_result`] telling the caller what to do next.
///
/// This enum represents the application-level actions needed after processing
/// an inbound message. The core library handles protocol-level concerns;
/// these actions represent what your application layer needs to do.
#[derive(Debug)]
pub enum DispatchAction {
    /// Core handled the result fully; nothing more to do.
    ///
    /// The message was processed and any necessary callbacks were made.
    Done,

    /// A group update request needs consensus voting.
    ///
    /// The application should spawn a background task to:
    /// 1. Call [`start_voting`] with the request
    /// 2. Store the proposal ID via `GroupHandle::store_voting_proposal`
    StartVoting(GroupUpdateRequest),

    /// Group MLS state was updated (batch commit applied).
    ///
    /// The application should:
    /// - Transition state machine from Waiting → Working
    /// - Refresh UI with new group state
    GroupUpdated,

    /// The user was removed from the group.
    ///
    /// The application should:
    /// - Remove the group from its registry
    /// - Clean up any associated state
    /// - Notify the UI
    LeaveGroup,

    /// The user successfully joined a group.
    ///
    /// The application should:
    /// - Transition state machine from PendingJoin → Working
    /// - Start epoch timing synchronization
    JoinedGroup,
}

/// Create a consensus proposal for a group update request and start voting.
///
/// This function:
/// 1. Creates a `CreateProposalRequest` with the encoded group update
/// 2. Submits it to the consensus service
/// 3. Emits a `VotePayload` via the handler for UI display
///
/// # Arguments
/// * `group_name` - The name of the group
/// * `request` - The group update request (add/remove member)
/// * `expected_voters` - Number of group members (for quorum calculation)
/// * `identity_string` - The proposer's identity
/// * `consensus` - The consensus service
/// * `handler` - Event handler for UI notifications
///
/// # Returns
/// The proposal ID, which should be stored via `GroupHandle::store_voting_proposal`.
///
/// # Errors
/// - [`CoreError::ConsensusError`] if proposal creation fails
pub async fn start_voting<P: DeMlsProvider>(
    group_name: &str,
    request: &GroupUpdateRequest,
    expected_voters: u32,
    identity_string: String,
    consensus: &P::Consensus,
    handler: &dyn GroupEventHandler,
) -> Result<u32, CoreError> {
    let payload = request.encode_to_vec();
    let create_request = CreateProposalRequest::new(
        uuid::Uuid::new_v4().to_string(),
        payload.clone(),
        identity_string.into(),
        expected_voters,
        3600,
        true,
    )?;

    let scope = P::Scope::from(group_name.to_string());
    let proposal = consensus
        .create_proposal_with_config(
            &scope,
            create_request,
            Some(ConsensusConfig::gossipsub().with_timeout(Duration::from_secs(15))?),
        )
        .await?;

    info!(
        "[start_voting]: Created proposal {} with {} expected voters",
        proposal.proposal_id, expected_voters
    );

    let vote_payload: AppMessage = VotePayload {
        group_id: group_name.to_string(),
        proposal_id: proposal.proposal_id,
        payload,
        timestamp: proposal.timestamp,
    }
    .into();

    handler.on_app_message(group_name, vote_payload).await?;

    Ok(proposal.proposal_id)
}

/// Forward a proposal received from another peer to the consensus service.
///
/// When a peer broadcasts their proposal, other members receive it as an
/// MLS application message. This function forwards it to the local consensus
/// service and emits a `VotePayload` so the UI can display the pending vote.
///
/// # Arguments
/// * `group_name` - The name of the group
/// * `proposal` - The received consensus proposal
/// * `consensus` - The consensus service
/// * `handler` - Event handler for UI notifications
pub async fn forward_incoming_proposal<P: DeMlsProvider>(
    group_name: &str,
    proposal: Proposal,
    consensus: &P::Consensus,
    handler: &dyn GroupEventHandler,
) -> Result<(), CoreError> {
    let scope = P::Scope::from(group_name.to_string());
    consensus
        .process_incoming_proposal(&scope, proposal.clone())
        .await?;

    let vote_payload: AppMessage = VotePayload {
        group_id: group_name.to_string(),
        proposal_id: proposal.proposal_id,
        payload: proposal.payload.clone(),
        timestamp: std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)?
            .as_secs(),
    }
    .into();

    handler.on_app_message(group_name, vote_payload).await?;
    Ok(())
}

/// Forward a vote received from another peer to the consensus service.
///
/// When a peer casts their vote, it's broadcast as an MLS application message.
/// This function forwards it to the local consensus service for tallying.
///
/// # Arguments
/// * `group_name` - The name of the group
/// * `vote` - The received vote
/// * `consensus` - The consensus service
pub async fn forward_incoming_vote<P: DeMlsProvider>(
    group_name: &str,
    vote: Vote,
    consensus: &P::Consensus,
) -> Result<(), CoreError> {
    let scope = P::Scope::from(group_name.to_string());
    consensus.process_incoming_vote(&scope, vote).await?;
    Ok(())
}

/// Cast a vote on a proposal and broadcast it to the group.
///
/// This function handles two cases:
/// - **Proposal owner**: Calls `cast_vote_and_get_proposal` and broadcasts
///   the full `Proposal` (so others can process it)
/// - **Non-owner**: Calls `cast_vote` and broadcasts just the `Vote`
///
/// # Arguments
/// * `handle` - The group handle (for proposal ownership check)
/// * `group_name` - The name of the group
/// * `proposal_id` - The proposal to vote on
/// * `vote` - true = approve, false = reject
/// * `consensus` - The consensus service
/// * `signer` - Ethereum signer for vote authentication
/// * `mls` - MLS service for message encryption
/// * `handler` - Event handler for sending the outbound message
///
/// # Errors
/// - [`CoreError::ConsensusError`] if voting fails
/// - [`CoreError::MlsServiceError`] if message encryption fails
#[allow(clippy::too_many_arguments)]
pub async fn cast_vote<P, SN, S>(
    handle: &GroupHandle,
    group_name: &str,
    proposal_id: u32,
    vote: bool,
    consensus: &P::Consensus,
    signer: SN,
    mls: &MlsService<S>,
    handler: &dyn GroupEventHandler,
) -> Result<(), CoreError>
where
    P: DeMlsProvider,
    SN: Signer + Send + Sync,
    S: DeMlsStorage<MlsStorage = MemoryStorage>,
{
    let is_owner = handle.is_owner_of_proposal(proposal_id);
    let scope = P::Scope::from(group_name.to_string());

    let app_message: AppMessage = if is_owner {
        info!("[cast_vote]: Owner voting on proposal {proposal_id}");
        let proposal = consensus
            .cast_vote_and_get_proposal(&scope, proposal_id, vote, signer)
            .await?;
        proposal.into()
    } else {
        info!("[cast_vote]: User voting on proposal {proposal_id}");
        let vote_msg = consensus
            .cast_vote(&scope, proposal_id, vote, signer)
            .await?;
        vote_msg.into()
    };

    let packet = build_message(handle, mls, &app_message)?;
    handler.on_outbound(group_name, packet).await?;
    Ok(())
}

/// Update handle state based on a consensus outcome event.
///
/// Called when the consensus service emits a [`ConsensusEvent`]. Updates
/// the group handle's proposal state based on the outcome:
///
/// - `ConsensusReached { result: true }` as owner → mark proposal as approved
/// - `ConsensusReached { result: true }` as non-owner → fetch and insert approved proposal
/// - `ConsensusReached { result: false }` → mark proposal as rejected
/// - `ConsensusFailed` → mark proposal as rejected
///
/// # Arguments
/// * `handle` - The group handle (will be mutated)
/// * `group_name` - The name of the group
/// * `event` - The consensus event to process
/// * `consensus` - The consensus service (for fetching proposal payloads)
pub async fn handle_consensus_event<P: DeMlsProvider>(
    handle: &mut GroupHandle,
    group_name: &str,
    event: ConsensusEvent,
    consensus: &P::Consensus,
) -> Result<(), CoreError> {
    match event {
        ConsensusEvent::ConsensusReached {
            proposal_id,
            result,
            timestamp: _,
        } => {
            info!("Consensus reached for proposal {proposal_id}: result={result}");
            let is_owner = handle.is_owner_of_proposal(proposal_id);
            if result && is_owner {
                handle.mark_proposal_as_approved(proposal_id);
            } else if !result && is_owner {
                handle.mark_proposal_as_rejected(proposal_id);
            } else if result && !is_owner {
                let scope = P::Scope::from(group_name.to_string());
                let payload = consensus.get_proposal_payload(&scope, proposal_id).await?;
                let update_request = GroupUpdateRequest::decode(payload.as_slice())?;
                handle.insert_approved_proposal(proposal_id, update_request);
            } else {
                // !result && !is_owner: proposal rejected, we weren't the owner
                // TODO: Emit a rejection event to the UI so pending votes can be cleared.
                // Currently non-owners see VotePayload but get no notification when rejected.
                info!("Proposal {proposal_id} rejected (not owner, no local state to update)");
            }
        }
        ConsensusEvent::ConsensusFailed {
            proposal_id,
            timestamp: _,
        } => {
            info!("Consensus failed for proposal {proposal_id}");
            handle.mark_proposal_as_rejected(proposal_id);
        }
    }

    Ok(())
}

/// Request steward re-election due to steward fault.
///
/// Called when the current steward fails to commit pending proposals
/// within the expected timeout. This should initiate a consensus vote
/// to elect a new steward.
///
/// # Current Implementation
/// This is a placeholder that clears approved proposals to prevent
/// infinite timeout loops. Full re-election is not yet implemented.
///
/// # TODO
/// - Define re-election proposal type in protos
/// - Implement voting logic for steward election
/// - Handle steward handoff (pending proposals transfer)
pub async fn request_steward_reelection<P: DeMlsProvider>(
    handle: &mut GroupHandle,
    group_name: &str,
    _consensus: &P::Consensus,
    _handler: &dyn GroupEventHandler,
) -> Result<(), CoreError> {
    tracing::warn!(
        "[request_steward_reelection] Steward fault detected for group {group_name}, \
         re-election not yet implemented"
    );

    // Clear approved proposals to prevent infinite timeout loop.
    // TODO: In real implementation, these should be preserved for new steward
    handle.clear_approved_proposals();

    Ok(())
}

/// Dispatch a [`ProcessResult`] to the appropriate handlers.
///
/// This is the main routing function that connects [`process_inbound`](super::process_inbound)
/// results to consensus and event handlers. It returns a [`DispatchAction`]
/// telling your application what to do next.
///
/// # Arguments
/// * `handle` - The group handle
/// * `group_name` - The name of the group
/// * `result` - The result from `process_inbound`
/// * `consensus` - The consensus service
/// * `handler` - Event handler for callbacks
/// * `mls` - MLS service for message building
///
/// # Returns
/// A [`DispatchAction`] indicating what the application should do:
/// - `Done` - Nothing more needed
/// - `StartVoting(request)` - Spawn voting task
/// - `GroupUpdated` - Update state machine
/// - `LeaveGroup` - Clean up group
/// - `JoinedGroup` - Transition to Working state
pub async fn dispatch_result<P, S>(
    handle: &GroupHandle,
    group_name: &str,
    result: ProcessResult,
    consensus: &P::Consensus,
    handler: &dyn GroupEventHandler,
    mls: &MlsService<S>,
) -> Result<DispatchAction, CoreError>
where
    P: DeMlsProvider,
    S: DeMlsStorage<MlsStorage = MemoryStorage>,
{
    match result {
        ProcessResult::AppMessage(msg) => {
            handler.on_app_message(group_name, msg).await?;
            Ok(DispatchAction::Done)
        }
        ProcessResult::LeaveGroup => Ok(DispatchAction::LeaveGroup),
        ProcessResult::Proposal(proposal) => {
            forward_incoming_proposal::<P>(group_name, proposal, consensus, handler).await?;
            Ok(DispatchAction::Done)
        }
        ProcessResult::Vote(vote) => {
            forward_incoming_vote::<P>(group_name, vote, consensus).await?;
            Ok(DispatchAction::Done)
        }
        ProcessResult::GetUpdateRequest(request) => Ok(DispatchAction::StartVoting(request)),
        ProcessResult::JoinedGroup(name) => {
            let msg: AppMessage = ConversationMessage {
                message: format!("User {} joined the group", mls.wallet_hex()).into_bytes(),
                sender: "SYSTEM".to_string(),
                group_name: name.clone(),
            }
            .into();

            let packet = build_message(handle, mls, &msg)?;
            handler.on_outbound(&name, packet).await?;
            handler.on_joined_group(&name).await?;
            Ok(DispatchAction::JoinedGroup)
        }
        ProcessResult::GroupUpdated => Ok(DispatchAction::GroupUpdated),
        ProcessResult::Noop => Ok(DispatchAction::Done),
    }
}