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

Commit

Permalink
feat: new API: Routing::section_key
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam authored and oetyng committed Feb 24, 2021
1 parent afd33e3 commit 486ee61
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
35 changes: 24 additions & 11 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ impl Approved {
.clone())
}

/// Returns the latest known public key of the section with `prefix`.
pub fn section_key(&self, prefix: &Prefix) -> Option<&bls::PublicKey> {
if prefix == self.section.prefix() || prefix.is_extension_of(self.section.prefix()) {
Some(self.section.chain().last_key())
} else {
self.network.key_by_prefix(prefix).or_else(|| {
if self.is_elder() {
// We are elder - the first key is the genesis key
Some(self.section.chain().first_key())
} else {
// We are not elder - the chain might be truncated so the first key is not
// necessarily the genesis key.
None
}
})
}
}

/// Returns our index in the current BLS group if this node is a member of one, or
/// `Error::MissingSecretKeyShare` otherwise.
pub fn our_index(&self) -> Result<usize> {
Expand Down Expand Up @@ -1678,6 +1696,12 @@ impl Approved {
commands.extend(self.send_sync(self.section.clone(), self.network.clone())?);
}

let sibling_key = if new_prefix != old_prefix {
self.section_key(&new_prefix.sibling()).copied()
} else {
None
};

let self_status_change = if !old_is_elder && new_is_elder {
info!("Promoted to elder");
NodeElderChange::Promoted
Expand All @@ -1691,17 +1715,6 @@ impl Approved {
NodeElderChange::None
};

let sibling_key = if new_prefix != old_prefix {
Some(
*self
.network
.key_by_prefix(&new_prefix.sibling())
.unwrap_or_else(|| self.section.chain().last_key()),
)
} else {
None
};

self.send_event(Event::EldersChanged {
prefix: *self.section.prefix(),
key: *self.section.chain().last_key(),
Expand Down
5 changes: 5 additions & 0 deletions src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ impl Routing {
.collect()
}

/// Returns the last known public key of the section with `prefix`.
pub async fn section_key(&self, prefix: &Prefix) -> Option<bls::PublicKey> {
self.stage.state.lock().await.section_key(prefix).copied()
}

/// Returns the info about the section matches the name.
pub async fn match_section(
&self,
Expand Down

0 comments on commit 486ee61

Please sign in to comment.