Skip to content

Commit

Permalink
Handle Critical and Non-Critical Errors in the Async Protocol (#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex authored Feb 16, 2023
1 parent 2208463 commit 703b7a6
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 55 additions & 11 deletions dkg-gadget/src/async_protocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ use multi_party_ecdsa::protocols::multi_party_ecdsa::gg_2020::state_machine::{
keygen::LocalKey, sign::CompletedOfflineStage,
};
use parking_lot::RwLock;
use round_based::{async_runtime::watcher::StderrWatcher, AsyncProtocol, Msg, StateMachine};
use round_based::{
async_runtime::{self, watcher::StderrWatcher},
AsyncProtocol, IsCritical, Msg, StateMachine,
};
use serde::Serialize;
use std::{
fmt::{Debug, Formatter},
Expand Down Expand Up @@ -268,16 +271,57 @@ where
let params_for_end_of_proto = params.clone();

let async_proto = Box::pin(async move {
let res = async_proto
.run()
.await
.map_err(|err| DKGError::GenericError { reason: format!("{err:?}") });
match res {
Ok(v) => SM::on_finish(v, params_for_end_of_proto, additional_param, async_index).await,
Err(err) => {
dkg_logging::error!(target: "dkg", "Async Proto Errored: {:?}", err);
Err(err)
},
// Loop and wait for the protocol to finish.
loop {
let res = async_proto.run().await;
match res {
Ok(v) =>
return SM::on_finish(v, params_for_end_of_proto, additional_param, async_index)
.await,
Err(err) => match err {
async_runtime::Error::Recv(e) |
async_runtime::Error::Proceed(e) |
async_runtime::Error::HandleIncoming(e) |
async_runtime::Error::HandleIncomingTimeout(e) |
async_runtime::Error::Finish(e)
if e.is_critical() =>
{
dkg_logging::error!(target: "dkg", "Async Proto Cought Critical Error: {e:?}");
return Err(DKGError::GenericError { reason: format!("{e:?}") })
},
async_runtime::Error::Send(e) => {
dkg_logging::error!(target: "dkg", "Async Proto Failed to send outgoing messages: {e:?}");
return Err(DKGError::GenericError { reason: format!("{e:?}") })
},
async_runtime::Error::ProceedPanicked(e) => {
dkg_logging::error!(target: "dkg", "Async Proto `proceed` method panicked: {e:?}");
return Err(DKGError::GenericError { reason: format!("{e:?}") })
},
async_runtime::Error::InternalError(e) => {
dkg_logging::error!(target: "dkg", "Async Proto Internal Error: {e:?}");
return Err(DKGError::GenericError { reason: format!("{e:?}") })
},
async_runtime::Error::Exhausted => {
dkg_logging::error!(target: "dkg", "Async Proto Exhausted");
return Err(DKGError::GenericError { reason: String::from("Exhausted") })
},
async_runtime::Error::RecvEof => {
dkg_logging::error!(target: "dkg", "Async Proto Incoming channel closed");
return Err(DKGError::GenericError {
reason: String::from("RecvEof: Incomming channel closed"),
})
},
async_runtime::Error::BadStateMachine(e) => {
dkg_logging::error!(target: "dkg", "Async Proto Bad State Machine: {e:?}");
return Err(DKGError::GenericError { reason: format!("{e:?}") })
},
_ => {
// If the protocol errored, but it's not a critical error, then we
// should continue to run the protocol.
dkg_logging::error!(target: "dkg", "Async Proto Cought Non-Critical Error: {err:?}");
},
},
};
}
});

Expand Down
1 change: 0 additions & 1 deletion pallets/dkg-proposal-handler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::{mock::*, UnsignedProposalQueue};
use codec::Encode;
use frame_support::{
assert_err, assert_ok,
dispatch::DispatchClass,
traits::{Hooks, OnFinalize},
weights::constants::RocksDbWeight,
};
Expand Down

0 comments on commit 703b7a6

Please sign in to comment.