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
use crate::state_machine::{
    coordinator::CoordinatorState,
    phases::{Idle, Phase, PhaseName, PhaseState, Shutdown},
    requests::RequestReceiver,
    RoundFailed,
    StateMachine,
};
use thiserror::Error;

/// Error that can occur during the execution of the [`StateMachine`].
#[derive(Error, Debug)]
pub enum StateError {
    #[error("state failed: channel error: {0}")]
    ChannelError(&'static str),
    #[error("state failed: round error: {0}")]
    RoundError(#[from] RoundFailed),
    #[error("state failed: phase timeout: {0}")]
    TimeoutError(#[from] tokio::time::Elapsed),
}

impl<R> PhaseState<R, StateError> {
    /// Creates a new error state.
    pub fn new(
        coordinator_state: CoordinatorState,
        request_rx: RequestReceiver<R>,
        error: StateError,
    ) -> Self {
        info!("state transition");
        Self {
            inner: error,
            coordinator_state,
            request_rx,
        }
    }
}

#[async_trait]
impl<R> Phase<R> for PhaseState<R, StateError>
where
    R: Send,
{
    const NAME: PhaseName = PhaseName::Error;

    async fn run(&mut self) -> Result<(), StateError> {
        error!("state transition failed! error: {:?}", self.inner);

        info!("broadcasting error phase event");
        self.coordinator_state.events.broadcast_phase(
            self.coordinator_state.round_params.seed.clone(),
            PhaseName::Error,
        );

        Ok(())
    }

    /// Moves from the error state to the next state.
    ///
    /// See the [module level documentation](../index.html) for more details.
    fn next(self) -> Option<StateMachine<R>> {
        Some(match self.inner {
            StateError::ChannelError(_) => {
                PhaseState::<R, Shutdown>::new(self.coordinator_state, self.request_rx).into()
            }
            _ => PhaseState::<R, Idle>::new(self.coordinator_state, self.request_rx).into(),
        })
    }
}