diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/federation/client.rs | 53 | ||||
| -rw-r--r-- | src/federation/party.rs | 13 |
2 files changed, 54 insertions, 12 deletions
diff --git a/src/federation/client.rs b/src/federation/client.rs index 0f06ca3..88e370f 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -303,10 +303,7 @@ impl Record { if !yes { return Ok(Vec::new()); } - let version = Version::read(proposed).map_err(|why| format!("no version: {why}"))?; - version - .changes_validly(Some(current)) - .map_err(|why| format!("no version changing the current one: {why}"))?; + backable(current, proposed)?; let mut kept = Vec::new(); for earlier in cast.iter().filter(|vote| vote.yes) { if !dead_by(current, &earlier.proposal, nays) { @@ -364,6 +361,30 @@ impl Record { } } +/// The version `proposed` is, where a party may back it on `current`, and so propose it: a +/// version, changing `current` as a version must (`Version::changes_validly`), and naming +/// none of the heads of the checkpoint `current` names: a version's checkpoint is after the +/// current one's, and one naming those heads again is never written. +pub(crate) fn backable(current: &Version, proposed: &Json) -> Result<Version, String> { + let version = Version::read(proposed).map_err(|why| format!("no version: {why}"))?; + version + .changes_validly(Some(current)) + .map_err(|why| format!("no version changing the current one: {why}"))?; + let named = version.checkpoint.as_deref().unwrap_or_default(); + if let Some(head) = current + .checkpoint + .iter() + .flatten() + .find(|head| named.contains(*head)) + { + return Err(format!( + "its checkpoint names {head}, a head of the current version's checkpoint: a \ + version naming those again is never written" + )); + } + Ok(version) +} + /// What the version `proposed` changes of `current`, a line each: each field whose value /// differs, and of `keys` and `nodes` each party's, as `field: before -> after`, its JCS /// on either side, `-` where a version lacks it. @@ -698,6 +719,30 @@ mod tests { assert!(again.contains(&Id::of(&later)) && again.contains(&Id::of(&keys))); } + /// A party backs, and proposes, only a version changing the current one whose + /// checkpoint, where it names one, names none of the current checkpoint's heads. + #[test] + fn a_version_backed_changes_the_current_one_and_moves_on() { + let (_, _, _, first, current, one, _) = three(); + assert!(backable(¤t, &one.json()).is_ok()); + let mut far = one.clone(); + far.number += 1; + assert!(backable(¤t, &far.json()).is_err()); + assert!(backable(¤t, &first.after(Id::of(b"another")).json()).is_err()); + assert!(backable(¤t, &harness::str("no version")).is_err()); + let (head, later) = (Id::of(b"a head"), Id::of(b"a later head")); + let mut moved = one.clone(); + moved.checkpoint = Some(vec![head]); + let moved_to = Version::read(&moved.json()).unwrap(); + let mut again = moved.after(moved_to.hash); + again.checkpoint = Some(vec![head]); + assert!(backable(&moved_to, &again.json()).is_err()); + again.checkpoint = Some(vec![later]); + assert!(backable(&moved_to, &again.json()).is_ok()); + again.checkpoint = None; + assert!(backable(&moved_to, &again.json()).is_ok()); + } + /// What a proposed version changes: each field apart, each party's keys and nodes. #[test] fn changes_name_each_field_and_party_a_version_changes() { diff --git a/src/federation/party.rs b/src/federation/party.rs index 788bde9..60b7e18 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -16,9 +16,9 @@ //! 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 it changes the -//! current one as a version must (`Version::changes_validly`), and says the proposal's -//! hash; with `--yes`, votes `true` on it too, as `vote` does; +//! * `propose FILE`: proposes the version `FILE` holds (JSON), where a party may back it +//! (`client::backable`), and says the proposal's hash; with `--yes`, votes `true` on it +//! too, as `vote` does; //! * `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; @@ -425,11 +425,8 @@ pub(crate) async fn main() -> Result<()> { let version = version_in(sub.get_one::<String>("file").expect("required"))?; let current = node.current().await?; signer.named_by(¤t)?; - let proposed = Version::read(&version) - .and_then(|proposed| proposed.changes_validly(Some(¤t)).map(|()| proposed)) - .map_err(|why| { - anyhow!("not a version changing version {}: {why}", current.number) - })?; + let proposed = client::backable(¤t, &version) + .map_err(|why| anyhow!("not a version a party may back: {why}"))?; sign( &node, &signer, |
