diff options
Diffstat (limited to 'src/api')
| -rw-r--r-- | src/api/access.rs | 6 | ||||
| -rw-r--r-- | src/api/events.rs | 31 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/api/access.rs b/src/api/access.rs index 339d9de..9e8a018 100644 --- a/src/api/access.rs +++ b/src/api/access.rs @@ -452,7 +452,8 @@ fn federated(state: &AppState, request: &Request<Body>, user: &str) -> Option<Re } /// Whether a request is federation work: reading the events API (`GET` and `HEAD` of -/// `/events` and below), checking a fork (`POST /events/fork`), and keeping a session +/// `/events` and below), checking a fork (`POST /events/fork`), asking whether a `true` is +/// taken (`POST /events/back`), and keeping a session /// (`GET /auth/whoami`, `POST /auth/refresh`, `POST /auth/logout`); with `puts`, putting /// an event (`POST /events`) too. Nothing of the document (`/store`, `/stream`), the /// node (`/peers`, `/admin/…`), nor what a node's operator does to its store @@ -463,7 +464,7 @@ fn federation_work(method: &Method, path: &str, puts: bool) -> bool { let posts = *method == Method::POST; match path { "/auth/whoami" => reads, - "/auth/refresh" | "/auth/logout" | "/events/fork" => posts, + "/auth/refresh" | "/auth/logout" | "/events/fork" | "/events/back" => posts, "/events" => reads || (puts && posts), _ => reads && path.starts_with("/events/"), } @@ -493,6 +494,7 @@ mod tests { assert!(work(Method::GET, "/events/counts", false)); assert!(work(Method::HEAD, "/events/0c7e", false)); assert!(work(Method::POST, "/events/fork", false)); + assert!(work(Method::POST, "/events/back", false)); assert!(work(Method::POST, "/auth/refresh", false)); assert!(work(Method::GET, "/auth/whoami", false)); assert!(!work(Method::POST, "/events", false)); diff --git a/src/api/events.rs b/src/api/events.rs index 1d275f9..55b29f1 100644 --- a/src/api/events.rs +++ b/src/api/events.rs @@ -32,6 +32,7 @@ pub(super) fn router() -> Router<AppState> { .route("/events/counts", get(counts)) .route("/events/refused", get(refused)) .route("/events/fork", post(check_fork)) + .route("/events/back", post(check_back)) .route("/events/{id}", get(event)) .route("/admin/events/drop", post(drop_taken)) .route("/admin/events/allow", post(allow)) @@ -486,6 +487,36 @@ async fn make_fork(State(state): State<AppState>, Json(forking): Json<Forking>) } } +/// A `true` a party's client is about to sign: its party, the proposal's hash, and the +/// events it would build on. +#[derive(Deserialize)] +struct Backing { + party: String, + proposal: String, + on: Vec<String>, +} + +/// `POST /events/back`: whether this node takes a `true` of `party` on the proposal of +/// hash `proposal` on the current version, built on the events `on`, as far as the party's +/// other votes go (`vote::takes_true`); `why` where it doesn't. A party's client asks +/// before it signs and records one: nothing signed comes here, so the node gets nothing +/// to pass on, and a vote every node would refuse is never cast. +async fn check_back(State(state): State<AppState>, Json(backing): Json<Backing>) -> Response { + let Some(proposal) = Id::from_hex(&backing.proposal) else { + return error_response(StatusCode::BAD_REQUEST, "not a proposal's hash", None); + }; + let on = match read_ids(&backing.on) { + Ok(on) => on, + Err(error) => return error.into_response(), + }; + with_store(&state, |store| { + match crate::federation::vote::takes_true(store, &backing.party, &proposal, &on) { + Ok(()) => json!({"takes": true, "why": null}), + Err(why) => json!({"takes": false, "why": why}), + } + }) +} + /// `POST /events/fork`: what this node finds of the fork whose root is the body's first /// line, as the file its founders start from holds it: whether it builds that root at /// its heads, and why not; what it holds that those heads lack; what the fork's version |
