diff options
| author | tv <tv@krebsco.de> | 2026-11-29 03:15:05 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-29 03:15:05 +0000 |
| commit | 9188fa6e13ea7facfab5fb6f55bf26444ec2c01e (patch) | |
| tree | 28b33908d0519bd21b03ee18bb34126c484ec0bb /src/swarm/members.rs | |
| parent | c396d33a34ee6685e9a561baa4d3ae4956b697be (diff) | |
document: values read and written through a view any kind of document gives
document::view: Read (the value at a path, plain, typed, a map's keys)
and Write (a value written, one deleted), which Automerge's documents,
writes in progress and documents as they were all are. The member
registry, the users, access control's reads and rules, the topology,
forgetting and bootstrap read and write through them, and name neither
Automerge's ReadDoc and Transactable nor document::store, which is what
an Automerge document can do beyond.
How a swarm holding a store judges what a change writes is handed to
the rules (rules::judge_seen), being its kind of document's to say.
Diffstat (limited to 'src/swarm/members.rs')
| -rw-r--r-- | src/swarm/members.rs | 99 |
1 files changed, 46 insertions, 53 deletions
diff --git a/src/swarm/members.rs b/src/swarm/members.rs index de33185..4039b2a 100644 --- a/src/swarm/members.rs +++ b/src/swarm/members.rs @@ -24,9 +24,10 @@ use std::time::Duration; use serde_json::{Value as JsonValue, json}; use tracing::{debug, info, warn}; +use crate::document::Document; use crate::document::meta; -use crate::document::store::{self, StoreError}; -use crate::document::{Document, ReadDoc, Transactable}; +use crate::document::store::StoreError; +use crate::document::view::{self, Read, Write}; use crate::state::AppState; pub(crate) fn members_dir() -> Vec<String> { @@ -40,8 +41,8 @@ pub(crate) fn member_path(pk: &str) -> Vec<String> { } /// Public keys of all registered members, sorted. -pub(crate) fn members(doc: &impl ReadDoc) -> Vec<String> { - let mut members = store::keys_at(doc, &members_dir()) +pub(crate) fn members(doc: &impl Read) -> Vec<String> { + let mut members = view::keys_at(doc, &members_dir()) .ok() .flatten() .unwrap_or_default(); @@ -74,18 +75,14 @@ fn tier_path(pk: &str) -> Vec<String> { /// `pk`'s tier; transient unless its entry says stable. pub(crate) fn tier(doc: &Document, pk: &str) -> Tier { - match store::read_path(doc, &tier_path(pk)) { + match view::read_path(doc, &tier_path(pk)) { Ok(Some(JsonValue::String(tier))) if tier == "stable" => Tier::Stable, _ => Tier::Transient, } } -pub(crate) fn set_tier( - doc: &mut impl Transactable, - pk: &str, - tier: Tier, -) -> Result<(), StoreError> { - store::write_path(doc, &tier_path(pk), &json!(tier.as_str())) +pub(crate) fn set_tier(doc: &mut impl Write, pk: &str, tier: Tier) -> Result<(), StoreError> { + view::write_path(doc, &tier_path(pk), &json!(tier.as_str())) } fn alias_path(pk: &str) -> Vec<String> { @@ -103,8 +100,8 @@ 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 ReadDoc, pk: &str) -> Option<String> { - match store::read_path(doc, &party_path(pk)) { +pub(crate) fn party(doc: &impl Read, pk: &str) -> Option<String> { + match view::read_path(doc, &party_path(pk)) { Ok(Some(JsonValue::String(party))) => Some(party), _ => None, } @@ -112,7 +109,7 @@ pub(crate) fn party(doc: &impl ReadDoc, pk: &str) -> Option<String> { /// `pk`'s alias, if it gave itself one. pub(crate) fn alias(doc: &Document, pk: &str) -> Option<String> { - match store::read_path(doc, &alias_path(pk)) { + match view::read_path(doc, &alias_path(pk)) { Ok(Some(JsonValue::String(alias))) => Some(alias), _ => None, } @@ -126,10 +123,10 @@ pub(crate) fn aliases(doc: &Document) -> HashMap<String, String> { .collect() } -fn set_alias(doc: &mut impl Transactable, pk: &str, alias: Option<&str>) -> Result<(), StoreError> { +fn set_alias(doc: &mut impl Write, pk: &str, alias: Option<&str>) -> Result<(), StoreError> { match alias { - Some(alias) => store::write_path(doc, &alias_path(pk), &json!(alias)), - None => match store::delete_path(doc, &alias_path(pk), false) { + Some(alias) => view::write_path(doc, &alias_path(pk), &json!(alias)), + None => match view::delete_path(doc, &alias_path(pk), false) { Err(StoreError::NotFound) => Ok(()), result => result, }, @@ -144,20 +141,16 @@ fn dashboard_path(pk: &str) -> Vec<String> { /// Where people reach `pk`'s dashboard, if it says. pub(crate) fn dashboard(doc: &Document, pk: &str) -> Option<String> { - match store::read_path(doc, &dashboard_path(pk)) { + match view::read_path(doc, &dashboard_path(pk)) { Ok(Some(JsonValue::String(url))) => Some(url), _ => None, } } -fn set_dashboard( - doc: &mut impl Transactable, - pk: &str, - url: Option<&str>, -) -> Result<(), StoreError> { +fn set_dashboard(doc: &mut impl Write, pk: &str, url: Option<&str>) -> Result<(), StoreError> { match url { - Some(url) => store::write_path(doc, &dashboard_path(pk), &json!(url)), - None => match store::delete_path(doc, &dashboard_path(pk), false) { + Some(url) => view::write_path(doc, &dashboard_path(pk), &json!(url)), + None => match view::delete_path(doc, &dashboard_path(pk), false) { Err(StoreError::NotFound) => Ok(()), result => result, }, @@ -178,7 +171,7 @@ const MAX_ADDRESSES: usize = 16; /// Where `pk` says it can be reached (at most `MAX_ADDRESSES`); none if its entry /// doesn't say. pub(crate) fn addresses(doc: &Document, pk: &str) -> Vec<String> { - match store::read_path(doc, &addresses_path(pk)) { + match view::read_path(doc, &addresses_path(pk)) { Ok(Some(JsonValue::Array(items))) => items .into_iter() .filter_map(|item| match item { @@ -232,7 +225,7 @@ const MAX_RELAYS: usize = 32; /// aren't members, e.g. nodes that only relay. Every member's transport may /// pick them (`Transport::learn_relays`), after the relays it is configured with. pub(crate) fn relays(doc: &Document) -> Vec<String> { - match store::read_path(doc, &meta::path(&[meta::RELAYS])) { + match view::read_path(doc, &meta::path(&[meta::RELAYS])) { Ok(Some(JsonValue::Array(items))) => items .into_iter() .filter_map(|item| match item { @@ -251,15 +244,15 @@ fn alive_path(pk: &str) -> Vec<String> { path } -fn millis_at(doc: &impl ReadDoc, path: &[String]) -> Option<u64> { - match store::read_path(doc, path) { +fn millis_at(doc: &impl Read, path: &[String]) -> Option<u64> { + match view::read_path(doc, path) { Ok(Some(JsonValue::Number(number))) => number.as_u64(), _ => None, } } /// When `pk` last said it is alive (Unix ms): its last renewal, or its registration. -pub(crate) fn alive_at(doc: &impl ReadDoc, pk: &str) -> Option<u64> { +pub(crate) fn alive_at(doc: &impl Read, 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)) @@ -269,7 +262,7 @@ pub(crate) fn alive_at(doc: &impl ReadDoc, 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 ReadDoc, now_ms: u64, window: Duration) -> Vec<String> { +pub(crate) fn electorate(doc: &impl Read, 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) @@ -288,7 +281,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 ReadDoc, own: Duration) -> Duration { +pub(crate) fn shortest_window(doc: &impl Read, own: Duration) -> Duration { members(doc) .iter() .filter_map(|pk| millis_at(doc, &window_path(pk))) @@ -311,8 +304,8 @@ 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 ReadDoc, node: &str) -> bool { - store::read_path(doc, &member_path(node)) +pub(crate) fn registered(doc: &impl Read, node: &str) -> bool { + view::read_path(doc, &member_path(node)) .ok() .flatten() .is_some() @@ -382,7 +375,7 @@ pub(crate) async fn ensure_registered(state: &AppState) { if !registered { let entry = entry(state); match state - .mutate_doc_registering(move |doc| store::write_path(doc, &own, &entry)) + .mutate_doc_registering(move |doc| view::write_path(doc, &own, &entry)) .await { Ok(()) => info!( @@ -397,7 +390,7 @@ pub(crate) async fn ensure_registered(state: &AppState) { if recorded_window != Some(window) { let path = window_path(&local); match state - .mutate_doc(move |doc| store::write_path(doc, &path, &json!(window))) + .mutate_doc(move |doc| view::write_path(doc, &path, &json!(window))) .await { Ok(()) => debug!( @@ -412,7 +405,7 @@ pub(crate) async fn ensure_registered(state: &AppState) { if alive.is_none_or(|at| now.saturating_sub(at) >= renewal) { let path = alive_path(&local); match state - .mutate_doc(move |doc| store::write_path(doc, &path, &json!(now))) + .mutate_doc(move |doc| view::write_path(doc, &path, &json!(now))) .await { Ok(()) => debug!("Renewed our alive stamp in the member list"), @@ -424,7 +417,7 @@ pub(crate) async fn ensure_registered(state: &AppState) { let path = addresses_path(&local); let value = json!(own_addresses); match state - .mutate_doc(move |doc| store::write_path(doc, &path, &value)) + .mutate_doc(move |doc| view::write_path(doc, &path, &value)) .await { Ok(()) => info!(addresses = ?own_addresses, "Addresses updated in the member list"), @@ -454,7 +447,7 @@ pub(crate) async fn ensure_registered(state: &AppState) { if recorded.as_deref() != Some(ours) { let (path, value) = (party_path(&local), json!(ours)); match state - .mutate_doc(move |doc| store::write_path(doc, &path, &value)) + .mutate_doc(move |doc| view::write_path(doc, &path, &value)) .await { Ok(()) => info!( @@ -512,8 +505,8 @@ 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 ReadDoc, pk: &str) -> RecordedLinks { - let Ok(Some(JsonValue::Object(links))) = store::read_path(doc, &links_path(pk)) else { +pub(crate) fn links(doc: &impl Read, pk: &str) -> RecordedLinks { + let Ok(Some(JsonValue::Object(links))) = view::read_path(doc, &links_path(pk)) else { return RecordedLinks::default(); }; let set = |name: &str| -> BTreeSet<String> { @@ -535,12 +528,12 @@ pub(crate) fn links(doc: &impl ReadDoc, 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 Transactable, + doc: &mut impl Write, pk: &str, links: &RecordedLinks, ) -> Result<(), StoreError> { if links.is_empty() { - return match store::delete_path(doc, &links_path(pk), false) { + return match view::delete_path(doc, &links_path(pk), false) { Err(StoreError::NotFound) => Ok(()), result => result, }; @@ -552,12 +545,12 @@ pub(crate) fn set_links( "unreached": list(&links.unreached), "reached": list(&links.reached), }); - store::write_path(doc, &links_path(pk), &value) + view::write_path(doc, &links_path(pk), &value) } /// Removes `pk`'s entry (an operator forgot it). Absent is fine. -pub(crate) fn unregister(doc: &mut impl Transactable, pk: &str) -> Result<(), StoreError> { - match store::delete_path(doc, &member_path(pk), false) { +pub(crate) fn unregister(doc: &mut impl Write, pk: &str) -> Result<(), StoreError> { + match view::delete_path(doc, &member_path(pk), false) { Err(StoreError::NotFound) => Ok(()), result => result, } @@ -584,7 +577,7 @@ mod tests { ]; change(&mut doc, |doc| { for (pk, entry) in entries { - store::write_path(doc, &member_path(pk), &entry).unwrap(); + view::write_path(doc, &member_path(pk), &entry).unwrap(); } }); assert_eq!( @@ -599,11 +592,11 @@ mod tests { assert!(relays(&doc).is_empty()); let relay = "libp2p:/ip6/fd00::1/tcp/9661"; change(&mut doc, |doc| { - store::write_path(doc, &meta::path(&[meta::RELAYS]), &json!([relay, 7])).unwrap() + view::write_path(doc, &meta::path(&[meta::RELAYS]), &json!([relay, 7])).unwrap() }); assert_eq!(relays(&doc), [relay]); // Anything but text is ignored. change(&mut doc, |doc| { - store::write_path(doc, &meta::path(&[meta::RELAYS]), &json!(relay)).unwrap() + view::write_path(doc, &meta::path(&[meta::RELAYS]), &json!(relay)).unwrap() }); assert!(relays(&doc).is_empty()); // Not a list. } @@ -614,19 +607,19 @@ mod tests { let mut doc = Document::new(); assert_eq!(shortest_window(&doc, 7 * day), 7 * day); change(&mut doc, |doc| { - store::write_path( + view::write_path( doc, &member_path("a"), &json!({ "window": 30 * 86_400_000_u64 }), ) .unwrap(); - store::write_path(doc, &member_path("b"), &json!({ "window": 86_400_000 })).unwrap(); - store::write_path(doc, &member_path("c"), &json!({ "since": 1 })).unwrap(); + view::write_path(doc, &member_path("b"), &json!({ "window": 86_400_000 })).unwrap(); + view::write_path(doc, &member_path("c"), &json!({ "since": 1 })).unwrap(); }); assert_eq!(shortest_window(&doc, 7 * day), day); // Below what any node accepts: as the shortest it accepts. change(&mut doc, |doc| { - store::write_path(doc, &window_path("c"), &json!(1)).unwrap(); + view::write_path(doc, &window_path("c"), &json!(1)).unwrap(); }); assert_eq!(shortest_window(&doc, 7 * day), MIN_ELECTORATE_WINDOW); } |
