aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm/peer/members.rs
blob: 3df7674dd1d25a592f8195f0c1b910f0a266a1cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! In a swarm holding a store, a node syncs with members only (`docs/store.md`, Members):
//! the nodes its store names, each with the party its handshakes name. A handshake from
//! 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. 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 {
    let named = match message {
        ParsedMessage::Hello(hello) => Some(hello.party.clone()),
        ParsedMessage::HelloReply(reply) => Some(reply.party.clone()),
        _ => None,
    };
    let party = match &named {
        Some(party) => party.clone(),
        // Holding no store, it can't tell.
        None if !state.holding.tells_members() => return true,
        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) {
        if named.is_some() {
            state
                .get_or_create_peer_state(peer)
                .await
                .lock()
                .await
                .party = party;
        }
        return true;
    }
    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");
        }
        crate::swarm::topology::change_peer(state, &mut peer_state, |p| {
            p.standing = Standing::Stranger
        });
        peer_state.hello_state = HelloState::NotSent;
        peer_state.reset_sync();
    } else {
        debug!(peer = %peer, party = party.as_deref(), "A handshake of a node the store doesn't name: unanswered");
    }
    false
}