Skip to content

Commit

Permalink
Add more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
survived committed Jul 11, 2022
1 parent 0e8be6b commit 3787269
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 9 deletions.
60 changes: 56 additions & 4 deletions round-based-ing/src/generic.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use std::fmt;

use round_based::{Delivery, Incoming, MessageDestination, Mpc, MpcParty, Outgoing};

use ecdsa_mpc::protocol::{Address, InputMessage, OutputMessage, PartyIndex};
use ecdsa_mpc::state_machine::{self, State, Transition};

use futures::{SinkExt, StreamExt};
use thiserror::Error;
use tracing::{error, trace, trace_span, warn};

pub async fn execute_ing_protocol<S, T, M>(
protocol_name: &'static str,
party: M,
initial_state: S,
party_index: u16,
Expand All @@ -15,23 +19,35 @@ pub async fn execute_ing_protocol<S, T, M>(
where
S: State<T> + Send,
T: StateMachineTraits,
T::ErrorState: fmt::Debug,
M: Mpc<ProtocolMessage = T::Msg>,
{
let span = trace_span!("MPC protocol execution", protocol = %protocol_name, i = party_index);
trace!(parent: &span, "Starting the protocol");

let MpcParty { delivery, .. } = party.into_party();
let (mut incomings, mut outgoings) = delivery.split();

let mut state: Box<dyn State<T> + Send> = Box::new(initial_state);

loop {
for round_i in 1u16.. {
trace!(parent: &span, i = round_i, "Proceeding to round `i`");

if let Some(msgs_to_send) = state.start() {
for msg in msgs_to_send {
let msg = match convert_output_message_to_outgoing(&parties, msg) {
Ok(m) => m,
Err(UnknownDestination { recipient }) => {
tracing::warn!(?recipient, "Protocol wants to send message to the party that doesn't take part in computation. Ignore that message.");
warn!(?recipient, "Protocol wants to send message to the party that doesn't take part in computation. Ignore that message.");
continue;
}
};
trace!(
parent: &span,
recipient = ?msg.recipient,
is_broadcast = msg.is_broadcast(),
"Sending message to `recipient`"
);
outgoings.feed(msg).await.map_err(Error::SendMessage)?;
}
outgoings.flush().await.map_err(Error::SendMessage)?;
Expand All @@ -50,8 +66,18 @@ where
continue;
}

trace!(
parent: &span,
sender = incoming.sender,
is_broadcast = incoming.is_broadcast(),
"Received message from `sender`"
);
let msg = convert_incoming_to_input_message(&parties, incoming)?;
if !state.is_message_expected(&msg, &received_msgs) {
error!(
parent: &span,
"State machine reported that message was not expected, aborting"
);
return Err(Error::ReceivedUnexpectedMessage { sender });
}
received_msgs.push(msg);
Expand All @@ -62,10 +88,18 @@ where
state = new_state;
continue;
}
Transition::FinalState(Ok(output)) => return Ok(output),
Transition::FinalState(Err(err)) => return Err(Error::ProtocolError(err)),
Transition::FinalState(Ok(output)) => {
trace!(parent: &span, "Protocol terminated successfully");
return Ok(output);
}
Transition::FinalState(Err(err)) => {
error!(parent: &span, ?err, "Protocol terminated with error");
return Err(Error::ProtocolError(err));
}
}
}

Err(BugReason::NoResult)?
}

fn convert_output_message_to_outgoing<M>(
Expand Down Expand Up @@ -116,6 +150,8 @@ pub enum Error<PErr, IErr, OErr> {
// UnknownDestination(#[from] UnknownDestination),
#[error(transparent)]
UnknownSender(#[from] UnknownSender),
#[error("bug occurred")]
Bug(#[source] Bug),
}

#[derive(Debug, Error)]
Expand All @@ -130,6 +166,22 @@ pub struct UnknownSender {
sender: u16,
}

#[derive(Debug, Error)]
#[error(transparent)]
pub struct Bug(BugReason);

#[derive(Debug, Error)]
enum BugReason {
#[error("for loop yielded no result")]
NoResult,
}

impl<PErr, IErr, OErr> From<BugReason> for Error<PErr, IErr, OErr> {
fn from(err: BugReason) -> Self {
Error::Bug(Bug(err))
}
}

/// Extension of [`StateMachineTraits`](ecdsa_mpc::state_machine::StateMachineTraits)
///
/// Ensures that input message is of type [`InputMessage<Msg>`], and output message is [`OutputMessage<Msg>`]
Expand Down
25 changes: 20 additions & 5 deletions round-based-ing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ impl Keygen {

let share = if let Some(span) = self.debugging {
generic::execute_ing_protocol(
"ecdsa-gg18 keygen",
party,
Debugging::new(initial_state).set_span(span),
self.i,
Expand All @@ -160,10 +161,16 @@ impl Keygen {
.map_err(KeygenError::ProtocolExecution)?
.multiparty_shared_info
} else {
generic::execute_ing_protocol(party, initial_state, self.i, parties)
.await
.map_err(KeygenError::ProtocolExecution)?
.multiparty_shared_info
generic::execute_ing_protocol(
"ecdsa-gg18 keygen",
party,
initial_state,
self.i,
parties,
)
.await
.map_err(KeygenError::ProtocolExecution)?
.multiparty_shared_info
};

let share = KeyShare::try_from(share).map_err(BugReason::IncorrectKeyShare)?;
Expand Down Expand Up @@ -346,14 +353,22 @@ impl Signing {

let signature = if let Some(span) = self.debugging {
generic::execute_ing_protocol(
"ecdsa-gg18 signing",
party,
Debugging::new(initial_state).set_span(span),
i,
self.signers,
)
.await?
} else {
generic::execute_ing_protocol(party, initial_state, i, self.signers).await?
generic::execute_ing_protocol(
"ecdsa-gg18 signing",
party,
initial_state,
i,
self.signers,
)
.await?
};

let signature = ecdsa_mpc::ecdsa::Signature {
Expand Down

0 comments on commit 3787269

Please sign in to comment.