Trait round_based::StateMachine [−][src]
pub trait StateMachine { type MessageBody; type Err: IsCritical; type Output; fn handle_incoming(
&mut self,
msg: Msg<Self::MessageBody>
) -> Result<(), Self::Err>; fn message_queue(&mut self) -> &mut Vec<Msg<Self::MessageBody>>; fn wants_to_proceed(&self) -> bool; fn proceed(&mut self) -> Result<(), Self::Err>; fn round_timeout(&self) -> Option<Duration>; fn round_timeout_reached(&mut self) -> Self::Err; fn is_finished(&self) -> bool; fn pick_output(&mut self) -> Option<Result<Self::Output, Self::Err>>; fn current_round(&self) -> u16; fn total_rounds(&self) -> Option<u16>; fn party_ind(&self) -> u16; fn parties(&self) -> u16; }
Expand description
State machine of party involved in round-based protocol
Associated Types
type MessageBody
type MessageBody
Body of transmitting messages
Actual type of transmitting messages will be Msg<SM::MessageBody>
(see Msg struct)
type Err: IsCritical
type Err: IsCritical
Error type used by StateMachine
Errors are divided on critical and not critical to follow different error-handling strategies
on appearing one of them. For more details, see method returning Result
s, e.g.
handle_incoming or proceed
Required methods
fn handle_incoming(
&mut self,
msg: Msg<Self::MessageBody>
) -> Result<(), Self::Err>
fn handle_incoming(
&mut self,
msg: Msg<Self::MessageBody>
) -> Result<(), Self::Err>
Process received message
Returns
Handling message might result in error, but it doesn’t mean that computation should be aborted. Returned error needs to be examined whether it critical or not (by calling is_critical method).
If occurs:
- Critical error: protocol must be aborted
- Non-critical error: it should be reported, but protocol must continue
Example of non-critical error is receiving message which we didn’t expect to see. It could be either network lag or bug in implementation or attempt to sabotage the protocol, but protocol might be resistant to this, so it still has a chance to successfully complete.
Blocking
This method should not block or perform expensive computation. E.g. it might do deserialization (if needed) or cheap checks.
fn message_queue(&mut self) -> &mut Vec<Msg<Self::MessageBody>>
fn message_queue(&mut self) -> &mut Vec<Msg<Self::MessageBody>>
Queue of messages to be sent
New messages can be appended to queue only as result of calling proceed or handle_incoming methods.
Messages can be sent in any order.
fn wants_to_proceed(&self) -> bool
fn wants_to_proceed(&self) -> bool
Indicates whether StateMachine wants to perform some expensive computation
Performs some expensive computation
If StateMachine
is executed at green thread (in async environment), it will be typically
moved to dedicated thread at thread pool before calling .proceed()
method.
Returns
Returns Ok(())
if either computation successfully completes or computation was not
required (i.e. self.wants_to_proceed() == false
).
If it returns Err(err)
, then err
is examined whether it’s critical or not (by
calling is_critical method).
If occurs:
- Critical error: protocol must be aborted
- Non-critical error: it should be reported, but protocol must continue
For example, in .proceed()
at verification stage we could find some party trying to
sabotage the protocol, but protocol might be designed to be resistant to such attack, so
it’s not a critical error, but obviously it should be reported.
fn round_timeout(&self) -> Option<Duration>
fn round_timeout(&self) -> Option<Duration>
Deadline for a particular round
After reaching deadline (if set) round_timeout_reached will be called.
After proceeding on the next round (increasing current_round), timer will be reset, new timeout will be requested (by calling this method), and new deadline will be set.
fn round_timeout_reached(&mut self) -> Self::Err
fn round_timeout_reached(&mut self) -> Self::Err
Method is triggered after reaching round_timeout
Reaching timeout always aborts computation, no matter what error is returned: critical or not.
fn is_finished(&self) -> bool
fn is_finished(&self) -> bool
Indicates whether protocol is finished and output can be obtained by calling pick_output method.
Obtains protocol output
Returns
None
, if protocol is not finished yet i.e.protocol.is_finished() == false
Some(Err(_))
, if protocol terminated with errorSome(Ok(_))
, if protocol successfully terminated
After Some(_)
has been obtained via this method, StateMachine must be utilized (dropped).
fn current_round(&self) -> u16
fn current_round(&self) -> u16
Sequential number of current round
Can be increased by 1 as result of calling either proceed or handle_incoming methods. Changing round number in any other way (or in any other method) might cause strange behaviour.
fn total_rounds(&self) -> Option<u16>
fn total_rounds(&self) -> Option<u16>
Total amount of rounds (if known)
Index of this party
Must be in interval [1; n]
where n = self.parties()