-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from grumbach/dkg_messages
DKG votes, sigs, knowledge and AE ground work
- Loading branch information
Showing
7 changed files
with
447 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2022 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::sdkg::Error as DkgError; | ||
use thiserror::Error; | ||
|
||
/// All the Dkg Errors | ||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
/// SDKG faults and errors | ||
#[error("Dkg Error")] | ||
Sdkg(#[from] DkgError), | ||
/// Encoding | ||
#[error("Failed to encode with bincode")] | ||
Encoding(#[from] bincode::Error), | ||
/// Invalid DkgState init input parameters | ||
#[error("Failed to initialize DkgState: secret key is not in provided pub key set")] | ||
NotInPubKeySet, | ||
/// Invalid signature | ||
#[error("Invalid signature")] | ||
InvalidSignature, | ||
/// Got vote from an unknown sender id | ||
#[error("Unknown sender")] | ||
UnknownSender, | ||
/// Got a faulty vote, some nodes are dishonest or dysfunctional | ||
#[error("Faulty vote {0}")] | ||
FaultyVote(String), | ||
/// Unexpectedly failed to generate secret key share | ||
#[error("Unexpectedly failed to generate secret key share")] | ||
FailedToGenerateSecretKeyShare, | ||
} | ||
|
||
pub type Result<T> = std::result::Result<T, Error>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright 2022 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 log::debug; | ||
use std::collections::{BTreeMap, BTreeSet}; | ||
use thiserror::Error; | ||
|
||
use crate::vote::{DkgVote, IdAck, IdPart, NodeId}; | ||
|
||
pub(crate) struct Knowledge { | ||
pub parts: BTreeSet<IdPart>, | ||
pub part_acks: BTreeMap<IdPart, BTreeSet<IdAck>>, | ||
pub agreed_with_all_acks: BTreeSet<NodeId>, | ||
} | ||
|
||
#[derive(Error, Debug)] | ||
pub(crate) enum KnowledgeFault { | ||
#[error("Missing Parts to deal with Acks in received vote")] | ||
MissingParts, | ||
#[error("Parts in received vote differ from ours")] | ||
IncompatibleParts, | ||
#[error("Missing Acks to deal with AllAcks in received vote")] | ||
MissingAcks, | ||
#[error("Acks received in vote differ from ours")] | ||
IncompatibleAcks, | ||
} | ||
|
||
impl Knowledge { | ||
fn new() -> Self { | ||
Knowledge { | ||
parts: BTreeSet::new(), | ||
part_acks: BTreeMap::new(), | ||
agreed_with_all_acks: BTreeSet::new(), | ||
} | ||
} | ||
|
||
pub fn from_votes(mut votes: Vec<(DkgVote, NodeId)>) -> Result<Self, KnowledgeFault> { | ||
votes.sort(); | ||
let mut knowledge = Knowledge::new(); | ||
for (vote, voter_id) in votes { | ||
knowledge.handle_vote(vote, voter_id)?; | ||
} | ||
|
||
Ok(knowledge) | ||
} | ||
|
||
pub fn got_all_acks(&self, num_participants: usize) -> bool { | ||
self.part_acks.len() == num_participants | ||
&& self | ||
.part_acks | ||
.iter() | ||
.all(|(_, a)| a.len() == num_participants) | ||
} | ||
|
||
fn handle_vote(&mut self, vote: DkgVote, id: NodeId) -> Result<(), KnowledgeFault> { | ||
match vote { | ||
DkgVote::SinglePart(part) => { | ||
self.parts.insert((id, part)); | ||
} | ||
DkgVote::SingleAck(acked_parts) => { | ||
let parts: BTreeSet<_> = acked_parts.keys().cloned().collect(); | ||
if self.parts.len() < parts.len() { | ||
return Err(KnowledgeFault::MissingParts); | ||
} else if self.parts != parts { | ||
debug!( | ||
"IncompatibleParts: ours: {:?}, theirs: {:?}", | ||
self.parts, parts | ||
); | ||
return Err(KnowledgeFault::IncompatibleParts); | ||
} | ||
for (id_part, ack) in acked_parts { | ||
let acks = self.part_acks.entry(id_part).or_default(); | ||
acks.insert((id, ack)); | ||
} | ||
} | ||
DkgVote::AllAcks(all_acks) => { | ||
if !self.got_all_acks(self.parts.len()) { | ||
return Err(KnowledgeFault::MissingAcks); | ||
} else if all_acks != self.part_acks { | ||
debug!( | ||
"IncompatibleAcks: ours: {:?}, theirs: {:?}", | ||
self.part_acks, all_acks | ||
); | ||
return Err(KnowledgeFault::IncompatibleAcks); | ||
} | ||
self.agreed_with_all_acks.insert(id); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.