From 33be6b959c9777c40ca2b16507e9f4f60d6d2d44 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 19 Nov 2026 14:42:45 +0000 Subject: vote: a second `true` builds on the nays that freed its party The federation takes a party's `true` only where, at its history, every other proposal it backed on that version is dead. A party's client, and a node voting by itself, built a vote on the events proposing what it votes on alone. A proposal made while the party's first `true` was alive, at a history holding it, holds that `true` in its history, and not the nays that ended the first proposal later: the party's `true` on it was refused, though its record cast it. The compromise case: five back X, Y is proposed, X dies, its backers back Y. A vote now builds on one event proposing what it votes on, and on the nays that freed its party (`client::building`), so the first proposal is dead at its history too. One event proposing it is enough, so many proposing it again don't push a vote past the dependencies an event may name. A checkpoint's vote builds on its proposal alone: one of those nays in the history of its heads, named, would put the vote beside them. A party freed only after the heads were named backs that checkpoint not, and its client says so before casting; it backs one proposed after. Row 74 is tested (`federation_attack_74`); federation.md and the guides say so. --- src/federation/client.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ src/federation/held.rs | 29 ++++++++++++++------ src/federation/party.rs | 20 +++++++++++--- 3 files changed, 107 insertions(+), 11 deletions(-) (limited to 'src/federation') diff --git a/src/federation/client.rs b/src/federation/client.rs index f0c196f..57e20e1 100644 --- a/src/federation/client.rs +++ b/src/federation/client.rs @@ -395,6 +395,37 @@ pub(crate) fn backable(current: &Version, proposed: &Json) -> Result], +) -> Result, String> { + let mut on = vec![proposing]; + if !checkpoint { + on.extend(nays.iter().map(|nay| Id::of(nay))); + on.sort_unstable(); + on.dedup(); + } + if on.len() > super::event::DEPENDENCIES { + return Err(format!( + "the proposal and the nays that freed the party are {} events, more than a vote \ + may build on ({})", + on.len(), + super::event::DEPENDENCIES + )); + } + Ok(on) +} + /// Where a `true` of `party` on a proposal on `current`, on which the parties of `yes` and /// `no` voted so far, binds it until the proposal passes unless every party yet to vote, or /// more, votes `false`, those being a third of the parties or fewer: a `true` holds its @@ -971,6 +1002,44 @@ mod tests { ); } + /// Row 74 (`docs/federation.md`): a party backs X, and Y is proposed at a history + /// holding that `true`; X dies after. Its `true` on Y built on Y's proposal alone is + /// refused, X being alive at its history; built as a vote builds (`building`), on the + /// nays that freed it too, it is taken. + #[test] + fn federation_attack_74() { + use crate::federation::fold::Federation; + use crate::federation::store::memory::{Offered, Store}; + let (tv, lass, makefu, first, _, one, two) = three(); + let mut store = Store::::new(first.root(), Vec::new()).unwrap(); + let (root, on) = (store.id(), first.hash()); + let mut take = |bytes: Vec| -> Id { + let id = Id::of(&bytes); + assert!(matches!(store.offer(bytes), Offered::Taken(_)), "not taken"); + id + }; + let x = take(tv.propose(&[root], &one.json())); + let backed = take(tv.vote(&[x], on, one.hash(), true)); + let y = take(lass.propose(&[backed], &two.json())); + let nays = [ + lass.vote(&[x], on, one.hash(), false), + makefu.vote(&[x], on, one.hash(), false), + ]; + for nay in &nays { + take(nay.clone()); + } + let alone = tv.vote(&[y], on, two.hash(), true); + assert!(matches!(store.offer(alone), Offered::Refused(_))); + let on_them = building(y, false, &nays).unwrap(); + assert_eq!(on_them.len(), 3); + let freed = tv.vote(&on_them, on, two.hash(), true); + assert!(matches!(store.offer(freed), Offered::Taken(_))); + // A checkpoint's vote builds on its proposal alone. + assert_eq!(building(y, true, &nays).unwrap(), [y]); + let many: Vec> = (0..64u8).map(|n| vec![n]).collect(); + assert!(building(y, false, &many).is_err()); + } + /// Row 72 (`docs/federation.md`): a party admitted under the name of a place's segment /// moves who writes there, rules unchanged, and so does a change of the rules' order; /// the places it moves are named, the others left out. diff --git a/src/federation/held.rs b/src/federation/held.rs index 6e00e51..9c87995 100644 --- a/src/federation/held.rs +++ b/src/federation/held.rs @@ -29,7 +29,7 @@ use anyhow::{Context, Result, bail}; use serde::{Deserialize, Serialize}; use tracing::{error, info, warn}; -use super::client::Record; +use super::client::{self, Record}; use super::event::{self, Does, Event, Signed}; use super::fold::Federation; use super::revision::Feature; @@ -740,11 +740,14 @@ impl Holding { yes, nays, } => { - // On the events proposing it, those above the heads it names for a - // checkpoint, so a version event may be written on it, which the - // node's heads, holding what is beside them, may not be. - let proposing = store.state().proposing(¤t.hash, &proposal); - if proposing.is_empty() { + // On an event proposing it, of a checkpoint one above the heads it + // names, so a version event may be written on it, which the node's + // heads, holding what is beside them, may not be; and on the nays that + // freed its party (`client::building`). + let state = store.state(); + let Some(proposing) = + state.proposing(¤t.hash, &proposal).first().copied() + else { if hand.is_some() { return Err(format!( "no event proposing {proposal} is above the heads it names: \ @@ -752,12 +755,22 @@ impl Holding { )); } continue; - } + }; + let checkpoint = state + .proposal(&proposal) + .and_then(|json| Version::read(json).ok()) + .and_then(|version| version.checkpoint) + .is_some(); + let on = match client::building(proposing, checkpoint, &nays) { + Ok(on) => on, + Err(why) if hand.is_some() => return Err(why), + Err(_) => continue, + }; let cast = event::signed( &voter.key, party, &line, - &proposing, + &on, event::vote(current.hash, proposal, yes), ); record.cast(¤t, &cast, nays); diff --git a/src/federation/party.rs b/src/federation/party.rs index f64746b..a544155 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -836,9 +836,23 @@ async fn vote( eprintln!("Warning: {why}"); } } - // On the events proposing it the node shows, those above the heads it names for a - // checkpoint, so a version event may be written on it. - let cast = signer.sign(&proposals, event::vote(current.hash, proposal, yes))?; + // On an event proposing it the node shows, of a checkpoint one above the heads it + // names, and on the nays that freed the party (`client::building`). + let checkpoint = Version::read(&proposed) + .ok() + .and_then(|version| version.checkpoint) + .is_some(); + if checkpoint && yes && !kept.is_empty() { + eprintln!( + "Warning: this `true` builds on {proposing} alone, not on the nays that freed \ + {party}: where they came after the heads it names, and {party}'s `true` they \ + ended is in the history of {proposing}, nodes refuse it; a checkpoint proposed \ + after them takes {party}'s `true`", + party = signer.party + ); + } + let on = client::building(proposing, checkpoint, &kept).map_err(|why| anyhow!(why))?; + 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 -- cgit v1.3.1