node: follow up on session renaming

Rename the module to session from peer to be consistent with recent
renaming of the type from Peer to Session.  Remove stutter by renaming
session::SessionState to session::State, and session::SessionError to
session::Error.

Signed-off-by: Slack Coder <slackcoder@server.ky>
This commit is contained in:
Slack Coder 2022-10-14 09:44:58 -05:00
parent c11f5b13bf
commit a8895aee80
4 changed files with 48 additions and 45 deletions

View File

@ -1,9 +1,9 @@
pub mod config; pub mod config;
pub mod filter; pub mod filter;
pub mod message; pub mod message;
pub mod peer;
pub mod reactor; pub mod reactor;
pub mod routing; pub mod routing;
pub mod session;
use std::collections::hash_map::Entry; use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
@ -32,13 +32,12 @@ use crate::node;
use crate::service::config::ProjectTracking; use crate::service::config::ProjectTracking;
use crate::service::message::{Address, Announcement, AnnouncementMessage, Ping}; use crate::service::message::{Address, Announcement, AnnouncementMessage, Ping};
use crate::service::message::{NodeAnnouncement, RefsAnnouncement}; use crate::service::message::{NodeAnnouncement, RefsAnnouncement};
use crate::service::peer::{PingState, SessionError, SessionState};
use crate::storage; use crate::storage;
use crate::storage::{Inventory, ReadRepository, RefUpdate, WriteRepository, WriteStorage}; use crate::storage::{Inventory, ReadRepository, RefUpdate, WriteRepository, WriteStorage};
pub use crate::service::config::{Config, Network}; pub use crate::service::config::{Config, Network};
pub use crate::service::message::{Message, ZeroBytes}; pub use crate::service::message::{Message, ZeroBytes};
pub use crate::service::peer::Session; pub use crate::service::session::Session;
use self::gossip::Gossip; use self::gossip::Gossip;
use self::message::InventoryAnnouncement; use self::message::InventoryAnnouncement;
@ -550,7 +549,7 @@ where
debug!("Disconnected from {} ({})", ip, reason); debug!("Disconnected from {} ({})", ip, reason);
if let Some(peer) = self.sessions.get_mut(&ip) { if let Some(peer) = self.sessions.get_mut(&ip) {
peer.state = SessionState::Disconnected { since }; peer.state = session::State::Disconnected { since };
// Attempt to re-connect to persistent peers. // Attempt to re-connect to persistent peers.
if self.config.is_persistent(&address) && peer.attempts() < MAX_CONNECTION_ATTEMPTS { if self.config.is_persistent(&address) && peer.attempts() < MAX_CONNECTION_ATTEMPTS {
@ -579,7 +578,7 @@ where
pub fn received_message(&mut self, addr: &net::SocketAddr, message: Message) { pub fn received_message(&mut self, addr: &net::SocketAddr, message: Message) {
match self.handle_message(addr, message) { match self.handle_message(addr, message) {
Err(SessionError::NotFound(ip)) => { Err(session::Error::NotFound(ip)) => {
error!("Session not found for {ip}"); error!("Session not found for {ip}");
} }
Err(err) => { Err(err) => {
@ -602,9 +601,9 @@ where
&mut self, &mut self,
session: &NodeId, session: &NodeId,
announcement: &Announcement, announcement: &Announcement,
) -> Result<bool, peer::SessionError> { ) -> Result<bool, session::Error> {
if !announcement.verify() { if !announcement.verify() {
return Err(SessionError::Misbehavior); return Err(session::Error::Misbehavior);
} }
let Announcement { node, message, .. } = announcement; let Announcement { node, message, .. } = announcement;
let now = self.clock.local_time(); let now = self.clock.local_time();
@ -614,7 +613,7 @@ where
// Don't allow messages from too far in the future. // Don't allow messages from too far in the future.
if timestamp.saturating_sub(now.as_secs()) > MAX_TIME_DELTA.as_secs() { if timestamp.saturating_sub(now.as_secs()) > MAX_TIME_DELTA.as_secs() {
return Err(SessionError::InvalidTimestamp(timestamp)); return Err(session::Error::InvalidTimestamp(timestamp));
} }
match message { match message {
@ -632,7 +631,7 @@ where
if let Error::Fetch(storage::FetchError::Verify(err)) = err { if let Error::Fetch(storage::FetchError::Verify(err)) = err {
// Disconnect the peer if it is the signer of this message. // Disconnect the peer if it is the signer of this message.
if node == session { if node == session {
return Err(peer::SessionError::VerificationFailed(err)); return Err(session::Error::VerificationFailed(err));
} }
} }
// There's not much we can do if the peer sending us this message isn't the // There's not much we can do if the peer sending us this message isn't the
@ -741,21 +740,21 @@ where
&mut self, &mut self,
remote: &net::SocketAddr, remote: &net::SocketAddr,
message: Message, message: Message,
) -> Result<(), peer::SessionError> { ) -> Result<(), session::Error> {
let peer_ip = remote.ip(); let peer_ip = remote.ip();
let peer = if let Some(peer) = self.sessions.get_mut(&peer_ip) { let peer = if let Some(peer) = self.sessions.get_mut(&peer_ip) {
peer peer
} else { } else {
return Err(SessionError::NotFound(remote.ip())); return Err(session::Error::NotFound(remote.ip()));
}; };
peer.last_active = self.clock.local_time(); peer.last_active = self.clock.local_time();
debug!("Received {:?} from {}", &message, peer.ip()); debug!("Received {:?} from {}", &message, peer.ip());
match (&mut peer.state, message) { match (&mut peer.state, message) {
(SessionState::Initial, Message::Initialize { id, version, addrs }) => { (session::State::Initial, Message::Initialize { id, version, addrs }) => {
if version != PROTOCOL_VERSION { if version != PROTOCOL_VERSION {
return Err(SessionError::WrongVersion(version)); return Err(session::Error::WrongVersion(version));
} }
// Nb. This is a very primitive handshake. Eventually we should have anyhow // Nb. This is a very primitive handshake. Eventually we should have anyhow
// extra "acknowledgment" message sent when the `Initialize` is well received. // extra "acknowledgment" message sent when the `Initialize` is well received.
@ -773,22 +772,22 @@ where
// Nb. we don't set the peer timestamp here, since it is going to be // Nb. we don't set the peer timestamp here, since it is going to be
// set after the first message is received only. Setting it here would // set after the first message is received only. Setting it here would
// mean that messages received right after the handshake could be ignored. // mean that messages received right after the handshake could be ignored.
peer.state = SessionState::Negotiated { peer.state = session::State::Negotiated {
id, id,
since: self.clock.local_time(), since: self.clock.local_time(),
addrs, addrs,
ping: Default::default(), ping: Default::default(),
}; };
} }
(SessionState::Initial, _) => { (session::State::Initial, _) => {
debug!( debug!(
"Disconnecting peer {} for sending us a message before handshake", "Disconnecting peer {} for sending us a message before handshake",
peer.ip() peer.ip()
); );
return Err(SessionError::Misbehavior); return Err(session::Error::Misbehavior);
} }
// Process a peer announcement. // Process a peer announcement.
(SessionState::Negotiated { id, .. }, Message::Announcement(ann)) => { (session::State::Negotiated { id, .. }, Message::Announcement(ann)) => {
let id = *id; let id = *id;
// Returning true here means that the message should be relayed. // Returning true here means that the message should be relayed.
@ -809,7 +808,7 @@ where
return Ok(()); return Ok(());
} }
} }
(SessionState::Negotiated { .. }, Message::Subscribe(subscribe)) => { (session::State::Negotiated { .. }, Message::Subscribe(subscribe)) => {
for msg in self for msg in self
.gossip .gossip
.filtered(&subscribe.filter, subscribe.since, subscribe.until) .filtered(&subscribe.filter, subscribe.since, subscribe.until)
@ -818,14 +817,14 @@ where
} }
peer.subscribe = Some(subscribe); peer.subscribe = Some(subscribe);
} }
(SessionState::Negotiated { .. }, Message::Initialize { .. }) => { (session::State::Negotiated { .. }, Message::Initialize { .. }) => {
debug!( debug!(
"Disconnecting peer {} for sending us a redundant handshake message", "Disconnecting peer {} for sending us a redundant handshake message",
peer.ip() peer.ip()
); );
return Err(SessionError::Misbehavior); return Err(session::Error::Misbehavior);
} }
(SessionState::Negotiated { .. }, Message::Ping(Ping { ponglen, .. })) => { (session::State::Negotiated { .. }, Message::Ping(Ping { ponglen, .. })) => {
// Ignore pings which ask for too much data. // Ignore pings which ask for too much data.
if ponglen > Ping::MAX_PONG_ZEROES { if ponglen > Ping::MAX_PONG_ZEROES {
return Ok(()); return Ok(());
@ -837,14 +836,14 @@ where
}, },
); );
} }
(SessionState::Negotiated { ping, .. }, Message::Pong { zeroes }) => { (session::State::Negotiated { ping, .. }, Message::Pong { zeroes }) => {
if let PingState::AwaitingResponse(ponglen) = *ping { if let session::PingState::AwaitingResponse(ponglen) = *ping {
if (ponglen as usize) == zeroes.len() { if (ponglen as usize) == zeroes.len() {
*ping = PingState::Ok; *ping = session::PingState::Ok;
} }
} }
} }
(SessionState::Disconnected { .. }, msg) => { (session::State::Disconnected { .. }, msg) => {
debug!("Ignoring {:?} from disconnected peer {}", msg, peer.ip()); debug!("Ignoring {:?} from disconnected peer {}", msg, peer.ip());
} }
} }
@ -916,8 +915,10 @@ where
.negotiated() .negotiated()
.filter(|(_, _, session)| session.last_active < *now - STALE_CONNECTION_TIMEOUT); .filter(|(_, _, session)| session.last_active < *now - STALE_CONNECTION_TIMEOUT);
for (_, _, session) in stale { for (_, _, session) in stale {
self.reactor self.reactor.disconnect(
.disconnect(session.addr, DisconnectReason::Error(SessionError::Timeout)); session.addr,
DisconnectReason::Error(session::Error::Timeout),
);
} }
} }
@ -995,7 +996,7 @@ where
#[derive(Debug)] #[derive(Debug)]
pub enum DisconnectReason { pub enum DisconnectReason {
User, User,
Error(SessionError), Error(session::Error),
} }
impl DisconnectReason { impl DisconnectReason {
@ -1113,7 +1114,7 @@ impl Sessions {
pub fn by_id(&self, id: &NodeId) -> Option<&Session> { pub fn by_id(&self, id: &NodeId) -> Option<&Session> {
self.0.values().find(|p| { self.0.values().find(|p| {
if let SessionState::Negotiated { id: _id, .. } = &p.state { if let session::State::Negotiated { id: _id, .. } = &p.state {
_id == id _id == id
} else { } else {
false false
@ -1126,7 +1127,7 @@ impl Sessions {
self.0 self.0
.iter() .iter()
.filter_map(move |(ip, sess)| match &sess.state { .filter_map(move |(ip, sess)| match &sess.state {
SessionState::Negotiated { id, .. } => Some((ip, id, sess)), session::State::Negotiated { id, .. } => Some((ip, id, sess)),
_ => None, _ => None,
}) })
} }

View File

@ -4,7 +4,7 @@ use std::net;
use log::*; use log::*;
use crate::prelude::*; use crate::prelude::*;
use crate::service::peer::Session; use crate::service::session::Session;
use super::message::{Announcement, AnnouncementMessage}; use super::message::{Announcement, AnnouncementMessage};

View File

@ -1,5 +1,8 @@
use crate::service::message::*; use crate::service::message;
use crate::service::*; use crate::service::message::Message;
use crate::service::net;
use crate::service::storage;
use crate::service::{Link, LocalTime, NodeId, Reactor, Rng};
#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)] #[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
pub enum PingState { pub enum PingState {
@ -14,7 +17,7 @@ pub enum PingState {
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
#[allow(clippy::large_enum_variant)] #[allow(clippy::large_enum_variant)]
pub enum SessionState { pub enum State {
/// Initial peer state. For outgoing peers this /// Initial peer state. For outgoing peers this
/// means we've attempted a connection. For incoming /// means we've attempted a connection. For incoming
/// peers, this means they've successfully connected /// peers, this means they've successfully connected
@ -27,7 +30,7 @@ pub enum SessionState {
id: NodeId, id: NodeId,
since: LocalTime, since: LocalTime,
/// Addresses this peer is reachable on. /// Addresses this peer is reachable on.
addrs: Vec<Address>, addrs: Vec<message::Address>,
ping: PingState, ping: PingState,
}, },
/// When a peer is disconnected. /// When a peer is disconnected.
@ -35,7 +38,7 @@ pub enum SessionState {
} }
#[derive(thiserror::Error, Debug)] #[derive(thiserror::Error, Debug)]
pub enum SessionError { pub enum Error {
#[error("wrong protocol version in message: {0}")] #[error("wrong protocol version in message: {0}")]
WrongVersion(u32), WrongVersion(u32),
#[error("invalid announcement timestamp: {0}")] #[error("invalid announcement timestamp: {0}")]
@ -61,9 +64,9 @@ pub struct Session {
/// to this peer upon disconnection. /// to this peer upon disconnection.
pub persistent: bool, pub persistent: bool,
/// Peer connection state. /// Peer connection state.
pub state: SessionState, pub state: State,
/// Peer subscription. /// Peer subscription.
pub subscribe: Option<Subscribe>, pub subscribe: Option<message::Subscribe>,
/// Last time a message was received from the peer. /// Last time a message was received from the peer.
pub last_active: LocalTime, pub last_active: LocalTime,
@ -80,7 +83,7 @@ impl Session {
pub fn new(addr: net::SocketAddr, link: Link, persistent: bool, rng: Rng) -> Self { pub fn new(addr: net::SocketAddr, link: Link, persistent: bool, rng: Rng) -> Self {
Self { Self {
addr, addr,
state: SessionState::default(), state: State::default(),
link, link,
subscribe: None, subscribe: None,
persistent, persistent,
@ -90,12 +93,12 @@ impl Session {
} }
} }
pub fn ip(&self) -> IpAddr { pub fn ip(&self) -> net::IpAddr {
self.addr.ip() self.addr.ip()
} }
pub fn is_negotiated(&self) -> bool { pub fn is_negotiated(&self) -> bool {
matches!(self.state, SessionState::Negotiated { .. }) matches!(self.state, State::Negotiated { .. })
} }
pub fn attempts(&self) -> usize { pub fn attempts(&self) -> usize {
@ -110,8 +113,8 @@ impl Session {
self.attempts = 0; self.attempts = 0;
} }
pub fn ping(&mut self, reactor: &mut Reactor) -> Result<(), SessionError> { pub fn ping(&mut self, reactor: &mut Reactor) -> Result<(), Error> {
if let SessionState::Negotiated { ping, .. } = &mut self.state { if let State::Negotiated { ping, .. } = &mut self.state {
let msg = message::Ping::new(&mut self.rng); let msg = message::Ping::new(&mut self.rng);
*ping = PingState::AwaitingResponse(msg.ponglen); *ping = PingState::AwaitingResponse(msg.ponglen);

View File

@ -10,7 +10,6 @@ use crate::prelude::{LocalDuration, Timestamp};
use crate::service::config::*; use crate::service::config::*;
use crate::service::filter::Filter; use crate::service::filter::Filter;
use crate::service::message::*; use crate::service::message::*;
use crate::service::peer::*;
use crate::service::reactor::Io; use crate::service::reactor::Io;
use crate::service::ServiceState as _; use crate::service::ServiceState as _;
use crate::service::*; use crate::service::*;
@ -280,7 +279,7 @@ fn test_inventory_relay_bad_timestamp() {
); );
assert_matches!( assert_matches!(
alice.outbox().next(), alice.outbox().next(),
Some(Io::Disconnect(addr, DisconnectReason::Error(SessionError::InvalidTimestamp(t)))) Some(Io::Disconnect(addr, DisconnectReason::Error(session::Error::InvalidTimestamp(t))))
if addr == bob.addr() && t == timestamp if addr == bob.addr() && t == timestamp
); );
} }