aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/config/mod.rs7
-rw-r--r--src/federation/party.rs116
2 files changed, 100 insertions, 23 deletions
diff --git a/src/config/mod.rs b/src/config/mod.rs
index 0b6a58a..1f6ad60 100644
--- a/src/config/mod.rs
+++ b/src/config/mod.rs
@@ -781,6 +781,13 @@ pub(crate) const PARTY_OPTIONS: &[OptionSpec] = &[
None,
"The party's record of its votes, written before each vote is sent",
),
+ option(
+ "party",
+ Kind::Text,
+ "NAME",
+ None,
+ "The party it works as where no record names it, for reading: the one it logs in to the node as, by --key, where --node names no user and no --token is given",
+ ),
];
#[cfg(not(feature = "federation"))]
const PARTY_OPTIONS: &[OptionSpec] = &[];
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<String>,
key: Option<String>,
record: Option<String>,
+ party: Option<String>,
}
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::<String>("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<Option<String>> {
+ let named = if name == "init" {
+ sub.get_one::<String>("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() {