diff options
| author | tv <tv@krebsco.de> | 2026-11-19 21:49:18 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-11-19 22:54:03 +0100 |
| commit | 4c2a4dfdb88fb008e0ba915047207efb8418ce77 (patch) | |
| tree | 7d550d6372a7ac38a65c7dea930c9474e949ac6a /src/federation/party.rs | |
| parent | 5aa649bcb366ed6863732dac68ce081bacdbe4a8 (diff) | |
continuum-party status: everything but the values, in colour, and as JSON
`status` becomes the one place to look (party/status.rs): everything
about the federation at the node but the values, which `get` and
`values` read.
* the store: its ID, checkpoint, heads, what it took, its stop;
* the node: its party, whether it votes by itself, the revisions it knows;
* the version: number, hash, federation, revision, how many pass a change;
* every party: the keys its version names and its key set, --key's
marked, and its nodes, each with its name, how long ago the node heard
from it, and what it runs (wire versions, newest revision);
* every proposal on the current version: how it stands, who voted what
and who not yet, who proposed it and what it does (version_lines, as
`show` says it), what moving to its checkpoint refuses, why no client
backs it;
* what the node refused of its own;
* the party the client works as: its --key, its record held against the
node's votes, what `again` signs again;
* the warnings of what may keep votes away (cut_off).
It is built as one JSON object (`model`), which `--json` writes, its
`format` (FORMAT, 1) raised where it changes otherwise than by more
members; what people read is rendered from it (`render`), and may change
from release to release. On a terminal, in colour (Paint): true colour
where COLORTERM says so, 256 where TERM does, else the basic eight; none
with NO_COLOR, TERM=dumb, or off a terminal. What is fine is green, what
needs a look yellow, what is wrong red; headings and the shortest prefix
naming a proposal bold.
Diffstat (limited to 'src/federation/party.rs')
| -rw-r--r-- | src/federation/party.rs | 343 |
1 files changed, 20 insertions, 323 deletions
diff --git a/src/federation/party.rs b/src/federation/party.rs index 29d9cb8..e030048 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -35,8 +35,8 @@ //! twice; //! * `proposals`: the proposals on the current version at the node, each with whether it //! passed or is dead, who voted what, and what its version changes (`client::changes`); -//! * `status`: the store, its version and parties, the party's keys and nodes, the node, -//! the proposals, the versions the node refused of its own, what to sign again; +//! * `status`: everything about the federation at the node but the values (`status`); +//! with `--json`, as one JSON object of a versioned format; //! * `values [PATH]`: the values that count, all or under `PATH`, with who writes there; //! * `get [PATH]`: what counts at `PATH` as one JSON value (`tree`): a place's, or what lies //! under it, places and the values within theirs; with `-r`, a string as it is; @@ -81,6 +81,8 @@ use super::version::Version; use crate::access::sshsig::PublicKey; use crate::config; +mod status; + /// The client's options (`config::PARTY_OPTIONS`), as the command line and configuration /// files set them. #[derive(Deserialize)] @@ -284,10 +286,21 @@ fn command() -> Command { .subcommand(Command::new("proposals").about( "The proposals on the current version, each as show says it", )) - .subcommand(Command::new("status").about( - "The store, its version and parties, the party's keys and nodes, the node, the \ - proposals on the current version, and what the party has to sign again", - )) + .subcommand( + Command::new("status") + .about( + "Everything about the federation at the node but the values: the store, the \ + node, the version, every party's keys and nodes, the proposals and what \ + each does, what the node refused, and the party's own key, record and \ + what it signs again", + ) + .arg( + Arg::new("json") + .long("json") + .action(ArgAction::SetTrue) + .help("As one JSON object, of a format versioned by its `format`"), + ), + ) .subcommand( Command::new("get") .about( @@ -679,7 +692,7 @@ pub(crate) async fn main() -> Result<()> { return proposals(&node).await; } if name == "status" { - return status(&node, &options, party.as_deref()).await; + return status::status(&node, &options, party.as_deref(), sub.get_flag("json")).await; } if name == "values" { return values(&node, sub).await; @@ -1817,186 +1830,6 @@ fn working_as(name: &str, sub: &ArgMatches, options: &Options) -> Result<Option< } } -/// What the node and the party's record say at a glance: the store, its checkpoint and -/// heads; the current version, its federation and revision, and its parties; the party's -/// keys and node list as they count, and whether the version names `--key`; the node's -/// party, whether it votes for it by itself, and where it stopped; the proposals on the -/// current version, each as `show` begins; the versions the node refused of its own; and -/// what a move refused that the party signs again (`again`). -async fn status(node: &Node, options: &Options, party: Option<&str>) -> Result<()> { - let summary = node.get("/events").await?; - let counts = node.counts().await?; - let current = current(&counts)?; - 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 {}", text(&summary["store"])); - println!(" checkpoint {}", text(&summary["checkpoint"])); - println!( - " {}, {} taken after the checkpoint", - plural(heads, "head", "heads"), - plural( - summary["taken"].as_u64().unwrap_or_default() as usize, - "event", - "events" - ) - ); - let needed = (2 * current.parties.len()).div_ceil(3); - println!("version {} ({})", current.number, current.hash); - 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() - .map(PathBuf::from) - .filter(|path| path.exists()) - .map(|path| Record::load(&path).map_err(|why| anyhow!(why))) - .transpose()?; - let party = party - .map(str::to_owned) - .or_else(|| summary["party"].as_str().map(str::to_owned)); - if let Some(party) = &party { - 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 by_itself = if summary["votesByItself"].as_bool() == Some(true) { - ", voting for it by itself" - } else { - "" - }; - match summary["party"].as_str() { - Some(of) => println!("the node: {of}'s{by_itself}"), - None => println!("the node: of no party"), - } - if let Some(stopped) = summary["stopped"].as_object() { - println!( - " stopped at {}: {}", - stopped - .get("id") - .and_then(JsonValue::as_str) - .unwrap_or_default(), - stopped - .get("why") - .and_then(JsonValue::as_str) - .unwrap_or_default() - ); - } - let hashes = proposal_hashes(&counts); - match hashes.len() { - 0 => println!("no proposal on version {}", current.number), - 1 => println!("1 proposal on version {} (show says more):", current.number), - n => println!( - "{n} proposals on version {} (show says more):", - current.number - ), - } - for hash in &hashes { - println!(" {}", ballot_line(&counts, ¤t, hash, &hashes)); - } - let refused = node.get("/events/refused").await?; - let own = refused["own"].as_array().map_or(0, Vec::len); - if own > 0 { - println!( - "the node refused {own} version event{} by what it took before: keys may have \ - voted twice, and nodes be apart (GET /events/refused)", - if own == 1 { "" } else { "s" } - ); - } - if let Some(record) = &record { - let moved: BTreeSet<Id> = refused["refused"] - .as_array() - .into_iter() - .flatten() - .filter(|entry| matches!(entry["kind"].as_str(), Some("notAbove" | "builtOn"))) - .filter_map(|entry| entry["id"].as_str().and_then(Id::from_hex)) - .collect(); - let again = record.again(|id| moved.contains(id)).len(); - if again > 0 { - println!( - "{} of {}'s latest events a move refused: `again` signs {} again", - again, - record.party, - if again == 1 { "it" } else { "them" } - ); - } - } - 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. @@ -3214,142 +3047,6 @@ 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"); - } - - /// `get` reads what counts as JSON: a place's value; what lies under a path, places and - /// the values within theirs, as one object; a list's item by its index; a deeper place - /// winning over what a value above it held there. - #[test] - fn what_counts_as_json() { - use serde_json::json; - let values = json!({ - "home/tv/motd": "hello", - "home/tv/hosts/laptop": {"ip6": "42::1", "ports": [22, 80]}, - "home/tv/hosts": {"desk": {"ip6": "42::2"}, "laptop": {"ip6": "old"}}, - "home/lass/motd": "hi", - "home/tvx/motd": "beside", - }); - let values = values.as_object().unwrap(); - let get = |path: &str| super::at(&super::tree(values, path), path).cloned(); - assert_eq!(get("home/tv/motd"), Some(json!("hello"))); - assert_eq!( - get("home/tv"), - Some(json!({ - "motd": "hello", - "hosts": { - "desk": {"ip6": "42::2"}, - "laptop": {"ip6": "42::1", "ports": [22, 80]}, - }, - })) - ); - assert_eq!(get("home/tv/hosts/desk/ip6"), Some(json!("42::2"))); - assert_eq!(get("home/tv/hosts/laptop/ports/1"), Some(json!(80))); - assert_eq!(get("home/tv/hosts/laptop/ports/2"), None); - assert_eq!(get("home/tv/elsewhere"), None); - assert_eq!(get("home").unwrap()["lass"], json!({"motd": "hi"})); - assert_eq!(get("").unwrap()["home"]["tvx"]["motd"], "beside"); - } - - /// Before a vote, the party's votes the node shows are held against the record's: one - /// the node shows that the record lacks refuses the vote, one the record holds that - /// the node doesn't show is said; nothing where they agree. - #[test] - fn a_record_held_against_the_votes_a_node_shows() { - use crate::federation::client::Record; - use crate::federation::harness::{Party, Spec}; - use crate::federation::store::Id; - use crate::federation::version::Version; - let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); - let first = Spec::first(&[&tv, &lass], &[]); - let current = Version::read(&first.json()).unwrap(); - let (one, two) = (Id::of(b"one"), Id::of(b"two")); - let mut record = Record::before(Id::of(b"root"), "tv"); - record.cast( - ¤t, - &tv.vote(&[Id::of(b"root")], first.hash(), one, true), - Vec::new(), - ); - let counts = |yes_one: &[&str], no_two: &[&str]| { - serde_json::json!({"proposals": { - one.to_string(): {"yes": yes_one, "no": []}, - two.to_string(): {"yes": [], "no": no_two}, - }}) - }; - let agreeing = counts(&["tv", "lass"], &[]); - assert_eq!( - super::unrecorded(&agreeing, ¤t, &record), - (Vec::new(), Vec::new()) - ); - assert!(super::consistent(&agreeing, ¤t, &record).is_ok()); - let elsewhere = counts(&["tv"], &["tv"]); - let (theirs, ours) = super::unrecorded(&elsewhere, ¤t, &record); - assert_eq!(theirs.len(), 1); - assert!(theirs[0].starts_with("`false` on "), "{theirs:?}"); - assert!(ours.is_empty()); - assert!(super::consistent(&elsewhere, ¤t, &record).is_err()); - let behind = counts(&["lass"], &[]); - let (theirs, ours) = super::unrecorded(&behind, ¤t, &record); - assert!(theirs.is_empty()); - assert_eq!(ours.len(), 1); - assert!(super::consistent(&behind, ¤t, &record).is_ok()); - } - - /// What may keep a vote from the node: the party's own nodes not heard from lately; - /// and parties none of whose nodes were, where they are a third of those with nodes. - #[test] - fn nodes_cut_off_are_said() { - let ago = super::LATELY + 1; - let counts = |lass_heard: Option<u64>| { - let lass = match lass_heard { - Some(ago) => serde_json::json!({"n3": ago}), - None => serde_json::json!({}), - }; - serde_json::json!({"parties": { - "tv": {"nodes": ["n1", "n2"], "names": {"n2": "beta"}, "heard": {"n1": 0, "n2": ago}}, - "lass": {"nodes": ["n3"], "names": {}, "heard": lass}, - "makefu": {"nodes": ["n4"], "names": {}, "heard": {"n4": 3}}, - "krebs": {"nodes": [], "names": {}, "heard": {}}, - }}) - }; - let lines = super::cut_off(&counts(Some(10)), "tv"); - assert_eq!(lines.len(), 1); - assert!(lines[0].contains("n2 (beta)"), "{lines:?}"); - let lines = super::cut_off(&counts(None), "makefu"); - assert_eq!(lines.len(), 1); - assert!(lines[0].contains("any node of lass"), "{lines:?}"); - assert!(super::cut_off(&counts(Some(10)), "makefu").is_empty()); - } - /// `values` shows a place and those under it, not those beside it. #[test] fn places_under_a_path() { |
