aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/api/events.rs14
-rw-r--r--src/federation/fold.rs49
2 files changed, 62 insertions, 1 deletions
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<AppState>, Path(id): Path<String>) -> 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<AppState>) -> Response {
with_store(&state, |store| {
let read = store.state();
@@ -131,6 +132,16 @@ async fn counts(State(state): State<AppState>) -> Response {
.iter()
.map(|(path, value)| (path.clone(), plain(value)))
.collect();
+ let proposals: serde_json::Map<String, JsonValue> = read
+ .ballots(&current.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<AppState>) -> 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<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());