Module state_machine

Source
Available on crate feature state-machine only.
Expand description

Wraps the protocol defined as async function and provides sync API to execute it

In round_based framework, MPC protocols are defined as async function. However, sometimes it may not be possible/desirable to have async runtime which drives the futures until completion. For such use-cases, we provide wrap_protocol function that wraps an MPC protocol defined as async function and returns the StateMachine that exposes sync API to carry out the protocol.

§Example

use round_based::{Mpc, PartyIndex};
use anyhow::{Result, Error, Context as _};

// Any MPC protocol
pub async fn protocol_of_random_generation<M>(
    party: M,
    i: PartyIndex,
    n: u16
) -> Result<Randomness>
where
    M: Mpc<ProtocolMessage = Msg>
{
    // ...
}

// `state` implements `round_based::state_machine::StateMachine` trait.
// Its methods can be used to advance protocol until completion.
let mut state = round_based::state_machine::wrap_protocol(
    |party| protocol_of_random_generation(party, 0, 3)
);

fn send(msg: round_based::Outgoing<Msg>) -> Result<()> {
    // sends outgoing message...
}
fn recv() -> Result<round_based::Incoming<Msg>> {
    // receives incoming message...
}

use round_based::state_machine::{StateMachine as _, ProceedResult};
let output = loop {
    match state.proceed() {
        ProceedResult::SendMsg(msg) => {
            send(msg)?
        }
        ProceedResult::NeedsOneMoreMessage => {
            let msg = recv()?;
            state.received_msg(msg)
                .map_err(|_| anyhow::format_err!("state machine rejected received message"))?;
        }
        ProceedResult::Yielded => {},
        ProceedResult::Output(out) => break Ok(out),
        ProceedResult::Error(err) => break Err(err),
    }
};

Structs§

ExecutionError
Error type which indicates that state machine failed to carry out the protocol
Incomings
Stream of incoming messages
Outgoings
Sink for outgoing messages
Runtime
State machine runtime
SendErr
Error returned by Outgoings sink
YieldNow
Future returned by runtime.yield_now()

Enums§

ProceedResult
Tells why protocol execution stopped

Traits§

StateMachine
Provides interface to execute the protocol

Functions§

wrap_protocol
Wraps the protocol and provides sync API to execute it

Type Aliases§

Delivery
Delivery implementation used in the state machine
MpcParty
MpcParty instantiated with state machine implementation of delivery and async runtime