aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/client.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/client.rs')
-rw-r--r--src/federation/client.rs68
1 files changed, 51 insertions, 17 deletions
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<Version, St
/// What the version `proposed` does to `current`, in words, a line each: the parties it
/// lets in or takes out, the keys and node lists it names anew, the rules it adds or drops,
/// the checkpoint it moves the store to, and the revision it moves to; what a party votes
-/// on, without its JSON. Its number and the hash it changes go without saying.
-pub(crate) fn described(current: &Version, proposed: &Version) -> Vec<String> {
+/// 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<String, String>,
+) -> Vec<String> {
+ 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<String> {
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, String>) -> 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<String, String> {
+ 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<Str
.into_iter()
.flatten()
.filter_map(|(node, knows)| {
+ let node = match of["names"][node].as_str() {
+ Some(name) => 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(&current, &current).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(&current, &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"
]