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

Commit

Permalink
feat: joins_allowed flag to toggle accept new node or not
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Nov 26, 2020
1 parent 327bb4f commit 5def794
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/consensus/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ pub(crate) enum Vote {
message: Box<PlainMessage>,
proof_chain: SectionProofChain,
},

// Voted to concensus whether new node shall be allowed to join
JoinsAllowed(bool),
}

impl Vote {
Expand Down Expand Up @@ -95,6 +98,7 @@ impl<'a> Serialize for SignableView<'a> {
Vote::TheirKey { prefix, key } => (prefix, key).serialize(serializer),
Vote::TheirKnowledge { prefix, key_index } => (prefix, key_index).serialize(serializer),
Vote::SendMessage { message, .. } => message.as_signable().serialize(serializer),
Vote::JoinsAllowed(joins_allowed) => joins_allowed.serialize(serializer),
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub(crate) struct Approved {
relocate_state: Option<RelocateState>,
msg_filter: MessageFilter,
pub(super) event_tx: mpsc::UnboundedSender<Event>,
joins_allowed: bool,
}

impl Approved {
Expand Down Expand Up @@ -84,6 +85,7 @@ impl Approved {
relocate_state: None,
msg_filter: MessageFilter::new(),
event_tx,
joins_allowed: true,
}
}

Expand Down Expand Up @@ -196,6 +198,10 @@ impl Approved {
proof_chain,
proof,
)?]),
Vote::JoinsAllowed(joins_allowed) => {
self.joins_allowed = joins_allowed;
Ok(vec![])
}
}
}

Expand Down Expand Up @@ -992,6 +998,12 @@ impl Approved {
Some(details.pub_id),
Some(details.destination_key),
)
} else if !self.joins_allowed {
debug!(
"Ignoring JoinRequest from {} - new node not acceptable.",
pub_id,
);
return Ok(vec![]);
} else {
(MIN_AGE, None, None)
};
Expand Down Expand Up @@ -1405,6 +1417,7 @@ impl Approved {
fn update_state(&mut self, section: Section, network: Network) -> Result<Vec<Command>> {
let mut commands = vec![];

let old_elders_info = self.section.elders_info().clone();
let old_is_elder = self.is_elder();
let old_last_key_index = self.section.chain().last_key_index();
let old_prefix = *self.section.prefix();
Expand Down Expand Up @@ -1476,6 +1489,9 @@ impl Approved {

if !new_is_elder {
commands.extend(self.return_relocate_promise());
} else if &old_elders_info != self.section.elders_info() {
// Whenever there is an elders change, casting a round of joins_allowed vote to sync.
commands.extend(self.vote(Vote::JoinsAllowed(self.joins_allowed))?);
}

Ok(commands)
Expand Down Expand Up @@ -1715,6 +1731,14 @@ impl Approved {
Ok(Some(command))
}

pub fn set_joins_allowed(&mut self, joins_allowed: bool) -> Result<Vec<Command>> {
let mut commands = Vec::new();
if self.is_elder() && joins_allowed != self.joins_allowed {
commands.extend(self.vote(Vote::JoinsAllowed(joins_allowed))?);
}
Ok(commands)
}

pub fn send_user_message(
&mut self,
src: SrcLocation,
Expand Down
6 changes: 6 additions & 0 deletions src/routing/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub(crate) enum Command {
/// Message receiver to pass to the bootstrap task.
message_rx: mpsc::Receiver<(Message, SocketAddr)>,
},
/// Attempt to set JoinsAllowed flag.
SetJoinsAllowed(bool),
}

impl Command {
Expand Down Expand Up @@ -179,6 +181,10 @@ impl Debug for Command {
.field("bootstrap_addrs", bootstrap_addrs)
.field("details", details)
.finish(),
Self::SetJoinsAllowed(joins_allowed) => f
.debug_tuple("SetJoinsAllowed")
.field(joins_allowed)
.finish(),
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ impl Routing {
Ok((routing, event_stream))
}

/// Sets the JoinsAllowed flag.
pub async fn set_joins_allowed(&self, joins_allowed: bool) -> Result<()> {
let command = Command::SetJoinsAllowed(joins_allowed);
self.stage.clone().handle_commands(command).await
}

/// Returns the current age of this node.
pub async fn age(&self) -> u8 {
self.stage.state.lock().await.node().age
Expand Down
3 changes: 3 additions & 0 deletions src/routing/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ impl Stage {
self.handle_relocate(bootstrap_addrs, details, message_rx)
.await
}
Command::SetJoinsAllowed(joins_allowed) => {
self.state.lock().await.set_joins_allowed(joins_allowed)
}
}
}
.await;
Expand Down

0 comments on commit 5def794

Please sign in to comment.