aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/federation/held.rs33
-rw-r--r--src/simtest/holding.rs4
-rw-r--r--src/state/mod.rs15
-rw-r--r--src/state/peer.rs4
-rw-r--r--src/swarm/peer/handshake.rs18
-rw-r--r--src/swarm/peer/inbound.rs22
6 files changed, 74 insertions, 22 deletions
diff --git a/src/federation/held.rs b/src/federation/held.rs
index 51d8ef0..154544f 100644
--- a/src/federation/held.rs
+++ b/src/federation/held.rs
@@ -117,6 +117,9 @@ pub(crate) struct Holding {
written: Mutex<Option<super::store::passing::Stamp>>,
/// Whether a set of events carries the store (`carrying`), from the node's start on.
of_a_set: bool,
+ /// Whether the node holds the store in the document a swarm syncs otherwise too
+ /// (`of_both`): a node of both kinds, by which a running swarm moves to sets.
+ both: bool,
// A std mutex: never held while waiting for the document.
inner: Mutex<Inner>,
}
@@ -278,6 +281,7 @@ impl Holding {
members: RwLock::default(),
voter: None,
of_a_set: false,
+ both: false,
document: None,
written: Mutex::default(),
inner: Mutex::new(Inner {
@@ -1145,18 +1149,35 @@ impl Holding {
/// The kind of the document that carries the store: a set of events, or the
/// document the node holds, which the store is read from.
pub(crate) fn kind(&self) -> &'static str {
- if self.of_a_set {
- super::store::set::KIND
- } else {
- crate::document::KIND
- }
+ self.kinds()[0]
}
- /// Whether a set of events carries the store.
+ /// Whether a set of events carries the store, and nothing else does.
pub(crate) fn of_a_set(&self) -> bool {
+ self.of_a_set && !self.both
+ }
+
+ /// Whether the node holds a set of events, alone or beside the Automerge document.
+ pub(crate) fn has_a_set(&self) -> bool {
self.of_a_set
}
+ /// Itself, holding its store in the Automerge document too, beside the set.
+ pub(crate) fn of_both(mut self) -> Self {
+ self.both = true;
+ self
+ }
+
+ /// The kinds of document the node holds its store in, which it says in
+ /// handshakes: the one it says first, first.
+ pub(crate) fn kinds(&self) -> &'static [&'static str] {
+ match (self.of_a_set, self.both) {
+ (true, true) => &[crate::document::KIND, super::store::set::KIND],
+ (true, false) => &[super::store::set::KIND],
+ (false, _) => &[crate::document::KIND],
+ }
+ }
+
/// What `f` makes of the set of events carrying the store, where one does.
pub(crate) fn with_set<T>(&self, f: impl FnOnce(&Set<Federation>) -> T) -> Option<T> {
match &self.inner.lock_or_recover().store {
diff --git a/src/simtest/holding.rs b/src/simtest/holding.rs
index ebc7528..0841a75 100644
--- a/src/simtest/holding.rs
+++ b/src/simtest/holding.rs
@@ -1778,7 +1778,7 @@ async fn a_swarm_of_sets_passes_entries_and_events_on() {
let members = &members;
sim.wait_ready(members, SOON).await;
for node in members.iter() {
- assert_eq!(node.kind(), crate::federation::store::set::KIND);
+ assert_eq!(node.kinds(), [crate::federation::store::set::KIND]);
assert!(node.holding.tells_members());
}
sim.wait_until("each holds the three entries", SOON, move || async move {
@@ -1967,7 +1967,7 @@ async fn a_node_of_a_set_joins_with_nothing_but_the_stores_id() {
);
},
);
- assert_eq!(c.kind(), crate::federation::store::set::KIND);
+ assert_eq!(c.kinds(), [crate::federation::store::set::KIND]);
assert!(read(&c, |_| ()).is_none());
sim.wait_ready(&[&c], SOON).await;
let (all, ids) = (&[&a, &b, &c], &ids);
diff --git a/src/state/mod.rs b/src/state/mod.rs
index 21478de..0ffa91e 100644
--- a/src/state/mod.rs
+++ b/src/state/mod.rs
@@ -381,14 +381,17 @@ impl AppState {
*self.closing.borrow()
}
- /// The kind of this node's document (`document::KIND`, or a set of events where
- /// one carries its store).
- pub(crate) fn kind(&self) -> &'static str {
+ /// The kinds of document this node holds, which it says in handshakes: more than
+ /// one where it holds its store in both (`Holding::kinds`).
+ pub(crate) fn kinds(&self) -> &'static [&'static str] {
#[cfg(feature = "federation")]
- if self.holding.of_a_set() {
- return self.holding.kind();
+ {
+ self.holding.kinds()
+ }
+ #[cfg(not(feature = "federation"))]
+ {
+ &[crate::document::KIND]
}
- crate::document::KIND
}
/// Whether this node's document is a set of events (`federation::store::set`):
diff --git a/src/state/peer.rs b/src/state/peer.rs
index 721a754..356736a 100644
--- a/src/state/peer.rs
+++ b/src/state/peer.rs
@@ -92,6 +92,9 @@ pub(crate) struct PeerState {
/// The party its handshakes name, in a swarm holding a store (`swarm::peer::members`).
#[cfg(feature = "federation")]
pub(crate) party: Option<String>,
+ /// The kind of document its handshakes say it holds, of those this node holds: what
+ /// this node says to it in turn, and passes on to it (`swarm::peer::inbound`).
+ pub(crate) kind: Option<&'static str>,
pub(crate) hello_state: HelloState,
/// Wait between unanswered HELLOs; reset whenever a new handshake starts.
pub(crate) hello_backoff: Backoff,
@@ -339,6 +342,7 @@ impl PeerState {
tree_writer: None,
left: false,
hello_attempts: 0,
+ kind: None,
last_hello_at: None,
unreachable_since: None,
incidents: Incidents::default(),
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