aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/events.rs67
1 files changed, 63 insertions, 4 deletions
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<String>,
+}
+
+/// 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<Federation>, party: &str) -> Option<Id> {
+ 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<AppState>) -> Response {
+async fn summary(State(state): State<AppState>, Query(summarized): Query<Summarized>) -> 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<AppState>) -> 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::<Vec<_>>(),
+ )),
"taken": store.taken().len(),
"stopped": stopped,
"node": state.local_id(),
@@ -578,3 +609,31 @@ async fn allow(State(state): State<AppState>, Json(named): Json<Named>) -> 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::<Federation>::new(first.root(), Vec::new()).unwrap();
+ let root = store.id();
+ let mut take = |bytes: Vec<u8>| -> 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);
+ }
+}