diff options
Diffstat (limited to 'src/federation')
| -rw-r--r-- | src/federation/client.rs | 65 | ||||
| -rw-r--r-- | src/federation/party.rs | 312 |
2 files changed, 372 insertions, 5 deletions
diff --git a/src/federation/client.rs b/src/federation/client.rs index 846d29c..b95cb3c 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -264,6 +264,42 @@ impl Record { .collect() } + /// Its votes on `on`, each by its proposal's hash and whether it is `true`. + pub(crate) fn votes_cast(&self, on: &Id) -> Vec<(Id, bool)> { + self.votes_on(on) + .into_iter() + .map(|vote| (vote.proposal, vote.yes)) + .collect() + } + + /// Takes in its party's votes on `current` among `votes`, each as signed, as a node + /// holds them, that it lacks (`continuum-party restore`): best effort, as one on its + /// way from elsewhere isn't among them. A record begun on `current` casts on it then, + /// by them. How many it took. + pub(crate) fn restore(&mut self, current: &Version, votes: &[Vec<u8>]) -> usize { + let mut taken = 0; + for bytes in votes { + let Some(vote) = Vote::read(bytes, true) else { + continue; + }; + if vote.party != self.party || vote.on != current.hash { + continue; + } + if self + .votes + .contains(&String::from_utf8_lossy(bytes).into_owned()) + { + continue; + } + self.cast(current, bytes, Vec::new()); + taken += 1; + } + if self.began == current.hash.to_string() { + self.began = String::new(); + } + taken + } + /// Its votes on `on`, as it cast them. fn votes_on(&self, on: &Id) -> Vec<Vote> { self.votes @@ -980,6 +1016,35 @@ mod tests { assert!(json(&serde_json::json!(1u64 << 60)).is_err()); } + /// A record restored from a node's votes takes its party's on the current version that + /// it lacks, none of another party's or version, none twice; one begun on the current + /// version casts on it then, by them. + #[test] + fn a_record_restored_from_the_votes_a_node_holds() { + let (tv, lass, _, first, current, one, two) = three(); + let root = Id::of(b"root"); + let mut record = Record::begin(root, "tv", ¤t); + let mine = tv.vote(&[root], first.hash(), one.hash(), true); + let theirs = lass.vote(&[root], first.hash(), one.hash(), true); + let elsewhere = tv.vote(&[root], one.hash(), two.hash(), true); + let votes = [mine.clone(), theirs, elsewhere, mine]; + assert_eq!(record.restore(¤t, &votes), 1); + assert_eq!(record.votes_cast(¤t.hash), [(one.hash(), true)]); + assert_eq!(record.began, ""); + assert_eq!(record.restore(¤t, &votes), 0); + // By what it took: never `false` beside that `true`, nor another `true` beside it. + assert!( + record + .may_vote(¤t, one.hash(), &one.json(), false, &[]) + .is_err() + ); + assert!( + record + .may_vote(¤t, two.hash(), &two.json(), true, &[]) + .is_err() + ); + } + #[test] fn a_record_votes_one_true_at_a_time_and_never_both() { let (tv, lass, makefu, first, current, one, two) = three(); diff --git a/src/federation/party.rs b/src/federation/party.rs index 4902f0f..a1eb6a0 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -29,6 +29,8 @@ //! 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; +//! * `restore`: takes the party's votes on the current version, as the node holds them, +//! into its record, or a new one (`Record::restore`), best effort; //! * `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 @@ -188,9 +190,14 @@ fn command() -> Command { .long("through") .action(ArgAction::SetTrue) .help("Through the node, which votes for the party and keeps its record"), - ), + ) + .arg(waiting()), ) .subcommand(Command::new("again").about("Signs again what a move refused")) + .subcommand(Command::new("restore").about( + "Takes into the party's record, or a new one, its votes on the current version as \ + the node holds them: best effort, as one on its way from elsewhere isn't among them", + )) .subcommand( Command::new("found") .about( @@ -328,9 +335,24 @@ fn command() -> Command { ) } +/// How long a vote waits before it is cast, and asks the node again whether another of +/// its party's came meanwhile (`vote`). +fn waiting() -> Arg { + Arg::new("wait") + .long("wait") + .value_name("SECONDS") + .default_value("5") + .value_parser(clap::value_parser!(u64)) + .help( + "Before casting, waits so long and asks the node again for its party's votes, so \ + that one cast elsewhere just before arrives first", + ) +} + /// `propose`'s own options, and each of its subcommands'. -fn proposing() -> [Arg; 2] { +fn proposing() -> [Arg; 3] { [ + waiting(), Arg::new("yes") .long("yes") .action(ArgAction::SetTrue) @@ -860,6 +882,9 @@ pub(crate) async fn main() -> Result<()> { println!("began a record of {party}, of the store {}", record.store); return Ok(()); } + if name == "restore" { + return restore(&node, &path, party.as_deref()).await; + } 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") { @@ -964,6 +989,7 @@ pub(crate) async fn main() -> Result<()> { marked(&proposed.hash.to_string(), &proposal_hashes(&counts)) ); if yes { + let wait = waited(proposing_flags(sub)); vote( &node, &signer, @@ -972,6 +998,7 @@ pub(crate) async fn main() -> Result<()> { proposed.hash, true, false, + wait, ) .await?; } @@ -980,7 +1007,17 @@ pub(crate) async fn main() -> Result<()> { ("vote", sub) => { let proposal = proposal_named(&node.counts().await?, sub)?; let yes = sub.get_one::<String>("yes").map(String::as_str) == Some("yes"); - vote(&node, &signer, &mut record, &path, proposal, yes, true).await + vote( + &node, + &signer, + &mut record, + &path, + proposal, + yes, + true, + waited(sub), + ) + .await } ("again", _) => again(&node, &signer, &mut record, &path).await, _ => unreachable!("every subcommand handled"), @@ -1006,6 +1043,11 @@ fn proposal_named(counts: &JsonValue, sub: &ArgMatches) -> Result<Id> { client::named(given, &hashes, "proposal").map_err(|why| anyhow!(why)) } +/// `--wait` of `matches`. +fn waited(matches: &ArgMatches) -> std::time::Duration { + std::time::Duration::from_secs(matches.get_one::<u64>("wait").copied().unwrap_or(5)) +} + /// The matches of `propose` holding `--yes` and `--print`: its subcommand's, where it has /// one. fn proposing_flags(sub: &ArgMatches) -> &ArgMatches { @@ -1380,8 +1422,12 @@ async fn sign( 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. +/// A vote, by the record: written to it before it is sent. Refused where the node shows a +/// vote of its party on the current version that the record lacks (`consistent`); what +/// may keep another from the node said (`cut_off`). With `ask`, what it votes on is said +/// first, and asked about on a terminal (`confirmed`); without, that was done. It waits +/// `wait` before it casts, and asks the node again. +#[allow(clippy::too_many_arguments)] async fn vote( node: &Node, signer: &Signer, @@ -1390,10 +1436,15 @@ async fn vote( proposal: Id, yes: bool, ask: bool, + wait: std::time::Duration, ) -> Result<()> { let counts = node.counts().await?; let current = current(&counts)?; signer.named_by(¤t)?; + consistent(&counts, ¤t, record)?; + for line in cut_off(&counts, &record.party) { + eprintln!("Warning: {line}"); + } let ballots = &counts["proposals"]; let ids = |hash: &Id, what: &str| -> Vec<Id> { ballots[hash.to_string()][what] @@ -1506,12 +1557,193 @@ async fn vote( println!("nothing cast"); return Ok(()); } + if !wait.is_zero() { + tokio::time::sleep(wait).await; + let again = node.counts().await?; + if self::current(&again)?.hash != current.hash { + bail!( + "a version passed meanwhile: the proposal lapsed with version {}", + current.number + ); + } + consistent(&again, ¤t, record)?; + } 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 } +/// How long ago a node heard from another for it to count as heard from lately (`cut_off`). +const LATELY: u64 = 5 * 60; + +/// Refuses where the node shows a vote of `record`'s party on `current`, by `counts`, that +/// the record lacks (`unrecorded`): another record votes for the party, or this one was +/// begun anew. Of the record's votes the node lacks, says that they may be on their way. +fn consistent(counts: &JsonValue, current: &Version, record: &Record) -> Result<()> { + let party = &record.party; + let (theirs, ours) = unrecorded(counts, current, record); + if !theirs.is_empty() { + bail!( + "the node shows votes of {party} on version {} that this record lacks: {}. Another \ + record votes for {party}, or this one was begun anew: casting beside them may be \ + voting twice. `continuum-party restore` takes them into this record; then vote", + current.number, + theirs.join(", ") + ); + } + if !ours.is_empty() { + eprintln!( + "Note: the node doesn't show this record's {} yet: on their way, or held back where \ + they were put", + ours.join(", ") + ); + } + Ok(()) +} + +/// How the party's votes on `current` that `counts` shows differ from `record`'s: those the +/// node shows that the record lacks, and those the record holds that the node doesn't show; +/// each as `true on 8d02…`. +fn unrecorded( + counts: &JsonValue, + current: &Version, + record: &Record, +) -> (Vec<String>, Vec<String>) { + let party = record.party.as_str(); + let short = |hash: &str| format!("{}…", hash.get(..12).unwrap_or(hash)); + let mut shown = Vec::new(); + for (hash, ballot) in counts["proposals"].as_object().into_iter().flatten() { + for (what, yes) in [("yes", true), ("no", false)] { + let voted = ballot[what] + .as_array() + .is_some_and(|parties| parties.iter().any(|of| of.as_str() == Some(party))); + if voted { + shown.push((hash.clone(), yes)); + } + } + } + let held: Vec<(String, bool)> = record + .votes_cast(¤t.hash) + .into_iter() + .map(|(proposal, yes)| (proposal.to_string(), yes)) + .collect(); + let spelled = |(hash, yes): &(String, bool)| format!("`{yes}` on {}", short(hash)); + ( + shown + .iter() + .filter(|vote| !held.contains(vote)) + .map(spelled) + .collect(), + held.iter() + .filter(|vote| !shown.contains(vote)) + .map(spelled) + .collect(), + ) +} + +/// What may keep a vote of `party`'s from the node, by `counts`: the party's nodes the node +/// didn't hear from within `LATELY`, through which another of its votes may have gone; and, +/// where those are a third of the parties with nodes or more, the parties none of whose +/// nodes it heard from within it, which a part of the swarm cut off would be. A line each. +fn cut_off(counts: &JsonValue, party: &str) -> Vec<String> { + let parties = counts["parties"].as_object(); + let quiet = |of: &JsonValue| -> Vec<String> { + let names = &of["names"]; + of["nodes"] + .as_array() + .into_iter() + .flatten() + .filter_map(JsonValue::as_str) + .filter(|node| of["heard"][*node].as_u64().is_none_or(|ago| ago > LATELY)) + .map(|node| match names[node].as_str() { + Some(name) => format!("{node} ({name})"), + None => node.to_owned(), + }) + .collect() + }; + let mut lines = Vec::new(); + if let Some(own) = parties.and_then(|parties| parties.get(party)) { + let quiet = quiet(own); + if !quiet.is_empty() { + lines.push(format!( + "the node hasn't heard from these nodes of {party} lately: {}: a vote of \ + {party} put through one of them may not have reached it", + quiet.join(", ") + )); + } + } + let with_nodes: Vec<(&String, &JsonValue)> = parties + .into_iter() + .flatten() + .filter(|(_, of)| { + of["nodes"] + .as_array() + .is_some_and(|nodes| !nodes.is_empty()) + }) + .collect(); + let unheard: Vec<&str> = with_nodes + .iter() + .filter(|(_, of)| quiet(of).len() == of["nodes"].as_array().map_or(0, Vec::len)) + .map(|(name, _)| name.as_str()) + .collect(); + if !unheard.is_empty() && 3 * unheard.len() >= with_nodes.len() { + lines.push(format!( + "the node hasn't heard from any node of {} lately: a part of the swarm may be cut \ + off, and its votes not here", + unheard.join(", ") + )); + } + lines +} + +/// Takes into the record at `path`, or a new one of `party`, the party's votes on the current +/// version as the node holds them (`Record::restore`): best effort, as one on its way from +/// elsewhere isn't among them. What it signs anew after a move (`again`) it doesn't learn. +async fn restore(node: &Node, path: &Path, party: Option<&str>) -> Result<()> { + let (store, _) = node.summary().await?; + let counts = node.counts().await?; + let current = current(&counts)?; + let mut record = if path.exists() { + Record::load(path).map_err(|why| anyhow!(why))? + } else { + let party = party.context("no record, and no --party saying whose votes to take")?; + Record::before(store, party) + }; + if record.store != store.to_string() { + bail!("{} is a record of another store", path.display()); + } + let began_here = record.began == current.hash.to_string(); + let mut votes = Vec::new(); + for hash in proposal_hashes(&counts) { + for id in counts["proposals"][&hash]["votes"] + .as_array() + .into_iter() + .flatten() + .filter_map(|id| id.as_str().and_then(Id::from_hex)) + { + votes.push(node.event(&id).await?); + } + } + let taken = record.restore(¤t, &votes); + record.save(path).map_err(|why| anyhow!(why))?; + println!( + "took {taken} vote{} of {} on version {} into {}, as the node holds them; one on its \ + way from elsewhere isn't among them", + if taken == 1 { "" } else { "s" }, + record.party, + current.number, + path.display() + ); + if began_here { + println!("it casts on version {} now, by them", current.number); + } + for line in cut_off(&counts, &record.party) { + eprintln!("Warning: {line}"); + } + Ok(()) +} + /// 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); @@ -3057,6 +3289,76 @@ mod tests { assert_eq!(get("").unwrap()["home"]["tvx"]["motd"], "beside"); } + /// Before a vote, the party's votes the node shows are held against the record's: one + /// the node shows that the record lacks refuses the vote, one the record holds that + /// the node doesn't show is said; nothing where they agree. + #[test] + fn a_record_held_against_the_votes_a_node_shows() { + use crate::federation::client::Record; + use crate::federation::harness::{Party, Spec}; + use crate::federation::store::Id; + use crate::federation::version::Version; + let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); + let first = Spec::first(&[&tv, &lass], &[]); + let current = Version::read(&first.json()).unwrap(); + let (one, two) = (Id::of(b"one"), Id::of(b"two")); + let mut record = Record::before(Id::of(b"root"), "tv"); + record.cast( + ¤t, + &tv.vote(&[Id::of(b"root")], first.hash(), one, true), + Vec::new(), + ); + let counts = |yes_one: &[&str], no_two: &[&str]| { + serde_json::json!({"proposals": { + one.to_string(): {"yes": yes_one, "no": []}, + two.to_string(): {"yes": [], "no": no_two}, + }}) + }; + let agreeing = counts(&["tv", "lass"], &[]); + assert_eq!( + super::unrecorded(&agreeing, ¤t, &record), + (Vec::new(), Vec::new()) + ); + assert!(super::consistent(&agreeing, ¤t, &record).is_ok()); + let elsewhere = counts(&["tv"], &["tv"]); + let (theirs, ours) = super::unrecorded(&elsewhere, ¤t, &record); + assert_eq!(theirs.len(), 1); + assert!(theirs[0].starts_with("`false` on "), "{theirs:?}"); + assert!(ours.is_empty()); + assert!(super::consistent(&elsewhere, ¤t, &record).is_err()); + let behind = counts(&["lass"], &[]); + let (theirs, ours) = super::unrecorded(&behind, ¤t, &record); + assert!(theirs.is_empty()); + assert_eq!(ours.len(), 1); + assert!(super::consistent(&behind, ¤t, &record).is_ok()); + } + + /// What may keep a vote from the node: the party's own nodes not heard from lately; + /// and parties none of whose nodes were, where they are a third of those with nodes. + #[test] + fn nodes_cut_off_are_said() { + let ago = super::LATELY + 1; + let counts = |lass_heard: Option<u64>| { + let lass = match lass_heard { + Some(ago) => serde_json::json!({"n3": ago}), + None => serde_json::json!({}), + }; + serde_json::json!({"parties": { + "tv": {"nodes": ["n1", "n2"], "names": {"n2": "beta"}, "heard": {"n1": 0, "n2": ago}}, + "lass": {"nodes": ["n3"], "names": {}, "heard": lass}, + "makefu": {"nodes": ["n4"], "names": {}, "heard": {"n4": 3}}, + "krebs": {"nodes": [], "names": {}, "heard": {}}, + }}) + }; + let lines = super::cut_off(&counts(Some(10)), "tv"); + assert_eq!(lines.len(), 1); + assert!(lines[0].contains("n2 (beta)"), "{lines:?}"); + let lines = super::cut_off(&counts(None), "makefu"); + assert_eq!(lines.len(), 1); + assert!(lines[0].contains("any node of lass"), "{lines:?}"); + assert!(super::cut_off(&counts(Some(10)), "makefu").is_empty()); + } + /// `values` shows a place and those under it, not those beside it. #[test] fn places_under_a_path() { |
