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/api/events.rs | 14 +++++++++++++- src/federation/fold.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/api/events.rs b/src/api/events.rs index 3291f02..2fa6719 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -108,7 +108,8 @@ async fn event(State(state): State, Path(id): Path) -> Respons } /// `GET /events/counts`: what counts, as the store reads now: the current version, each -/// party's keys and nodes, the values at places, and the members. +/// party's keys and nodes, the values at places, the members, and the proposals on the +/// current version, each with the events proposing it and the votes on it. async fn counts(State(state): State) -> Response { with_store(&state, |store| { let read = store.state(); @@ -131,6 +132,16 @@ async fn counts(State(state): State) -> Response { .iter() .map(|(path, value)| (path.clone(), plain(value))) .collect(); + let proposals: serde_json::Map = read + .ballots(¤t.hash) + .into_iter() + .map(|(hash, ballot)| { + ( + hash.to_string(), + json!({"proposals": ids(ballot.proposals), "votes": ids(ballot.votes)}), + ) + }) + .collect(); json!({ "version": { "number": current.number, @@ -140,6 +151,7 @@ async fn counts(State(state): State) -> Response { "parties": parties, "values": values, "members": read.members(), + "proposals": proposals, }) }) } 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