From 0c213626ba238fd6d8d69d08475d57e486f6f27a Mon Sep 17 00:00:00 2001 From: tv Date: Mon, 16 Nov 2026 21:45:11 +0000 Subject: api: what counts names the proposals on the current version, and their votes A party's client votes by the record it keeps, and keeps the votes that made its party's earlier proposal dead; it reads them from a node, and checks them itself, as each is signed. `GET /events/counts` names the proposals on the current version, by their hash, each with the events proposing it and the votes on it, by ID (`State::ballots`). --- src/federation/fold.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/federation/fold.rs') diff --git a/src/federation/fold.rs b/src/federation/fold.rs index 8ce6e60..6ffdc5a 100644 --- a/src/federation/fold.rs +++ b/src/federation/fold.rs @@ -42,6 +42,13 @@ pub(crate) struct State { proposed: BTreeMap, } +/// A proposal on a version: the events proposing it, and the votes on it, by ID. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub(crate) struct Ballot { + pub(crate) proposals: Vec, + pub(crate) votes: Vec, +} + /// A party's key set or node list. #[derive(Clone, Copy, PartialEq, Eq)] enum List { @@ -301,6 +308,26 @@ impl State { ids.into_iter().collect() } + /// The proposals on `on`, by their hash, each with the events proposing it and the + /// votes on it, by ID: what a party's client reads the votes it keeps from. + pub(crate) fn ballots(&self, on: &Id) -> BTreeMap { + let mut ballots: BTreeMap = BTreeMap::new(); + for (id, signed, under) in self.signed() { + match &signed.does { + Does::Proposal { hash, .. } if under == on => { + ballots.entry(*hash).or_default().proposals.push(*id); + } + Does::Vote { + on: voted_on, + proposal, + .. + } if voted_on == on => ballots.entry(*proposal).or_default().votes.push(*id), + _ => {} + } + } + ballots + } + /// Whether a proposal of hash `proposal` is proposed on `on` here. fn proposed_on(&self, proposal: &Id, on: &Id) -> bool { self.signed().any(|(_, signed, under)| { @@ -704,6 +731,28 @@ mod tests { matches!(offered, Offered::Refused(Refusal::Judged(_))) } + /// The proposals on a version, each with the events proposing it and the votes on it. + #[test] + fn ballots_name_proposals_and_their_votes() { + let (tv, lass, makefu) = (tv(), lass(), makefu()); + let spec = Spec::first(&[&tv, &lass, &makefu], &[]); + let mut store = federation(&spec); + let root = store.id(); + let on = spec.hash(); + let next = spec.after(on); + let proposal = taken(store.offer(tv.propose(&[root], &next.json()))); + let yes = taken(store.offer(lass.vote(&[proposal], on, next.hash(), true))); + let no = taken(store.offer(makefu.vote(&[proposal], on, next.hash(), false))); + let ballots = store.state().ballots(&on); + assert_eq!(ballots.len(), 1); + let ballot = &ballots[&next.hash()]; + assert_eq!(ballot.proposals, vec![proposal]); + let mut votes = vec![yes, no]; + votes.sort(); + assert_eq!(ballot.votes, votes); + assert!(store.state().ballots(&next.hash()).is_empty()); + } + #[test] fn a_party_writes_where_a_rule_gives_it_the_place() { let (tv, lass) = (tv(), lass()); -- cgit v1.3.1