aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation')
-rw-r--r--src/federation/client.rs68
-rw-r--r--src/federation/party.rs4
-rw-r--r--src/federation/party/status.rs37
3 files changed, 81 insertions, 28 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"
]
diff --git a/src/federation/party.rs b/src/federation/party.rs
index 8f5143b..8357d22 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -973,7 +973,7 @@ pub(crate) async fn main() -> Result<()> {
let version = to_propose(&node, &current, sub).await?;
let proposed = client::backable(&current, &version)
.map_err(|why| anyhow!("not a version a party may back: {why}"))?;
- if client::described(&current, &proposed).is_empty() {
+ if client::described(&current, &proposed, &Default::default()).is_empty() {
bail!(
"it changes nothing of version {} but its number",
current.number
@@ -2055,7 +2055,7 @@ fn ballot_line(counts: &JsonValue, current: &Version, hash: &str, hashes: &[Stri
/// (`client::described`), the places holding a value in `counts` whose writer it moves,
/// and the nodes not known to know its revision.
fn version_lines(counts: &JsonValue, current: &Version, proposed: &Version) -> Vec<String> {
- let mut lines = client::described(current, proposed);
+ let mut lines = client::described(current, proposed, &client::node_names(counts));
let places: Vec<&str> = counts["values"]
.as_object()
.into_iter()
diff --git a/src/federation/party/status.rs b/src/federation/party/status.rs
index 4c637fa..47c32e5 100644
--- a/src/federation/party/status.rs
+++ b/src/federation/party/status.rs
@@ -114,6 +114,9 @@ async fn model(node: &Node, options: &Options, party: Option<&str>) -> Result<Js
},
"node": {
"id": summary["node"],
+ "name": summary["node"]
+ .as_str()
+ .and_then(|id| client::node_names(&counts).remove(id)),
"party": summary["party"],
"votesByItself": summary["votesByItself"],
"revisions": revisions,
@@ -448,7 +451,20 @@ fn render(model: &JsonValue, paint: &Paint) -> Vec<String> {
}
let node = &model["node"];
- out.push(heading("the node"));
+ // A node's name after its ID, where it gave itself one.
+ let named = |node: &JsonValue| match node["name"].as_str() {
+ Some(name) => format!(" ({})", paint.paint(Style::Name, name)),
+ None => String::new(),
+ };
+ out.push(match node["id"].as_str() {
+ Some(id) => format!(
+ "{} {}{}",
+ heading("the node"),
+ paint.paint(Style::Id, id),
+ named(node)
+ ),
+ None => heading("the node"),
+ });
out.push(match node["party"].as_str() {
Some(party) => format!(
" {}'s{}",
@@ -521,13 +537,10 @@ fn render(model: &JsonValue, paint: &Paint) -> Vec<String> {
out.push(" nodes:".to_owned());
}
for node in &nodes {
- let name = match node["name"].as_str() {
- Some(name) => format!(" ({})", paint.paint(Style::Name, name)),
- None => String::new(),
- };
out.push(format!(
- " {}{name}",
- paint.paint(Style::Id, &text(&node["id"]))
+ " {}{}",
+ paint.paint(Style::Id, &text(&node["id"])),
+ named(node)
));
let heard = match node["heard"].as_u64() {
_ if node["here"].as_bool() == Some(true) => paint.paint(Style::Good, "this node"),
@@ -759,7 +772,13 @@ mod tests {
json!({
"format": FORMAT,
"store": {"id": "6d1e", "checkpoint": "6d1e", "heads": 3, "taken": 1, "stopped": null},
- "node": {"party": "lass", "votesByItself": true, "revisions": ["the base revision"]},
+ "node": {
+ "id": "continuum:9c07",
+ "name": "beta",
+ "party": "lass",
+ "votesByItself": true,
+ "revisions": ["the base revision"],
+ },
"version": {
"number": 3,
"hash": "a7c2",
@@ -844,7 +863,7 @@ mod tests {
);
};
has("store 6d1e");
- has("the node");
+ has("the node continuum:9c07 (beta)");
has(" lass's, voting for it by itself");
has("version 3 (a7c2)");
has(" 2 parties, 2 passing a change");