//! `continuum-party`: a party's client of a federation (`docs/federation.md`). It signs its //! party's events with one of its keys (`--key`: an OpenSSH private key file, or a public //! key the ssh-agent at `SSH_AUTH_SOCK` holds, given itself or in a file), puts them //! through a node's API (`--node`, its user and password in the URL, or `--token`), and //! keeps its party's record (`--record`, `client::Record`), by which it votes, written //! before a vote is sent. Its options may be set in configuration files as the node's are //! (`--config`, `config::PARTY_OPTIONS`): the same format, imports and all. //! //! * `init PARTY`: begins a record of `PARTY`, on the version current now; with `--root //! FILE`, from the file the store's swarm starts from, for a party that never voted in //! it: the record casts on every version, the first too; //! * `write PATH VALUE`: writes `VALUE` (JSON) at `PATH`; //! * `keys`, `nodes`: the party's key set (`authorized_keys` lines, each one argument, or //! files of them, their comments left out) or node list (node IDs) as it counts at the //! node, one a line (`--list`); or written: with `--add` and `--remove`, the set or list //! as it counts, changed; with `--set`, set whole. One left empty is written with //! `--force` only; //! * `next`: the version after the current one, to change and propose: the current one //! numbered on, naming its hash, without its checkpoint (JSON), to standard output; //! * `propose FILE`: proposes the version `FILE` holds (JSON), where a party may back it //! (`client::backable`), saying first what it does and asking on a terminal, and says //! the proposal's hash; with `--yes`, votes `true` on it too, as `vote` does; with //! `--print`, writes the version to standard output instead. `propose admit`, `remove`, //! `give`, `take`, `keys`, `nodes`, `checkpoint` and `revision` propose the next //! version changed as they say (`Intent`), likewise; //! * `vote PROPOSAL yes|no`: votes on the proposal of hash `PROPOSAL` on the current //! version, by the record; with `--through`, through the node that votes for the //! party by itself and keeps its record, as such a party votes by hand; //! * `again`: signs again what a move refused; //! * `twice RECORD...`: names, from the records of parties' clients, a party that voted //! 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; //! * `values [PATH]`: the values that count, all or under `PATH`, with who writes there; //! * `found PARTY=KEY|FILE...`: the first version of a federation, from its parties' keys, //! node lists (`--nodes`) and rules (`--rule`), to standard output; //! * `root FILE`: the root of the federation whose first version `FILE` holds (JSON), //! the file its founders start its swarm from (`--store-root`), to standard output; //! * `fork FILE [HEAD...]`: the root of the fork at the heads named (those the node shows, //! where none are) by the version `FILE` holds, as the node builds it, with the events //! it keeps: the file the fork's founders start its swarm from, to standard output. //! `fork proposal PROPOSAL` forks by the version a proposal proposes, and `fork admit`, //! `remove`, `give`, `take`, `keys`, `nodes` and `revision` by the next version changed //! as they say, as `propose` does; at the heads `--at` names, `--founder PARTY=NODE,…` //! naming a party's node list anew in the fork; //! * `check FILE`: what the node finds of the fork whose root `FILE` holds: whether it //! builds it, what it holds that its heads lack, and the parties whose keys it gives //! otherwise. //! //! Votes, proposals, key sets and node lists are signed with a key the current version //! names for the party, which the client checks first (`Signer::named_by`): where its key //! isn't one, only a version naming the party's keys or list anew changes them. use std::collections::BTreeSet; use std::path::{Path, PathBuf}; use anyhow::{Context, Result, anyhow, bail}; use clap::{Arg, ArgAction, ArgGroup, ArgMatches, Command}; use ed25519_dalek::SigningKey; use serde::Deserialize; use serde_json::Value as JsonValue; use super::client::{self, Record}; use super::event::{self, Does, Event, Signed}; use super::fold::Federation; use super::jcs::Json; use super::judge; use super::keys::{self, Key}; use super::revision::{FEATURES, Revision}; use super::store::Id; use super::store::memory::Store; use super::version::Version; use crate::access::sshsig::PublicKey; use crate::config; /// The client's options (`config::PARTY_OPTIONS`), as the command line and configuration /// files set them. #[derive(Deserialize)] #[serde(rename_all = "kebab-case", deny_unknown_fields)] struct Options { #[serde(default = "default_node")] node: String, token: Option, key: Option, record: Option, } fn default_node() -> String { "http://localhost:3000".to_owned() } fn command() -> Command { let id = |name: &'static str| Arg::new(name).required(true); let command = Command::new("continuum-party") .version(env!("CARGO_PKG_VERSION")) .long_version(crate::release::described()) .about("A party's client of a federation: signs its events, and votes by its record"); config::with_options(command, config::PARTY_OPTIONS) .arg( Arg::new("federation") .long("federation") .value_name("NAME") .help( "The federation to work with, by the name of its directory in \ ~/.config/continuum/federations: its party.toml, record and key. Where \ there is one, and no --config, that one", ), ) .subcommand_required(true) .subcommand( Command::new("init") .about("Begins a record") .arg(id("party")) .arg( Arg::new("root") .long("root") .value_name("FILE") .help("From the file the store's swarm starts from, for a party that never voted in it"), ), ) .subcommand( Command::new("write") .about("Writes a value (JSON) at a path") .arg(id("path")) .arg(id("value")), ) .subcommand( Command::new("keys") .about( "The party's key set as it counts at the node, or written with keys added, \ taken off, or set whole", ) .args(changing("KEY|FILE", "keys", "key set")) .group(changes()), ) .subcommand( Command::new("nodes") .about( "The party's node list as it counts at the node, or written with nodes \ added, taken off, or set whole", ) .args(changing("NODE", "nodes", "node list")) .group(changes()), ) .subcommand( Command::new("next") .about( "The version after the current one, to change and propose: numbered on, \ naming the current one's hash, without its checkpoint", ) .arg( Arg::new("revision") .long("revision") .value_name("FEATURE") .help( "Following the revision that adds FEATURE (federation.md, Changing \ the federation): pass it once every party's nodes run a continuum \ that knows it", ), ), ) .subcommand( Command::new("propose") .about( "Proposes a version, saying what it does and asking first on a terminal: the \ version a file holds (JSON), or the next one changed as a subcommand says", ) .arg(id("file").help("A file holding the version (JSON)")) .args(proposing()) .args_conflicts_with_subcommands(true) .subcommand_negates_reqs(true) .subcommands(intents().into_iter().map(|intent| intent.args(proposing()))), ) .subcommand( Command::new("vote") .about("Votes on a proposal on the current version, by its hash") .arg(id("proposal")) .arg(Arg::new("yes").required(true).value_parser(["yes", "no"])) .arg( Arg::new("through") .long("through") .action(ArgAction::SetTrue) .help("Through the node, which votes for the party and keeps its record"), ), ) .subcommand(Command::new("again").about("Signs again what a move refused")) .subcommand( Command::new("found") .about( "The first version of a federation (JSON), to standard output, from its \ parties' keys, node lists and rules: what root makes the root of", ) .arg( Arg::new("parties") .value_name("PARTY=KEY|FILE") .num_args(1..) .required(true) .action(ArgAction::Append) .help( "A party and its keys: an authorized_keys line, one argument, or a \ file of them; again for more keys of it", ), ) .arg( Arg::new("nodes") .long("nodes") .value_name("PARTY=NODE,…") .action(ArgAction::Append) .help("A party's node list, by node IDs (continuum node-id): none where not given"), ) .arg( Arg::new("rule") .long("rule") .value_name("PATH=WRITER") .action(ArgAction::Append) .help( "A rule, in order: the places PATH matches to WRITER, a party, \ {party} or nobody. Without any, each party writes under \ home/{party}/", ), ) .arg( Arg::new("revision") .long("revision") .value_name("FEATURE") .help("Following the revision that adds FEATURE, as next --revision"), ), ) .subcommand( Command::new("root") .about("The root of the federation whose first version a file holds (JSON)") .arg(id("file")), ) .subcommand( Command::new("fork") .about( "The root of a fork, as the node builds it, to standard output: at the \ heads the node shows, or those named; by the version a file holds (JSON), \ a proposal's, or the next one changed as a subcommand says", ) .arg(id("file").help("A file holding the fork's version (JSON)")) .arg( Arg::new("heads") .num_args(1..) .help("The heads it forks at, by ID or prefix: those the node shows where none are"), ) .args_conflicts_with_subcommands(true) .subcommand_negates_reqs(true) .subcommand( Command::new("proposal") .about("By the version a proposal on the current version proposes") .arg(id("proposal")), ) .subcommands( intents() .into_iter() .filter(|intent| intent.get_name() != "checkpoint"), ) .mut_subcommands(|intent| intent.args(forking())), ) .subcommand( Command::new("check") .about("What the node finds of the fork whose root a file holds") .arg(id("file")), ) .subcommand(Command::new("judge").about( "What a store makes of the histories its input spells, in a format every release \ keeps (federation-tests.md, Against released revisions)", )) .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("values") .about("The values that count, each with who writes there: all, or under a path") .arg(Arg::new("path").help("The place, and those under it")) .arg( Arg::new("json") .long("json") .action(ArgAction::SetTrue) .help("As one JSON object, by place"), ), ) .subcommand( Command::new("show") .about( "What a proposal on the current version does, in words, before a vote: \ who voted what, the parties, keys, nodes and rules it changes, who writes \ where after it", ) .arg(id("proposal")), ) .subcommand( Command::new("twice") .about("Names, from records, a party that voted twice") .arg( Arg::new("records") .num_args(1..) .required(true) .action(ArgAction::Append), ), ) } /// `propose`'s own options, and each of its subcommands'. fn proposing() -> [Arg; 2] { [ Arg::new("yes") .long("yes") .action(ArgAction::SetTrue) .help("Votes true on it too, by the record, as vote does"), Arg::new("print") .long("print") .action(ArgAction::SetTrue) .help("Writes the version to standard output (JSON) instead, to change and propose"), ] } /// The options of `fork`'s subcommands: the heads, and the founders' node lists. fn forking() -> [Arg; 2] { [ Arg::new("at") .long("at") .value_name("HEAD") .num_args(1..) .action(ArgAction::Append) .help("The heads it forks at, by ID or prefix: those the node shows where none are"), Arg::new("founder") .long("founder") .value_name("PARTY=NODE,…") .action(ArgAction::Append) .help( "Names the party's node list anew in the fork, so that these nodes are members \ there: a founder's, its nodes there", ), ] } /// The changes `propose` makes by what they are for (`Intent`), a subcommand each; `fork` /// takes them too, but `checkpoint`. fn intents() -> Vec { let required = |name: &'static str| Arg::new(name).required(true); let keys = || { Arg::new("keys") .value_name("KEY|FILE") .num_args(1..) .action(ArgAction::Append) .help("Keys, each an authorized_keys line, one argument, or a file of such lines") }; let nodes = || { Arg::new("nodes") .value_name("NODE") .num_args(0..) .action(ArgAction::Append) .help("Node IDs (continuum:…), as `continuum node-id` says them") }; vec![ Command::new("admit") .about("Lets a party in, naming its keys and its node list") .arg(required("party")) .arg(keys().long("keys").required(true)) .arg(nodes().long("nodes")), Command::new("remove") .about("Takes a party out, with the rules giving it places") .arg(required("party")), Command::new("give") .about( "Gives the places a path matches to a party, or each to the party it names \ ({party}), by a rule before every other", ) .arg(required("path")) .arg(required("writer").value_name("PARTY|{party}")), Command::new("take") .about( "Gives the places a path matches to nobody: its rule goes, and another \ still giving one of them is overruled", ) .arg(required("path")), Command::new("keys") .about("Names a party's keys anew: every key set it wrote before counts no more") .arg(required("party")) .arg(keys().required(true)), Command::new("nodes") .about("Names a party's node list anew: every node list it wrote before counts no more") .arg(required("party")) .arg(nodes()), Command::new("checkpoint") .about("Moves the store to a checkpoint at the heads the node shows"), Command::new("revision") .about( "Follows the revision that adds a feature (federation.md, Changing the \ federation): once every party's nodes run a continuum that knows it", ) .arg(required("feature")), ] } /// What lists or changes the party's key set or node list (`whole`), of `items` named /// `value`: `--list`, as it counts, what the command does given none of the others; /// `--add` and `--remove`, changing it as it counts; `--set`, setting it whole; `--force`, /// writing it empty. fn changing(value: &'static str, items: &str, whole: &str) -> [Arg; 5] { [ Arg::new("list") .long("list") .action(ArgAction::SetTrue) .conflicts_with("changes") .help(format!( "Shows the party's {whole} as it counts at the node, one a line: what the \ command does given nothing" )), Arg::new("add") .long("add") .value_name(value) .num_args(1..) .action(ArgAction::Append) .help(format!( "Adds these {items} to the party's {whole}, as it counts at the node" )), Arg::new("remove") .long("remove") .value_name(value) .num_args(1..) .action(ArgAction::Append) .help(format!( "Takes these {items} off the party's {whole}, as it counts at the node" )), Arg::new("set") .long("set") .value_name(value) .num_args(0..) .action(ArgAction::Append) .conflicts_with_all(["add", "remove"]) .help(format!("Sets the party's {whole} to these {items}, whole")), Arg::new("force") .long("force") .action(ArgAction::SetTrue) .requires("changes") .help(format!( "Writes the party's {whole} where the change leaves it empty" )), ] } /// What changes a key set or node list: `--add`, `--remove`, `--set`. fn changes() -> ArgGroup { ArgGroup::new("changes") .args(["add", "remove", "set"]) .multiple(true) } /// Where the client keeps a directory for each federation, by its name: `continuum/ /// federations` in `$XDG_CONFIG_HOME`, else in `~/.config`. fn federations() -> Option { let base = std::env::var_os("XDG_CONFIG_HOME") .filter(|dir| !dir.is_empty()) .map(PathBuf::from) .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".config")))?; Some(base.join("continuum").join("federations")) } /// The directory of the federation the client works with: the one `--federation` names; /// else, without `--config`, the only one in `all` (`federations`). None where there is /// none, and an error where there are several. fn federation(matches: &ArgMatches, all: Option<&Path>) -> Result> { if let Some(name) = matches.get_one::("federation") { if name.is_empty() || name.contains('/') || name == "." || name == ".." { bail!("--federation {name:?}: a federation is named by one segment of a path"); } let all = all.context("no directory for federations: set HOME or XDG_CONFIG_HOME")?; return Ok(Some(all.join(name))); } let Some(all) = all else { return Ok(None); }; if matches.get_many::("config").is_some() || !all.is_dir() { return Ok(None); } let mut names: Vec = std::fs::read_dir(all) .with_context(|| format!("Reading {}", all.display()))? .filter_map(|entry| entry.ok()) .filter(|entry| entry.path().is_dir()) .filter_map(|entry| entry.file_name().into_string().ok()) .collect(); names.sort(); match names.as_slice() { [] => Ok(None), [one] => Ok(Some(all.join(one))), several => bail!( "{} federations in {} ({}): choose one with --federation", several.len(), all.display(), several.join(", ") ), } } /// A federation's directory begun by `init`: made, only its owner reading it, with a /// `party.toml` holding the options given on the command line, `node`, `token` and `key`, /// the key's path made absolute. fn begin_federation(dir: &Path, matches: &ArgMatches) -> Result<()> { use clap::parser::ValueSource; use std::os::unix::fs::{DirBuilderExt, OpenOptionsExt}; std::fs::DirBuilder::new() .recursive(true) .mode(0o700) .create(dir) .with_context(|| format!("Making {}", dir.display()))?; let mut given = toml::Table::new(); for name in ["node", "token", "key"] { if matches.value_source(name) != Some(ValueSource::CommandLine) { continue; } let Some(value) = matches.get_one::(name) else { continue; }; let value = if name == "key" && Path::new(value).exists() { std::path::absolute(value)?.display().to_string() } else { value.clone() }; given.insert(name.to_owned(), toml::Value::String(value)); } let file = dir.join("party.toml"); if given.is_empty() || file.exists() { return Ok(()); } let mut out = std::fs::OpenOptions::new() .write(true) .create_new(true) .mode(0o600) .open(&file) .with_context(|| format!("Writing {}", file.display()))?; std::io::Write::write_all(&mut out, toml::to_string(&given)?.as_bytes())?; println!("wrote {}", file.display()); Ok(()) } /// Runs the client, as its arguments say. pub(crate) async fn main() -> Result<()> { let matches = command().get_matches(); let (name, sub) = matches.subcommand().expect("a subcommand is required"); // Those that use no options read no federation's. let dir = if matches!(name, "twice" | "root" | "judge" | "found") { None } else { federation(&matches, federations().as_deref())? }; if let Some(dir) = &dir { if name == "init" { begin_federation(dir, &matches)?; } else if !dir.is_dir() { bail!( "no federation {}: {} isn't there; `init` begins one", dir.file_name().unwrap_or_default().to_string_lossy(), dir.display() ); } } let first: Vec = dir .iter() .map(|dir| dir.join("party.toml")) .filter(|file| file.exists()) .collect(); let (merged, _) = config::merged_with(&matches, config::PARTY_OPTIONS, &first)?; let mut options: Options = config::read(&merged)?; // In a federation's directory, its record and key, where none other is named. if let Some(dir) = &dir { if options.record.is_none() { options.record = Some(dir.join("record").display().to_string()); } if options.key.is_none() && dir.join("key").exists() { options.key = Some(dir.join("key").display().to_string()); } } if name == "twice" { let records = sub .get_many::("records") .into_iter() .flatten() .map(|path| Record::load(Path::new(path)).map_err(|why| anyhow!(why))) .collect::>>()?; for (version, party, why) in client::voted_twice(&records) { println!("{party} voted twice on version {version}: {why}"); } return Ok(()); } if name == "found" { println!("{}", serde_json::to_string_pretty(&founded(sub)?)?); return Ok(()); } if name == "root" { let version = version_in(sub.get_one::("file").expect("required"))?; let version = Version::read(&version).map_err(|why| anyhow!("no version: {why}"))?; let root = event::Checkpoint { at: Vec::new(), keeps: Vec::new(), version, } .bytes(); if let Err(refused) = Store::::new(root.clone(), Vec::new()) { bail!("no federation's root: {}", refused.0); } return out(&super::fork::root_file(&root, &[])); } if name == "judge" { let mut input = String::new(); std::io::Read::read_to_string(&mut std::io::stdin(), &mut input)?; let histories = judge::read(&input).map_err(|why| anyhow!(why))?; print!("{}", judge::judge(&histories)); return Ok(()); } let node = Node::new(&options)?; if name == "propose" && proposing_flags(sub).get_flag("print") { let current = node.current().await?; let version = to_propose(&node, ¤t, sub).await?; let version: JsonValue = serde_json::from_slice(&version.bytes())?; println!("{}", serde_json::to_string_pretty(&version)?); return Ok(()); } if name == "proposals" { return proposals(&node).await; } if name == "status" { return status(&node, &options).await; } if name == "values" { return values(&node, sub).await; } if name == "show" { let counts = node.counts().await?; let proposal = proposal_named(&counts, sub)?.to_string(); for line in preview(&node, &counts, &proposal, &proposal_hashes(&counts)).await? { println!("{line}"); } return Ok(()); } if name == "next" { let current = node.current().await?; let feature = sub.get_one::("revision"); let next = next(¤t, feature.map(String::as_str))?; if feature.is_none() { // A later revision this client and the node know, said. let known = node.revisions().await?; if let Some(later) = Revision::known() .filter(|revision| *revision > current.revision && known.contains(&revision.id())) .last() { let name = later .features() .last() .expect("a later revision adds one") .name(); eprintln!( "The node knows {later}, the current version follows {}: --revision {name} \ moves to it, once every party's nodes run a continuum that knows it", current.revision ); warn_unready(&node.counts().await?, later); } } else if let Some(revision) = next["revision"] .as_str() .and_then(Id::from_hex) .and_then(|id| Revision::of(&id)) && revision != current.revision { warn_unready(&node.counts().await?, revision); } println!("{}", serde_json::to_string_pretty(&next)?); return Ok(()); } if name == "fork" { let (version, heads) = match sub.subcommand() { None => ( version_in(sub.get_one::("file").expect("required"))?, strings(sub, "heads"), ), Some(("proposal", given)) => { let counts = node.counts().await?; let proposal = proposal_named(&counts, given)?; let version = proposal_version(&node, &counts, proposal).await?; let mut version: JsonValue = serde_json::from_slice(&version.bytes())?; founders(&mut version, given)?; let version = client::json(&version).map_err(|why| anyhow!(why))?; (version, strings(given, "at")) } Some((intent, given)) => { let current = node.current().await?; let mut version = intended(¤t, &intent_of(&node, intent, given).await?)?; founders(&mut version, given)?; let version = client::json(&version).map_err(|why| anyhow!(why))?; (version, strings(given, "at")) } }; // Each head by its ID, or a prefix of one of the heads the node shows; those it // shows, where none is named. let (_, shown) = node.summary().await?; let shown: Vec = shown.iter().map(Id::to_string).collect(); let heads = if heads.is_empty() { shown } else { let shown: Vec<&str> = shown.iter().map(String::as_str).collect(); heads .iter() .map(|head| { client::named(head, &shown, "head") .map(|id| id.to_string()) .map_err(|why| anyhow!(why)) }) .collect::>>()? }; let body = serde_json::json!({ "heads": heads, "version": serde_json::from_slice::(&version.bytes())?, }); return out(&node.post_for_bytes("/admin/events/fork", &body).await?); } if name == "check" { let file = sub.get_one::("file").expect("required"); let bytes = std::fs::read(file).with_context(|| format!("Reading {file}"))?; let check = node.post_bytes("/events/fork", bytes).await?; let fork = check["fork"].as_str().unwrap_or_default(); match (check["builds"].as_bool(), check["why"].as_str()) { (Some(true), _) => println!("{fork}: built here, byte for byte"), (_, Some(why)) => println!("{fork}: not built here: {why}"), _ => println!("{fork}: not built here: another root at its heads"), } for id in check["lacking"].as_array().into_iter().flatten() { println!("its heads lack {}", id.as_str().unwrap_or_default()); } let changes: Vec<&str> = check["changes"] .as_array() .into_iter() .flatten() .filter_map(JsonValue::as_str) .collect(); if !changes.is_empty() { println!("its version changes:"); } for line in changes { println!(" {line}"); } for line in check["writers"].as_array().into_iter().flatten() { println!(" who writes {}", line.as_str().unwrap_or_default()); } for (party, keys) in check["keys"].as_object().into_iter().flatten() { println!( "{party}'s keys: {} here, {} in the fork", keys["there"], keys["fork"] ); } return Ok(()); } // A party that has a node vote by itself votes by hand only through it. if name == "vote" && sub.get_flag("through") { let counts = node.counts().await?; let proposal = proposal_named(&counts, sub)?; let yes = sub.get_one::("yes").map(String::as_str) == Some("yes"); let hashes = proposal_hashes(&counts); for line in preview(&node, &counts, &proposal.to_string(), &hashes).await? { println!("{line}"); } let word = if yes { "`true`" } else { "`false`" }; let question = format!( "Vote {word} on {}, through the node?", marked(&proposal.to_string(), &hashes) ); if !confirmed(&question)? { println!("nothing cast"); return Ok(()); } let answer = node .post( "/admin/events/vote", &serde_json::json!({"proposal": proposal.to_string(), "yes": yes}), ) .await?; println!("{} cast", answer["id"].as_str().unwrap_or_default()); return Ok(()); } let path = PathBuf::from( options .record .as_ref() .context("--record names the party's record")?, ); if name == "init" { if path.exists() { bail!("{} holds a record already", path.display()); } let party = sub.get_one::("party").expect("required"); let record = match sub.get_one::("root") { Some(file) => { let bytes = std::fs::read(file).with_context(|| format!("Reading {file}"))?; let (root, kept) = super::fork::read_root_file(&bytes); if let Err(refused) = Store::::new(root.clone(), kept) { bail!("no store's root: {}", refused.0); } Record::before(Id::of(&root), party) } None => { let (store, _) = node.summary().await?; Record::begin(store, party, &node.current().await?) } }; record.save(&path).map_err(|why| anyhow!(why))?; println!("began a record of {party}, of the store {}", record.store); return Ok(()); } let mut record = Record::load(&path).map_err(|why| anyhow!(why))?; // The party's key set or node list as it counts, given no change: no key needed. if matches!(name, "keys" | "nodes") && !sub.contains_id("changes") { for item in listed(&node.counts().await?, &record.party, name) { println!("{item}"); } return Ok(()); } let key = signing_key(&options)?; let signer = Signer { key, party: record.party.clone(), }; match (name, sub) { ("write", sub) => { let path_to = sub.get_one::("path").expect("required"); let value: JsonValue = serde_json::from_str(sub.get_one::("value").expect("required")) .context("the value isn't JSON")?; let value = client::json(&value).map_err(|why| anyhow!(why))?; sign( &node, &signer, &mut record, &path, event::write(path_to, value), ) .await } ("keys", sub) => { // As the federation spells a key: without the line's comment. let spelled = |name: &str| keys_given(&strings(sub, name)); let counts = node.counts().await?; signer.named_by(¤t(&counts)?)?; let party = &signer.party; let keys = changed( listed(&counts, party, "keys"), sub.contains_id("set").then(|| spelled("set")).transpose()?, spelled("add")?, spelled("remove")?, sub.get_flag("force"), &format!("{party}'s key set"), &format!("none of {party}'s writes would count"), )?; sign(&node, &signer, &mut record, &path, event::keys(&keys)).await } ("nodes", sub) => { let given = |name: &str| node_ids(strings(sub, name)); let counts = node.counts().await?; signer.named_by(¤t(&counts)?)?; let party = &signer.party; let nodes = changed( listed(&counts, party, "nodes"), sub.contains_id("set").then(|| given("set")).transpose()?, given("add")?, given("remove")?, sub.get_flag("force"), &format!("{party}'s node list"), &format!("none of {party}'s nodes would be members"), )?; sign(&node, &signer, &mut record, &path, event::nodes(&nodes)).await } ("propose", sub) => { let counts = node.counts().await?; let current = current(&counts)?; signer.named_by(¤t)?; let version = to_propose(&node, ¤t, sub).await?; let proposed = client::backable(¤t, &version) .map_err(|why| anyhow!("not a version a party may back: {why}"))?; if client::described(¤t, &proposed).is_empty() { bail!( "it changes nothing of version {} but its number", current.number ); } // What it proposes, said before anything is signed. println!("version {} on {}:", proposed.number, current.number); for line in version_lines(&counts, ¤t, &proposed) { println!(" {line}"); } let yes = proposing_flags(sub).get_flag("yes"); let question = if yes { "Propose it, and vote `true` on it?" } else { "Propose it?" }; if !confirmed(question)? { println!("nothing proposed"); return Ok(()); } sign( &node, &signer, &mut record, &path, event::proposal(&version), ) .await?; let counts = node.counts().await?; println!( "proposal {}", marked(&proposed.hash.to_string(), &proposal_hashes(&counts)) ); if yes { vote( &node, &signer, &mut record, &path, proposed.hash, true, false, ) .await?; } Ok(()) } ("vote", sub) => { let proposal = proposal_named(&node.counts().await?, sub)?; let yes = sub.get_one::("yes").map(String::as_str) == Some("yes"); vote(&node, &signer, &mut record, &path, proposal, yes, true).await } ("again", _) => again(&node, &signer, &mut record, &path).await, _ => unreachable!("every subcommand handled"), } } /// The hashes of the proposals on the current version, as `counts` names them. fn proposal_hashes(counts: &JsonValue) -> Vec { counts["proposals"] .as_object() .into_iter() .flatten() .map(|(hash, _)| hash.clone()) .collect() } /// The proposal the argument `proposal` names: by its hash, or a prefix of the hash of one /// on the current version (`client::named`). fn proposal_named(counts: &JsonValue, sub: &ArgMatches) -> Result { let given = sub.get_one::("proposal").expect("required"); let hashes = proposal_hashes(counts); let hashes: Vec<&str> = hashes.iter().map(String::as_str).collect(); client::named(given, &hashes, "proposal").map_err(|why| anyhow!(why)) } /// The matches of `propose` holding `--yes` and `--print`: its subcommand's, where it has /// one. fn proposing_flags(sub: &ArgMatches) -> &ArgMatches { sub.subcommand().map_or(sub, |(_, intent)| intent) } /// What `propose` proposes: the version its file holds, or the version after `current` /// changed as its subcommand says (`Intent`, `intended`). async fn to_propose(node: &Node, current: &Version, sub: &ArgMatches) -> Result { let Some((name, given)) = sub.subcommand() else { return version_in(sub.get_one::("file").expect("required")); }; let intent = intent_of(node, name, given).await?; client::json(&intended(current, &intent)?).map_err(|why| anyhow!(why)) } /// The change an intent's subcommand `name` asks for, with its arguments `given` /// (`intents`); of a checkpoint, at the heads `node` shows. async fn intent_of(node: &Node, name: &str, given: &ArgMatches) -> Result { let one = |arg: &str| given.get_one::(arg).expect("required").clone(); Ok(match name { "admit" => Intent::Admit { party: one("party"), keys: key_lines(&strings(given, "keys"))?, nodes: node_ids(strings(given, "nodes"))?, }, "remove" => Intent::Remove(one("party")), "give" => Intent::Give { path: one("path"), writer: one("writer"), }, "take" => Intent::Take(one("path")), "keys" => Intent::Keys { party: one("party"), keys: key_lines(&strings(given, "keys"))?, }, "nodes" => Intent::Nodes { party: one("party"), nodes: node_ids(strings(given, "nodes"))?, }, "checkpoint" => Intent::Checkpoint(node.summary().await?.1), "revision" => Intent::Revision(one("feature")), _ => unreachable!("every intent handled"), }) } /// `--founder PARTY=NODE,…` of `given`: each party's node list named anew in the fork's /// version `version`, `since` its number, so that those nodes are members there. fn founders(version: &mut JsonValue, given: &ArgMatches) -> Result<()> { let number = version["number"].clone(); for item in strings(given, "founder") { let (party, nodes) = item .split_once('=') .with_context(|| format!("--founder {item:?}: PARTY=NODE,… it is"))?; if version["keys"].get(party).is_none() { bail!("--founder {party}: no party of the fork's version"); } let nodes = node_ids( nodes .split(',') .filter(|node| !node.is_empty()) .map(str::to_owned) .collect(), )?; version["nodes"][party] = serde_json::json!({"since": number, "nodes": nodes}); } Ok(()) } /// The version the proposal `proposal` on the current version proposes, as `counts` names /// the event proposing it, and the node holds it. async fn proposal_version(node: &Node, counts: &JsonValue, proposal: Id) -> Result { let ballot = &counts["proposals"][proposal.to_string()]; let Some(id) = ballot["proposals"][0].as_str().and_then(Id::from_hex) else { bail!( "{proposal}: {}", ballot["none"] .as_str() .unwrap_or("the node names no event proposing it") ); }; match Event::read(&node.event(&id).await?) { Some(Event::Signed(Signed { does: Does::Proposal { hash, version }, .. })) if hash == proposal => Ok(version), _ => bail!("the node's {id} isn't the proposal it names"), } } /// The keys `given` names, as the federation spells them (without comments): each an /// `authorized_keys` line itself, or a file of such lines, blank ones and `#` comments /// left out; none twice, one at least. fn key_lines(given: &[String]) -> Result> { let lines = keys_given(given)?; if lines.is_empty() { bail!("no key given: a party signs with the keys a version names"); } Ok(lines) } /// As `key_lines`, none too. fn keys_given(given: &[String]) -> Result> { let mut lines: Vec = Vec::new(); for item in given { let found = match Key::parse(item) { Ok(key) => vec![key.line()], Err(itself) => std::fs::read_to_string(item) .map_err(|error| anyhow!("{item}: no key ({itself}), nor a file ({error})"))? .lines() .map(str::trim) .filter(|line| !line.is_empty() && !line.starts_with('#')) .map(|line| { Key::parse(line) .map(|key| key.line()) .map_err(|why| anyhow!("{item}: {line:?}: {why}")) }) .collect::>>()?, }; for line in found { if lines.contains(&line) { bail!("{} given twice", client::fingerprint(&line)); } lines.push(line); } } Ok(lines) } /// `given`, where each is a node ID. fn node_ids(given: Vec) -> Result> { match given.iter().find(|id| !keys::node_id(id)) { Some(none) => bail!("{none:?} isn't a node ID"), None => Ok(given), } } /// `hash`, its shortest prefix that no other of `among` starts with marked, as bold, where /// it goes to a terminal and `NO_COLOR` is unset: the part that names it to the client. fn marked(hash: &str, among: &[String]) -> String { use std::io::IsTerminal; if !std::io::stdout().is_terminal() || std::env::var_os("NO_COLOR").is_some() { return hash.to_owned(); } let mut ids: Vec<&str> = among.iter().map(String::as_str).collect(); if !ids.contains(&hash) { ids.push(hash); } let at = ids.iter().position(|id| *id == hash).expect("among them"); let length = client::prefixes(&ids)[at]; format!("\x1b[1m{}\x1b[0m{}", &hash[..length], &hash[length..]) } /// The party's keys or nodes (`what`) as they count, as `counts` names them. fn listed(counts: &JsonValue, party: &str, what: &str) -> Vec { counts["parties"][party][what] .as_array() .into_iter() .flatten() .filter_map(|value| value.as_str().map(str::to_owned)) .collect() } /// The key set or node list to write: `set`, where given, else `now`, as it counts, with /// `add` added and `remove` taken off, each of which must change it. Left empty, only with /// `force`, as then `empty` (`what` names the set or list in the errors). Said before it /// is signed. fn changed( now: Vec, set: Option>, add: Vec, remove: Vec, force: bool, what: &str, empty: &str, ) -> Result> { let mut list = Vec::new(); for item in set.unwrap_or(now) { if list.contains(&item) { bail!("{what} would hold {item} twice"); } list.push(item); } for item in add { if list.contains(&item) { bail!("{what} holds {item} already"); } list.push(item); } for item in remove { let Some(at) = list.iter().position(|held| *held == item) else { bail!("{what} doesn't hold {item}"); }; list.remove(at); } if list.is_empty() && !force { bail!("{what} would be empty: {empty}. --force writes it so"); } let shown = if list.is_empty() { "none".to_owned() } else { list.join(" ") }; println!("{what}: {shown}"); Ok(list) } /// The version the file `file` holds, as JSON the federation takes. fn version_in(file: &str) -> Result { let text = std::fs::read_to_string(file).with_context(|| format!("Reading {file}"))?; let version: JsonValue = serde_json::from_str(&text).with_context(|| format!("{file} isn't JSON"))?; client::json(&version).map_err(|why| anyhow!(why)) } /// `bytes`, to standard output. fn out(bytes: &[u8]) -> Result<()> { use std::io::Write; let mut stdout = std::io::stdout().lock(); stdout.write_all(bytes)?; stdout.flush()?; Ok(()) } fn strings(matches: &ArgMatches, name: &str) -> Vec { matches .get_many::(name) .into_iter() .flatten() .cloned() .collect() } /// What `--key` gives: a private key, from an OpenSSH private key file; or a public key, /// given itself (an `authorized_keys` line), or the first of a file of such lines, with /// how many more the file holds. enum Given { Private(SigningKey), Public(Key, usize), } /// What `given`, `--key`'s value, gives (`Given`). fn given_key(given: &str) -> Result { let itself = match Key::parse(given) { Ok(key) => return Ok(Given::Public(key, 0)), Err(why) => why, }; let text = match std::fs::read_to_string(given) { Ok(text) => text, Err(error) if error.kind() == std::io::ErrorKind::NotFound => { bail!("{given}: no file, nor a key ({itself})") } Err(error) => return Err(error).with_context(|| format!("Reading {given}")), }; if text.trim_start().starts_with("-----BEGIN") { let key = client::signing_key(&text).map_err(|why| anyhow!("{given}: {why}"))?; return Ok(Given::Private(key)); } let mut lines = text .lines() .map(str::trim) .filter(|line| !line.is_empty() && !line.starts_with('#')); let first = lines .next() .with_context(|| format!("{given} holds no key"))?; let key = Key::parse(first).map_err(|why| anyhow!("{given}: {why}"))?; Ok(Given::Public(key, lines.count())) } /// The key `--key` gives (`given_key`): a private key, signing here; or a public key, /// signing through the ssh-agent at `SSH_AUTH_SOCK`, which must hold it. Of a file of /// several public keys, the first, with a warning: another is given as `--key` itself. fn signing_key(options: &Options) -> Result { let given = options .key .as_ref() .context("--key names the party's key")?; let key = match given_key(given)? { Given::Private(key) => return Ok(Signing::File(key)), Given::Public(key, 0) => key, Given::Public(key, more) => { eprintln!( "Warning: {given} holds {} keys: signing with the first, {}; to sign with \ another, give that key itself as --key", more + 1, key.line() ); key } }; let socket = std::env::var_os("SSH_AUTH_SOCK").with_context(|| { format!( "{} signs through an ssh-agent: SSH_AUTH_SOCK names none", key.line() ) })?; Ok(Signing::Agent(key, PathBuf::from(socket))) } /// What signs: a key at hand, or a key the ssh-agent at a socket holds. enum Signing { File(SigningKey), Agent(Key, PathBuf), } /// Who signs: the party, with one of its keys. struct Signer { key: Signing, party: String, } impl Signer { /// The key it signs with, as the federation spells it. fn line(&self) -> String { match &self.key { Signing::File(key) => PublicKey(key.verifying_key().to_bytes()).line(), Signing::Agent(key, _) => key.line(), } } /// Refuses where `current` doesn't name the key it signs with for its party: only those /// sign the party's votes, proposals, key set and node list, and otherwise only a /// version naming the party's keys or list anew, by the parties' votes, changes them. fn named_by(&self, current: &Version) -> Result<()> { let (party, number) = (&self.party, current.number); let Some(given) = current.parties.get(party) else { bail!( "{party} is no party of version {number}: only a version letting it in names its \ keys and node list, which a party of it proposes and the parties pass" ); }; if !given.keys.contains(&self.line()) { bail!( "version {number} doesn't name the key --key gives for {party}: only the keys it \ names sign {party}'s votes, proposals, key set and node list. A version naming \ its keys or its list anew changes them: another party proposes it \ (continuum-party next, then propose), and the parties pass it" ); } Ok(()) } fn sign(&self, deps: &[Id], does: (&str, Json)) -> Result> { match &self.key { Signing::File(key) => Ok(event::signed(key, &self.party, &self.line(), deps, does)), Signing::Agent(key, socket) => { // The agent's answer is waited for blocking the thread, in async code: the // client signs one event at a time and does nothing else meanwhile, and an // agent may wait for its user (a touch, a confirmation) as long as that // takes, which nothing here should cut short. event::signed_by(&self.party, &key.line(), deps, does, |bytes| { keys::sign_through(socket, key, bytes) }) .map_err(|why| anyhow!(why)) } } } } /// An event signed on the node's heads, recorded, and put. async fn sign( node: &Node, signer: &Signer, record: &mut Record, path: &Path, does: (&str, Json), ) -> Result<()> { let (_, heads) = node.summary().await?; let event = signer.sign(&heads, does)?; record.signed(&event); record.save(path).map_err(|why| anyhow!(why))?; node.put(&event).await } /// A vote, by the record: written to it before it is sent. With `ask`, what it votes on is /// said first, and asked about on a terminal (`confirmed`); without, that was done. async fn vote( node: &Node, signer: &Signer, record: &mut Record, path: &Path, proposal: Id, yes: bool, ask: bool, ) -> Result<()> { let counts = node.counts().await?; let current = current(&counts)?; signer.named_by(¤t)?; let ballots = &counts["proposals"]; let ids = |hash: &Id, what: &str| -> Vec { ballots[hash.to_string()][what] .as_array() .into_iter() .flatten() .filter_map(|id| id.as_str().and_then(Id::from_hex)) .collect() }; let proposals = ids(&proposal, "proposals"); let Some(proposing) = proposals.first().copied() else { if let Some(why) = ballots[proposal.to_string()]["none"].as_str() { bail!("{proposal}: {why}"); } bail!("no proposal of hash {proposal} on the current version, as the node reads it"); }; let proposed = match Event::read(&node.event(&proposing).await?) { Some(Event::Signed(Signed { does: Does::Proposal { hash, version }, .. })) if hash == proposal => version, _ => bail!("the node's {proposing} isn't the proposal it names"), }; // What it votes on, said before anything is cast. let hashes = proposal_hashes(&counts); if ask { for line in preview(node, &counts, &proposal.to_string(), &hashes).await? { println!("{line}"); } } // A later revision: the node knows it, or it stops at the version once it passes; and // so does every other node not known to know it. if yes && let Ok(version) = Version::read(&proposed) && version.revision != current.revision { if !node.revisions().await?.contains(&version.revision.id()) { bail!( "{proposal} moves to {}, which the node doesn't know: once it passed, a node \ that doesn't stops at it, and votes nothing till upgraded; upgrade this node \ and every other of the party to a continuum that knows it, then back it", version.revision ); } warn_unready(&counts, version.revision); } // The votes on its party's earlier proposals it backed, for the nays among them. let mut nays = Vec::new(); let backed: BTreeSet = record .votes .iter() .filter_map(|text| match Event::read_taken(text.as_bytes()) { Some(Event::Signed(Signed { does: Does::Vote { on, proposal, yes: true, }, .. })) if on == current.hash => Some(proposal), _ => None, }) .collect(); for earlier in &backed { for id in ids(earlier, "votes") { nays.push(node.event(&id).await?); } } let kept = record .may_vote(¤t, proposal, &proposed, yes, &nays) .map_err(|why| anyhow!(why))?; if yes { let parties = |what: &str| -> BTreeSet { ballots[proposal.to_string()][what] .as_array() .into_iter() .flatten() .filter_map(|party| party.as_str().map(str::to_owned)) .collect() }; if let Some(why) = client::binding(¤t, &signer.party, &parties("yes"), &parties("no")) { eprintln!("Warning: {why}"); } } // On an event proposing it the node shows, of a checkpoint one above the heads it // names, and on the nays that freed the party (`client::building`). let checkpoint = Version::read(&proposed) .ok() .and_then(|version| version.checkpoint) .is_some(); if checkpoint && yes && !kept.is_empty() { eprintln!( "Warning: this `true` builds on {proposing} alone, not on the nays that freed \ {party}: where they came after the heads it names, and {party}'s `true` they \ ended is in the history of {proposing}, nodes refuse it; a checkpoint proposed \ after them takes {party}'s `true`", party = signer.party ); } let on = client::building(proposing, checkpoint, &kept).map_err(|why| anyhow!(why))?; let word = if yes { "`true`" } else { "`false`" }; if ask && !confirmed(&format!( "Vote {word} on {}?", marked(&proposal.to_string(), &hashes) ))? { println!("nothing cast"); return Ok(()); } let cast = signer.sign(&on, event::vote(current.hash, proposal, yes))?; record.cast(¤t, &cast, kept); record.save(path).map_err(|why| anyhow!(why))?; node.put(&cast).await } /// Warns of the nodes not known to know `revision`, by `counts` (`client::unready`). fn warn_unready(counts: &JsonValue, revision: Revision) { let behind = client::unready(counts, revision); if behind.is_empty() { return; } eprintln!( "Warning: these nodes aren't known to know {revision}, as far as the node heard from \ them; once a version of it passes, each stops at it till upgraded:" ); for line in behind { eprintln!(" {line}"); } } /// Signs again what a move refused: of the latest event it signed at each place, its key /// set and its node list, those the node refuses as not above its checkpoint's heads, or /// as built on one it refuses. async fn again(node: &Node, signer: &Signer, record: &mut Record, path: &Path) -> Result<()> { let refused = node.get("/events/refused").await?; let moved: BTreeSet = 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)); if again.is_empty() { println!("nothing to sign again"); } for (refused, does) in again { let does = match does { Does::Write { path, value } => event::write(&path.join("/"), value), Does::Keys(keys) => event::keys(&keys), Does::Nodes(nodes) => event::nodes(&nodes), Does::Proposal { .. } | Does::Vote { .. } | Does::Merge => continue, }; println!("signing again {refused}"); sign(node, signer, record, path, does).await?; } Ok(()) } /// 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) -> Result<()> { let summary = node.get("/events").await?; let counts = node.counts().await?; let current = current(&counts)?; let short = |id: &str| match id.get(..12) { Some(start) if id.len() > 12 => format!("{start}…"), _ => id.to_owned(), }; let text = |value: &JsonValue| value.as_str().unwrap_or_default().to_owned(); let heads = summary["heads"].as_array().map_or(0, Vec::len); println!( "store {}: checkpoint {}, {heads} head{}, {} events taken after it", short(&text(&summary["store"])), short(&text(&summary["checkpoint"])), if heads == 1 { "" } else { "s" }, summary["taken"] ); let parties: Vec<&String> = current.parties.keys().collect(); let needed = (2 * parties.len()).div_ceil(3); println!( "version {} ({}) of federation {}, {}: {} part{}, {needed} passing a change ({})", 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::>() .join(", ") ); 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 = record .as_ref() .map(|record| record.party.clone()) .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" } } _ => "", }; let spelled = |items: &[String], spell: fn(&str) -> String| { items .iter() .map(|item| spell(item)) .collect::>() .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) { ", 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 = 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(()) } /// 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. async fn values(node: &Node, sub: &ArgMatches) -> Result<()> { let counts = node.counts().await?; let current = current(&counts)?; let under = sub .get_one::("path") .map(|path| path.trim_end_matches('/').to_owned()); let selected: serde_json::Map = counts["values"] .as_object() .into_iter() .flatten() .filter(|(place, _)| is_under(place, under.as_deref())) .map(|(place, value)| (place.clone(), value.clone())) .collect(); if sub.get_flag("json") { println!("{}", serde_json::to_string_pretty(&selected)?); return Ok(()); } for (place, value) in &selected { let writer = super::version::path(place) .and_then(|segments| current.writer(&segments).map(str::to_owned)) .unwrap_or_else(|| "nobody".to_owned()); println!("{place} {} ({writer})", serde_json::to_string(value)?); } Ok(()) } /// Whether `place` is the place `under` names, or under it; any, where `under` is none. fn is_under(place: &str, under: Option<&str>) -> bool { under.is_none_or(|under| { place == under || place .strip_prefix(under) .is_some_and(|rest| rest.starts_with('/')) }) } /// The proposals on the current version, each as `preview` says it. async fn proposals(node: &Node) -> Result<()> { let counts = node.counts().await?; let hashes = proposal_hashes(&counts); for hash in &hashes { for line in preview(node, &counts, hash, &hashes).await? { println!("{line}"); } } Ok(()) } /// What a party votes on, in words: the proposal of `hash` on the current version, as /// `counts` reads it, named among `hashes` (`marked`): whether it passed or is dead, who /// voted what, and how many more `true` pass it; who proposed it; what its version does /// (`client::described`); what moving to its checkpoint refuses of what this node took; /// the places holding a value whose writer it moves; the nodes not known to know its /// revision; and why no client backs it, where none does. async fn preview( node: &Node, counts: &JsonValue, hash: &str, hashes: &[String], ) -> Result> { let current = current(counts)?; let ballot = &counts["proposals"][hash]; let mut lines = vec![ballot_line(counts, ¤t, hash, hashes)]; let Some(id) = ballot["proposals"][0].as_str() else { if let Some(why) = ballot["none"].as_str() { lines.push(format!(" backed by no client: {why}")); } return Ok(lines); }; let id = Id::from_hex(id).ok_or_else(|| anyhow!("the node names no event ID: {id}"))?; let (party, version) = match Event::read(&node.event(&id).await?) { Some(Event::Signed(Signed { party, does: Does::Proposal { hash: of, version }, .. })) if of.to_string() == hash => (party, version), _ => { lines.push(format!(" the event {id} proposes no version of that hash")); return Ok(lines); } }; let proposed = match client::backable(¤t, &version) { Ok(proposed) => proposed, Err(why) => { lines.push(format!(" proposed by {party}; backed by no client: {why}")); return Ok(lines); } }; lines.push(format!( " proposed by {party}: version {} on {}", proposed.number, current.number )); lines.extend( version_lines(counts, ¤t, &proposed) .into_iter() .map(|line| format!(" {line}")), ); if proposed.checkpoint.is_some() { let beside = ballot["beside"].as_array().map_or(0, Vec::len); lines.push(match beside { 0 => " moving there refuses nothing this node took".to_owned(), n => format!( " moving there refuses {n} of the writes, key sets and node lists this node \ took, which their parties sign again (`beside` in GET /events/counts)" ), }); } Ok(lines) } /// The proposal of `hash` on `current`, as `counts` reads it, named among `hashes` /// (`marked`): whether it passed or is dead, how many more `true` pass it, and who voted /// what. fn ballot_line(counts: &JsonValue, current: &Version, hash: &str, hashes: &[String]) -> String { let ballot = &counts["proposals"][hash]; let names = |what: &str| -> (usize, String) { let names: Vec<&str> = ballot[what] .as_array() .into_iter() .flatten() .filter_map(JsonValue::as_str) .collect(); let spelled = if names.is_empty() { "nobody".to_owned() } else { names.join(", ") }; (names.len(), spelled) }; let ((yes, backers), (_, nays)) = (names("yes"), names("no")); let parties = current.parties.len(); let needed = (2 * parties).div_ceil(3); let standing = if ballot["passed"].as_bool() == Some(true) { "passed".to_owned() } else if ballot["dead"].as_bool() == Some(true) { "dead".to_owned() } else { let more = needed.saturating_sub(yes); format!("can still pass, {more} more `true` passing it ({needed} of {parties})") }; format!( "{}: {standing}; true: {backers}; false: {nays}", marked(hash, hashes) ) } /// What `proposed` does to `current`, in words, a line each: what its version does /// (`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 { let mut lines = client::described(current, proposed); let places: Vec<&str> = counts["values"] .as_object() .into_iter() .flatten() .map(|(place, _)| place.as_str()) .collect(); lines.extend( client::writers(current, proposed, places) .into_iter() .map(|line| format!("who writes {line}")), ); if proposed.revision != current.revision { lines.extend( client::unready(counts, proposed.revision) .into_iter() .map(|line| format!("not known to know {}: {line}", proposed.revision)), ); } lines } /// Whether the user, asked `question` where both its input and the client's errors are a /// terminal, answers yes. Asked nothing, as in a script, it does. fn confirmed(question: &str) -> Result { use std::io::{BufRead, IsTerminal, Write}; if !std::io::stdin().is_terminal() || !std::io::stderr().is_terminal() { return Ok(true); } eprint!("{question} [y/N] "); std::io::stderr().flush()?; let mut answer = String::new(); std::io::stdin().lock().read_line(&mut answer)?; Ok(matches!( answer.trim().to_ascii_lowercase().as_str(), "y" | "yes" )) } /// The version after `current`, to change and propose: `current` numbered on, naming its /// hash as the version it changes, and without the checkpoint it names, whose heads are /// behind: a version naming them again is never written. With `feature`, following the /// revision that adds it, where that isn't earlier than the current one's. fn next(current: &Version, feature: Option<&str>) -> Result { let mut next: JsonValue = serde_json::from_slice(¤t.json.bytes())?; let fields = next .as_object_mut() .context("a version that is no object")?; fields.insert("number".to_owned(), (current.number + 1).into()); fields.insert("changes".to_owned(), current.hash.to_string().into()); fields.remove("checkpoint"); if let Some(name) = feature { let revision = adding(name)?; if revision < current.revision { bail!( "the current version follows {}, later than {revision}: a version never \ follows an earlier revision than the one it changes", current.revision ); } fields.insert("revision".to_owned(), revision.id().to_string().into()); } Ok(next) } /// A change to a version by what it is for, as `propose` and `fork` take it: the version /// after the current one, changed so (`intended`). #[derive(Debug)] enum Intent { /// Lets a party in, naming its keys and node list. Admit { party: String, keys: Vec, nodes: Vec, }, /// Takes a party out, with the rules giving it places. Remove(String), /// Gives the places a path matches to `writer`, a party or `{party}`, by a rule before /// every other; a rule of that path before goes. Give { path: String, writer: String }, /// Gives the places a path matches to nobody: the rules of that path go, and where /// another may still give one of them, a rule giving them to nobody goes first. Take(String), /// Names a party's keys anew. Keys { party: String, keys: Vec }, /// Names a party's node list anew. Nodes { party: String, nodes: Vec }, /// Moves the store to a checkpoint at these heads. Checkpoint(Vec), /// Follows the revision that adds this feature (`next`). Revision(String), } /// The version after `current` (`next`), changed as `intent` says: checked here where the /// change needs `current` (the party it names is one, or isn't yet), and otherwise when it /// is proposed (`client::backable`). fn intended(current: &Version, intent: &Intent) -> Result { if let Intent::Revision(feature) = intent { return next(current, Some(feature)); } let mut next = next(current, None)?; let number = next["number"].clone(); let of = |party: &str| -> Result<()> { if !current.parties.contains_key(party) { bail!("{party} is no party of version {}", current.number); } Ok(()) }; match intent { Intent::Admit { party, keys, nodes } => { if current.parties.contains_key(party) { bail!("{party} is a party of version {} already", current.number); } if !super::version::is_party(party) { bail!("{party:?} is no party's name: 1 to 64 of A-Z a-z 0-9 . _ @ -"); } next["keys"][party.as_str()] = serde_json::json!({"since": number, "keys": keys}); next["nodes"][party.as_str()] = serde_json::json!({"since": number, "nodes": nodes}); } Intent::Remove(party) => { of(party)?; for field in ["keys", "nodes"] { if let Some(given) = next[field].as_object_mut() { given.remove(party); } } rules_of(&mut next)?.retain(|rule| rule["writer"].as_str() != Some(party.as_str())); } Intent::Give { path, writer } => { if writer != "{party}" { of(writer)?; } let rules = rules_of(&mut next)?; rules.retain(|rule| rule["path"].as_str() != Some(path.as_str())); rules.insert(0, serde_json::json!({"path": path, "writer": writer})); } Intent::Take(path) => { let segments = super::version::path(path).with_context(|| format!("{path:?} is no path"))?; let rules = rules_of(&mut next)?; let before = rules.len(); rules.retain(|rule| rule["path"].as_str() != Some(path.as_str())); let dropped = rules.len() < before; let still = rules.iter().any(|rule| { rule["writer"] != JsonValue::Bool(false) && rule["path"] .as_str() .and_then(super::version::path) .is_some_and(|other| overlaps(&segments, &other)) }); if still { rules.insert(0, serde_json::json!({"path": path, "writer": false})); } else if !dropped { bail!("nobody writes at {path}: no rule gives a place there"); } } Intent::Keys { party, keys } => { of(party)?; next["keys"][party.as_str()] = serde_json::json!({"since": number, "keys": keys}); } Intent::Nodes { party, nodes } => { of(party)?; next["nodes"][party.as_str()] = serde_json::json!({"since": number, "nodes": nodes}); } Intent::Checkpoint(heads) => { let mut heads = heads.clone(); heads.sort(); heads.dedup(); next["checkpoint"] = heads.iter().map(Id::to_string).collect::>().into(); } Intent::Revision(_) => unreachable!("returned above"), } Ok(next) } /// The rules of the version `json`. fn rules_of(json: &mut JsonValue) -> Result<&mut Vec> { json["rules"] .as_array_mut() .context("a version whose rules are no list") } /// Whether some path matches both the rule paths `a` and `b`, as rules spell them (`*` any /// one segment, `**` last, one or more, `{party}` a party's name): where unsure, as of /// `{party}` and a name, yes. fn overlaps(a: &[String], b: &[String]) -> bool { match (a.split_first(), b.split_first()) { (None, None) => true, (Some((first, _)), _) if first.as_str() == "**" => !b.is_empty(), (_, Some((first, _))) if first.as_str() == "**" => !a.is_empty(), (Some((x, a)), Some((y, b))) => { let any = |segment: &str| matches!(segment, "*" | "{party}"); (any(x) || any(y) || x == y) && overlaps(a, b) } _ => false, } } /// The revision that adds the feature `name`. fn adding(name: &str) -> Result { let Some(feature) = FEATURES.iter().find(|feature| feature.name() == name) else { let names: Vec<&str> = FEATURES.iter().map(|feature| feature.name()).collect(); bail!( "no feature {name:?} this continuum knows: it knows {}", if names.is_empty() { "none".to_owned() } else { names.join(", ") } ); }; Ok(feature.revision()) } /// The first version of a federation, as `found`'s arguments `sub` say: its parties, each /// with its keys (`PARTY=KEY|FILE`, again for more), `since` 0; their node lists /// (`--nodes PARTY=NODE,…`), empty where not given; its rules in order (`--rule /// PATH=WRITER`, `nobody` writing `false`), each party writing under `home/{party}/` where /// none is given; and its revision (`--revision`). Checked as a first version. fn founded(sub: &ArgMatches) -> Result { let mut keys: std::collections::BTreeMap> = Default::default(); for item in strings(sub, "parties") { let (party, given) = item .split_once('=') .with_context(|| format!("{item:?}: PARTY=KEY or PARTY=FILE it is"))?; let held = keys.entry(party.to_owned()).or_default(); for line in key_lines(&[given.to_owned()])? { if held.contains(&line) { bail!("{party}: {} given twice", client::fingerprint(&line)); } held.push(line); } } let mut nodes: std::collections::BTreeMap> = keys .keys() .map(|party| (party.clone(), Vec::new())) .collect(); for item in strings(sub, "nodes") { let (party, given) = item .split_once('=') .with_context(|| format!("--nodes {item:?}: PARTY=NODE,… it is"))?; let Some(list) = nodes.get_mut(party) else { bail!("--nodes {party}: no party given keys"); }; list.extend(node_ids( given .split(',') .filter(|node| !node.is_empty()) .map(str::to_owned) .collect(), )?); } let given = strings(sub, "rule"); let rules: Vec = if given.is_empty() { vec![serde_json::json!({"path": "home/{party}/**", "writer": "{party}"})] } else { given .iter() .map(|rule| { let (path, writer) = rule .rsplit_once('=') .with_context(|| format!("--rule {rule:?}: PATH=WRITER it is"))?; let writer = match writer { "nobody" => JsonValue::Bool(false), writer => writer.into(), }; Ok(serde_json::json!({"path": path, "writer": writer})) }) .collect::>()? }; let given = |lists: &std::collections::BTreeMap>, what: &str| { lists .iter() .map(|(party, items)| (party.clone(), serde_json::json!({"since": 0, what: items}))) .collect::>() }; let mut version = serde_json::json!({ "federation": super::version::FEDERATION, "number": 0, "changes": null, "keys": given(&keys, "keys"), "nodes": given(&nodes, "nodes"), "rules": rules, }); if let Some(name) = sub.get_one::("revision") { let revision = adding(name)?; if revision != Revision::default() { version["revision"] = revision.id().to_string().into(); } } let read = Version::read(&client::json(&version).map_err(|why| anyhow!(why))?) .map_err(|why| anyhow!("no first version: {why}"))?; read.changes_validly(None) .map_err(|why| anyhow!("no first version: {why}"))?; Ok(version) } /// The current version, as `counts` names it, checked: its hash its own. fn current(counts: &JsonValue) -> Result { let json = client::json(&counts["version"]["version"]).map_err(|why| anyhow!(why))?; let version = Version::read(&json).map_err(|why| anyhow!("the node's current version: {why}"))?; if Some(version.hash.to_string().as_str()) != counts["version"]["hash"].as_str() { bail!("the node's current version isn't of the hash it names"); } Ok(version) } /// A node's API. struct Node { url: reqwest::Url, http: reqwest::Client, password: Option<(String, Option)>, token: Option, } impl Node { fn new(options: &Options) -> Result { let _ = rustls::crypto::ring::default_provider().install_default(); let mut url: reqwest::Url = options.node.parse().context("--node isn't a URL")?; let password = (!url.username().is_empty()) .then(|| (url.username().to_owned(), url.password().map(str::to_owned))); let _ = url.set_username(""); let _ = url.set_password(None); Ok(Node { url, http: reqwest::Client::new(), password, token: options.token.clone(), }) } fn request(&self, method: reqwest::Method, path: &str) -> Result { let url = self.url.join(path).context("a path of the node's API")?; let request = self.http.request(method, url); Ok(match (&self.token, &self.password) { (Some(token), _) => request.bearer_auth(token), (None, Some((user, password))) => request.basic_auth(user, password.as_ref()), (None, None) => request, }) } async fn answer(response: reqwest::Response) -> Result { let status = response.status(); if status.is_success() { return Ok(response); } let body = response.text().await.unwrap_or_default(); bail!("the node answers {status}: {body}") } async fn get(&self, path: &str) -> Result { let response = self.request(reqwest::Method::GET, path)?.send().await?; Ok(Self::answer(response).await?.json().await?) } /// The store's ID, and its heads. async fn summary(&self) -> Result<(Id, Vec)> { let summary = self.get("/events").await?; let store = summary["store"] .as_str() .and_then(Id::from_hex) .context("the node names no store")?; let heads = summary["heads"] .as_array() .into_iter() .flatten() .filter_map(|id| id.as_str().and_then(Id::from_hex)) .collect(); Ok((store, heads)) } async fn counts(&self) -> Result { self.get("/events/counts").await } async fn current(&self) -> Result { current(&self.counts().await?) } /// The IDs of the revisions the node knows: none where it says none, as one too old to /// know the base revision by name. async fn revisions(&self) -> Result> { let summary = self.get("/events").await?; Ok(summary["revisions"] .as_array() .into_iter() .flatten() .filter_map(|revision| revision["id"].as_str().and_then(Id::from_hex)) .collect()) } /// The bytes of an event the node's store holds. async fn event(&self, id: &Id) -> Result> { let response = self .request(reqwest::Method::GET, &format!("/events/{id}"))? .send() .await?; let bytes = Self::answer(response).await?.bytes().await?.to_vec(); if Id::of(&bytes) != *id { bail!("the node's event {id} isn't of that ID"); } Ok(bytes) } async fn post(&self, path: &str, body: &JsonValue) -> Result { let response = self .request(reqwest::Method::POST, path)? .json(body) .send() .await?; Ok(Self::answer(response).await?.json().await?) } /// What the node answers to `body`, as bytes. async fn post_for_bytes(&self, path: &str, body: &JsonValue) -> Result> { let response = self .request(reqwest::Method::POST, path)? .json(body) .send() .await?; Ok(Self::answer(response).await?.bytes().await?.to_vec()) } /// What the node answers to the bytes `body`. async fn post_bytes(&self, path: &str, body: Vec) -> Result { let response = self .request(reqwest::Method::POST, path)? .header(reqwest::header::CONTENT_TYPE, "application/octet-stream") .body(body) .send() .await?; Ok(Self::answer(response).await?.json().await?) } async fn put(&self, event: &[u8]) -> Result<()> { let response = self .request(reqwest::Method::POST, "/events")? .header(reqwest::header::CONTENT_TYPE, "application/json") .body(event.to_vec()) .send() .await?; let answer: JsonValue = Self::answer(response).await?.json().await?; println!( "{} {}", answer["id"].as_str().unwrap_or_default(), answer["event"].as_str().unwrap_or_default() ); Ok(()) } } #[cfg(test)] mod tests { #[test] fn its_arguments_are_well_formed() { super::command().debug_assert(); } /// `propose` and `fork` take a file, or a subcommand saying what the version is for, /// with their options after it. #[test] fn a_version_by_its_file_or_by_what_it_is_for() { let parsed = |args: &[&str]| { super::command().try_get_matches_from(["continuum-party"].iter().chain(args)) }; let matches = parsed(&["propose", "next.json", "--yes"]).unwrap(); let (_, propose) = matches.subcommand().unwrap(); assert!(propose.subcommand().is_none()); assert!(super::proposing_flags(propose).get_flag("yes")); let matches = parsed(&[ "propose", "admit", "makefu", "--keys", "a.pub", "b.pub", "--nodes", "n1", "--yes", ]) .unwrap(); let (_, propose) = matches.subcommand().unwrap(); let (intent, admit) = propose.subcommand().unwrap(); assert_eq!(intent, "admit"); assert_eq!(super::strings(admit, "keys"), ["a.pub", "b.pub"]); assert_eq!(super::strings(admit, "nodes"), ["n1"]); assert!(super::proposing_flags(propose).get_flag("yes")); assert!(!super::proposing_flags(propose).get_flag("print")); assert!(parsed(&["propose", "checkpoint", "--print"]).is_ok()); assert!(parsed(&["propose"]).is_err()); assert!(parsed(&["propose", "next.json", "admit", "makefu"]).is_err()); let matches = parsed(&["fork", "fork.json"]).unwrap(); let (_, fork) = matches.subcommand().unwrap(); assert!(super::strings(fork, "heads").is_empty()); let matches = parsed(&[ "fork", "remove", "lass", "--at", "4a1d", "9e30", "--founder", "tv=n1,n2", ]) .unwrap(); let (_, fork) = matches.subcommand().unwrap(); let (intent, remove) = fork.subcommand().unwrap(); assert_eq!(intent, "remove"); assert_eq!(super::strings(remove, "at"), ["4a1d", "9e30"]); assert_eq!(super::strings(remove, "founder"), ["tv=n1,n2"]); let matches = parsed(&["fork", "proposal", "8d02"]).unwrap(); let (_, fork) = matches.subcommand().unwrap(); assert_eq!(fork.subcommand_name(), Some("proposal")); // A fork is at heads of its own: `checkpoint` is a file's name there. let matches = parsed(&["fork", "checkpoint"]).unwrap(); let (_, fork) = matches.subcommand().unwrap(); assert!(fork.subcommand().is_none()); } /// `--founder PARTY=NODE,…` names a party's node list anew in a fork's version, `since` /// its number; one that isn't a party of it, or a node that isn't one, refused. #[test] fn founders_name_their_nodes_anew() { use crate::federation::harness::node_id; let founder = |given: &str| { let matches = super::command() .try_get_matches_from([ "continuum-party", "fork", "remove", "lass", "--founder", given, ]) .unwrap(); let (_, fork) = matches.subcommand().unwrap(); let (_, remove) = fork.subcommand().unwrap(); let mut version = serde_json::json!({ "number": 5, "keys": {"tv": {"since": 0, "keys": []}}, "nodes": {"tv": {"since": 0, "nodes": []}}, }); super::founders(&mut version, remove).map(|()| version) }; let version = founder(&format!("tv={},{}", node_id(1), node_id(2))).unwrap(); assert_eq!( version["nodes"]["tv"], serde_json::json!({"since": 5, "nodes": [node_id(1), node_id(2)]}) ); assert!(founder(&format!("lass={}", node_id(1))).is_err()); assert!(founder("tv=elsewhere").is_err()); assert!(founder("tv").is_err()); } /// `--key` gives a public key itself, or the first of a file's with how many more it /// holds, or a private key file's key; a file of neither, or none, gives nothing. #[test] fn the_key_given() { use super::{Given, given_key}; use crate::federation::vectors::ECDSA_KEY; let ed25519 = crate::federation::harness::key_line(1); let dir = crate::files::scratch_dir("party-key"); let file = |name: &str, text: &str| { let path = dir.join(name); std::fs::write(&path, text).unwrap(); path.to_str().unwrap().to_owned() }; let public = |given: &str| match given_key(given).unwrap() { Given::Public(key, more) => (key.line(), more), Given::Private(_) => panic!("{given}: a private key"), }; assert_eq!(public(ECDSA_KEY), (ECDSA_KEY.to_owned(), 0)); assert_eq!( public(&format!("{ed25519} tv@laptop")), (ed25519.clone(), 0) ); let one = file("phone.pub", &format!("{ECDSA_KEY} tv@phone\n")); assert_eq!(public(&one), (ECDSA_KEY.to_owned(), 0)); let two = file( "agent.pub", &format!("{ed25519} tv@laptop\n\n{ECDSA_KEY} tv@phone\n"), ); assert_eq!(public(&two), (ed25519, 1)); assert!(given_key(&file("rsa.pub", "ssh-rsa AAAAB3NzaC1yc2E tv\n")).is_err()); assert!(given_key(&file("empty.pub", "\n")).is_err()); assert!(given_key(dir.join("missing").to_str().unwrap()).is_err()); let private = file( "tv.key", "-----BEGIN OPENSSH PRIVATE KEY-----\n-----END OPENSSH PRIVATE KEY-----\n", ); let why = given_key(&private).err().expect("no key").to_string(); assert!(why.contains("OpenSSH private key"), "{why}"); } /// A key set or node list changed as it counts: each item added absent, each taken off /// held; set whole, it is as set, none twice; left empty only by force. #[test] fn a_list_changed_as_it_counts() { use super::changed; let list = |items: &[&str]| -> Vec { items.iter().map(|item| (*item).to_owned()).collect() }; let now = list(&["a", "b"]); let changed_by = |set: Option<&[&str]>, add: &[&str], remove: &[&str], force: bool| { let set = set.map(list); let (add, remove) = (list(add), list(remove)); changed( now.clone(), set, add, remove, force, "tv's list", "nothing left", ) }; assert_eq!( changed_by(Some(&["c"]), &[], &[], false).unwrap(), list(&["c"]) ); assert_eq!( changed_by(None, &["c"], &["a"], false).unwrap(), list(&["b", "c"]) ); assert!(changed_by(None, &["a"], &[], false).is_err()); assert!(changed_by(None, &[], &["c"], false).is_err()); assert!(changed_by(Some(&["c", "c"]), &[], &[], false).is_err()); assert!(changed_by(None, &[], &["a", "b"], false).is_err()); assert!(changed_by(Some(&[]), &[], &[], false).is_err()); assert_eq!(changed_by(None, &[], &["a", "b"], true).unwrap(), list(&[])); assert_eq!(changed_by(Some(&[]), &[], &[], true).unwrap(), list(&[])); } /// The federation the client works with: the one `--federation` names; else, without /// `--config`, the only one there is; none where there is none; and an error where /// there are several. `init` begins one, with the options given on the command line. #[test] fn the_federation_is_the_one_there_is_or_the_one_named() { let all = crate::files::scratch_dir("party-federations"); let matches = |args: &[&str]| { super::command() .try_get_matches_from(["continuum-party"].iter().chain(args).chain(&["proposals"])) .unwrap() }; let chosen = |args: &[&str]| super::federation(&matches(args), Some(&all)); assert_eq!(chosen(&[]).unwrap(), None); std::fs::create_dir(all.join("krebs")).unwrap(); assert_eq!(chosen(&[]).unwrap(), Some(all.join("krebs"))); assert_eq!(chosen(&["--config", "x.toml"]).unwrap(), None); std::fs::create_dir(all.join("test")).unwrap(); let several = chosen(&[]).unwrap_err().to_string(); assert!(several.contains("krebs, test"), "{several}"); assert_eq!( chosen(&["--federation", "test"]).unwrap(), Some(all.join("test")) ); assert!(chosen(&["--federation", "../x"]).is_err()); assert_eq!(super::federation(&matches(&[]), None).unwrap(), None); // Begun with the options given on the command line, the default node left out. let begun = all.join("new"); super::begin_federation(&begun, &matches(&["--token", "t"])).unwrap(); let written = std::fs::read_to_string(begun.join("party.toml")).unwrap(); assert_eq!(written.trim(), r#"token = "t""#); use std::os::unix::fs::PermissionsExt; let mode = std::fs::metadata(begun.join("party.toml")) .unwrap() .permissions() .mode(); assert_eq!(mode & 0o777, 0o600); } /// Votes, proposals, key sets and node lists are signed with a key the current version /// names for their party; another key, or a party the version doesn't have, is refused /// before signing. #[test] fn what_only_a_versions_keys_sign_is_signed_with_one() { use super::{Signer, Signing}; use crate::federation::harness::{Party, Spec}; use crate::federation::version::Version; let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); let current = Version::read(&Spec::first(&[&tv, &lass], &[]).json()).unwrap(); let signer = |key: &Party, party: &str| Signer { key: Signing::File(key.key.clone()), party: party.to_owned(), }; assert!(signer(&tv, "tv").named_by(¤t).is_ok()); assert!(signer(&lass, "tv").named_by(¤t).is_err()); let makefu = Party::new("makefu", 3); assert!(signer(&makefu, "makefu").named_by(¤t).is_err()); } /// The next version is the current one numbered on, naming its hash, without its /// checkpoint: a version changing it, as its parties may pass. With a feature, it /// follows the revision adding it, never an earlier one than the current version's. #[test] fn the_next_version_changes_the_current_one() { use crate::federation::harness::{Party, Spec}; use crate::federation::revision::{Feature, Revision}; use crate::federation::store::Id; use crate::federation::version::Version; let first = Spec::first(&[&Party::new("tv", 1), &Party::new("lass", 2)], &[]); let mut second = first.after(first.hash()); second.checkpoint = Some(vec![Id::of(b"a head")]); let current = Version::read(&second.json()).unwrap(); let next = super::next(¤t, None).unwrap(); assert_eq!(next["number"], current.number + 1); assert_eq!(next["changes"], current.hash.to_string()); assert!(next.get("checkpoint").is_none()); assert!(next.get("revision").is_none()); let next = Version::read(&crate::federation::client::json(&next).unwrap()).unwrap(); next.changes_validly(Some(¤t)).unwrap(); let revised = super::next(¤t, Some("merge-events")).unwrap(); let revised = Version::read(&crate::federation::client::json(&revised).unwrap()).unwrap(); assert_eq!(revised.revision, Feature::MergeEvents.revision()); revised.changes_validly(Some(¤t)).unwrap(); // Kept on, where nothing is said. let kept = super::next(&revised, None).unwrap(); let kept = Version::read(&crate::federation::client::json(&kept).unwrap()).unwrap(); assert_eq!(kept.revision, revised.revision); let why = super::next(¤t, Some("elsewhere")) .unwrap_err() .to_string(); assert!(why.contains("merge-events"), "{why}"); assert_ne!(revised.revision, Revision::default()); } /// A change by what it is for makes the next version so: a party let in or taken out /// (its rules with it), a place given (first, the rule of its path before gone) or /// taken (to nobody, where another rule still gives it), keys and nodes named anew /// `since` its number, a checkpoint at heads, a revision; each a version the parties /// may pass, and a party named that isn't one, or one let in that is, refused. #[test] fn a_change_by_what_it_is_for() { use super::{Intent, intended}; use crate::federation::client; use crate::federation::harness::{Party, Spec, node_id, str}; use crate::federation::store::Id; use crate::federation::version::Version; let (tv, lass, makefu) = ( Party::new("tv", 1), Party::new("lass", 2), Party::new("makefu", 3), ); let current = Version::read( &Spec::first( &[&tv, &lass], &[ ("names/r/wiki", str("lass")), ("home/{party}/**", str("{party}")), ("names/**", str("tv")), ], ) .json(), ) .unwrap(); let made = |intent: Intent| -> Version { let next = intended(¤t, &intent).unwrap(); client::backable(¤t, &client::json(&next).unwrap()).unwrap() }; let rules = |version: &Version| -> Vec { let json: serde_json::Value = serde_json::from_slice(&version.json.bytes()).unwrap(); json["rules"] .as_array() .unwrap() .iter() .map(|rule| format!("{} {}", rule["path"].as_str().unwrap(), rule["writer"])) .collect() }; let admitted = made(Intent::Admit { party: "makefu".to_owned(), keys: vec![makefu.key_line()], nodes: vec![node_id(3)], }); let given = &admitted.parties["makefu"]; assert_eq!((given.keys_since, given.nodes_since), (1, 1)); assert_eq!(given.nodes, [node_id(3)]); assert!( intended( ¤t, &Intent::Admit { party: "tv".to_owned(), keys: vec![tv.key_line()], nodes: Vec::new(), }, ) .is_err() ); let removed = made(Intent::Remove("lass".to_owned())); assert!(!removed.parties.contains_key("lass")); assert!(!rules(&removed).iter().any(|rule| rule.contains("\"lass\""))); assert!(intended(¤t, &Intent::Remove("makefu".to_owned())).is_err()); let given = made(Intent::Give { path: "names/r/wiki".to_owned(), writer: "tv".to_owned(), }); assert_eq!(rules(&given)[0], r#"names/r/wiki "tv""#); assert_eq!(rules(&given).len(), 3); let zone = made(Intent::Give { path: "names/r/{party}".to_owned(), writer: "{party}".to_owned(), }); assert_eq!(rules(&zone)[0], r#"names/r/{party} "{party}""#); assert!( intended( ¤t, &Intent::Give { path: "x".to_owned(), writer: "makefu".to_owned() } ) .is_err() ); // names/** still gives names/r/wiki once its rule goes: to nobody, first. let taken = made(Intent::Take("names/r/wiki".to_owned())); assert_eq!(rules(&taken)[0], "names/r/wiki false"); assert_eq!( taken.writer(&["names".into(), "r".into(), "wiki".into()]), None ); // Every place names/** matches, lass's wiki among them. let taken = made(Intent::Take("names/**".to_owned())); assert_eq!(rules(&taken)[0], "names/** false"); assert_eq!(rules(&taken).len(), 3); // Given by its own rule alone: that rule goes. let taken = made(Intent::Take("home/{party}/**".to_owned())); assert_eq!(rules(&taken).len(), 2); assert!(intended(¤t, &Intent::Take("ip4/10".to_owned())).is_err()); let keys = made(Intent::Keys { party: "tv".to_owned(), keys: vec![makefu.key_line()], }); assert_eq!(keys.parties["tv"].keys, [makefu.key_line()]); assert_eq!(keys.parties["tv"].keys_since, 1); let nodes = made(Intent::Nodes { party: "lass".to_owned(), nodes: vec![node_id(5)], }); assert_eq!(nodes.parties["lass"].nodes_since, 1); let (a, b) = (Id::of(b"a"), Id::of(b"b")); let moved = made(Intent::Checkpoint(vec![b, a, b])); let mut heads = vec![a, b]; heads.sort(); assert_eq!(moved.checkpoint, Some(heads)); assert!(moved.only_moves_on(¤t)); let revised = made(Intent::Revision("merge-events".to_owned())); assert_ne!(revised.revision, current.revision); } /// `found` writes a first version from the parties' keys, node lists and rules: each /// party writing under `home/{party}/` where no rule is given; one rooting a /// federation, as `root` takes it. #[test] fn a_first_version_founded() { use crate::federation::harness::{Party, node_id}; use crate::federation::version::Version; let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); let dir = crate::files::scratch_dir("party-found"); let lass_file = dir.join("lass.pub"); std::fs::write(&lass_file, format!("{} lass@host\n", lass.key_line())).unwrap(); let founded = |args: &[String]| { let matches = super::command() .try_get_matches_from( ["continuum-party", "found"] .into_iter() .map(str::to_owned) .chain(args.iter().cloned()), ) .unwrap(); let (_, found) = matches.subcommand().unwrap(); super::founded(found) }; let tv_key = format!("tv={}", tv.key_line()); let lass_key = format!("lass={}", lass_file.display()); let version = founded(&[ tv_key.clone(), lass_key.clone(), "--nodes".to_owned(), format!("tv={},{}", node_id(1), node_id(2)), ]) .unwrap(); assert_eq!(version["number"], 0); assert_eq!(version["keys"]["lass"]["keys"][0], lass.key_line().as_str()); assert_eq!(version["nodes"]["tv"]["nodes"][1], node_id(2).as_str()); assert_eq!(version["nodes"]["lass"]["nodes"], serde_json::json!([])); assert_eq!( version["rules"], serde_json::json!([{"path": "home/{party}/**", "writer": "{party}"}]) ); let json = crate::federation::client::json(&version).unwrap(); Version::read(&json).unwrap().changes_validly(None).unwrap(); let ruled = founded(&[ tv_key.clone(), "--rule".to_owned(), "names/r/wiki=tv".to_owned(), "--rule".to_owned(), "names/**=nobody".to_owned(), "--revision".to_owned(), "merge-events".to_owned(), ]) .unwrap(); assert_eq!(ruled["rules"][1]["writer"], false); assert!(ruled.get("revision").is_some()); assert!(founded(&[tv_key.clone(), tv_key.clone()]).is_err()); assert!( founded(&[ tv_key.clone(), "--nodes".to_owned(), format!("lass={}", node_id(2)) ]) .is_err() ); assert!(founded(&[tv_key, "--rule".to_owned(), "names/r/wiki=lass".to_owned()]).is_err()); } /// `values` shows a place and those under it, not those beside it. #[test] fn places_under_a_path() { use super::is_under; assert!(is_under("home/tv/motd", None)); assert!(is_under("home/tv/motd", Some("home/tv"))); assert!(is_under("home/tv", Some("home/tv"))); assert!(!is_under("home/tvx/motd", Some("home/tv"))); assert!(!is_under("home", Some("home/tv"))); } /// Two rule paths overlap where some path matches both. #[test] fn rule_paths_overlap() { let path = |text: &str| crate::federation::version::path(text).unwrap(); let overlaps = |a: &str, b: &str| super::overlaps(&path(a), &path(b)); assert!(overlaps("names/r/wiki", "names/**")); assert!(overlaps("names/r/*", "names/r/wiki")); assert!(overlaps("home/{party}/**", "home/tv/motd")); assert!(!overlaps("names/r/wiki", "names/r/wiki/x")); assert!(!overlaps("names/**", "names")); assert!(!overlaps("names/r/wiki", "home/**")); assert!(overlaps("a/**", "a/b/**")); } /// Its options come from configuration files as the node's do, arguments winning. #[test] fn its_options_come_from_configuration_files_and_arguments() { use crate::config; let dir = crate::files::scratch_dir("party-config"); let file = dir.join("party.toml"); std::fs::write( &file, "node = \"http://tv:secret@localhost:3001\"\nkey = \"tv.key\"\nrecord = \"tv.record\"\n", ) .unwrap(); let path = file.to_str().unwrap(); let matches = super::command() .try_get_matches_from([ "continuum-party", "--config", path, "--key", "laptop.key", "proposals", ]) .unwrap(); let (merged, _) = config::merged(&matches, config::PARTY_OPTIONS).unwrap(); let options: super::Options = config::read(&merged).unwrap(); assert_eq!(options.node, "http://tv:secret@localhost:3001"); assert_eq!(options.key.as_deref(), Some("laptop.key")); assert_eq!(options.record.as_deref(), Some("tv.record")); assert_eq!(options.token, None); // A key naming none of the client's options is an error, as in the node's files. std::fs::write(&file, "bind-addr = \"127.0.0.1:3000\"\n").unwrap(); assert!(config::merged(&matches, config::PARTY_OPTIONS).is_err()); } }