From e31d93577ccefdf540fd3df518a7d9837cb20cbb Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 20 Nov 2026 00:55:34 +0000 Subject: continuum-party: nodes by their names wherever it shows them A node goes by the name it gave itself (`--alias`), as `names` in `GET /events/counts` shows it, in what a proposal does (`show`, `proposals`, `status`, before a vote), among the nodes not known to know a revision, and at the head of `status`'s section on the node it asks, which now names that node by its ID too. A node no party lists yet has no name there: only members' entries reach the node. --- src/federation/client.rs | 68 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 17 deletions(-) (limited to 'src/federation/client.rs') diff --git a/src/federation/client.rs b/src/federation/client.rs index b95cb3c..ca2db86 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -434,15 +434,21 @@ pub(crate) fn backable(current: &Version, proposed: &Json) -> Result Vec { +/// on, without its JSON. Its number and the hash it changes go without saying. A node goes +/// by its name where `names` has one (`node_names`). +pub(crate) fn described( + current: &Version, + proposed: &Version, + names: &BTreeMap, +) -> Vec { + let node_spelled = |node: &String| node_spelled(node, names); let mut lines = Vec::new(); for (party, given) in &proposed.parties { if !current.parties.contains_key(party) { lines.push(format!( "lets {party} in: {}; {}", counted(&given.keys, "key", |key| fingerprint(key)), - counted(&given.nodes, "node", |node| node_spelled(node)), + counted(&given.nodes, "node", node_spelled), )); } } @@ -464,9 +470,7 @@ pub(crate) fn described(current: &Version, proposed: &Version) -> Vec { if is.nodes_since != was.nodes_since || is.nodes != was.nodes { lines.push(format!( "names {party}'s node list anew{}", - differing(&was.nodes, &is.nodes, "as it was", |node| node_spelled( - node - )) + differing(&was.nodes, &is.nodes, "as it was", node_spelled) )); } } @@ -548,15 +552,33 @@ pub(crate) fn fingerprint(line: &str) -> String { } } -/// A node's ID, its first twelve hex digits enough to tell it. -fn node_spelled(node: &str) -> String { +/// A node's ID, its first twelve hex digits enough to tell it, and its name after it +/// where `names` has one. +fn node_spelled(node: &str, names: &BTreeMap) -> String { let at = node.find(':').map_or(0, |colon| colon + 1); - match node.get(..at + 12) { + let id = match node.get(..at + 12) { Some(start) if node.len() > at + 12 => format!("{start}…"), _ => node.to_owned(), + }; + match names.get(node) { + Some(name) => format!("{id} ({name})"), + None => id, } } +/// The names the nodes of every party gave themselves, as `counts` (`GET /events/counts`) +/// shows them (`names`), by node ID. +pub(crate) fn node_names(counts: &serde_json::Value) -> BTreeMap { + counts["parties"] + .as_object() + .into_iter() + .flatten() + .filter_map(|(_, of)| of["names"].as_object()) + .flatten() + .filter_map(|(node, name)| Some((node.clone(), name.as_str()?.to_owned()))) + .collect() +} + /// A rule's path as a version spells it. fn rule_path(path: &[super::version::Segment]) -> String { use super::version::Segment; @@ -849,6 +871,10 @@ pub(crate) fn unready(counts: &serde_json::Value, revision: Revision) -> Vec format!("{node} ({name})"), + None => node.clone(), + }; let newest = &knows["revision"]; let named = newest["named"].as_str().unwrap_or("?"); match newest["number"].as_u64() { @@ -1350,6 +1376,9 @@ mod tests { let read = |spec: &Spec| Version::read(&spec.json()).unwrap(); let first = Spec::first(&[&tv, &lass], &rules); let current = read(&first); + let names = BTreeMap::from([(harness::node_id(1), "alpha".to_owned())]); + let described = + |current: &Version, proposed: &Version| described(current, proposed, &names); assert!(described(¤t, ¤t).is_empty()); let admitted = Spec::first(&[&tv, &lass, &makefu], &rules); assert_eq!( @@ -1361,6 +1390,7 @@ mod tests { ); let mut changed = Spec::first(&[&tv], &[]); changed.parties[0].keys_since = 1; + changed.parties[0].nodes.push(harness::node_id(1)); changed .rules .insert(0, ("names/r/wiki".to_owned(), harness::str("tv"))); @@ -1369,12 +1399,16 @@ mod tests { assert_eq!( described(¤t, &read(&changed)), [ - "takes lass out", - "names tv's keys anew, as they were", - "adds rule 1: names/r/wiki -> tv", - "drops rule 2: names/** -> tv", - "moves the store to a checkpoint at 2 heads", - "moves to revision 1 (merge-events)", + "takes lass out".to_owned(), + "names tv's keys anew, as they were".to_owned(), + format!( + "names tv's node list anew: + {}… (alpha)", + &harness::node_id(1)[..22] + ), + "adds rule 1: names/r/wiki -> tv".to_owned(), + "drops rule 2: names/** -> tv".to_owned(), + "moves the store to a checkpoint at 2 heads".to_owned(), + "moves to revision 1 (merge-events)".to_owned(), ] ); } @@ -1489,7 +1523,7 @@ mod tests { "lass": {"knows": { "a": {"wire": [52, 52], "revision": {"number": 1, "id": id, "named": "one"}}, }}, - "tv": {"knows": { + "tv": {"names": {"b": "beta"}, "knows": { "b": {"wire": [52, 52], "revision": {"number": 0, "named": "the base"}}, "c": null, "d": {"wire": [52, 52], "revision": {"number": null, "named": "x, unknown"}}, @@ -1500,7 +1534,7 @@ mod tests { assert_eq!( unready(&counts, one), [ - "tv: b knows the base at newest; c said nothing: a build from before SPEAKS, \ + "tv: b (beta) knows the base at newest; c said nothing: a build from before SPEAKS, \ or not heard from; d knows x, unknown; e knows no revision: built without \ the federation" ] -- cgit v1.3.1