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

Commit

Permalink
feat: allow rejoin with same name
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Nov 17, 2020
1 parent 28dccf1 commit ded038d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
46 changes: 42 additions & 4 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ use crate::{
SignedRelocateDetails,
},
section::{
EldersInfo, MemberInfo, Section, SectionKeyShare, SectionKeysProvider, SectionProofChain,
MIN_AGE,
EldersInfo, MemberInfo, PeerState, Section, SectionKeyShare, SectionKeysProvider,
SectionProofChain, MIN_AGE,
},
RECOMMENDED_SECTION_SIZE,
};
use bls_dkg::key_gen::outcome::Outcome as DkgOutcome;
use bytes::Bytes;
use itertools::Itertools;
use std::{net::SocketAddr, slice};
use std::{cmp, net::SocketAddr, slice};
use tokio::sync::mpsc;
use xor_name::{Prefix, XorName};

Expand Down Expand Up @@ -213,7 +213,7 @@ impl Approved {
}

if let Some(info) = self.section.members().get(&name) {
let info = info.clone().leave();
let info = info.clone().leave()?;
self.vote(Vote::Offline(info))
} else {
Ok(vec![])
Expand Down Expand Up @@ -1086,6 +1086,20 @@ impl Approved {
self.send_relocate(&peer, details)
}

fn relocate_rejoin_peer(&self, peer: Peer, age: u8) -> Result<Vec<Command>> {
let details =
RelocateDetails::with_age(&self.section, &self.network, &peer, *peer.name(), age);

trace!(
"Relocating {:?} to {} with age {} due to rejoin",
peer,
details.destination,
details.age
);

self.send_relocate(&peer, details)
}

// Are we in the startup phase? Startup phase is when the network consists of only one section
// and it has no more than `recommended_section_size` members.
fn is_in_startup_phase(&self) -> bool {
Expand All @@ -1105,6 +1119,30 @@ impl Approved {

let mut commands = vec![];

if let Some(info) = self.section.members().get(peer.name()) {
// This node is rejoin with same name.

if info.state != PeerState::Left {
debug!(
"Ignoring Online node {} - {:?} not Left.",
peer.name(),
info.state,
);
return Ok(commands);
}
// To avoid during startup_phase, a rejoin node being given a randmly higher age,
// force it to be halved.
if self.is_in_startup_phase() || info.peer.age() / 2 > MIN_AGE {
// TODO: consider handling the relocation inside the bootstrap phase, to avoid having
// to send this `NodeApproval`.
commands.push(self.send_node_approval(&peer, their_knowledge)?);
commands.extend(
self.relocate_rejoin_peer(peer, cmp::max(MIN_AGE, info.peer.age() / 2))?,
);
return Ok(commands);
}
}

if self.is_in_startup_phase() && peer.age() <= MIN_AGE {
// In startup phase, instantly relocate the joining peer in order to promote it to
// adult.
Expand Down
2 changes: 1 addition & 1 deletion src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ async fn handle_consensus_on_offline_of_elder() -> Result<()> {
.members()
.get(remove_peer.name())
.expect("member not found")
.leave();
.leave()?;

// Create our node
let (event_tx, mut event_rx) = mpsc::unbounded_channel();
Expand Down
12 changes: 8 additions & 4 deletions src/section/member_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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::peer::Peer;
use crate::{error::Error, peer::Peer};
use serde::{Deserialize, Serialize};
use xor_name::XorName;

Expand Down Expand Up @@ -35,11 +35,15 @@ impl MemberInfo {
self.peer.age() > MIN_AGE
}

pub fn leave(self) -> Self {
Self {
pub fn leave(self) -> Result<Self, Error> {
// Do not vote Offline when already relocated, to avoid rejoining with the same name.
if let PeerState::Relocated(_) = self.state {
return Err(Error::InvalidState);
}
Ok(Self {
state: PeerState::Left,
..self
}
})
}

// Convert this info into one with the state changed to `Relocated`.
Expand Down

0 comments on commit ded038d

Please sign in to comment.