diff options
| -rw-r--r-- | CHANGELOG.md | 6 | ||||
| -rw-r--r-- | docs/guides/federation/proposing-and-voting.md | 14 | ||||
| -rw-r--r-- | docs/guides/federation/running-a-node.md | 6 | ||||
| -rw-r--r-- | docs/openapi.yaml | 44 | ||||
| -rw-r--r-- | src/api/access.rs | 6 | ||||
| -rw-r--r-- | src/api/events.rs | 31 | ||||
| -rw-r--r-- | src/federation/party.rs | 64 | ||||
| -rw-r--r-- | src/federation/vote.rs | 34 |
8 files changed, 181 insertions, 24 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a26c42..52c627f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ it from the release before ([`docs/operations.md`](docs/operations.md), ## Unreleased +* `continuum-party vote` asks its node before it signs a `true` whether nodes take it + (`POST /events/back`, which carries nothing signed), and casts and records nothing where + they don't: such a vote, recorded, bound the party though no node held it + ([`docs/guides/federation/proposing-and-voting.md`](docs/guides/federation/proposing-and-voting.md); + [`src/api/events.rs`](src/api/events.rs)). + * `--store-nay-beside` is gone: a node votes `false` by no rule. A party it follows could count on that `false`, and voting twice have its followers pass two checkpoints, in a federation of any size. A node given the option says so and stops: remove it. Where two diff --git a/docs/guides/federation/proposing-and-voting.md b/docs/guides/federation/proposing-and-voting.md index 1769a1f..a718bc8 100644 --- a/docs/guides/federation/proposing-and-voting.md +++ b/docs/guides/federation/proposing-and-voting.md @@ -113,12 +113,14 @@ record before it sends the vote. It casts no vote that would make its party vote current checkpoint's heads; * one `true` at a time: another `true` on the same version only once the proposal the party backed is dead. The client fetches the votes that made it so from the node, keeps - them in the record beside the new `true`, and builds the `true` on them, so that the - first is dead at its history too: the proposal it backs next may have been proposed at - a history holding its first `true`. A checkpoint's `true` it builds on the proposal - alone, and says when it may be refused so: where the nays came after the heads the - checkpoint names, back the one proposed after. - + those that end it in the record beside the new `true` (one of each party, of as many + parties as it takes), and builds the `true` on them, so that the first is dead at its + history too: the proposal it backs next may have been proposed at a history holding its + first `true`. A checkpoint's `true` it builds on the proposal alone. Before it signs + one, it asks its node whether nodes take it, built so (`POST /events/back`), and casts + and records nothing where they don't: where the nays came after the heads a checkpoint + names, back the one proposed after. A node before 0.1.0 doesn't answer: the client says + so, and goes on. A `true` holds the party until its proposal passes or is dead, and only other parties' nays make it dead. Where ending it would take every party yet to vote, or more, and those are a third of the parties or fewer, the client says so before it casts the `true` diff --git a/docs/guides/federation/running-a-node.md b/docs/guides/federation/running-a-node.md index 164c00d..27ee3a0 100644 --- a/docs/guides/federation/running-a-node.md +++ b/docs/guides/federation/running-a-node.md @@ -66,7 +66,8 @@ alone, so that a party listing many nodes can't be all of a node's neighbours. there is no user registry. Its users are its operators, and may do everything at it: put events, read, have it drop what it took and take it back, vote through it, make a fork's root. Those named in `--store-readers` read the federation and do nothing - else: `GET` of the events API below, and checking a fork (`POST /events/fork`), but + else: `GET` of the events API below, checking a fork (`POST /events/fork`) and asking + whether a `true` is taken (`POST /events/back`), but nothing of the document (`/store`, `/stream`), the node (`/peers`, `/admin/…`), nor what an operator does to the store (`/admin/events/…`): for an application, or someone watching, that needn't act for the node. A token is taken by the node that @@ -85,7 +86,8 @@ alone, so that a party listing many nodes can't be all of a node's neighbours. checkpoint, its heads, how many events it took, the node's party, and whether it votes for it by itself), `POST /events` (an event, whoever signed it), `GET /events/{id}`, `GET /events/counts` (what counts), `GET /events/refused` (what the store refused, and - why), `POST /events/fork` (what the node finds of a fork's root); and its operator's: + why), `POST /events/fork` (what the node finds of a fork's root), `POST /events/back` + (whether it takes a `true` a party is about to cast); and its operator's: `POST /admin/events/drop`, `/admin/events/allow`, `/admin/events/vote`, `/admin/events/fork`. Each answers `404` while the node holds no store. * **The store API** (`/store`) reads the document, where events are bytes at diff --git a/docs/openapi.yaml b/docs/openapi.yaml index adc5426..d3063d8 100644 --- a/docs/openapi.yaml +++ b/docs/openapi.yaml @@ -1812,6 +1812,50 @@ paths: $ref: '#/components/responses/Error' '422': $ref: '#/components/responses/Error' + /events/back: + post: + tags: [Events] + summary: Whether this node takes a `true` a party is about to cast + description: | + 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: at that history, none of its `true`s on another proposal but on one that is + dead there. A party's client asks before it signs and records one, so that a + `true` every node refuses is never cast: nothing signed is sent for it. + requestBody: + required: true + content: + application/json: + schema: + type: object + required: [party, proposal, on] + properties: + party: + type: string + proposal: + type: string + on: + type: array + items: + type: string + responses: + '200': + description: Whether it takes it, and why not. + content: + application/json: + schema: + type: object + required: [takes, why] + properties: + takes: + type: boolean + why: + type: string + nullable: true + '400': + $ref: '#/components/responses/Error' + '404': + $ref: '#/components/responses/Error' /admin/events/drop: post: tags: [Events] 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 diff --git a/src/federation/party.rs b/src/federation/party.rs index ff66de3..c8f19ea 100644 --- a/src/federation/party.rs +++ b/src/federation/party.rs @@ -1586,16 +1586,35 @@ async fn vote( .ok() .and_then(|version| version.checkpoint) .is_some(); - if checkpoint && yes && !kept.is_empty() { - eprintln!( - "Warning: this `true` builds on {proposing} alone, not on the nays that freed \ - {party}: where they came after the heads it names, and {party}'s `true` they \ - ended is in the history of {proposing}, nodes refuse it; a checkpoint proposed \ - after them takes {party}'s `true`", - party = signer.party - ); - } let on = client::building(proposing, checkpoint, &kept).map_err(|why| anyhow!(why))?; + // Asked first, with nothing signed: a `true` every node refuses is neither cast nor + // recorded, where it would bind the party by a vote no node holds. + if yes { + match node.takes_true(&signer.party, &proposal, &on).await { + Ok(None) => {} + Ok(Some(why)) => bail!( + "nothing cast: {why}. {party}'s earlier `true` on this version is on a \ + proposal that isn't dead where this vote would build; of a checkpoint, the \ + nays that ended it came after its heads were named: back the next one", + party = signer.party + ), + Err(error) => { + eprintln!( + "Warning: the node didn't say whether it takes this `true` ({error:#}); \ + one before 0.1.0 doesn't" + ); + if checkpoint && !kept.is_empty() { + eprintln!( + "Warning: this `true` builds on {proposing} alone, not on the nays \ + that freed {party}: where they came after the heads it names, and \ + {party}'s `true` they ended is in the history of {proposing}, nodes \ + refuse it; a checkpoint proposed after them takes {party}'s `true`", + party = signer.party + ); + } + } + } + } let word = if yes { "`true`" } else { "`false`" }; if ask && !confirmed(&format!( @@ -2603,6 +2622,33 @@ impl Node { } /// What the node answers to the bytes `body`. + /// Whether the node takes a `true` of `party` on `proposal` built on `on` (`POST + /// /events/back`): why not, where it doesn't. Nothing signed is sent. + async fn takes_true(&self, party: &str, proposal: &Id, on: &[Id]) -> Result<Option<String>> { + let asked = serde_json::json!({ + "party": party, + "proposal": proposal.to_string(), + "on": on.iter().map(Id::to_string).collect::<Vec<_>>(), + }); + let response = self + .request(reqwest::Method::POST, "/events/back")? + .header(reqwest::header::CONTENT_TYPE, "application/json") + .body(asked.to_string()) + .send() + .await?; + let answer: JsonValue = self.answer(response).await?.json().await?; + match answer["takes"].as_bool() { + Some(true) => Ok(None), + Some(false) => Ok(Some( + answer["why"] + .as_str() + .unwrap_or("no store takes it") + .to_owned(), + )), + None => bail!("no answer of its"), + } + } + async fn post_bytes(&self, path: &str, body: Vec<u8>) -> Result<JsonValue> { let response = self .request(reqwest::Method::POST, path)? diff --git a/src/federation/vote.rs b/src/federation/vote.rs index 0bad500..9219656 100644 --- a/src/federation/vote.rs +++ b/src/federation/vote.rs @@ -234,6 +234,29 @@ pub(crate) fn backs_checkpoint( } } +/// Whether `store` takes a `true` of `party` on the proposal of hash `proposal` on its +/// current version, built on the events `on`, as far as the party's other votes go: at +/// that history, none of its `true`s on another proposal but on one that is dead there +/// (`State::may_back`). Why not, where it doesn't, or holds one of `on` not. A node asks +/// before it casts one (`builds_on`), and answers a party's client asking before it signs +/// one (`POST /events/back`): nothing signed goes to the node for that. +pub(crate) fn takes_true( + store: &Store<Federation>, + party: &str, + proposal: &Id, + on: &[Id], +) -> Result<(), String> { + if let Some(lacking) = on.iter().find(|id| !store.holds(id)) { + return Err(format!( + "this node holds no event {lacking}, which the vote builds on" + )); + } + store + .state_at(on) + .may_back(party, proposal, store.state().current()) + .map_err(|why| format!("no store takes that vote: {}", why.0)) +} + /// What a vote of `party` on the proposal of hash `proposal` builds on in `store`: an /// event proposing it, of a checkpoint one above the heads it names, so that a version /// event may be written on the vote; and, but for a checkpoint, the nays that freed its @@ -267,10 +290,7 @@ pub(crate) fn builds_on( .is_some(); let on = super::client::building(proposing, checkpoint, nays)?; if yes { - store - .state_at(&on) - .may_back(party, proposal, current) - .map_err(|why| format!("no store takes that vote: {}", why.0))?; + takes_true(store, party, proposal, &on)?; } Ok(on) } @@ -711,7 +731,11 @@ mod tests { .any(|act| matches!(act, Act::Vote { yes: true, .. })), "{acts:?}" ); - // What it would have cast, its store refuses. + // As it answers a client that asks first; and what it would have cast, its store + // refuses. + assert!(takes_true(&store, "makefu", &checkpoint.hash(), &[proposal]).is_err()); + assert!(takes_true(&store, "lass", &checkpoint.hash(), &[proposal]).is_ok()); + assert!(takes_true(&store, "lass", &checkpoint.hash(), &[Id::of(b"none")]).is_err()); assert!(matches!( store.offer(makefu.vote(&[proposal], on, checkpoint.hash(), true)), Offered::Refused(_) |
