Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
chore(refactor) Split dkg.rs file into consituent parts.
Browse files Browse the repository at this point in the history
Lot of seemingly useless traits hiding impl's in this mod. Will likely try and clean that up next.
  • Loading branch information
dirvine committed Jun 14, 2021
1 parent 4c064a4 commit 4c78cb1
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 298 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ target/
*.sublime-*
.cargo/
.DS_Store
.core
.core
core
123 changes: 123 additions & 0 deletions src/dkg/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2020 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{
error::Result, messages::RoutingMsgUtils, node::Node, routing::command::Command,
section::SectionKeyShare,
};
use bls_dkg::key_gen::message::Message as DkgMessage;
use sn_messaging::{
node::{DkgFailureSigned, DkgFailureSignedSet, DkgKey, RoutingMsg, Variant},
DestInfo, DstLocation, SectionAuthorityProvider,
};
use std::{collections::BTreeSet, fmt::Debug, net::SocketAddr, time::Duration};
use xor_name::XorName;
#[derive(Debug)]
pub(crate) enum DkgCommand {
SendMessage {
recipients: Vec<(XorName, SocketAddr)>,
dkg_key: DkgKey,
message: DkgMessage,
},
ScheduleTimeout {
duration: Duration,
token: u64,
},
HandleOutcome {
section_auth: SectionAuthorityProvider,
outcome: SectionKeyShare,
},
SendFailureObservation {
recipients: Vec<(XorName, SocketAddr)>,
dkg_key: DkgKey,
signed: DkgFailureSigned,
non_participants: BTreeSet<XorName>,
},
HandleFailureAgreement(DkgFailureSignedSet),
}

impl DkgCommand {
fn into_command(self, node: &Node, key: bls::PublicKey) -> Result<Command> {
match self {
Self::SendMessage {
recipients,
dkg_key,
message,
} => {
let variant = Variant::DkgMessage { dkg_key, message };
let message =
RoutingMsg::single_src(node, DstLocation::DirectAndUnrouted, variant, key)?;

Ok(Command::send_message_to_nodes(
recipients.clone(),
recipients.len(),
message,
DestInfo {
dest: XorName::random(),
dest_section_pk: key,
},
))
}
Self::ScheduleTimeout { duration, token } => {
Ok(Command::ScheduleTimeout { duration, token })
}
Self::HandleOutcome {
section_auth,
outcome,
} => Ok(Command::HandleDkgOutcome {
section_auth,
outcome,
}),
Self::SendFailureObservation {
recipients,
dkg_key,
signed,
non_participants,
} => {
let variant = Variant::DkgFailureObservation {
dkg_key,
signed,
non_participants,
};
let message =
RoutingMsg::single_src(node, DstLocation::DirectAndUnrouted, variant, key)?;

Ok(Command::send_message_to_nodes(
recipients.clone(),
recipients.len(),
message,
DestInfo {
dest: XorName::random(),
dest_section_pk: key,
},
))
}
Self::HandleFailureAgreement(signeds) => Ok(Command::HandleDkgFailure(signeds)),
}
}
}

pub(crate) trait DkgCommands {
fn into_commands(self, node: &Node, key: bls::PublicKey) -> Result<Vec<Command>>;
}

impl DkgCommands for Vec<DkgCommand> {
fn into_commands(self, node: &Node, key: bls::PublicKey) -> Result<Vec<Command>> {
self.into_iter()
.map(|command| command.into_command(node, key))
.collect()
}
}

impl DkgCommands for Option<DkgCommand> {
fn into_commands(self, node: &Node, key: bls::PublicKey) -> Result<Vec<Command>> {
self.into_iter()
.map(|command| command.into_command(node, key))
.collect()
}
}
8 changes: 5 additions & 3 deletions src/dkg/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

mod dkg;
pub(crate) mod commands;
mod dkg_msgs_utils;
mod proposal;
mod section_signed;
mod session;
#[cfg(test)]
pub mod test_utils;
mod voter;

pub(crate) use self::{
dkg::{DkgCommands, DkgVoter},
dkg_msgs_utils::{DkgFailureSignedSetUtils, DkgFailureSignedUtils, DkgKeyUtils},
dkg_msgs_utils::{DkgFailureSignedSetUtils, DkgKeyUtils},
proposal::{ProposalAggregator, ProposalError, ProposalUtils},
voter::DkgVoter,
};
pub use section_signed::SectionSignedUtils;
use serde::Serialize;
Expand Down
Loading

0 comments on commit 4c78cb1

Please sign in to comment.