aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 14:09:22 +0000
committertv <tv@krebsco.de>2026-11-29 14:09:22 +0000
commit598caecb7e5470e26379be4b2e199f6656ebb72c (patch)
tree7604f1bac650a719ea3ac8e071350ccc517b5d19 /src/federation
parentdde5570e80b0584a19f92011780e202db4924a74 (diff)
federation: a set keeps where its store stands, and its entries' names
Saying where a set stands read the store's heads anew, which costs its events, and finding an entry by its name hashed every entry: both for every message. A set now keeps the one, read anew where its store changed, and the other as entries come and go; in tests both are compared with what is read anew.
Diffstat (limited to 'src/federation')
-rw-r--r--src/federation/store/passing.rs11
-rw-r--r--src/federation/store/set.rs60
2 files changed, 56 insertions, 15 deletions
diff --git a/src/federation/store/passing.rs b/src/federation/store/passing.rs
index 95e1c3a..5a6e2af 100644
--- a/src/federation/store/passing.rs
+++ b/src/federation/store/passing.rs
@@ -18,7 +18,6 @@
use std::time::Instant;
use serde::{Deserialize, Serialize};
-use sha2::{Digest, Sha256};
use super::entry::Entry;
@@ -80,16 +79,8 @@ impl Took {
/// Where `set` stands.
pub(crate) fn stamp<F: Fold>(set: &Set<F>) -> Stamp {
- let mut hasher = Sha256::new();
- hasher.update(Id::of(set.store().checkpoint()).0);
- // In one order on every node: a store says its heads as it took them.
- let mut heads = set.store().heads();
- heads.sort();
- for head in heads {
- hasher.update(head.0);
- }
Stamp {
- heads: hasher.finalize().into(),
+ heads: set.stands(),
names: set.names().fingerprint(),
}
}
diff --git a/src/federation/store/set.rs b/src/federation/store/set.rs
index 5b0a63b..b033927 100644
--- a/src/federation/store/set.rs
+++ b/src/federation/store/set.rs
@@ -20,6 +20,7 @@ use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::time::{Duration, Instant};
use serde::{Deserialize, Serialize};
+use sha2::{Digest, Sha256};
use super::entry::Entry;
use super::memory::{Gone, Offered, Refusal, Store};
@@ -100,6 +101,20 @@ enum SavedBeside {
Forgotten(u64),
}
+/// Where `store` stands: of its checkpoint and its heads, the same on every node
+/// whose store holds the same.
+fn stands_of<F: Fold>(store: &Store<F>) -> [u8; 32] {
+ let mut hasher = Sha256::new();
+ hasher.update(Id::of(store.checkpoint()).0);
+ // In one order on every node: a store says its heads as it took them.
+ let mut heads = store.heads();
+ heads.sort();
+ for head in heads {
+ hasher.update(head.0);
+ }
+ hasher.finalize().into()
+}
+
/// A set of events, of a fold `F`.
pub(crate) struct Set<F: Fold> {
store: Store<F>,
@@ -119,12 +134,18 @@ pub(crate) struct Set<F: Fold> {
strangers: BTreeSet<Name>,
/// The names of all the document gives to others, kept as it changes.
names: Names,
+ /// The entries' names, each with its node.
+ entry_names: BTreeMap<Name, String>,
+ /// Where the store stands: of its checkpoint and its heads, read anew where it
+ /// changed.
+ stands: [u8; 32],
}
impl<F: Fold> Set<F> {
/// The document of `store`, keeping what a checkpoint forgot for `keeps_forgotten`.
pub(crate) fn new(store: Store<F>, keeps_forgotten: Duration) -> Self {
let members = members_of::<F>(&store);
+ let stands = stands_of(&store);
let names = store
.taken()
.iter()
@@ -141,6 +162,8 @@ impl<F: Fold> Set<F> {
entries: BTreeMap::new(),
strangers: BTreeSet::new(),
names,
+ entry_names: BTreeMap::new(),
+ stands,
}
}
@@ -166,9 +189,13 @@ impl<F: Fold> Set<F> {
Put::Older
}
_ => {
- self.names.insert(entry.name());
+ let name = entry.name();
+ self.names.insert(name);
+ self.entry_names.insert(name, node.clone());
if let Some(older) = self.entries.insert(node, entry) {
- self.names.remove(&older.name());
+ let older = older.name();
+ self.names.remove(&older);
+ self.entry_names.remove(&older);
}
#[cfg(test)]
self.check();
@@ -189,7 +216,9 @@ impl<F: Fold> Set<F> {
/// The entry named `name`, where the document holds it.
pub(crate) fn entry_named(&self, name: &Name) -> Option<&Entry> {
- self.entries.values().find(|entry| entry.name() == *name)
+ self.entry_names
+ .get(name)
+ .and_then(|node| self.entries.get(node))
}
/// Whether the document would have what is named `name`, which a peer holds: not
@@ -202,6 +231,11 @@ impl<F: Fold> Set<F> {
&& self.entry_named(name).is_none()
}
+ /// Where the store stands (`stands_of`).
+ pub(crate) fn stands(&self) -> [u8; 32] {
+ self.stands
+ }
+
/// The store: what the node reads.
pub(crate) fn store(&self) -> &Store<F> {
&self.store
@@ -236,11 +270,13 @@ impl<F: Fold> Set<F> {
fn members_anew(&mut self) {
let members = members_of::<F>(&self.store);
if members != self.members {
- let names = &mut self.names;
+ let (names, entry_names) = (&mut self.names, &mut self.entry_names);
self.entries.retain(|node, entry| {
let member = members.contains(node);
if !member {
- names.remove(&entry.name());
+ let name = entry.name();
+ names.remove(&name);
+ entry_names.remove(&name);
}
member
});
@@ -292,6 +328,7 @@ impl<F: Fold> Set<F> {
// reading that costs: asked only where the store changed.
if !took.is_empty() || !gone.is_empty() {
self.members_anew();
+ self.stands = stands_of(&self.store);
}
for id in took {
self.names.insert(id.0);
@@ -520,6 +557,19 @@ impl<F: Fold> Set<F> {
/// while they are few, and now and then when many.
#[cfg(test)]
fn check(&self) {
+ assert_eq!(
+ self.stands,
+ stands_of(&self.store),
+ "where the store stands"
+ );
+ assert!(
+ self.entries.len() == self.entry_names.len()
+ && self
+ .entries
+ .iter()
+ .all(|(node, entry)| self.entry_names.get(&entry.name()) == Some(node)),
+ "the entries' names"
+ );
if self.names.len() > 256 && !self.names.len().is_multiple_of(61) {
return;
}