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

Commit

Permalink
feat!: add sibling key to Event::EldersChanged
Browse files Browse the repository at this point in the history
BREAKING CHANGE: added new field to the `Event::EldersChanged` variant.
  • Loading branch information
madadam authored and oetyng committed Feb 24, 2021
1 parent 7009761 commit afd33e3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,13 @@ async fn handle_event(index: usize, node: &mut Routing, event: Event) -> bool {
Event::EldersChanged {
prefix,
key,
sibling_key,
elders,
self_status_change,
} => {
info!(
"Node #{} elders changed - prefix: {:b}, key: {:?}, elders: {:?}. Node elder status change: {:?}",
index, prefix, key, elders, self_status_change
"Node #{} elders changed - prefix: {:b}, key: {:?}, sibling key: {:?}, elders: {:?}, node elder status change: {:?}",
index, prefix, key, sibling_key, elders, self_status_change
);
}
Event::MessageReceived { content, src, dst } => info!(
Expand Down
1 change: 1 addition & 0 deletions examples/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl Network {
key,
elders,
self_status_change,
..
} => {
if let Some(Node::Joined {
name,
Expand Down
5 changes: 5 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub enum Event {
prefix: Prefix,
/// The BLS public key of our section.
key: bls::PublicKey,
/// The BLS public key of the sibling section, if this event is fired during a split.
/// Otherwise `None`.
sibling_key: Option<bls::PublicKey>,
/// The set of elders of our section.
elders: BTreeSet<XorName>,
/// Promoted, demoted or no change?
Expand Down Expand Up @@ -137,12 +140,14 @@ impl Debug for Event {
Self::EldersChanged {
prefix,
key,
sibling_key,
elders,
self_status_change,
} => formatter
.debug_struct("EldersChanged")
.field("prefix", prefix)
.field("key", key)
.field("sibling_key", sibling_key)
.field("elders", elders)
.field("self_status_change", self_status_change)
.finish(),
Expand Down
8 changes: 8 additions & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ impl Network {
self.keys.get_matching(name).map(|entry| &entry.value.1)
}

/// Returns the latest known key for a section with `prefix`.
/// If this returns `None` that means the latest known key is the genesis key.
pub fn key_by_prefix(&self, prefix: &Prefix) -> Option<&bls::PublicKey> {
self.keys
.get_equal_or_ancestor(prefix)
.map(|entry| &entry.value.1)
}

/// Returns the elders_info and the latest known key for the prefix that matches `name`,
/// excluding self section.
pub fn section_by_name(&self, name: &XorName) -> (Option<bls::PublicKey>, Option<EldersInfo>) {
Expand Down
12 changes: 12 additions & 0 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,9 +1691,21 @@ 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(),
sibling_key,
elders: self.section.elders_info().elders.keys().copied().collect(),
self_status_change,
});
Expand Down
1 change: 1 addition & 0 deletions src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl Routing {
state.send_event(Event::EldersChanged {
prefix: *section.prefix(),
key: *section.chain().last_key(),
sibling_key: None,
elders: section.elders_info().elders.keys().copied().collect(),
self_status_change: NodeElderChange::Promoted,
});
Expand Down

0 comments on commit afd33e3

Please sign in to comment.