aboutsummaryrefslogtreecommitdiffstats
path: root/src/swarm/members.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-10-31 13:25:11 +0100
committertv <tv@krebsco.de>2026-10-31 13:25:11 +0100
commit694347ad456766004e2792c4b4cda865a8a40386 (patch)
treeebcc97eef8ffd9954e3ecae75bcf5935f5c33d9a /src/swarm/members.rs
parent028f80f46784b483215ab7a2952cc54fdd46ea5d (diff)
rename AutoMerge to Document
Diffstat (limited to 'src/swarm/members.rs')
-rw-r--r--src/swarm/members.rs34
1 files changed, 17 insertions, 17 deletions
diff --git a/src/swarm/members.rs b/src/swarm/members.rs
index 94db4c9..0e27681 100644
--- a/src/swarm/members.rs
+++ b/src/swarm/members.rs
@@ -18,10 +18,10 @@
use std::collections::HashMap;
use std::time::Duration;
-use automerge::AutoCommit;
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::state::AppState;
@@ -37,7 +37,7 @@ pub(crate) fn member_path(pk: &str) -> Vec<String> {
}
/// Public keys of all registered members, sorted.
-pub(crate) fn members(doc: &AutoCommit) -> Vec<String> {
+pub(crate) fn members(doc: &Document) -> Vec<String> {
let mut members = store::keys_at(doc, &members_dir()).unwrap_or_default();
members.sort();
members
@@ -67,14 +67,14 @@ fn tier_path(pk: &str) -> Vec<String> {
}
/// `pk`'s tier; transient unless its entry says stable.
-pub(crate) fn tier(doc: &AutoCommit, pk: &str) -> Tier {
+pub(crate) fn tier(doc: &Document, pk: &str) -> Tier {
match store::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 AutoCommit, pk: &str, tier: Tier) -> Result<(), StoreError> {
+pub(crate) fn set_tier(doc: &mut Document, pk: &str, tier: Tier) -> Result<(), StoreError> {
store::write_path(doc, &tier_path(pk), &json!(tier.as_str()))
}
@@ -85,7 +85,7 @@ fn alias_path(pk: &str) -> Vec<String> {
}
/// `pk`'s alias, if it gave itself one.
-pub(crate) fn alias(doc: &AutoCommit, pk: &str) -> Option<String> {
+pub(crate) fn alias(doc: &Document, pk: &str) -> Option<String> {
match store::read_path(doc, &alias_path(pk)) {
Ok(Some(JsonValue::String(alias))) => Some(alias),
_ => None,
@@ -93,14 +93,14 @@ pub(crate) fn alias(doc: &AutoCommit, pk: &str) -> Option<String> {
}
/// The aliases of all members that have one.
-pub(crate) fn aliases(doc: &AutoCommit) -> HashMap<String, String> {
+pub(crate) fn aliases(doc: &Document) -> HashMap<String, String> {
members(doc)
.into_iter()
.filter_map(|pk| alias(doc, &pk).map(|alias| (pk, alias)))
.collect()
}
-fn set_alias(doc: &mut AutoCommit, pk: &str, alias: Option<&str>) -> Result<(), StoreError> {
+fn set_alias(doc: &mut Document, 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) {
@@ -121,7 +121,7 @@ const MAX_ADDRESSES: usize = 8;
/// Where `pk` says it can be reached (at most `MAX_ADDRESSES`); none if its entry
/// doesn't say.
-pub(crate) fn addresses(doc: &AutoCommit, pk: &str) -> Vec<String> {
+pub(crate) fn addresses(doc: &Document, pk: &str) -> Vec<String> {
match store::read_path(doc, &addresses_path(pk)) {
Ok(Some(JsonValue::Array(items))) => items
.into_iter()
@@ -138,7 +138,7 @@ pub(crate) fn addresses(doc: &AutoCommit, pk: &str) -> Vec<String> {
/// What the member list tells this node besides who its neighbours are: where members
/// are (`learn_addresses`), what they call themselves (`set_aliases`), and the relays
/// listed (`relays`). Done whenever the neighbours are recomputed (`topology::refresh`).
-pub(crate) fn apply(state: &AppState, doc: &AutoCommit) {
+pub(crate) fn apply(state: &AppState, doc: &Document) {
learn_addresses(state, doc);
set_aliases(state, aliases(doc));
state.transport.learn_relays(&relays(doc));
@@ -159,7 +159,7 @@ fn set_aliases(state: &AppState, aliases: HashMap<String, String>) {
/// Hands every other member's addresses to the transport (`Transport::learn_addresses`):
/// the member list is how a node finds where members it never met are.
-pub(crate) fn learn_addresses(state: &AppState, doc: &AutoCommit) {
+pub(crate) fn learn_addresses(state: &AppState, doc: &Document) {
let me = state.local_id();
for pk in members(doc) {
if pk != me {
@@ -175,7 +175,7 @@ const MAX_RELAYS: usize = 32;
/// addresses (`libp2p:<multiaddr>`, with or without the relay's peer ID) of relays that
/// 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: &AutoCommit) -> Vec<String> {
+pub(crate) fn relays(doc: &Document) -> Vec<String> {
match store::read_path(doc, &meta::path(&[meta::RELAYS])) {
Ok(Some(JsonValue::Array(items))) => items
.into_iter()
@@ -195,7 +195,7 @@ fn alive_path(pk: &str) -> Vec<String> {
path
}
-fn millis_at(doc: &AutoCommit, path: &[String]) -> Option<u64> {
+fn millis_at(doc: &Document, path: &[String]) -> Option<u64> {
match store::read_path(doc, path) {
Ok(Some(JsonValue::Number(number))) => number.as_u64(),
_ => None,
@@ -203,7 +203,7 @@ fn millis_at(doc: &AutoCommit, 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: &AutoCommit, pk: &str) -> Option<u64> {
+pub(crate) fn alive_at(doc: &Document, 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))
@@ -211,7 +211,7 @@ pub(crate) fn alive_at(doc: &AutoCommit, pk: &str) -> Option<u64> {
/// The members that said they are alive within `window` before `now_ms` (sorted): the
/// electorate of a remake (`remake::coordinator`).
-pub(crate) fn electorate(doc: &AutoCommit, now_ms: u64, window: Duration) -> Vec<String> {
+pub(crate) fn electorate(doc: &Document, now_ms: u64, window: Duration) -> Vec<String> {
let oldest = now_ms.saturating_sub(window.as_millis() as u64);
members(doc)
.into_iter()
@@ -314,7 +314,7 @@ pub(crate) async fn ensure_registered(state: &AppState) {
}
/// Removes `pk`'s entry (an operator forgot it). Absent is fine.
-pub(crate) fn unregister(doc: &mut AutoCommit, pk: &str) -> Result<(), StoreError> {
+pub(crate) fn unregister(doc: &mut Document, pk: &str) -> Result<(), StoreError> {
match store::delete_path(doc, &member_path(pk), false) {
Err(StoreError::NotFound) => Ok(()),
result => result,
@@ -329,7 +329,7 @@ mod tests {
fn electorate_by_alive_stamp() {
let day = 24 * 3600 * 1000;
let now = 100 * day;
- let mut doc = AutoCommit::new();
+ let mut doc = Document::new();
let entries = [
("fresh", json!({ "since": 1, "alive": now - day })),
("stale", json!({ "since": 1, "alive": now - 8 * day })),
@@ -347,7 +347,7 @@ mod tests {
#[test]
fn listed_relays() {
- let mut doc = AutoCommit::new();
+ let mut doc = Document::new();
assert!(relays(&doc).is_empty());
let relay = "libp2p:/ip6/fd00::1/tcp/9661";
store::write_path(&mut doc, &meta::path(&[meta::RELAYS]), &json!([relay, 7])).unwrap();