Module async_env

Source
Available on crate features sim and sim-async only.
Expand description

Fully async simulation

Simulation provided in a parent module should be used in most cases. It works by converting all parties (defined as async functions) into state machines, which has certain limitations. In particular, the protocol cannot await on any futures that aren’t provided by MpcParty, for instance, awaiting on the timer will cause a simulation error.

We suggest to avoid awaiting on the futures that aren’t provided by MpcParty in the MPC protocol implementation as it likely makes it runtime-dependent. However, if you do ultimately need to do that, then you can’t use regular simulation for the tests.

This module provides fully async simulation built for tokio runtime, so the protocol can await on any futures supported by the tokio.

§Limitations

To implement simulated network, we used tokio::sync::broadcast channels, which have internal buffer of stored messages, and once simulated network receives more messages than internal buffer can fit, some of the parties will not receive some of the messages, which will lead to execution error.

By default, internal buffer is preallocated to fit 500 messages, which should be more than sufficient for simulating protocols with small amount of parties (say, < 10).

If you need to preallocate bigger buffer, use Network::with_capacity.

§Example

Entry point to the simulation are run and run_with_setup functions

use round_based::{Mpc, PartyIndex};

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

let n = 3;

let output = round_based::sim::async_env::run(
    n,
    |i, party| protocol_of_random_generation(party, i, n),
)
.await
// unwrap `Result`s
.expect_ok()
// check that all parties produced the same response
.expect_eq();

println!("Output randomness: {}", hex::encode(output));

Structs§

MockedDelivery
Mocked networking
MockedIncoming
Incoming channel of mocked network
MockedOutgoing
Outgoing channel of mocked network
Network
Simulated async network

Functions§

run
Simulates execution of the protocol
run_with_capacity
Simulates execution of the protocol
run_with_capacity_and_setup
Simulates execution of the protocol
run_with_setup
Simulates execution of the protocol