From 2c9be74e184e0d942f03a8f1b549bdd7317e2dbd Mon Sep 17 00:00:00 2001 From: tv Date: Wed, 25 Nov 2026 15:34:56 +0000 Subject: store: a party's next event builds on its last, past 64 heads Where a store holds more heads than an event names, a new event builds on the earliest. A party's write may then lack its last one in its history: of two neither in the other's the lower ID counts, not the later, and a key set written so is written under an older version, and ranks below one written under the latest. building_on_with names a head holding each event it is asked to hold, beside the one holding what the fold judges by; GET /events?party=NAME asks it for that party's last write, key set or node list (latest_of), and continuum-party signs on the heads for its party. --- src/api/events.rs | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 4 deletions(-) (limited to 'src/api') diff --git a/src/api/events.rs b/src/api/events.rs index 55b29f1..1c4b826 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -7,7 +7,7 @@ use axum::{ Extension, Json, Router, body::Bytes, - extract::{Path, State}, + extract::{Path, Query, State}, http::{StatusCode, header::CONTENT_TYPE}, response::{IntoResponse, Response}, routing::{get, post}, @@ -83,12 +83,36 @@ fn why(refusal: &Refusal) -> String { } } +/// Whom `GET /events` names its heads for. +#[derive(Deserialize)] +struct Summarized { + party: Option, +} + +/// The last write, key set or node list `store` took of `party`: what its next is to have +/// in its history, as of two neither in the other's the lower ID counts, not the later. +fn latest_of(store: &Store, party: &str) -> Option { + use crate::federation::event::{Does, Event, Signed}; + store.taken().iter().rev().copied().find(|id| { + matches!( + store.get(id).and_then(Event::read_taken), + Some(Event::Signed(Signed { + party: signer, + does: Does::Write { .. } | Does::Keys(_) | Does::Nodes(_), + .. + })) if signer == party + ) + }) +} + /// `GET /events`: the store's ID, its checkpoint's, its heads, as many as an event may -/// name, what a new event builds on (`Store::building_on`), how many events it took +/// name, what a new event builds on (`Store::building_on`; with `?party=NAME`, one holding +/// that party's last write, key set or node list among them, where the store holds more +/// heads than that, `latest_of`), how many events it took /// after its checkpoint, the event it stopped at, if any, the node's ID and party, whether /// the node votes for it by itself, and the revisions of the federation its continuum /// knows. -async fn summary(State(state): State) -> Response { +async fn summary(State(state): State, Query(summarized): Query) -> Response { let party = state.holding.party().map(str::to_owned); let votes = state.holding.votes_by_itself(); with_store(&state, |store| { @@ -98,7 +122,14 @@ async fn summary(State(state): State) -> Response { json!({ "store": store.id().to_string(), "checkpoint": Id::of(store.checkpoint()).to_string(), - "heads": ids(store.building_on()), + "heads": ids(store.building_on_with( + &summarized + .party + .as_deref() + .and_then(|party| latest_of(store, party)) + .into_iter() + .collect::>(), + )), "taken": store.taken().len(), "stopped": stopped, "node": state.local_id(), @@ -578,3 +609,31 @@ async fn allow(State(state): State, Json(named): Json) -> Respo no_store() } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::federation::harness::{self, Party, Spec}; + + /// A party's latest event, for what its next builds on: its last write, key set or + /// node list, not a proposal or a vote of its, which hold no place's value. + #[test] + fn a_partys_latest_event_is_its_last_write_key_set_or_node_list() { + let (tv, lass) = (Party::new("tv", 1), Party::new("lass", 2)); + let first = Spec::first(&[&tv, &lass], &[]); + let mut store = Store::::new(first.root(), Vec::new()).unwrap(); + let root = store.id(); + let mut take = |bytes: Vec| -> Id { + let id = Id::of(&bytes); + assert!(matches!(store.offer(bytes), Offered::Taken(_))); + id + }; + take(tv.write(&[root], "home/tv/a", harness::str("1"))); + let keys = take(tv.keys(&[root], &[tv.key_line()])); + let theirs = take(lass.write(&[root], "home/lass/a", harness::str("1"))); + take(tv.propose(&[root], &first.after(first.hash()).json())); + assert_eq!(latest_of(&store, "tv"), Some(keys)); + assert_eq!(latest_of(&store, "lass"), Some(theirs)); + assert_eq!(latest_of(&store, "makefu"), None); + } +} -- cgit v1.3.1