node: Move `address` field out of `State`

We move the session address to the top-level struct, since it's needed
in a bunch of places. This change is required for the rate-limiting
code that is coming next.
This commit is contained in:
cloudhead 2023-08-08 11:39:01 +02:00
parent 66799e2211
commit 260b1a428c
No known key found for this signature in database
5 changed files with 31 additions and 33 deletions

View File

@ -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(),
),

View File

@ -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();

View File

@ -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));
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.
}
// 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(),

View File

@ -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

View File

@ -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,
}