aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/client.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/federation/client.rs')
-rw-r--r--src/federation/client.rs69
1 files changed, 69 insertions, 0 deletions
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<Version, St
Ok(version)
}
+/// What a vote on a proposal builds on: `proposing`, an event proposing it (of a
+/// checkpoint, one above the heads it names, so that a version event may be written on
+/// the vote); and, but for a checkpoint (`checkpoint`), the nays that freed its party
+/// (`nays`, as `Record::may_vote` keeps them), so that the proposal its party backed
+/// before is dead at the vote's history, as the federation takes a second `true` only so:
+/// that `true` may be in the history of the event proposing this one, and the nays not. A
+/// checkpoint's vote leaves them out: one in the history of its heads, named, would put
+/// the vote beside them, and one cast after they were named is beside them or above, which
+/// only a node can tell. None where they are more than an event may name.
+pub(crate) fn building(
+ proposing: Id,
+ checkpoint: bool,
+ nays: &[Vec<u8>],
+) -> Result<Vec<Id>, 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::<Federation>::new(first.root(), Vec::new()).unwrap();
+ let (root, on) = (store.id(), first.hash());
+ let mut take = |bytes: Vec<u8>| -> 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<Vec<u8>> = (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.