Repository navigation
fix(id/event-store): honor settled device authority in tag clears (#5656) #8144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,12 +95,12 @@ use p2panda_auth::group::GroupAction; | |
| use p2panda_core::{Hash, SeqNum}; | ||
| use redb::{ReadableTable, ReadableTableMetadata, WriteTransaction}; | ||
|
|
||
| use super::resolve::{self, decode_action, encode_action}; | ||
| use super::resolve::{self, decode_action, encode_action, encode_device_op}; | ||
| use super::waiting_rows::{Hold, Mirror}; | ||
| use super::{ | ||
| ACTIONS, APPLIED, CHAINS, Chain, EVENTS, EventStore, FORKS, ForkProof, HASHES, PROOF_HEADERS, | ||
| PROOF_IDS, StoreError, corrupt, decode_stored, lowest_proof, position_key, store_chain, | ||
| store_event, store_fork, | ||
| ACTIONS, APPLIED, CHAINS, Chain, DEVICE_OPS, EVENTS, EventStore, FORKS, ForkProof, HASHES, | ||
| PROOF_HEADERS, PROOF_IDS, StoreError, corrupt, decode_stored, lowest_proof, position_key, | ||
| store_chain, store_event, store_fork, | ||
| }; | ||
| use crate::chain::{self, Event}; | ||
| use crate::documents::{is_document_kind, is_removal_kind}; | ||
|
|
@@ -239,6 +239,12 @@ impl EventStore { | |
| /// - A writer-set or rescind event's verified body is retained with the store | ||
| /// (`writer_set_bodies`), so the stored object can be read back and projected | ||
| /// (issue #6690; see the `writer_set` submodule). | ||
| /// - A device operation carried by a `group.event` body is recorded with the store | ||
| /// (`device_ops`), so the resolution can fold it as a group device event (issue | ||
| /// #5656). The membership body codec does not carry device operations, so without | ||
| /// this channel a device grant could never reach the event-store fold. A body that | ||
| /// claims to be a `group.event` device operation but does not decode, or names an | ||
| /// author other than the event's signer, is refused. | ||
| /// - A tag event (kind `tag.set` or `tag.clear`) takes no body. Its operation is in its | ||
| /// header (#182), so a payload on a tag event is refused. Its tag takes no membership | ||
| /// action, and the fold reads it from the stored header (see `claims`). | ||
|
|
@@ -282,15 +288,55 @@ impl EventStore { | |
| Err(refusal) => return Ok(Ingest::Refused(refusal)), | ||
| } | ||
| }; | ||
| let device = if is_tag_kind(kind) || is_document_kind(kind) || is_removal_kind(kind) { | ||
| None | ||
| } else { | ||
| match body_device_op(&event, body) { | ||
| Ok(device) => device, | ||
| Err(refusal) => return Ok(Ingest::Refused(refusal)), | ||
| } | ||
| }; | ||
| let result = self.ingest_event(event, action.as_ref(), now_ms)?; | ||
| if let Some(body) = retain_body | ||
| && !matches!(result, Ingest::Refused(_)) | ||
| { | ||
| self.record_writer_body(&event_id, body)?; | ||
| } | ||
| if let Some((op, group)) = &device | ||
| && !matches!(result, Ingest::Refused(_)) | ||
| { | ||
| self.record_device_op(&event_id, op, group.as_ref())?; | ||
| } | ||
|
Comment on lines
+305
to
+309
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Ok(result) | ||
| } | ||
|
|
||
| /// Records the device operation a verified `group.event` body carries, by event ID | ||
| /// (issue #5656). Written after the ingest decision, in its own transaction, like | ||
| /// [`EventStore::record_writer_body`]: the row is keyed by the event's ID and read | ||
| /// back only for events the store holds, so recording one for an event that is held | ||
| /// or later released is inert until the event is stored, and a duplicate delivery | ||
| /// simply rewrites the same row. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns a redb error if the write fails, or [`StoreError::Corrupt`] if the op | ||
| /// does not encode. | ||
| fn record_device_op( | ||
| &self, | ||
| id: &Hash, | ||
| op: &crate::groups::DeviceOp, | ||
| group: Option<&Hash>, | ||
| ) -> Result<(), StoreError> { | ||
| let encoded = encode_device_op(op, group)?; | ||
| let txn = self.db.begin_write()?; | ||
| { | ||
| let mut device_ops = txn.open_table(DEVICE_OPS)?; | ||
| device_ops.insert(id.as_bytes().as_slice(), encoded.as_slice())?; | ||
| } | ||
| txn.commit()?; | ||
| Ok(()) | ||
| } | ||
|
Comment on lines
+313
to
+338
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| /// Verifies one received event, which may be a group operation, and applies what it allows. | ||
| /// `action` is the membership change of a group operation, handed in by the caller. The store | ||
| /// does not read bodies here. Other events take no action. The production path is | ||
|
|
@@ -438,6 +484,42 @@ impl EventStore { | |
| } | ||
| } | ||
|
|
||
| /// The first bytes of every `group.event` body: a CBOR array of seven items, then the | ||
| /// family's tag as a text string. A body that starts with these bytes claims to be a | ||
| /// group-layer event in the wire family of [`crate::groups::event`]. | ||
| const GROUP_EVENT_CLAIM: &[u8] = b"\x87\x6bgroup.event"; | ||
|
|
||
| /// The device operation an event's body carries, with the group the body names (issue | ||
| /// #5656). `Ok(None)` when the body is absent, is not a `group.event` body, or is a | ||
| /// `group.event` body of any other kind: those stay content, exactly as before. The | ||
| /// caller has already verified the body is the one the header commits to (see | ||
| /// [`body_action`]). A body that claims to be a `group.event` device operation and | ||
| /// does not decode is refused, as is one whose named author is not the event's | ||
| /// signer: the grant a device operation records is the signer's, so a body naming | ||
| /// another author is not the operation it claims to be. | ||
| fn body_device_op( | ||
| event: &Event, | ||
| body: Option<&[u8]>, | ||
| ) -> Result<Option<(crate::groups::DeviceOp, Option<Hash>)>, Refusal> { | ||
| let Some(body) = body else { | ||
| return Ok(None); | ||
| }; | ||
| if !body.starts_with(GROUP_EVENT_CLAIM) { | ||
| return Ok(None); | ||
| } | ||
| let wire = | ||
| crate::groups::event::decode(body).map_err(|err| Refusal::Malformed(err.to_string()))?; | ||
| let crate::groups::event::EventBody::Group(crate::groups::Body::Device(op)) = wire.body else { | ||
| return Ok(None); | ||
| }; | ||
| if wire.author != author_of(event) { | ||
| return Err(Refusal::Malformed( | ||
| "a device operation body names an author other than the event's signer".to_owned(), | ||
| )); | ||
| } | ||
| Ok(Some((op, wire.group))) | ||
| } | ||
|
|
||
| /// The membership action an event's body carries. `Ok(None)` makes the event content: it has no | ||
| /// body, or its body is not a group operation. A body that is not the one the header commits to is | ||
| /// refused, and so is a body that claims to be a group operation and does not decode. | ||
|
|
@@ -954,9 +1036,10 @@ pub(super) fn refresh(txn: &WriteTransaction) -> Result<(), StoreError> { | |
| let (stored, forks, headers) = { | ||
| let events = txn.open_table(EVENTS)?; | ||
| let actions = txn.open_table(ACTIONS)?; | ||
| let device_ops = txn.open_table(DEVICE_OPS)?; | ||
| let forks = txn.open_table(FORKS)?; | ||
| let proof_headers = txn.open_table(PROOF_HEADERS)?; | ||
| resolve::load(&events, &actions, &forks, &proof_headers)? | ||
| resolve::load(&events, &actions, &device_ops, &forks, &proof_headers)? | ||
| }; | ||
| let resolved = resolve::resolve(&stored, &forks, &headers)?; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -123,6 +123,14 @@ const WRITER_SET_BODIES: TableDefinition<'static, &'static [u8], &'static [u8]> | |
| /// stored, so a release reads it back without the caller. The resolution reads it again. | ||
| const ACTIONS: TableDefinition<'static, &'static [u8], &'static [u8]> = | ||
| TableDefinition::new("actions"); | ||
| /// Each device operation's encoded op and the group its body names, by event ID (P8.3, | ||
| /// issue #5656). A device operation travels as a `group.event` body, which the membership | ||
| /// body codec does not carry, so it has no [`ACTIONS`] row; this table is its storage | ||
| /// channel to the resolution, whose bridge folds it as a group device event. Written only | ||
| /// from the ingest path, after the body has been verified against the header's payload | ||
| /// commitment and the body's author checked against the event's signer. | ||
| const DEVICE_OPS: TableDefinition<'static, &'static [u8], &'static [u8]> = | ||
| TableDefinition::new("device_ops"); | ||
| /// The IDs of the applied events, with an empty value. Written by the resolution. | ||
| const APPLIED: TableDefinition<'static, &'static [u8], &'static [u8]> = | ||
| TableDefinition::new("applied"); | ||
|
|
@@ -657,13 +665,14 @@ impl EventStore { | |
| create_fresh(storage)? | ||
| }; | ||
| verify_schema(&db)?; | ||
| // The blocks and writer-set-bodies tables are additive: stores created | ||
| // before them gain them here, on the write every open already | ||
| // performs, instead of a format change that would refuse them. | ||
| // The blocks, writer-set-bodies and device-ops tables are additive: | ||
| // stores created before them gain them here, on the write every open | ||
| // already performs, instead of a format change that would refuse them. | ||
| { | ||
| let txn = db.begin_write()?; | ||
| txn.open_table(BLOCKS)?; | ||
| txn.open_table(WRITER_SET_BODIES)?; | ||
| txn.open_table(DEVICE_OPS)?; | ||
|
Comment on lines
+668
to
+675
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| txn.commit()?; | ||
| } | ||
| let mut store = Self { | ||
|
|
@@ -750,9 +759,10 @@ impl EventStore { | |
| let (stored, forks, headers) = { | ||
| let events = txn.open_table(EVENTS)?; | ||
| let actions = txn.open_table(ACTIONS)?; | ||
| let device_ops = txn.open_table(DEVICE_OPS)?; | ||
| let forks = txn.open_table(FORKS)?; | ||
| let proof_headers = txn.open_table(PROOF_HEADERS)?; | ||
| resolve::load(&events, &actions, &forks, &proof_headers)? | ||
| resolve::load(&events, &actions, &device_ops, &forks, &proof_headers)? | ||
| }; | ||
| let resolved = resolve::resolve(&stored, &forks, &headers)?; | ||
| Ok(resolve::roster_of(&resolved, group)) | ||
|
|
@@ -977,6 +987,7 @@ impl EventStore { | |
| let mut hashes_table = txn.open_table(HASHES)?; | ||
| let mut groups_table = txn.open_multimap_table(GROUPS)?; | ||
| let mut actions_table = txn.open_table(ACTIONS)?; | ||
| let mut device_ops_table = txn.open_table(DEVICE_OPS)?; | ||
| let mut applied_table = txn.open_table(APPLIED)?; | ||
|
|
||
| for id in &to_prune { | ||
|
|
@@ -991,6 +1002,7 @@ impl EventStore { | |
| if meta.has_action { | ||
| actions_table.remove(id.as_bytes().as_slice())?; | ||
| } | ||
| device_ops_table.remove(id.as_bytes().as_slice())?; | ||
| applied_table.remove(id.as_bytes().as_slice())?; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: developing-today/code
Length of output: 8178
Script executed:
Repository: developing-today/code
Length of output: 42292
Script executed:
Repository: developing-today/code
Length of output: 36654
Script executed:
Repository: developing-today/code
Length of output: 25136
Propagate device operations during sync
When a fresh peer receives an Admin-device grant through range sync, it stores only the header and does not populate
DEVICE_OPS. Even if it has the group membership state, its default view can retain a claim that a directly ingested peer hides. Sync must deliver the committed device-operation body and ingest it on the receiving peer.