aboutsummaryrefslogtreecommitdiffstats
path: root/src/federation/fold.rs
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-16 21:45:11 +0000
committertv <tv@krebsco.de>2026-11-16 23:07:29 +0100
commit0c213626ba238fd6d8d69d08475d57e486f6f27a (patch)
treecee091c61ef3fd9505a04eaadba482785f422a1e /src/federation/fold.rs
parentd893a092c469688eaf98c98725fedf6dfad99614 (diff)
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`).
Diffstat (limited to 'src/federation/fold.rs')
-rw-r--r--src/federation/fold.rs49
1 files changed, 49 insertions, 0 deletions
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<Id, Json>,
}
+/// 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<Id>,
+ pub(crate) votes: Vec<Id>,
+}
+
/// 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<Id, Ballot> {
+ let mut ballots: BTreeMap<Id, Ballot> = 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());