aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/federation/held.rs23
-rw-r--r--src/state/peer.rs5
-rw-r--r--src/swarm/peer/inbound.rs7
-rw-r--r--src/swarm/peer/members.rs56
-rw-r--r--src/swarm/peer/mod.rs3
5 files changed, 94 insertions, 0 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 5b9a740..fe8b6a9 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -337,6 +337,11 @@ impl Holding {
Some(party.is_some_and(|party| members.contains(&(node.to_owned(), party.to_owned()))))
}
+ /// Whether the node holds a store, and so tells who is a member (`member`).
+ pub(crate) fn tells_members(&self) -> bool {
+ self.members.read_or_recover().is_some()
+ }
+
/// The members the store names, each with its party, while the node holds a store.
pub(crate) fn members(&self) -> Option<BTreeSet<(String, String)>> {
self.members.read_or_recover().clone()
@@ -391,6 +396,8 @@ fn found(pin: &Id, root: &[u8], events: &BTreeMap<Id, Vec<u8>>) -> Option<Built>
pub(crate) async fn run_worker(state: crate::state::AppState) {
let mut changed = state.change_tx.subscribe();
let mut due: Option<tokio::time::Instant> = None;
+ // Whether this node is a member of its store, as last said.
+ let mut listed: Option<bool> = None;
let mut looks = tokio::time::interval(LOOK);
looks.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
let mut look = false;
@@ -423,6 +430,22 @@ pub(crate) async fn run_worker(state: crate::state::AppState) {
error!(error = %error, "Reading the store from the document failed");
false
});
+ let member = state
+ .holding
+ .member(state.local_id(), state.holding.party());
+ if member.is_some() && member != listed {
+ match member {
+ Some(false) => warn!(
+ party = state.holding.party(),
+ "This node isn't a member of its store, as it reads now: its party doesn't list it, and members don't sync with it"
+ ),
+ _ => info!(
+ party = state.holding.party(),
+ "This node is a member of its store"
+ ),
+ }
+ listed = member;
+ }
due = match due {
_ if !lacking => None,
Some(due) => Some(due),
diff --git a/src/state/peer.rs b/src/state/peer.rs
index a7c6cc9..88cbb58 100644
--- a/src/state/peer.rs
+++ b/src/state/peer.rs
@@ -89,6 +89,9 @@ pub(crate) struct PeerState {
pub(crate) leads: bool,
pub(crate) standing: Standing,
+ /// The party its handshakes name, in a swarm holding a store (`swarm::peer::members`).
+ #[cfg(feature = "federation")]
+ pub(crate) party: Option<String>,
pub(crate) hello_state: HelloState,
/// Wait between unanswered HELLOs; reset whenever a new handshake starts.
pub(crate) hello_backoff: Backoff,
@@ -300,6 +303,8 @@ impl PeerState {
Self {
leads,
standing: Standing::default(),
+ #[cfg(feature = "federation")]
+ party: None,
hello_state: HelloState::default(),
hello_backoff: Backoff::new(HELLO_RETRY, HELLO_RETRY_MAX),
probe_backoff: timing.probe_backoff(),
diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs
index 4a5b6ac..d266e5f 100644
--- a/src/swarm/peer/inbound.rs
+++ b/src/swarm/peer/inbound.rs
@@ -96,6 +96,13 @@ pub(crate) async fn handle_message(
return Ok(());
}
+ // In a swarm holding a store, its members only, each as the party its handshakes name
+ // (`members`).
+ #[cfg(feature = "federation")]
+ if !super::members::member(state, &peer, &message).await {
+ return Ok(());
+ }
+
// The root its document starts from: another than ours, of our epoch, is another
// history; what a joiner takes as its root follows those named (`swarm::roots`).
match &message {
diff --git a/src/swarm/peer/members.rs b/src/swarm/peer/members.rs
new file mode 100644
index 0000000..ddbd4a7
--- /dev/null
+++ b/src/swarm/peer/members.rs
@@ -0,0 +1,56 @@
+//! 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.
+
+use tracing::{debug, warn};
+
+use crate::state::{AppState, HelloState, Standing};
+use crate::transport::ParsedMessage;
+
+/// 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 => match state.get_peer_state(peer).await {
+ Some(peer_state) => peer_state.lock().await.party.clone(),
+ None => None,
+ },
+ };
+ // 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;
+ 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
+}
diff --git a/src/swarm/peer/mod.rs b/src/swarm/peer/mod.rs
index 1826ec6..0b5500d 100644
--- a/src/swarm/peer/mod.rs
+++ b/src/swarm/peer/mod.rs
@@ -6,6 +6,7 @@
//! re-handshake.
//! * `epoch`: the epochs peers advertise.
//! * `admit`: admission with a swarm key.
+//! * `members`: in a swarm holding a store, its members only.
//! * `forget`: swarm-wide forget records.
//! * `worker`: the periodic peer worker.
//! * `inbound`: dispatch of inbound messages.
@@ -18,6 +19,8 @@ mod handshake;
mod inbound;
mod keepalive;
mod lanes;
+#[cfg(feature = "federation")]
+mod members;
mod worker;
pub(crate) use forget::{affects_forget_records, forget_unless_alive, forgotten_path};