aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm/peer
diff options
context:
space:
mode:
Diffstat (limited to 'src/swarm/peer')
-rw-r--r--src/swarm/peer/handshake.rs18
-rw-r--r--src/swarm/peer/inbound.rs22
2 files changed, 32 insertions, 8 deletions
diff --git a/src/swarm/peer/handshake.rs b/src/swarm/peer/handshake.rs
index fc1bc86..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: state.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: state.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 0354356..35d67c8 100644
--- a/src/swarm/peer/inbound.rs
+++ b/src/swarm/peer/inbound.rs
@@ -114,12 +114,22 @@ pub(crate) async fn handle_message(
ParsedMessage::HelloReply(reply) => Some(&reply.kind),
_ => None,
};
- if let Some(theirs) = kind.filter(|kind| *kind != state.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",
- state.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.
+ state
+ .get_or_create_peer_state(&peer)
+ .await
+ .lock()
+ .await
+ .kind = Some(held);
}
// The root its document starts from: another than ours, of our epoch, is another