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

Commit

Permalink
feat: remove old DKG sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Jan 18, 2021
1 parent 3391c7f commit c8db72f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
24 changes: 15 additions & 9 deletions src/consensus/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,17 @@ impl DkgVoter {
keypair: &Keypair,
dkg_key: DkgKey,
elders_info: EldersInfo,
key_index: u64,
) -> Vec<DkgCommand> {
if let Some(session) = self.sessions.get(&dkg_key) {
if !session.complete {
if !session.complete && session.key_index >= key_index {
trace!("DKG for {} already in progress", elders_info);
return vec![];
}
}

let name = crypto::name(&keypair.public);
let index = if let Some(index) = elders_info.position(&name) {
let participant_index = if let Some(index) = elders_info.position(&name) {
index
} else {
error!(
Expand All @@ -132,7 +133,7 @@ impl DkgVoter {
elders_info,
outcome: SectionKeyShare {
public_key_set: secret_key_set.public_keys(),
index,
index: participant_index,
secret_key_share: secret_key_set.secret_key_share(0),
},
}];
Expand All @@ -153,7 +154,8 @@ impl DkgVoter {
let mut session = Session {
key_gen,
elders_info,
index,
key_index,
participant_index,
timer_token: 0,
failures: Default::default(),
complete: false,
Expand All @@ -169,6 +171,8 @@ impl DkgVoter {
);

let _ = self.sessions.insert(dkg_key, session);
self.sessions
.retain(|_, session| session.key_index >= key_index);

commands
}
Expand Down Expand Up @@ -222,8 +226,10 @@ impl DkgVoter {
// Data for a DKG participant.
struct Session {
elders_info: EldersInfo,
// Section chain index of the key to be generated.
key_index: u64,
// Our participant index.
index: usize,
participant_index: usize,
key_gen: KeyGen,
timer_token: u64,
failures: DkgFailureProofSet,
Expand Down Expand Up @@ -266,7 +272,7 @@ impl Session {
self.elders_info
.peers()
.enumerate()
.filter(|(index, _)| *index != self.index)
.filter(|(index, _)| *index != self.participant_index)
.map(|(_, peer)| peer.addr())
.copied()
.collect()
Expand Down Expand Up @@ -345,7 +351,7 @@ impl Session {

let outcome = SectionKeyShare {
public_key_set: outcome.public_key_set,
index: self.index,
index: self.participant_index,
secret_key_share: outcome.secret_key_share,
};

Expand Down Expand Up @@ -672,7 +678,7 @@ mod tests {
let elders_info = EldersInfo::new(iter::once(node.peer()), Prefix::default());
let dkg_key = DkgKey::new(&elders_info);

let commands = voter.start(&node.keypair, dkg_key, elders_info);
let commands = voter.start(&node.keypair, dkg_key, elders_info, 0);
assert_matches!(&commands[..], &[DkgCommand::HandleOutcome { .. }]);
}

Expand Down Expand Up @@ -702,7 +708,7 @@ mod tests {
for actor in actors.values_mut() {
let commands = actor
.voter
.start(&actor.node.keypair, dkg_key, elders_info.clone());
.start(&actor.node.keypair, dkg_key, elders_info.clone(), 0);

for command in commands {
messages.extend(actor.handle(command, &dkg_key))
Expand Down
4 changes: 4 additions & 0 deletions src/messages/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ pub(crate) enum Variant {
dkg_key: DkgKey,
/// The DKG particpants.
elders_info: EldersInfo,
/// The section chain index of the key to be generated.
key_index: u64,
},
/// Message exchanged for DKG process.
DKGMessage {
Expand Down Expand Up @@ -199,10 +201,12 @@ impl Debug for Variant {
Self::DKGStart {
dkg_key,
elders_info,
key_index,
} => f
.debug_struct("DKGStart")
.field("dkg_key", dkg_key)
.field("elders_info", elders_info)
.field("key_index", key_index)
.finish(),
Self::DKGMessage { dkg_key, message } => f
.debug_struct("DKGMessage")
Expand Down
7 changes: 5 additions & 2 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,8 @@ impl Approved {
Variant::DKGStart {
dkg_key,
elders_info,
} => self.handle_dkg_start(*dkg_key, elders_info.clone()),
key_index,
} => self.handle_dkg_start(*dkg_key, elders_info.clone(), *key_index),
Variant::DKGMessage { dkg_key, message } => {
self.handle_dkg_message(*dkg_key, message.clone(), msg.src().to_node_name()?)
}
Expand Down Expand Up @@ -1097,10 +1098,11 @@ impl Approved {
&mut self,
dkg_key: DkgKey,
new_elders_info: EldersInfo,
key_index: u64,
) -> Result<Vec<Command>> {
trace!("Received DKGStart for {}", new_elders_info);
self.dkg_voter
.start(&self.node.keypair, dkg_key, new_elders_info)
.start(&self.node.keypair, dkg_key, new_elders_info, key_index)
.into_commands(&self.node)
}

Expand Down Expand Up @@ -1742,6 +1744,7 @@ impl Approved {
let variant = Variant::DKGStart {
dkg_key,
elders_info,
key_index: self.section.chain().last_key_index() + 1,
};
let vote = self.create_send_message_vote(DstLocation::Direct, variant, None)?;
self.send_vote(recipients, vote)
Expand Down

0 comments on commit c8db72f

Please sign in to comment.