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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
//! State machine for steward epoch management and group operations.
use async_trait::async_trait;
use std::{
fmt::Display,
time::{Duration, Instant},
};
use tracing::info;
use crate::app::scheduler::DEFAULT_EPOCH_DURATION;
/// Configuration for a group's epoch behavior.
///
/// This struct is extensible for future per-group settings.
#[derive(Debug, Clone)]
pub struct GroupConfig {
/// Duration of each epoch.
pub epoch_duration: Duration,
}
impl Default for GroupConfig {
fn default() -> Self {
Self {
epoch_duration: DEFAULT_EPOCH_DURATION,
}
}
}
impl GroupConfig {
/// Create a new config with custom epoch duration.
pub fn with_epoch_duration(epoch_duration: Duration) -> Self {
Self { epoch_duration }
}
}
/// Trait for handling state machine state changes.
///
/// This is an app-layer trait (not part of core API) for receiving
/// notifications when the group state changes.
#[async_trait]
pub trait StateChangeHandler: Send + Sync {
/// Called when the group state changes (PendingJoin, Working, Waiting, Leaving).
///
/// # Arguments
/// * `group_name` - The name of the group
/// * `state` - The new state
async fn on_state_changed(&self, group_name: &str, state: GroupState);
}
/// Represents the different states a group can be in during the steward epoch flow.
#[derive(Debug, Clone, PartialEq)]
pub enum GroupState {
/// Waiting for a welcome message after sending a key package.
PendingJoin,
/// Normal operation state - users can send any message freely.
Working,
/// Waiting state during steward epoch - only steward can send BATCH_PROPOSALS_MESSAGE.
Waiting,
/// User has requested to leave; waiting for the removal commit to arrive.
Leaving,
}
impl Display for GroupState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let state = match self {
GroupState::PendingJoin => "PendingJoin",
GroupState::Working => "Working",
GroupState::Waiting => "Waiting",
GroupState::Leaving => "Leaving",
};
write!(f, "{state}")
}
}
/// Result of checking commit timeout status.
#[derive(Debug, PartialEq)]
pub enum CommitTimeoutStatus {
/// Not in Waiting state — nothing to check.
NotWaiting,
/// In Waiting state but timeout hasn't been reached yet.
StillWaiting,
/// Timeout reached and state reverted to Working.
/// `has_proposals` indicates if approved proposals still existed (steward fault).
TimedOut { has_proposals: bool },
}
/// State machine for managing group steward epoch flow.
#[derive(Debug, Clone)]
pub struct GroupStateMachine {
/// Current state of the group.
state: GroupState,
/// Whether this user is the steward for this group.
is_steward: bool,
/// Timestamp when PendingJoin state was entered (for timeout).
pending_join_started_at: Option<Instant>,
/// Timestamp when Waiting state was entered (for commit timeout).
waiting_started_at: Option<Instant>,
/// Timestamp of the last epoch boundary (commit/welcome reception).
/// Used by members to sync their epoch with the steward.
last_epoch_boundary: Option<Instant>,
/// Duration of each epoch.
epoch_duration: Duration,
}
impl Default for GroupStateMachine {
fn default() -> Self {
Self::new_as_member()
}
}
impl GroupStateMachine {
/// Create a new group state machine (not steward) with default config.
pub fn new_as_member() -> Self {
Self::new_as_member_with_config(GroupConfig::default())
}
/// Create a new group state machine (not steward) with custom config.
pub fn new_as_member_with_config(config: GroupConfig) -> Self {
Self {
state: GroupState::Working,
is_steward: false,
pending_join_started_at: None,
waiting_started_at: None,
last_epoch_boundary: None,
epoch_duration: config.epoch_duration,
}
}
/// Create a new group state machine as steward with default config.
pub fn new_as_steward() -> Self {
Self::new_as_steward_with_config(GroupConfig::default())
}
/// Create a new group state machine as steward with custom config.
pub fn new_as_steward_with_config(config: GroupConfig) -> Self {
Self {
state: GroupState::Working,
is_steward: true,
pending_join_started_at: None,
waiting_started_at: None,
last_epoch_boundary: None,
epoch_duration: config.epoch_duration,
}
}
/// Create a new group state machine in PendingJoin state with default config.
pub fn new_as_pending_join() -> Self {
Self::new_as_pending_join_with_config(GroupConfig::default())
}
/// Create a new group state machine in PendingJoin state with custom config.
pub fn new_as_pending_join_with_config(config: GroupConfig) -> Self {
Self {
state: GroupState::PendingJoin,
is_steward: false,
pending_join_started_at: Some(Instant::now()),
waiting_started_at: None,
last_epoch_boundary: None,
epoch_duration: config.epoch_duration,
}
}
/// Get the current state.
pub fn current_state(&self) -> GroupState {
self.state.clone()
}
/// Check if this is a steward state machine.
pub fn is_steward(&self) -> bool {
self.is_steward
}
/// Set steward status.
pub fn set_steward(&mut self, is_steward: bool) {
self.is_steward = is_steward;
}
/// Start working state.
pub fn start_working(&mut self) {
self.state = GroupState::Working;
self.waiting_started_at = None;
info!("[start_working] Transitioning to Working state");
}
/// Start waiting state.
pub fn start_waiting(&mut self) {
self.state = GroupState::Waiting;
self.waiting_started_at = Some(Instant::now());
info!("[start_waiting] Transitioning to Waiting state");
}
/// Transition to Leaving state.
///
/// Caller must ensure valid state transition (typically from Working or Waiting).
/// The `User::leave_group` method handles PendingJoin and Leaving states separately.
pub fn start_leaving(&mut self) {
self.state = GroupState::Leaving;
info!("[start_leaving] Transitioning to Leaving state");
}
// ─────────────────────────── Pending Join ───────────────────────────
/// Check if the pending join has expired (time-based).
///
/// Expiration happens when ~2 epoch durations have passed since join attempt.
/// If the member hasn't received a welcome by then, assume rejection.
pub fn is_pending_join_expired(&self) -> bool {
if self.state != GroupState::PendingJoin {
return false;
}
if let Some(started_at) = self.pending_join_started_at {
let max_wait = self.epoch_duration * 2;
if Instant::now() >= started_at + max_wait {
return true;
}
}
false
}
// ─────────────────────────── Commit Timeout ───────────────────────────
/// Check if the commit has timed out while in Waiting state.
///
/// Returns `true` if the member has been in Waiting for longer than
/// `epoch_duration / 2` without receiving a commit from the steward.
pub fn is_commit_timed_out(&self) -> bool {
if self.state != GroupState::Waiting {
return false;
}
if let Some(started_at) = self.waiting_started_at {
let timeout = self.epoch_duration / 2;
if Instant::now() >= started_at + timeout {
return true;
}
}
false
}
// ─────────────────────────── Epoch Synchronization ───────────────────────────
/// Sync the epoch boundary to now.
/// Called when a commit or welcome (for joining) is received.
/// This is the synchronization point between steward and member epochs.
pub fn sync_epoch_boundary(&mut self) {
self.last_epoch_boundary = Some(Instant::now());
info!("[sync_epoch_boundary] Epoch boundary synchronized");
}
/// Check if we've reached the expected epoch boundary and should enter Waiting.
///
/// Called by the member epoch timer. Returns `true` if entering Waiting state
/// (meaning a commit timeout should be started).
///
/// # Arguments
/// * `approved_proposals_count` - Number of approved proposals waiting for commit
///
/// # Returns
/// `true` if transitioned to Waiting state, `false` otherwise.
pub fn check_epoch_boundary(&mut self, approved_proposals_count: usize) -> bool {
// Skip if steward (they manage their own epoch) or not initialized
if self.is_steward {
return false;
}
// Skip if in PendingJoin or Leaving state
if self.state == GroupState::PendingJoin || self.state == GroupState::Leaving {
return false;
}
// Already Waiting for commit — don't re-enter or reset the timeout timer.
// The commit timeout mechanism handles this case.
if self.state == GroupState::Waiting {
return false;
}
// Check if we've reached the expected boundary
if let Some(last_boundary) = self.last_epoch_boundary {
let expected = last_boundary + self.epoch_duration;
if Instant::now() >= expected {
// Advance boundary for next epoch
self.last_epoch_boundary = Some(expected);
if approved_proposals_count > 0 {
// We have approved proposals → freeze and wait for commit
self.state = GroupState::Waiting;
info!(
"[check_epoch_boundary] Entering Waiting state with {} approved proposals",
approved_proposals_count
);
return true;
}
// No proposals → stay Working, just advanced the boundary
info!("[check_epoch_boundary] No proposals, staying in Working state");
}
}
// No last_epoch_boundary set means we haven't synced yet (first epoch after join)
// Just wait for the first commit to sync
false
}
/// Get the time until the next expected epoch boundary.
/// Returns `None` if no epoch boundary has been set yet.
pub fn time_until_next_boundary(&self) -> Option<Duration> {
self.last_epoch_boundary.map(|last| {
let expected = last + self.epoch_duration;
expected.saturating_duration_since(Instant::now())
})
}
// ─────────────────────────── Steward Operations ───────────────────────────
/// Start steward epoch with state validation.
/// # Errors
/// - If not in Working state
/// - If not a steward
pub fn start_steward_epoch(&mut self) -> Result<(), StateMachineError> {
if self.state != GroupState::Working {
return Err(StateMachineError::InvalidTransition {
from: self.state.to_string(),
to: "Waiting".to_string(),
});
}
if !self.is_steward {
return Err(StateMachineError::NotSteward);
}
self.start_waiting();
Ok(())
}
}
/// Errors from state machine operations.
#[derive(Debug, thiserror::Error)]
pub enum StateMachineError {
/// Invalid state transition attempted.
#[error("Invalid state transition from {from} to {to}")]
InvalidTransition { from: String, to: String },
/// Operation requires steward status.
#[error("Not a steward")]
NotSteward,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_state_machine_creation() {
let state_machine = GroupStateMachine::new_as_member();
assert_eq!(state_machine.current_state(), GroupState::Working);
assert!(!state_machine.is_steward());
}
#[test]
fn test_state_machine_as_steward() {
let state_machine = GroupStateMachine::new_as_steward();
assert_eq!(state_machine.current_state(), GroupState::Working);
assert!(state_machine.is_steward());
}
#[test]
fn test_state_machine_pending_join() {
let state_machine = GroupStateMachine::new_as_pending_join();
assert_eq!(state_machine.current_state(), GroupState::PendingJoin);
assert!(!state_machine.is_steward());
assert!(!state_machine.is_pending_join_expired());
}
#[test]
fn test_pending_join_timeout() {
let mut state_machine = GroupStateMachine::new_as_pending_join();
assert!(!state_machine.is_pending_join_expired());
// Simulate time passing (~2 epochs) by backdating the start time
state_machine.pending_join_started_at = Some(Instant::now() - Duration::from_secs(120)); // Well past 2 epochs (60s)
// Should expire after ~2 epoch durations
assert!(state_machine.is_pending_join_expired());
}
#[test]
fn test_pending_join_not_expired_when_working() {
let state_machine = GroupStateMachine::new_as_member();
assert_eq!(state_machine.current_state(), GroupState::Working);
// Should not be expired when not in PendingJoin state
assert!(!state_machine.is_pending_join_expired());
}
#[test]
fn test_pending_join_to_working() {
let mut state_machine = GroupStateMachine::new_as_pending_join();
assert_eq!(state_machine.current_state(), GroupState::PendingJoin);
state_machine.start_working();
assert_eq!(state_machine.current_state(), GroupState::Working);
}
#[test]
fn test_leaving_state() {
let mut state_machine = GroupStateMachine::new_as_member();
assert_eq!(state_machine.current_state(), GroupState::Working);
state_machine.start_leaving();
assert_eq!(state_machine.current_state(), GroupState::Leaving);
}
#[test]
fn test_epoch_sync_and_boundary_check() {
let mut state_machine = GroupStateMachine::new_as_member();
// No boundary set initially
assert!(state_machine.time_until_next_boundary().is_none());
// Sync epoch boundary
state_machine.sync_epoch_boundary();
assert!(state_machine.time_until_next_boundary().is_some());
// Immediately after sync, boundary not reached
assert!(!state_machine.check_epoch_boundary(5));
assert_eq!(state_machine.current_state(), GroupState::Working);
}
#[test]
fn test_epoch_boundary_with_no_proposals() {
let mut state_machine = GroupStateMachine::new_as_member();
// Simulate past epoch boundary
state_machine.last_epoch_boundary = Some(Instant::now() - Duration::from_secs(60));
// No proposals → stay Working
assert!(!state_machine.check_epoch_boundary(0));
assert_eq!(state_machine.current_state(), GroupState::Working);
}
#[test]
fn test_epoch_boundary_with_proposals() {
let mut state_machine = GroupStateMachine::new_as_member();
// Simulate past epoch boundary
state_machine.last_epoch_boundary = Some(Instant::now() - Duration::from_secs(60));
// Has proposals → enter Waiting
assert!(state_machine.check_epoch_boundary(3));
assert_eq!(state_machine.current_state(), GroupState::Waiting);
}
#[test]
fn test_steward_skips_epoch_boundary_check() {
let mut state_machine = GroupStateMachine::new_as_steward();
state_machine.last_epoch_boundary = Some(Instant::now() - Duration::from_secs(60));
// Steward should not enter Waiting via check_epoch_boundary
assert!(!state_machine.check_epoch_boundary(5));
assert_eq!(state_machine.current_state(), GroupState::Working);
}
#[test]
fn test_commit_timeout_not_in_waiting() {
let state_machine = GroupStateMachine::new_as_member();
// Not in Waiting → not timed out
assert!(!state_machine.is_commit_timed_out());
}
#[test]
fn test_commit_timeout_fresh_waiting() {
let mut state_machine = GroupStateMachine::new_as_member();
state_machine.start_waiting();
// Just entered Waiting → not timed out yet
assert!(!state_machine.is_commit_timed_out());
}
#[test]
fn test_commit_timeout_expired() {
let mut state_machine = GroupStateMachine::new_as_member();
state_machine.start_waiting();
// Backdate waiting_started_at to well past epoch_duration/2 (15s for default 30s epoch)
state_machine.waiting_started_at = Some(Instant::now() - Duration::from_secs(30));
assert!(state_machine.is_commit_timed_out());
}
#[test]
fn test_commit_timeout_cleared_on_working() {
let mut state_machine = GroupStateMachine::new_as_member();
state_machine.start_waiting();
assert!(state_machine.waiting_started_at.is_some());
state_machine.start_working();
assert!(state_machine.waiting_started_at.is_none());
assert!(!state_machine.is_commit_timed_out());
}
#[test]
fn test_check_epoch_boundary_skips_when_already_waiting() {
let mut state_machine = GroupStateMachine::new_as_member();
state_machine.last_epoch_boundary = Some(Instant::now() - Duration::from_secs(60));
// First call: enters Waiting
assert!(state_machine.check_epoch_boundary(3));
assert_eq!(state_machine.current_state(), GroupState::Waiting);
// Advance boundary past next epoch
state_machine.last_epoch_boundary = Some(Instant::now() - Duration::from_secs(60));
// Second call while still Waiting: should NOT re-enter (returns false)
assert!(!state_machine.check_epoch_boundary(3));
assert_eq!(state_machine.current_state(), GroupState::Waiting);
}
}