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

Commit

Permalink
fix: send Event::Connected only after transition to Approved
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam authored and dirvine committed Oct 19, 2020
1 parent e62e65e commit dbe0593
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/instance/stage/joining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use super::{approved::Approved, Command, State};
use crate::{
error::{Error, Result},
event::{Connected, Event},
event::Connected,
messages::{BootstrapResponse, JoinRequest, Message, Variant, VerifyStatus},
node::Node,
peer::Peer,
Expand Down Expand Up @@ -89,7 +89,6 @@ impl Joining {
verify_message(&msg, trusted_key)?;

// Transition from Joining to Approved
let connect_type = self.connect_type();
let section_chain = msg.proof_chain()?.clone();

info!(
Expand All @@ -101,8 +100,6 @@ impl Joining {
let state = Approved::new(section, None, self.node.clone());
let state = State::Approved(state);

self.node.send_event(Event::Connected(connect_type));

Ok(vec![Command::Transition(Box::new(state))])
}
_ => {
Expand Down Expand Up @@ -146,7 +143,7 @@ impl Joining {
}

// Are we relocating or joining for the first time?
fn connect_type(&self) -> Connected {
pub fn connect_type(&self) -> Connected {
match &self.join_type {
JoinType::First { .. } => Connected::First,
JoinType::Relocate(payload) => Connected::Relocate {
Expand Down
14 changes: 13 additions & 1 deletion src/instance/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl Stage {
Ok(vec![self.handle_schedule_timeout(duration, token).await])
}
Command::Transition(state) => {
*self.state.lock().await = *state;
self.handle_transition(*state).await;
Ok(vec![])
}
}
Expand Down Expand Up @@ -394,6 +394,18 @@ impl Stage {
Command::HandleTimeout(token)
}

async fn handle_transition(&self, new_state: State) {
let mut old_state = self.state.lock().await;

// Sending `Event::Connected` here to make sure the transition is complete by the time the
// event is received.
if let (State::Joining(old), State::Approved(_)) = (&*old_state, &new_state) {
old.node.send_event(Event::Connected(old.connect_type()));
}

*old_state = new_state
}

async fn log_ident(&self) -> String {
match &*self.state.lock().await {
State::Bootstrapping(state) => format!("{}(?) ", state.node.name()),
Expand Down
1 change: 1 addition & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub(crate) struct Node {
pub addr: SocketAddr,
pub age: u8,
pub network_params: NetworkParams,
// TODO: move this event sender somewhere else. This is not an approprate place for it.
event_tx: mpsc::UnboundedSender<Event>,
}

Expand Down

0 comments on commit dbe0593

Please sign in to comment.