diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/api/events.rs | 9 | ||||
| -rw-r--r-- | src/federation/party.rs | 176 |
2 files changed, 128 insertions, 57 deletions
diff --git a/src/api/events.rs b/src/api/events.rs index 06a6e31..81f1481 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -155,7 +155,8 @@ async fn event(State(state): State<AppState>, Path(id): Path<String>) -> Respons } /// `GET /events/counts`: what counts, as the store reads now: the current version, each -/// party's keys and nodes, what each of those nodes says it speaks and knows (`knows`), +/// party's keys and nodes, the names those nodes gave themselves (`names`, `--alias`), what +/// each of them says it speaks and knows (`knows`), /// the values at places, the members, and the proposals on the /// current version, each with the events proposing it a vote may build on (`proposals`: /// for a checkpoint, those above its heads), the votes on it, the parties @@ -164,6 +165,7 @@ async fn event(State(state): State<AppState>, Path(id): Path<String>) -> Respons /// (`client::backable`), and, for a checkpoint, what its parties signed that moving there /// refuses (`beside`, `signed_beside`). async fn counts(State(state): State<AppState>) -> Response { + let aliases = crate::swarm::members::aliases(&state.doc.read().await.doc); with_store(&state, |store| { let read = store.state(); let current = read.current(); @@ -176,11 +178,16 @@ async fn counts(State(state): State<AppState>) -> Response { .iter() .map(|node| (node.clone(), knows(&state, node))) .collect(); + let names: serde_json::Map<String, JsonValue> = nodes + .iter() + .filter_map(|node| Some((node.clone(), aliases.get(node)?.clone().into()))) + .collect(); ( party.clone(), json!({ "keys": read.keys_of(party, current), "nodes": nodes, + "names": names, "knows": knows, }), ) diff --git a/src/federation/party.rs b/src/federation/party.rs index 17769f3..19aadbd 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -1566,30 +1566,37 @@ async fn status(node: &Node, options: &Options, party: Option<&str>) -> Result<( _ => id.to_owned(), }; let text = |value: &JsonValue| value.as_str().unwrap_or_default().to_owned(); + let plural = + |n: usize, one: &str, many: &str| format!("{n} {}", if n == 1 { one } else { many }); let heads = summary["heads"].as_array().map_or(0, Vec::len); + println!("store {}", short(&text(&summary["store"]))); + println!(" checkpoint {}", short(&text(&summary["checkpoint"]))); println!( - "store {}: checkpoint {}, {heads} head{}, {} events taken after it", - short(&text(&summary["store"])), - short(&text(&summary["checkpoint"])), - if heads == 1 { "" } else { "s" }, - summary["taken"] + " {}, {} taken after the checkpoint", + plural(heads, "head", "heads"), + plural( + summary["taken"].as_u64().unwrap_or_default() as usize, + "event", + "events" + ) ); - let parties: Vec<&String> = current.parties.keys().collect(); - let needed = (2 * parties.len()).div_ceil(3); + let needed = (2 * current.parties.len()).div_ceil(3); println!( - "version {} ({}) of federation {}, {}: {} part{}, {needed} passing a change ({})", + "version {} ({})", current.number, - short(¤t.hash.to_string()), - counts["version"]["version"]["federation"], - current.revision, - parties.len(), - if parties.len() == 1 { "y" } else { "ies" }, - parties - .iter() - .map(|party| party.as_str()) - .collect::<Vec<_>>() - .join(", ") + short(¤t.hash.to_string()) ); + println!( + " federation {}, {}", + counts["version"]["version"]["federation"], current.revision + ); + println!( + " {}, {needed} passing a change:", + plural(current.parties.len(), "party", "parties") + ); + for party in current.parties.keys() { + println!(" {party}"); + } let record = options .record .as_ref() @@ -1601,46 +1608,14 @@ async fn status(node: &Node, options: &Options, party: Option<&str>) -> Result<( .map(str::to_owned) .or_else(|| summary["party"].as_str().map(str::to_owned)); if let Some(party) = &party { - let keys = listed(&counts, party, "keys"); - let nodes = listed(&counts, party, "nodes"); - let named = match options.key.as_deref().map(given_key) { - Some(Ok(given)) => { - let line = match given { - Given::Private(key) => PublicKey(key.verifying_key().to_bytes()).line(), - Given::Public(key, _) => key.line(), - }; - let names = current - .parties - .get(party) - .is_some_and(|given| given.keys.contains(&line)); - if names { - "; the version names --key's" - } else { - "; the version doesn't name --key's: it signs none of its votes, proposals, \ - key set or node list" + println!("{party}"); + match current.parties.get(party) { + None => println!(" no party of version {}", current.number), + Some(given) => { + for line in party_lines(&counts, party, &given.keys, options.key.as_deref()) { + println!(" {line}"); } } - _ => "", - }; - let spelled = |items: &[String], spell: fn(&str) -> String| { - items - .iter() - .map(|item| spell(item)) - .collect::<Vec<_>>() - .join(", ") - }; - if current.parties.contains_key(party) { - println!( - "{party}: {} key{} ({}){named}; {} node{} ({})", - keys.len(), - if keys.len() == 1 { "" } else { "s" }, - spelled(&keys, client::fingerprint), - nodes.len(), - if nodes.len() == 1 { "" } else { "s" }, - spelled(&nodes, |node| node.to_owned()), - ); - } else { - println!("{party}: no party of version {}", current.number); } } let by_itself = if summary["votesByItself"].as_bool() == Some(true) { @@ -1707,6 +1682,63 @@ async fn status(node: &Node, options: &Options, party: Option<&str>) -> Result<( Ok(()) } +/// A party's keys and nodes, as `status` says them under its name, a line each, indented +/// beneath their heading: the keys the version names (`named`), which sign its votes, +/// proposals, key set and node list; those of its key set as it counts (`counts`), which +/// sign its writes; each by its fingerprint, `--key`'s (`given`) marked, and said where +/// it is neither; and its nodes as they count, by ID, each with the name it gave itself +/// where it did. +fn party_lines( + counts: &JsonValue, + party: &str, + named: &[String], + given: Option<&str>, +) -> Vec<String> { + let given = given.and_then(|given| match given_key(given).ok()? { + Given::Private(key) => Some(PublicKey(key.verifying_key().to_bytes()).line()), + Given::Public(key, _) => Some(key.line()), + }); + let key_line = |key: &String| { + let mark = if given.as_ref() == Some(key) { + " (--key)" + } else { + "" + }; + format!(" {}{mark}", client::fingerprint(key)) + }; + let set = listed(counts, party, "keys"); + let mut lines = + vec!["keys the version names (votes, proposals, key set, node list):".to_owned()]; + lines.extend(named.iter().map(key_line)); + if set == named { + lines.push("keys of its key set (writes): the same".to_owned()); + } else { + lines.push("keys of its key set (writes):".to_owned()); + lines.extend(set.iter().map(key_line)); + } + if let Some(given) = &given + && !named.contains(given) + && !set.contains(given) + { + lines.push(format!( + "--key, {}, is none of these: it signs nothing for {party}", + client::fingerprint(given) + )); + } + let nodes = listed(counts, party, "nodes"); + if nodes.is_empty() { + lines.push("nodes: none".to_owned()); + } else { + lines.push("nodes:".to_owned()); + } + let names = &counts["parties"][party]["names"]; + lines.extend(nodes.iter().map(|node| match names[node].as_str() { + Some(name) => format!(" {node} ({name})"), + None => format!(" {node}"), + })); + lines +} + /// The values that count, all or under the place `path` names (and it), each with who /// writes there, a line each: `PATH VALUE (WRITER)`, the value as JSON; or with `--json`, /// one JSON object of them, by place. @@ -2866,6 +2898,38 @@ mod tests { assert_eq!(working_as(&["status"], None, None).unwrap(), None); } + /// `status` says a party's keys and nodes a line each: the version's, its key set's + /// where they differ, `--key`'s marked, and each node with the name it gave itself. + #[test] + fn a_partys_keys_and_nodes_a_line_each() { + use crate::federation::client::fingerprint; + use crate::federation::harness::{key_line, node_id}; + let (named, laptop) = (key_line(1), key_line(2)); + let counts = serde_json::json!({"parties": {"tv": { + "keys": [laptop], + "nodes": [node_id(1), node_id(2)], + "names": {node_id(1): "alpha"}, + }}}); + let dir = crate::files::scratch_dir("party-lines"); + let given = dir.join("laptop.pub"); + std::fs::write(&given, format!("{laptop} tv@laptop\n")).unwrap(); + let lines = super::party_lines(&counts, "tv", std::slice::from_ref(&named), given.to_str()); + assert_eq!( + lines, + [ + "keys the version names (votes, proposals, key set, node list):".to_owned(), + format!(" {}", fingerprint(&named)), + "keys of its key set (writes):".to_owned(), + format!(" {} (--key)", fingerprint(&laptop)), + "nodes:".to_owned(), + format!(" {} (alpha)", node_id(1)), + format!(" {}", node_id(2)), + ] + ); + let lines = super::party_lines(&counts, "tv", std::slice::from_ref(&laptop), None); + assert_eq!(lines[2], "keys of its key set (writes): the same"); + } + /// `values` shows a place and those under it, not those beside it. #[test] fn places_under_a_path() { |
