Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Move `address` field out of `State`
cloudhead committed 2 years ago
commit 260b1a428cb719b02bbba1bf50f16b5666c59760
parent 66799e221183265d47d63de9b3c5654ec6fd32db
5 files changed +32 -34
modified radicle-cli/src/commands/node/control.rs
@@ -191,18 +191,18 @@ pub fn sessions(node: &Node) -> Result<Option<term::Table<4, term::Label>>, node
                term::Label::from(term::format::dim("initial")),
                term::Label::blank(),
            ),
-
            node::State::Attempted { addr } => (
-
                addr.to_string().into(),
+
            node::State::Attempted => (
+
                sess.addr.to_string().into(),
                term::Label::from(term::format::tertiary("attempted")),
                term::Label::blank(),
            ),
-
            node::State::Connected { addr, since, .. } => (
-
                addr.to_string().into(),
+
            node::State::Connected { since, .. } => (
+
                sess.addr.to_string().into(),
                term::Label::from(term::format::positive("connected")),
                term::format::dim(now - since).into(),
            ),
            node::State::Disconnected { retry_at, .. } => (
-
                term::Label::blank(),
+
                sess.addr.to_string().into(),
                term::Label::from(term::format::negative("disconnected")),
                term::format::dim(retry_at - now).into(),
            ),
modified radicle-node/src/runtime/handle.rs
@@ -221,6 +221,7 @@ impl radicle::node::Handle for Handle {
                .iter()
                .map(|(nid, s)| radicle::node::Session {
                    nid: *nid,
+
                    addr: s.addr.clone(),
                    state: s.state.clone(),
                })
                .collect();
modified radicle-node/src/service.rs
@@ -718,7 +718,7 @@ where
        debug!(target: "service", "Attempted connection to {nid} ({addr})");

        if let Some(sess) = self.sessions.get_mut(&nid) {
-
            sess.to_attempted(addr);
+
            sess.to_attempted();
        } else {
            #[cfg(debug_assertions)]
            panic!("Service::attempted: unknown session {nid}@{addr}");
@@ -730,13 +730,14 @@ where
        self.emitter.emit(Event::PeerConnected { nid: remote });

        let msgs = self.initial(link);
+
        let now = self.time();

        if link.is_outbound() {
            if let Some(peer) = self.sessions.get_mut(&remote) {
-
                let attempted = peer.to_connected(self.clock);
+
                peer.to_connected(self.clock);
                self.outbox.write_all(peer, msgs);

-
                if let Err(e) = self.addresses.connected(&remote, &attempted, self.time()) {
+
                if let Err(e) = self.addresses.connected(&remote, &peer.addr, now) {
                    error!(target: "service", "Error updating address book with connection: {e}");
                }
            }
@@ -811,17 +812,14 @@ where
    }

    pub fn received_message(&mut self, remote: NodeId, message: Message) {
-
        match self.handle_message(&remote, message) {
-
            Ok(_) => {}
-
            Err(err) => {
-
                // If there's an error, stop processing messages from this peer.
-
                // However, we still relay messages returned up to this point.
-
                self.outbox
-
                    .disconnect(remote, DisconnectReason::Session(err));
-

-
                // FIXME: The peer should be set in a state such that we don't
-
                // process further messages.
-
            }
+
        if let Err(err) = self.handle_message(&remote, message) {
+
            // If there's an error, stop processing messages from this peer.
+
            // However, we still relay messages returned up to this point.
+
            self.outbox
+
                .disconnect(remote, DisconnectReason::Session(err));
+

+
            // FIXME: The peer should be set in a state such that we don't
+
            // process further messages.
        }
    }

@@ -1106,9 +1104,9 @@ where

        match (&mut peer.state, message) {
            // Process a peer announcement.
-
            (session::State::Connected { addr, .. }, Message::Announcement(ann)) => {
+
            (session::State::Connected { .. }, Message::Announcement(ann)) => {
                let relayer = peer.id;
-
                let relayer_addr = addr.clone();
+
                let relayer_addr = peer.addr.clone();
                let announcer = ann.node;

                // Returning true here means that the message should be relayed.
@@ -1320,6 +1318,7 @@ where
            nid,
            Session::outbound(
                nid,
+
                addr.clone(),
                persistent,
                self.rng.clone(),
                self.config.limits.clone(),
modified radicle-node/src/service/session.rs
@@ -57,6 +57,8 @@ impl Error {
pub struct Session {
    /// Peer id.
    pub id: NodeId,
+
    /// Peer address.
+
    pub addr: Address,
    /// Connection direction.
    pub link: Link,
    /// Whether we should attempt to re-connect
@@ -101,9 +103,10 @@ impl fmt::Display for Session {
}

impl Session {
-
    pub fn outbound(id: NodeId, persistent: bool, rng: Rng, limits: Limits) -> Self {
+
    pub fn outbound(id: NodeId, addr: Address, persistent: bool, rng: Rng, limits: Limits) -> Self {
        Self {
            id,
+
            addr,
            state: State::Initial,
            link: Link::Outbound,
            subscribe: None,
@@ -126,8 +129,8 @@ impl Session {
    ) -> Self {
        Self {
            id,
+
            addr,
            state: State::Connected {
-
                addr,
                since: time,
                ping: PingState::default(),
                fetching: HashSet::default(),
@@ -193,30 +196,26 @@ impl Session {
        None
    }

-
    pub fn to_attempted(&mut self, addr: Address) {
+
    pub fn to_attempted(&mut self) {
        assert!(
            self.is_initial(),
            "Can only transition to 'attempted' state from 'initial' state"
        );
-
        self.state = State::Attempted { addr };
+
        self.state = State::Attempted;
        self.attempts += 1;
    }

-
    pub fn to_connected(&mut self, since: LocalTime) -> Address {
+
    pub fn to_connected(&mut self, since: LocalTime) {
        self.attempts = 0;

-
        let addr = if let State::Attempted { addr } = &self.state {
-
            addr.clone()
-
        } else {
+
        let State::Attempted = &self.state else {
            panic!("Session::to_connected: can only transition to 'connected' state from 'attempted' state");
        };
        self.state = State::Connected {
-
            addr: addr.clone(),
            since,
            ping: PingState::default(),
            fetching: HashSet::default(),
        };
-
        addr
    }

    /// Move the session state to "disconnected". Returns any pending RID
modified radicle/src/node.rs
@@ -72,11 +72,9 @@ pub enum State {
    /// Initial state for outgoing connections.
    Initial,
    /// Connection attempted successfully.
-
    Attempted { addr: Address },
+
    Attempted,
    /// Initial state after handshake protocol hand-off.
    Connected {
-
        /// Remote address.
-
        addr: Address,
        /// Connected since this time.
        since: LocalTime,
        /// Ping state.
@@ -375,6 +373,7 @@ impl Command {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Session {
    pub nid: NodeId,
+
    pub addr: Address,
    pub state: State,
}