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

Commit

Permalink
fix(dkg): allow multiple pending key shares
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Mar 4, 2021
1 parent 4a6e94b commit 92dfc70
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/section/section_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::error::{Error, Result};
use std::collections::VecDeque;
use std::collections::{HashMap, VecDeque};

/// All the key material needed to sign or combine signature for our section key.
#[derive(Debug)]
Expand All @@ -26,13 +26,17 @@ pub struct SectionKeysProvider {
/// A cache for current and previous section BLS keys.
cache: MiniKeyCache,
/// The new keys to use when section update completes.
pending: Option<SectionKeyShare>,
// TODO: evict outdated keys.
// TODO: alternatively, store the pending keys in DkgVoter instead. That way the outdated ones
// would get dropped when the DKG session itself gets dropped which we already have
// implemented.
pending: HashMap<bls::PublicKey, SectionKeyShare>,
}

impl SectionKeysProvider {
pub fn new(cache_size: u8, current: Option<SectionKeyShare>) -> Self {
let mut provider = Self {
pending: None,
pending: HashMap::new(),
cache: MiniKeyCache::with_capacity(cache_size as usize),
};
if let Some(share) = current {
Expand Down Expand Up @@ -60,16 +64,12 @@ impl SectionKeysProvider {
}

pub fn insert_dkg_outcome(&mut self, share: SectionKeyShare) {
self.pending = Some(share);
let public_key = share.public_key_set.public_key();
let _ = self.pending.insert(public_key, share);
}

pub fn finalise_dkg(&mut self, public_key: &bls::PublicKey) {
if let Some(share) = &self.pending {
if *public_key != share.public_key_set.public_key() {
return;
}
}
if let Some(share) = self.pending.take() {
if let Some(share) = self.pending.remove(public_key) {
if let Some(evicted) = self.cache.add(public_key, share) {
trace!("evicted old key from cache: {:?}", evicted);
}
Expand Down

0 comments on commit 92dfc70

Please sign in to comment.