aboutsummaryrefslogtreecommitdiffstats
path: root/src/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/api')
-rw-r--r--src/api/events.rs90
1 files changed, 78 insertions, 12 deletions
diff --git a/src/api/events.rs b/src/api/events.rs
index 8eb243d..9fa8786 100644
--- a/src/api/events.rs
+++ b/src/api/events.rs
@@ -1,7 +1,8 @@
//! A swarm holding a store at a node's API (`docs/store.md`, Members): whoever its operator
//! lets in may have it put events, whoever signed them; it shows its store (its ID, its
-//! checkpoint and its heads), an event it holds, what its fold reads from them, and what
-//! it refused; and its operator has it drop what it took, and take it back.
+//! checkpoint and its heads), an event it holds, what its fold reads from them, what it
+//! refused, and what it finds of a fork's root; and its operator has it drop what it
+//! took, take it back, and make a fork's root.
use axum::{
Extension, Json, Router,
@@ -17,6 +18,7 @@ use serde_json::{Value as JsonValue, json};
use super::auth::AuthenticatedUser;
use super::{ApiError, error_response, store_status};
use crate::federation::fold::Federation;
+use crate::federation::fork;
use crate::federation::jcs::Json as Jcs;
use crate::federation::store::Id;
use crate::federation::store::memory::{Offered, Refusal, Store};
@@ -27,10 +29,12 @@ pub(super) fn router() -> Router<AppState> {
.route("/events", get(summary).post(put))
.route("/events/counts", get(counts))
.route("/events/refused", get(refused))
+ .route("/events/fork", post(check_fork))
.route("/events/{id}", get(event))
.route("/admin/events/drop", post(drop_taken))
.route("/admin/events/allow", post(allow))
.route("/admin/events/vote", post(vote))
+ .route("/admin/events/fork", post(make_fork))
}
/// The answer while the node holds no store.
@@ -263,17 +267,79 @@ struct Named {
impl Named {
fn ids(&self) -> Result<Vec<Id>, ApiError> {
- self.ids
- .iter()
- .map(|id| {
- Id::from_hex(id).ok_or_else(|| {
- ApiError(
- StatusCode::BAD_REQUEST,
- Some(format!("{id:?} isn't an event's ID")),
- )
- })
+ read_ids(&self.ids)
+ }
+}
+
+/// `ids`, each an event's ID in hex.
+fn read_ids(ids: &[String]) -> Result<Vec<Id>, ApiError> {
+ ids.iter()
+ .map(|id| {
+ Id::from_hex(id).ok_or_else(|| {
+ ApiError(
+ StatusCode::BAD_REQUEST,
+ Some(format!("{id:?} isn't an event's ID")),
+ )
})
- .collect()
+ })
+ .collect()
+}
+
+/// A fork to make: the heads it forks at, and its version.
+#[derive(Deserialize)]
+struct Forking {
+ heads: Vec<String>,
+ version: JsonValue,
+}
+
+/// `POST /admin/events/fork`: the root of the fork at `heads` by `version`, as this node's
+/// store builds it, with the events the root keeps: the file its founders start its swarm
+/// from (`--store-root`), the root on its first line, each event it keeps on a line of
+/// its own.
+async fn make_fork(State(state): State<AppState>, Json(forking): Json<Forking>) -> Response {
+ let heads = match read_ids(&forking.heads) {
+ Ok(heads) => heads,
+ Err(error) => return error.into_response(),
+ };
+ let version = match crate::federation::client::json(&forking.version) {
+ Ok(version) => version,
+ Err(why) => return error_response(StatusCode::BAD_REQUEST, why, None),
+ };
+ match state
+ .holding
+ .with(|store| fork::make(store, &heads, &version))
+ {
+ None => no_store(),
+ Some(Err(why)) => error_response(StatusCode::UNPROCESSABLE_ENTITY, why, None),
+ Some(Ok(file)) => ([(CONTENT_TYPE, "application/octet-stream")], file).into_response(),
+ }
+}
+
+/// `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; and the parties whose keys
+/// the fork's version gives otherwise than they had there, each with its keys there and in
+/// the fork.
+async fn check_fork(State(state): State<AppState>, body: Bytes) -> Response {
+ let (root, _) = fork::read_root_file(&body);
+ match state.holding.with(|store| fork::check(store, &root)) {
+ None => no_store(),
+ Some(Err(why)) => error_response(StatusCode::UNPROCESSABLE_ENTITY, why, None),
+ Some(Ok(check)) => {
+ let keys: serde_json::Map<String, JsonValue> = check
+ .keys
+ .into_iter()
+ .map(|(party, there, forked)| (party, json!({"there": there, "fork": forked})))
+ .collect();
+ Json(json!({
+ "fork": Id::of(&root).to_string(),
+ "builds": check.builds,
+ "why": check.why,
+ "lacking": ids(check.lacking),
+ "keys": keys,
+ }))
+ .into_response()
+ }
}
}