diff options
| -rw-r--r-- | docs/openapi.yaml | 20 | ||||
| -rw-r--r-- | src/api/events.rs | 14 | ||||
| -rw-r--r-- | src/federation/fold.rs | 49 |
3 files changed, 80 insertions, 3 deletions
diff --git a/docs/openapi.yaml b/docs/openapi.yaml index 0bce165..c8f839c 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -1507,7 +1507,9 @@ paths: description: | What counts, as the store reads now (docs/federation.md): the current version (its number, its hash, and the version itself), each party's keys and nodes, the - value at each place, and the members, each `[node, party]`. + value at each place, the members, each `[node, party]`, and the proposals on the + current version, by their hash, each with the IDs of the events proposing it + (`proposals`) and of the votes on it (`votes`). responses: '200': description: What counts. @@ -1515,7 +1517,7 @@ paths: application/json: schema: type: object - required: [version, parties, values, members] + required: [version, parties, values, members, proposals] properties: version: type: object @@ -1529,6 +1531,20 @@ paths: type: array items: type: string + proposals: + type: object + additionalProperties: + type: object + required: [proposals, votes] + properties: + proposals: + type: array + items: + type: string + votes: + type: array + items: + type: string '404': $ref: '#/components/responses/Error' /events/refused: 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(¤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<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()); |
