diff options
Diffstat (limited to 'src/swarm/peer')
| -rw-r--r-- | src/swarm/peer/epoch.rs | 5 | ||||
| -rw-r--r-- | src/swarm/peer/handshake.rs | 18 | ||||
| -rw-r--r-- | src/swarm/peer/inbound.rs | 41 | ||||
| -rw-r--r-- | src/swarm/peer/keepalive.rs | 20 |
4 files changed, 73 insertions, 11 deletions
diff --git a/src/swarm/peer/epoch.rs b/src/swarm/peer/epoch.rs index d00a903..f276727 100644 --- a/src/swarm/peer/epoch.rs +++ b/src/swarm/peer/epoch.rs @@ -60,6 +60,11 @@ pub(super) async fn observe_peer_epoch( previous }; + // The remake worker looks whether to follow it, each time it says so. + if relation != EpochRelation::Same { + state.remake_wake.notify_one(); + } + // Log transitions only, not every keepalive. if previous != Some(remote) { match relation { diff --git a/src/swarm/peer/handshake.rs b/src/swarm/peer/handshake.rs index 2e5a315..fc0a31b 100644 --- a/src/swarm/peer/handshake.rs +++ b/src/swarm/peer/handshake.rs @@ -144,7 +144,7 @@ async fn transmit_hello(state: &AppState, peer: &str) -> Result<()> { .await .map(|root| root.0.to_vec()), party: party(state), - kind: crate::document::KIND.to_owned(), + kind: kind_to(state, peer).await.to_owned(), }), ) .await; @@ -180,6 +180,20 @@ async fn say_what_we_speak(state: &AppState, peer: &str) { } } +/// The kind of document this node says to `peer` it holds: the one the peer's +/// handshakes said, of those this node holds; where the peer said none yet, and this +/// node holds more than one, each in turn, HELLO by HELLO, until one is answered. +async fn kind_to(state: &AppState, peer: &str) -> &'static str { + let kinds = state.kinds(); + let Some(peer_state) = state.get_peer_state(peer).await else { + return kinds[0]; + }; + let peer_state = peer_state.lock().await; + peer_state + .kind + .unwrap_or(kinds[peer_state.hello_attempts as usize % kinds.len()]) +} + pub(super) async fn ensure_handshake(state: &AppState, peer: &str) { if let Err(error) = send_hello_once(state, peer).await { warn!(peer = %peer, error = %format!("{error:#}"), "HELLO failed; will retry"); @@ -255,7 +269,7 @@ pub(super) async fn send_hello_reply( .await .map(|root| root.0.to_vec()), party: party(state), - kind: crate::document::KIND.to_owned(), + kind: kind_to(state, peer).await.to_owned(), }; debug!(to = %peer, ready, "Sending HELLO_REPLY"); diff --git a/src/swarm/peer/inbound.rs b/src/swarm/peer/inbound.rs index a558ded..e16eb3f 100644 --- a/src/swarm/peer/inbound.rs +++ b/src/swarm/peer/inbound.rs @@ -114,12 +114,24 @@ pub(crate) async fn handle_message( ParsedMessage::HelloReply(reply) => Some(&reply.kind), _ => None, }; - if let Some(theirs) = kind.filter(|kind| *kind != crate::document::KIND) { - anyhow::bail!( - "{peer} holds a document of another kind ({theirs}; this node's is {}): they sync \ - nothing; a release that knows the kind does", - crate::document::KIND - ); + if let Some(theirs) = kind { + let Some(held) = state.kinds().iter().find(|held| *held == theirs) else { + anyhow::bail!( + "{peer} holds a document of another kind ({theirs}; this node's is {}): they \ + sync nothing; a release that knows the kind does", + state.kinds().join(" and ") + ); + }; + // Of the kinds this node holds, the one the peer holds: what it is told in + // turn, and passed on. + let peer_state = state.get_or_create_peer_state(&peer).await; + let mut peer_state = peer_state.lock().await; + // A handshake of another kind than before: of a node started anew. + #[cfg(feature = "federation")] + if peer_state.kind != Some(held) { + peer_state.set_beside = false; + } + peer_state.kind = Some(held); } // The root its document starts from: another than ours, of our epoch, is another @@ -218,6 +230,19 @@ pub(crate) async fn handle_message( .map_err(|error| anyhow!("Failed to notify sync worker: {error}"))?; } transport::ParsedMessage::Sync(envelope) => { + // Of a set of events: this node's set's to take, where it holds one + // (`swarm::set`); and of an Automerge document nothing, where it holds a set + // alone. + #[cfg(feature = "federation")] + if state.of_a_set() || matches!(envelope.body, transport::SyncBody::Kind(_)) { + if let transport::SyncBody::Kind(body) = envelope.body + && state.holding.has_a_set() + { + crate::swarm::set::holds_one_beside(state, &peer).await; + crate::swarm::set::received(state, &peer, body).await; + } + return Ok(()); + } if reactivated { ensure_handshake(state, &peer).await; } @@ -251,6 +276,8 @@ pub(crate) async fn handle_message( crate::swarm::blob::handle(state, &peer, blob).await; } transport::ParsedMessage::Status(remote) => { + #[cfg(feature = "federation")] + let remote = crate::swarm::set::heard_status(state, &peer, remote); // An announcement is no keepalive; it is answered only if we lack some of the // heads it announced (`relay::lacks`), so the announcer learns what we have // that it lacks while we pull. Having all of them, we are as far or further, @@ -290,6 +317,8 @@ pub(crate) async fn handle_message( } } transport::ParsedMessage::StatusReply(remote) => { + #[cfg(feature = "federation")] + let remote = crate::swarm::set::heard_status(state, &peer, remote); // An answer to our slow round's announcement: a tree missed. let announced = peer_state.lock().await.slow_announced_at.take(); if let Some(at) = announced { diff --git a/src/swarm/peer/keepalive.rs b/src/swarm/peer/keepalive.rs index 72ab033..5072f01 100644 --- a/src/swarm/peer/keepalive.rs +++ b/src/swarm/peer/keepalive.rs @@ -22,6 +22,9 @@ use crate::transport::{self, ParsedMessage}; /// Sends `peer` a STATUS with our status. pub(crate) async fn send_status(state: &AppState, peer: &str) -> Result<()> { + // A node of both kinds: where its set stands, beside (`swarm::set`). + #[cfg(feature = "federation")] + crate::swarm::set::say_beside(state, peer).await; let status = local_status(state, peer).await; if let Some(peer_state) = state.get_peer_state(peer).await { peer_state.lock().await.last_status_at = Some(Instant::now()); @@ -182,10 +185,21 @@ pub(crate) async fn local_status(state: &AppState, peer: &str) -> transport::Sta let stamp = state.stamp.borrow(); (stamp.summary(), stamp.epoch, stamp.epoch_seq) }; - let syncs_with_you = match state.get_peer_state(peer).await { - Some(peer_state) => peer_state.lock().await.syncs_on(epoch), - None => false, + let (syncs_with_you, _of_a_set) = match state.get_peer_state(peer).await { + Some(peer_state) => { + let peer_state = peer_state.lock().await; + ( + peer_state.syncs_on(epoch), + peer_state + .kind + .is_some_and(|kind| kind != crate::document::KIND), + ) + } + None => (false, false), }; + // To a peer that holds a set of events, where this node's stands (`swarm::set`). + #[cfg(feature = "federation")] + let heads = crate::swarm::set::stamp(state, _of_a_set).unwrap_or(heads); let (reaches, round_trips) = crate::swarm::reach::published_with_round_trips(state); transport::Status { heads, |
