diff options
Diffstat (limited to 'src/swarm')
| -rw-r--r-- | src/swarm/peer/members.rs | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/src/swarm/peer/members.rs b/src/swarm/peer/members.rs index ddbd4a7..3df7674 100644 --- a/src/swarm/peer/members.rs +++ b/src/swarm/peer/members.rs @@ -3,13 +3,30 @@ //! any other goes unanswered, and what we had with it ends, as with a banned node; and so //! does what we had with a member at its next message, once the store names it no more. //! While the node holds no store yet, joining, it can't tell, and takes everyone of its -//! swarm: its seeds, which take it only where its party lists it. +//! swarm: its seeds, which take it only where its party lists it. From a peer whose +//! handshake it hasn't taken (its reply was lost, say), it reads nothing, and says HELLO +//! to it, as to any peer whose reply it waits for: the party its reply names tells. +use tokio::time::Instant; use tracing::{debug, warn}; use crate::state::{AppState, HelloState, Standing}; use crate::transport::ParsedMessage; +/// A message from `peer`, whose handshake this node hasn't taken: where it knows the peer, +/// its HELLO goes now, the first, or again where it waits for a reply. +async fn handshake_first(state: &AppState, peer: &str) { + let Some(peer_state) = state.get_peer_state(peer).await else { + debug!(peer = %peer, "A message of a node it doesn't know, before any handshake: unread"); + return; + }; + debug!(peer = %peer, "A message before its handshake was taken: unread; saying HELLO"); + if peer_state.lock().await.hello_again_now(Instant::now()) { + state.peers_due.now(); + } + super::handshake::ensure_handshake(state, peer).await; +} + /// Whether `message` from `peer` is a member's, by the party its handshakes name, which a /// handshake records; where it isn't, what we had with it ends. pub(super) async fn member(state: &AppState, peer: &str, message: &ParsedMessage) -> bool { @@ -22,10 +39,17 @@ pub(super) async fn member(state: &AppState, peer: &str, message: &ParsedMessage Some(party) => party.clone(), // Holding no store, it can't tell. None if !state.holding.tells_members() => return true, - None => match state.get_peer_state(peer).await { - Some(peer_state) => peer_state.lock().await.party.clone(), - None => None, - }, + None => { + let known = match state.get_peer_state(peer).await { + Some(peer_state) => peer_state.lock().await.party.clone(), + None => None, + }; + if known.is_none() { + handshake_first(state, peer).await; + return false; + } + known + } }; // A member, or it can't tell yet: the party a handshake names is kept. if state.holding.member(peer, party.as_deref()) != Some(false) { @@ -41,6 +65,10 @@ pub(super) async fn member(state: &AppState, peer: &str, message: &ParsedMessage } if let Some(peer_state) = state.get_peer_state(peer).await { let mut peer_state = peer_state.lock().await; + // Kept, so that what it sends next is known to be a non-member's. + if named.is_some() { + peer_state.party = party.clone(); + } if peer_state.standing.is_admitted() { warn!(peer = %peer, party = party.as_deref(), "Not a member of the store, as it reads now: no longer synced with"); } |
