From 599cc6988e7e353da1b35dde085e544a5cc70402 Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Thu, 28 Nov 2024 12:02:51 +0100 Subject: [PATCH] Add Simulation::add_async_party Signed-off-by: Denis Varlakov --- round-based/src/simulation/mod.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/round-based/src/simulation/mod.rs b/round-based/src/simulation/mod.rs index 4a8cc3e..735c623 100644 --- a/round-based/src/simulation/mod.rs +++ b/round-based/src/simulation/mod.rs @@ -248,8 +248,7 @@ where { let mut sim = Self::with_capacity(n); for i in 0..n { - let party = crate::state_machine::wrap_protocol(|party| init(i, party)); - sim.add_party(party) + sim.add_async_party(|party| init(i, party)) } sim } @@ -282,6 +281,24 @@ where }) } + /// Adds new party, defined as an async function, into the protocol + /// + /// New party will be assigned index `i = n - 1` where `n` is amount of parties in the + /// simulation after this party was added. + /// + /// Async function will be converted into a [state machine](crate::state_machine). Because of that, + /// it cannot await on any futures that aren't provided by `MpcParty` (that is given as an argument + /// to this function). + pub fn add_async_party(&mut self, party: impl FnOnce(crate::state_machine::MpcParty) -> F) + where + F: core::future::Future + 'a, + { + self.parties.push(Party::Active { + party: Box::new(crate::state_machine::wrap_protocol(party)), + wants_one_more_msg: false, + }) + } + /// Returns amount of parties in the simulation pub fn parties_amount(&self) -> usize { self.parties.len()