aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/party.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/party.rs')
-rw-r--r--src/federation/party.rs223
1 files changed, 167 insertions, 56 deletions
diff --git a/src/federation/party.rs b/src/federation/party.rs
index bacca3d..35d791e 100644
--- a/src/federation/party.rs
+++ b/src/federation/party.rs
@@ -193,10 +193,18 @@ fn command() -> Command {
keeps (federation-tests.md, Against released revisions)",
))
.subcommand(Command::new("proposals").about(
- "The proposals on the current version: whether each passed or is dead, who voted \
- what, and what it changes",
+ "The proposals on the current version, each as show says it",
))
.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(
@@ -423,6 +431,14 @@ pub(crate) async fn main() -> Result<()> {
if name == "proposals" {
return proposals(&node).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::<String>("revision");
@@ -515,8 +531,22 @@ pub(crate) async fn main() -> Result<()> {
}
// A party that has a node vote by itself votes by hand only through it.
if name == "vote" && sub.get_flag("through") {
- let proposal = proposal_named(&node.counts().await?, sub)?;
+ let counts = node.counts().await?;
+ let proposal = proposal_named(&counts, sub)?;
let yes = sub.get_one::<String>("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",
@@ -957,6 +987,11 @@ async fn vote(
})) 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);
+ 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
@@ -1029,6 +1064,14 @@ async fn vote(
);
}
let on = client::building(proposing, checkpoint, &kept).map_err(|why| anyhow!(why))?;
+ let word = if yes { "`true`" } else { "`false`" };
+ if !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(&current, &cast, kept);
record.save(path).map_err(|why| anyhow!(why))?;
@@ -1079,73 +1122,141 @@ async fn again(node: &Node, signer: &Signer, record: &mut Record, path: &Path) -
Ok(())
}
-/// The proposals on the current version at `node`, each with whether it passed or is dead,
-/// who voted what, and what its version changes, as one of the events proposing it holds
-/// it.
+/// The proposals on the current version, each as `preview` says it.
async fn proposals(node: &Node) -> Result<()> {
let counts = node.counts().await?;
- let current = current(&counts)?;
- // The places holding a value, whose writer a proposal may move.
- let places: Vec<&str> = counts["values"]
- .as_object()
- .into_iter()
- .flatten()
- .map(|(place, _)| place.as_str())
- .collect();
- let parties = |names: &JsonValue| -> String {
- let names: Vec<&str> = names
+ 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<Vec<String>> {
+ let current = current(counts)?;
+ 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();
- if names.is_empty() {
+ 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})")
+ };
+ let mut lines = vec![format!(
+ "{}: {standing}; true: {backers}; false: {nays}",
+ marked(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 hashes = proposal_hashes(&counts);
- for (hash, proposal) in counts["proposals"].as_object().into_iter().flatten() {
- let standing = match (proposal["passed"].as_bool(), proposal["dead"].as_bool()) {
- (Some(true), _) => "passed",
- (_, Some(true)) => "dead",
- _ => "can still pass",
- };
- println!(
- "{}: {standing}; true: {}; false: {}",
- marked(hash, &hashes),
- parties(&proposal["yes"]),
- parties(&proposal["no"])
- );
- let Some(id) = proposal["proposals"][0].as_str() else {
- if let Some(why) = proposal["none"].as_str() {
- println!(" backed by no client: {why}");
- }
- continue;
- };
- let id = Id::from_hex(id).ok_or_else(|| anyhow!("the node names no event ID: {id}"))?;
- match Event::read(&node.event(&id).await?) {
- Some(Event::Signed(Signed {
- does: Does::Proposal { hash: of, version },
- ..
- })) if of.to_string() == *hash => {
- if let Err(why) = client::backable(&current, &version) {
- println!(" backed by no client: {why}");
- }
- for line in client::changes(&current.json, &version) {
- println!(" {line}");
- }
- if let Ok(proposed) = Version::read(&version) {
- for line in client::writers(&current, &proposed, places.iter().copied()) {
- println!(" who writes {line}");
- }
- }
- }
- _ => println!(" the event {id} proposes no version of that hash"),
+ 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(&current, &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(
+ client::described(&current, &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(())
+ 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)),
+ );
+ }
+ Ok(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<bool> {
+ 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