aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-29 03:21:43 +0000
committertv <tv@krebsco.de>2026-11-29 03:21:43 +0000
commite63d30c2581033a85489b10c3f5b252fdbb5049e (patch)
tree5f4e6f65f1cecfb32f841e7ad7d9d106aeb69b20 /src/swarm
parent9188fa6e13ea7facfab5fb6f55bf26444ec2c01e (diff)
state: plain writes take a view to write, Automerge's are asked for as such
AppState::mutate_doc, mutate_doc_registering and mutate_doc_by hand what they are given a document::view::Write: the member registry, the users, sessions, forgetting and bootstrap write through them, and take whatever kind of document gives it. AppState::mutate_doc_for is for what asks more of an Automerge document than values at paths (the store's API, a remake's host, a store's events), and gives the heads after the write. What reads a view takes one of any size (dyn Write among them).
Diffstat (limited to 'src/swarm')
-rw-r--r--src/swarm/members.rs38
1 files changed, 25 insertions, 13 deletions
diff --git a/src/swarm/members.rs b/src/swarm/members.rs
index 4039b2a..754b702 100644
--- a/src/swarm/members.rs
+++ b/src/swarm/members.rs
@@ -41,7 +41,7 @@ pub(crate) fn member_path(pk: &str) -> Vec<String> {
}
/// Public keys of all registered members, sorted.
-pub(crate) fn members(doc: &impl Read) -> Vec<String> {
+pub(crate) fn members(doc: &(impl Read + ?Sized)) -> Vec<String> {
let mut members = view::keys_at(doc, &members_dir())
.ok()
.flatten()
@@ -81,7 +81,11 @@ pub(crate) fn tier(doc: &Document, pk: &str) -> Tier {
}
}
-pub(crate) fn set_tier(doc: &mut impl Write, pk: &str, tier: Tier) -> Result<(), StoreError> {
+pub(crate) fn set_tier(
+ doc: &mut (impl Write + ?Sized),
+ pk: &str,
+ tier: Tier,
+) -> Result<(), StoreError> {
view::write_path(doc, &tier_path(pk), &json!(tier.as_str()))
}
@@ -100,7 +104,7 @@ fn party_path(pk: &str) -> Vec<String> {
/// The party `pk`'s entry names, in a swarm holding a store.
#[cfg(feature = "federation")]
-pub(crate) fn party(doc: &impl Read, pk: &str) -> Option<String> {
+pub(crate) fn party(doc: &(impl Read + ?Sized), pk: &str) -> Option<String> {
match view::read_path(doc, &party_path(pk)) {
Ok(Some(JsonValue::String(party))) => Some(party),
_ => None,
@@ -123,7 +127,11 @@ pub(crate) fn aliases(doc: &Document) -> HashMap<String, String> {
.collect()
}
-fn set_alias(doc: &mut impl Write, pk: &str, alias: Option<&str>) -> Result<(), StoreError> {
+fn set_alias(
+ doc: &mut (impl Write + ?Sized),
+ pk: &str,
+ alias: Option<&str>,
+) -> Result<(), StoreError> {
match alias {
Some(alias) => view::write_path(doc, &alias_path(pk), &json!(alias)),
None => match view::delete_path(doc, &alias_path(pk), false) {
@@ -147,7 +155,11 @@ pub(crate) fn dashboard(doc: &Document, pk: &str) -> Option<String> {
}
}
-fn set_dashboard(doc: &mut impl Write, pk: &str, url: Option<&str>) -> Result<(), StoreError> {
+fn set_dashboard(
+ doc: &mut (impl Write + ?Sized),
+ pk: &str,
+ url: Option<&str>,
+) -> Result<(), StoreError> {
match url {
Some(url) => view::write_path(doc, &dashboard_path(pk), &json!(url)),
None => match view::delete_path(doc, &dashboard_path(pk), false) {
@@ -244,7 +256,7 @@ fn alive_path(pk: &str) -> Vec<String> {
path
}
-fn millis_at(doc: &impl Read, path: &[String]) -> Option<u64> {
+fn millis_at(doc: &(impl Read + ?Sized), path: &[String]) -> Option<u64> {
match view::read_path(doc, path) {
Ok(Some(JsonValue::Number(number))) => number.as_u64(),
_ => None,
@@ -252,7 +264,7 @@ fn millis_at(doc: &impl Read, path: &[String]) -> Option<u64> {
}
/// When `pk` last said it is alive (Unix ms): its last renewal, or its registration.
-pub(crate) fn alive_at(doc: &impl Read, pk: &str) -> Option<u64> {
+pub(crate) fn alive_at(doc: &(impl Read + ?Sized), pk: &str) -> Option<u64> {
let mut since = member_path(pk);
since.push("since".to_owned());
millis_at(doc, &alive_path(pk)).max(millis_at(doc, &since))
@@ -262,7 +274,7 @@ pub(crate) fn alive_at(doc: &impl Read, pk: &str) -> Option<u64> {
/// electorate of a remake (`remake::coordinator`). A stamp further in the future than a
/// renewal interval (`alive_renewal`) doesn't count: its node's clock is that far ahead
/// (it warns about it, `swarm::skew`), and the stamp would never age out.
-pub(crate) fn electorate(doc: &impl Read, now_ms: u64, window: Duration) -> Vec<String> {
+pub(crate) fn electorate(doc: &(impl Read + ?Sized), now_ms: u64, window: Duration) -> Vec<String> {
let oldest = now_ms.saturating_sub(window.as_millis() as u64);
let newest = now_ms.saturating_add(alive_renewal(window).as_millis() as u64);
members(doc)
@@ -281,7 +293,7 @@ fn window_path(pk: &str) -> Vec<String> {
/// publish in their entries (ms, `window`): every node renews its alive stamp by it
/// (`alive_renewal`), so a coordinator with a shorter window than others' still counts
/// them all. Below `MIN_ELECTORATE_WINDOW` (which no node accepts) counts as that.
-pub(crate) fn shortest_window(doc: &impl Read, own: Duration) -> Duration {
+pub(crate) fn shortest_window(doc: &(impl Read + ?Sized), own: Duration) -> Duration {
members(doc)
.iter()
.filter_map(|pk| millis_at(doc, &window_path(pk)))
@@ -304,7 +316,7 @@ pub(crate) const MIN_ELECTORATE_WINDOW: Duration =
Duration::from_secs(7 * RENEWAL_MOST_OFTEN.as_secs());
/// Whether `node` is registered in `doc`'s member list.
-pub(crate) fn registered(doc: &impl Read, node: &str) -> bool {
+pub(crate) fn registered(doc: &(impl Read + ?Sized), node: &str) -> bool {
view::read_path(doc, &member_path(node))
.ok()
.flatten()
@@ -505,7 +517,7 @@ impl RecordedLinks {
/// `pk`'s recorded links; none if its entry doesn't say (at most `MAX_RECORDED_LINKS`
/// in each set).
-pub(crate) fn links(doc: &impl Read, pk: &str) -> RecordedLinks {
+pub(crate) fn links(doc: &(impl Read + ?Sized), pk: &str) -> RecordedLinks {
let Ok(Some(JsonValue::Object(links))) = view::read_path(doc, &links_path(pk)) else {
return RecordedLinks::default();
};
@@ -528,7 +540,7 @@ pub(crate) fn links(doc: &impl Read, pk: &str) -> RecordedLinks {
/// Records `pk`'s `links` (at most `MAX_RECORDED_LINKS` in each set); none removes them.
pub(crate) fn set_links(
- doc: &mut impl Write,
+ doc: &mut (impl Write + ?Sized),
pk: &str,
links: &RecordedLinks,
) -> Result<(), StoreError> {
@@ -549,7 +561,7 @@ pub(crate) fn set_links(
}
/// Removes `pk`'s entry (an operator forgot it). Absent is fine.
-pub(crate) fn unregister(doc: &mut impl Write, pk: &str) -> Result<(), StoreError> {
+pub(crate) fn unregister(doc: &mut (impl Write + ?Sized), pk: &str) -> Result<(), StoreError> {
match view::delete_path(doc, &member_path(pk), false) {
Err(StoreError::NotFound) => Ok(()),
result => result,