From 011560a72ca895d8b516983485cfc45c9984cd45 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 19 Nov 2026 20:55:59 +0000 Subject: continuum-party --party: the party it reads as, without a record A record is what votes go by, and what `again` signs anew from; for logging in as the party, and for `status`, it only named the party. So someone who only reads needed a record, begun with `init`, which should never vote beside the party's own. `--party NAME` (an option as node, key and record are, so party.toml holds it too) names the party where no record does (working_as): `init` the one it begins a record of, else the record's, else --party; --party naming another than these is an error. Commands that sign still take a record. --- src/federation/party.rs | 116 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 93 insertions(+), 23 deletions(-) (limited to 'src/federation/party.rs') diff --git a/src/federation/party.rs b/src/federation/party.rs index 72c18e9..17769f3 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -3,9 +3,10 @@ //! 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`; with //! neither, as its party, logging in there by that key: `Node::log_in`), 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. +//! party's record (`--record`, `client::Record`), by which it votes, written before a vote +//! is sent; without one, `--party` names the party it reads as. 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 @@ -86,6 +87,7 @@ struct Options { token: Option, key: Option, record: Option, + party: Option, } fn default_node() -> String { @@ -618,21 +620,14 @@ pub(crate) async fn main() -> Result<()> { return Ok(()); } let mut node = Node::new(&options)?; - // Without a user of the node or a token: as the party, by its key, where the record - // (or `init`) names the party. - if node.password.is_none() && node.token.is_none() && options.key.is_some() { - let party = match (name, &options.record) { - ("init", _) => sub.get_one::("party").cloned(), - (_, Some(path)) if Path::new(path).exists() => Some( - Record::load(Path::new(path)) - .map_err(|why| anyhow!(why))? - .party, - ), - _ => None, - }; - if let Some(party) = party { - node.log_in(&party, &signing_key(&options)?).await?; - } + let party = working_as(name, sub, &options)?; + // Without a user of the node or a token: as the party, by its key. + if node.password.is_none() + && node.token.is_none() + && options.key.is_some() + && let Some(party) = &party + { + node.log_in(party, &signing_key(&options)?).await?; } if name == "propose" && proposing_flags(sub).get_flag("print") { let current = node.current().await?; @@ -645,7 +640,7 @@ pub(crate) async fn main() -> Result<()> { return proposals(&node).await; } if name == "status" { - return status(&node, &options).await; + return status(&node, &options, party.as_deref()).await; } if name == "values" { return values(&node, sub).await; @@ -1526,13 +1521,43 @@ async fn again(node: &Node, signer: &Signer, record: &mut Record, path: &Path) - Ok(()) } +/// The party the client works as: for `init`, the one it begins a record of; else the +/// one its record names, where there is one, else `--party`; none where neither. `--party` +/// naming another than these is an error. +fn working_as(name: &str, sub: &ArgMatches, options: &Options) -> Result> { + let named = if name == "init" { + sub.get_one::("party").cloned() + } else { + options + .record + .as_ref() + .map(Path::new) + .filter(|path| path.exists()) + .map(|path| Record::load(path).map(|record| record.party)) + .transpose() + .map_err(|why| anyhow!(why))? + }; + match (named, &options.party) { + (Some(named), Some(given)) if named != *given => { + let whose = if name == "init" { + "init begins a record of" + } else { + "the record is" + }; + bail!("--party {given}, but {whose} {named}'s") + } + (Some(named), _) => Ok(Some(named)), + (None, given) => Ok(given.clone()), + } +} + /// 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<()> { +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)?; @@ -1572,9 +1597,8 @@ async fn status(node: &Node, options: &Options) -> Result<()> { .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()) + let party = party + .map(str::to_owned) .or_else(|| summary["party"].as_str().map(str::to_owned)); if let Some(party) = &party { let keys = listed(&counts, party, "keys"); @@ -2796,6 +2820,52 @@ mod tests { assert!(founded(&[tv_key, "--rule".to_owned(), "names/r/wiki=lass".to_owned()]).is_err()); } + /// The party the client works as: `init`'s, the record's, else `--party`; `--party` + /// naming another is an error. + #[test] + fn the_party_it_works_as() { + use crate::federation::client::Record; + use crate::federation::store::Id; + let dir = crate::files::scratch_dir("party-working-as"); + let record = dir.join("tv.record"); + Record::before(Id::of(b"a root"), "tv") + .save(&record) + .unwrap(); + let working_as = |args: &[&str], record: Option<&std::path::Path>, party: Option<&str>| { + let matches = super::command() + .try_get_matches_from(["continuum-party"].iter().chain(args)) + .unwrap(); + let (name, sub) = matches.subcommand().unwrap(); + let options = super::Options { + node: super::default_node(), + token: None, + key: None, + record: record.map(|path| path.display().to_string()), + party: party.map(str::to_owned), + }; + super::working_as(name, sub, &options) + }; + assert_eq!( + working_as(&["init", "tv"], None, None).unwrap().as_deref(), + Some("tv") + ); + assert!(working_as(&["init", "tv"], None, Some("lass")).is_err()); + assert_eq!( + working_as(&["status"], Some(&record), None) + .unwrap() + .as_deref(), + Some("tv") + ); + assert!(working_as(&["status"], Some(&record), Some("lass")).is_err()); + assert_eq!( + working_as(&["status"], Some(&dir.join("none")), Some("lass")) + .unwrap() + .as_deref(), + Some("lass") + ); + assert_eq!(working_as(&["status"], None, None).unwrap(), None); + } + /// `values` shows a place and those under it, not those beside it. #[test] fn places_under_a_path() { -- cgit v1.3.1