aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/store/set.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/store/set.rs')
-rw-r--r--src/federation/store/set.rs60
1 files changed, 55 insertions, 5 deletions
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;
}