aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm/set.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/swarm/set.rs')
-rw-r--r--src/swarm/set.rs69
1 files changed, 63 insertions, 6 deletions
diff --git a/src/swarm/set.rs b/src/swarm/set.rs
index 6df78ba..54a48fe 100644
--- a/src/swarm/set.rs
+++ b/src/swarm/set.rs
@@ -22,10 +22,22 @@ use crate::state::AppState;
use crate::swarm::topology;
use crate::transport::{self, HeadsSummary, ParsedMessage, SyncBody};
-/// Where this node's set stands, as its STATUSes say it; none, where no set carries its
-/// store.
-pub(crate) fn stamp(state: &AppState) -> Option<HeadsSummary> {
- if !state.holding.of_a_set() {
+/// Whether `peer` holds a set of events, as its handshakes said: what a node of both
+/// kinds passes its set on to. A node that holds a set alone takes handshakes of no
+/// other.
+pub(crate) async fn holds_a_set(state: &AppState, peer: &str) -> bool {
+ match state.get_peer_state(peer).await {
+ Some(peer_state) => {
+ peer_state.lock().await.kind == Some(crate::federation::store::set::KIND)
+ }
+ None => false,
+ }
+}
+
+/// Where this node's set stands, as its STATUSes say it to a peer that holds one
+/// (`to_a_set`); none, where the node holds no set, or the peer doesn't.
+pub(crate) fn stamp(state: &AppState, to_a_set: bool) -> Option<HeadsSummary> {
+ if !state.holding.has_a_set() || !(to_a_set || state.holding.of_a_set()) {
return None;
}
// A node still gathering what its set begins from stands nowhere yet.
@@ -45,9 +57,14 @@ pub(crate) fn heard_status(
peer: &str,
mut remote: transport::Status,
) -> transport::Status {
- let Some(ours) = stamp(state) else {
+ // A peer that says where a set stands holds one.
+ let of_a_set = matches!(remote.heads, HeadsSummary::Kind(_));
+ let Some(ours) = stamp(state, of_a_set) else {
return remote;
};
+ if !of_a_set && !state.holding.of_a_set() {
+ return remote;
+ }
if let HeadsSummary::Kind(bytes) = std::mem::replace(&mut remote.heads, ours)
&& let Ok(stamp) = rmp_serde::from_slice::<passing::Stamp>(&bytes)
{
@@ -174,7 +191,10 @@ async fn pass_on(state: &AppState, from: Option<&str>, took: Took) {
}
let message = Message::Units(units);
for peer in state.active_peers_on_local_epoch().await {
- if Some(peer.as_str()) != from && topology::is_neighbour(state, &peer) {
+ if Some(peer.as_str()) != from
+ && topology::is_neighbour(state, &peer)
+ && holds_a_set(state, &peer).await
+ {
send(state, &peer, &message).await;
}
}
@@ -219,3 +239,40 @@ pub(crate) async fn registered(state: &AppState) {
)
.await;
}
+
+/// On a node of both kinds, puts its entry as the document it holds has it as its entry
+/// in the set, where the two differ, and passes it on: what the node says of itself,
+/// it says to the peers of either kind.
+pub(crate) async fn mirror_entry(state: &AppState) {
+ use crate::federation::store::entry::Entry;
+ use crate::federation::store::set::Put;
+
+ if !state.holding.has_a_set() || state.holding.of_a_set() {
+ return;
+ }
+ let Some(key) = state.node_key.clone() else {
+ return;
+ };
+ let node = state.local_id().to_owned();
+ let own = crate::swarm::members::member_path(&node);
+ let written = crate::document::view::read_path(&state.doc.read().await.doc, &own);
+ let Ok(Some(serde_json::Value::Object(written))) = written else {
+ return;
+ };
+ let put = state.holding.change_set(|set| {
+ let held = set.entry(&node);
+ if held.and_then(|entry| entry.value()).as_ref() == Some(&written) {
+ return None;
+ }
+ let count = held.map_or(1, |entry| entry.count() + 1);
+ Some(set.put_entry(Entry::new(
+ &set.store().id(),
+ &key,
+ count,
+ &serde_json::Value::Object(written),
+ )))
+ });
+ if put.flatten() == Some(Put::Taken) {
+ registered(state).await;
+ }
+}