commit 25e93b2f03cb807d68ea61cdf60851f5ebf16060 parent e1d058d4c20e5e8651e4155f14021023c1083b13 Author: triesap <tyson@radroots.org> Date: Wed, 28 Oct 2026 19:51:08 +0000 storage: retain original stock gift wraps on the authority owner - Authenticate each original installation, recipient seal, and exact outer containment. - Append protected envelope history and derived indexes under catalog sixteen. - Preserve retained catalogs, owned drain, transaction fencing, and original bytes. - Verify native and Linux faults, reopen, process boundaries, and generated APIs. Diffstat:
57 files changed, 1941 insertions(+), 186 deletions(-)
diff --git a/contracts/api_baselines/radroots_storage_sqlite.txt b/contracts/api_baselines/radroots_storage_sqlite.txt @@ -497,6 +497,7 @@ pub fn radroots_storage_sqlite::workspace::NativeWorkspace::signing_attempt(&sel pub fn radroots_storage_sqlite::workspace::NativeWorkspace::signing_plan(&self, radroots_storage::workspace::WorkspaceBinding, radroots_domain::identifier::OperationId, radroots_domain::identifier::CommandDigest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::command::BoundSigningPlan>, radroots_storage::command::OperationError>> impl radroots_storage::inventory::InventoryStore for radroots_storage_sqlite::workspace::NativeWorkspace pub fn radroots_storage_sqlite::workspace::NativeWorkspace::abort_proven_unissued(&self, radroots_storage::inventory::abort::StockAbortRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::inventory::abort::StockAbortOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::capture_stock_envelope<'a>(&'a self, radroots_storage::inventory::envelope::StockEnvelopeRequest, radroots_storage::workspace::AttachmentProof, &'a radroots_storage::command::delivery::private::PrivateEnvelopeCandidate) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::inventory::envelope::StockEnvelopeOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::capture_stock_evidence<'a>(&'a self, &'a radroots_storage::inventory::evidence::StockEvidenceRequest, &'a radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::inventory::evidence::StockEvidenceOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::execute_inventory(&self, radroots_storage::inventory::InventoryRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::inventory::InventoryOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::inventory_operation(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::OperationId, core::option::Option<radroots_domain::identifier::CommandDigest>, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::inventory::InventoryReceipt>, radroots_storage::command::OperationError>> @@ -504,6 +505,7 @@ pub fn radroots_storage_sqlite::workspace::NativeWorkspace::option_mapping(&self pub fn radroots_storage_sqlite::workspace::NativeWorkspace::pool(&self, radroots_storage::workspace::WorkspaceBinding, radroots_storage::inventory::InventoryCheckpointRef, radroots_domain::identifier::PoolId, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_domain::stock::PoolView>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::prepare_stock_acceptance(&self, radroots_storage::inventory::acceptance::StockAcceptanceRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::inventory::acceptance::StockAcceptanceOutcome, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::reconcile_stock_evidence<'a>(&'a self, &'a radroots_storage::inventory::evidence::StockEvidenceRequest, &'a radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'a, core::result::Result<radroots_storage::inventory::evidence::StockEvidenceOutcome, radroots_storage::command::OperationError>> +pub fn radroots_storage_sqlite::workspace::NativeWorkspace::stock_envelope(&self, radroots_storage::inventory::envelope::StockEnvelopeRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::inventory::envelope::StockEnvelopeSource>, radroots_storage::command::OperationError>> pub fn radroots_storage_sqlite::workspace::NativeWorkspace::stock_preparation_abort(&self, radroots_storage::inventory::abort::StockAbortRequest, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<core::option::Option<radroots_storage::inventory::abort::StockAbortReceipt>, radroots_storage::command::OperationError>> impl radroots_storage::workspace::WorkspaceStore for radroots_storage_sqlite::workspace::NativeWorkspace pub fn radroots_storage_sqlite::workspace::NativeWorkspace::authenticate(&self, radroots_storage::workspace::AttachmentInput, radroots_storage::workspace::AttachmentProof) -> radroots_transport::source::BoxFuture<'_, core::result::Result<radroots_storage::workspace::WorkspaceBinding, radroots_storage::workspace::WorkspaceError>> diff --git a/contracts/architecture/decisions/shared_sdk_refactor.v1.json b/contracts/architecture/decisions/shared_sdk_refactor.v1.json @@ -82,7 +82,7 @@ "adapter": "radroots_nostr.signing.LocalSigner", "native_authorizer": "NativeWorkspace.authorize_stock_exact_replay", "captured_replay": "local_replay_safe", - "native_catalog": 15, + "native_catalog": 16, "worker_charge_bytes": 1048576, "existing_protected_claim_required": true, "current_execute_consent_required": true, @@ -111,7 +111,7 @@ "scope": "seller_stock_owner", "database": "stock_execution_v1.sqlite", "lock": ".stock_execution_v1.lock", - "schema_version": 15, + "schema_version": 16, "retained_enrollment_versions": [ 1 ], @@ -128,9 +128,10 @@ 11, 12, 13, - 14 + 14, + 15 ], - "authority_payload_schema": "radroots.seller_stock.authority.v15", + "authority_payload_schema": "radroots.seller_stock.authority.v16", "public_source_payload_schemas": [ "radroots.seller_stock.public_source_event.v1", "radroots.seller_stock.public_source_head.v1", @@ -393,7 +394,8 @@ ], "retained_v1_requires_absent_claims": true, "claimed_cleanup_catalogs": [ - 15 + 15, + 16 ], "untracked_claimed_catalogs": [ 13, @@ -406,6 +408,47 @@ "contradictory_evidence_retained": true, "contradiction_requires_recovery": true, "caller_reports_are_release_authority": false + }, + "envelope_retention": { + "journal_family": 10, + "payload_version": 1, + "payload_prefix_bytes": 213, + "supported_catalogs": [ + 16 + ], + "retained_unsupported_catalogs": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15 + ], + "raw_max_bytes": 65536, + "worker_charge_bytes": 1048576, + "capture_port": "InventoryStore.capture_stock_envelope", + "query_port": "InventoryStore.stock_envelope", + "original_preparation_plan_claim_seal_installation_required": true, + "temporary_key_derivation_and_exact_decryption_required": true, + "temporary_key_retained": false, + "original_party_as_outer_author_allowed": false, + "body_derived_projection_and_complete_ancestry_required": true, + "original_credential_capture_requires_live_clock_or_grant": false, + "query_requires_current_recovery_access": true, + "observation_nullable": true, + "existing_original_bytes_may_be_replaced": false, + "capture_grants_dispatch": false, + "capture_changes_physical_stock": false, + "provider_io_inside_sql": false } }, "provenance": { diff --git a/crates/storage_sqlite/src/workspace/crypto.rs b/crates/storage_sqlite/src/workspace/crypto.rs @@ -709,7 +709,7 @@ fn stock_root_context( input: &super::AttachmentInput, version: i64, ) -> Result<EnvelopeContext, WorkspaceError> { - if matches!(version, 4..=15) { + if matches!(version, 4..=16) { use sha2::{Digest, Sha256}; let namespace = radroots_storage::workspace::seller_stock::SellerAuthoritySelection::from_attachment( @@ -744,8 +744,10 @@ fn stock_root_context( b"radroots.seller_stock.authority_context.v13\0".as_slice() } else if version == 14 { b"radroots.seller_stock.authority_context.v14\0".as_slice() - } else { + } else if version == 15 { b"radroots.seller_stock.authority_context.v15\0".as_slice() + } else { + b"radroots.seller_stock.authority_context.v16\0".as_slice() }); hash.update(wire.as_slice()); let subject = radroots_domain::CommandDigest::from_bytes(hash.finalize().into()); @@ -776,8 +778,10 @@ fn stock_root_context( "radroots.seller_stock.authority.v13" } else if version == 14 { "radroots.seller_stock.authority.v14" - } else { + } else if version == 15 { "radroots.seller_stock.authority.v15" + } else { + "radroots.seller_stock.authority.v16" }) .map_err(|_| WorkspaceError::InvalidInput)?, )); @@ -895,12 +899,12 @@ fn stock_record_context_for_catalog( ) -> Result<EnvelopeContext, WorkspaceError> { let original = stock_record_context(input, kind, id)?; let schema = match (catalog, kind) { - (2, _) | (3..=15, "pool") => return Ok(original), + (2, _) | (3..=16, "pool") => return Ok(original), (3, "operation") => "radroots.seller_stock.operation.v2", (4, "operation") => "radroots.seller_stock.operation.v3", (5 | 6, "operation") => "radroots.seller_stock.operation.v4", - (7..=15, "operation") => "radroots.seller_stock.operation.v5", - (3..=15, "movement") => "radroots.seller_stock.movement.v2", + (7..=16, "operation") => "radroots.seller_stock.operation.v5", + (3..=16, "movement") => "radroots.seller_stock.movement.v2", _ => return Err(WorkspaceError::RecoveryRequired), }; Ok(EnvelopeContext::new( diff --git a/crates/storage_sqlite/src/workspace/delivery/containment.rs b/crates/storage_sqlite/src/workspace/delivery/containment.rs @@ -0,0 +1,38 @@ +//! Mechanical outer containment only. Each owner authenticates original work. +use nostr::JsonUtil; +use radroots_domain::PublicKey; +use radroots_storage::command::OperationError; +use sha2::{Digest, Sha256}; +use subtle::ConstantTimeEq; +use zeroize::Zeroizing; + +pub(in crate::workspace) fn verify( + raw: &str, + author: PublicKey, + wrapping_key: &[u8; 32], + recipient: PublicKey, + original: &str, + original_digest: &[u8; 32], +) -> Result<(), OperationError> { + let event = nostr::Event::from_json(raw).map_err(|_| OperationError::InvalidEvidence)?; + event + .verify() + .map_err(|_| OperationError::InvalidEvidence)?; + let recipient = nostr::PublicKey::from_slice(recipient.as_bytes()) + .map_err(|_| OperationError::InvalidEvidence)?; + let secret = + nostr::SecretKey::from_slice(wrapping_key).map_err(|_| OperationError::InvalidEvidence)?; + let keys = nostr::Keys::new(secret); + if keys.public_key().to_bytes() != *author.as_bytes() { + return Err(OperationError::InvalidEvidence); + } + let opened = Zeroizing::new( + nostr::nips::nip44::decrypt(keys.secret_key(), &recipient, &event.content) + .map_err(|_| OperationError::InvalidEvidence)?, + ); + let digest: [u8; 32] = Sha256::digest(opened.as_bytes()).into(); + if digest.ct_eq(original_digest).unwrap_u8() != 1 || opened.as_bytes() != original.as_bytes() { + return Err(OperationError::InvalidEvidence); + } + Ok(()) +} diff --git a/crates/storage_sqlite/src/workspace/delivery/mod.rs b/crates/storage_sqlite/src/workspace/delivery/mod.rs @@ -1,6 +1,7 @@ //! Append-only protected transport attempts on the existing workspace owner. mod codec; +pub(super) mod containment; pub(super) mod graph; mod json; pub(super) mod observation; diff --git a/crates/storage_sqlite/src/workspace/delivery/operations.rs b/crates/storage_sqlite/src/workspace/delivery/operations.rs @@ -5,16 +5,13 @@ use super::super::{ snapshot::RetainedGraph, }; use super::{codec, graph::Graph, observation::Observations}; -use nostr::JsonUtil; use radroots_domain::{CommandDigest, OperationId, TransactionId}; use radroots_storage::command::{ CommitAttempt, DeliveryAttempt, DeliveryAttemptOutcome, DeliveryAttemptRequest, EnvelopeCandidate, OperationError, PublicationArtifact, PublicationWork, SigningTarget, }; use radroots_transport::sink::DeliveryRequestId; -use sha2::{Digest, Sha256}; use sqlx::Connection; -use subtle::ConstantTimeEq; use zeroize::Zeroizing; fn contains_original( @@ -43,30 +40,16 @@ fn contains_original( { return Err(OperationError::InvalidEvidence); } - let event = nostr::Event::from_json(candidate.raw_json()) - .map_err(|_| OperationError::InvalidEvidence)?; - event - .verify() - .map_err(|_| OperationError::InvalidEvidence)?; - let recipient = nostr::PublicKey::from_slice(work.recipient().as_bytes()) - .map_err(|_| OperationError::InvalidEvidence)?; - let opened = candidate.with_wrapping_key(|key| { - let secret = - nostr::SecretKey::from_slice(key).map_err(|_| OperationError::InvalidEvidence)?; - let keys = nostr::Keys::new(secret); - if keys.public_key().to_bytes() != *candidate.author().as_bytes() { - return Err(OperationError::InvalidEvidence); - } - nostr::nips::nip44::decrypt(keys.secret_key(), &recipient, &event.content) - .map(Zeroizing::new) - .map_err(|_| OperationError::InvalidEvidence) + candidate.with_wrapping_key(|key| { + super::containment::verify( + candidate.raw_json(), + candidate.author(), + key, + work.recipient(), + source.raw_json(), + source.digest(), + ) })?; - let digest: [u8; 32] = Sha256::digest(opened.as_bytes()).into(); - if digest.ct_eq(source.digest()).unwrap_u8() != 1 - || opened.as_bytes() != source.raw_json().as_bytes() - { - return Err(OperationError::InvalidEvidence); - } Ok(()) } fn work( diff --git a/crates/storage_sqlite/src/workspace/stock.rs b/crates/storage_sqlite/src/workspace/stock.rs @@ -444,10 +444,10 @@ struct AuthorityV8 { } impl Authority { fn encode(&self) -> Result<zeroize::Zeroizing<Vec<u8>>, WorkspaceError> { - if !matches!(self.catalog, 8..=15) && self.public_sources.is_some() { + if !matches!(self.catalog, 8..=16) && self.public_sources.is_some() { return Err(WorkspaceError::RecoveryRequired); } - if matches!(self.catalog, 8..=15) { + if matches!(self.catalog, 8..=16) { let public_sources = self .public_sources .ok_or(WorkspaceError::RecoveryRequired)?; @@ -699,7 +699,7 @@ impl Authority { public_sources: None, } } - 8..=15 => { + 8..=16 => { let value: AuthorityV8 = closed_record(bytes)?; value.public_sources.validate()?; if i64::from(value.version) != catalog @@ -746,6 +746,7 @@ pub(super) struct Initial { pub expires_at: radroots_domain::UnixSeconds, pub clock: Arc<dyn WorkspaceClock>, } +pub(in crate::workspace) mod envelope; pub(super) mod inventory; mod schema; pub(super) async fn initialize( @@ -829,7 +830,7 @@ pub(super) async fn initialize( .await .map_err(|_| WorkspaceError::StorageFailure)?; } - sqlx::query("PRAGMA user_version=15") + sqlx::query("PRAGMA user_version=16") .execute(&mut *tx) .await .map_err(|_| WorkspaceError::StorageFailure)?; @@ -1158,6 +1159,37 @@ impl radroots_storage::inventory::InventoryStore for NativeWorkspace { .await }) } + fn capture_stock_envelope<'a>( + &'a self, + request: radroots_storage::inventory::StockEnvelopeRequest, + proof: AttachmentProof, + candidate: &'a radroots_storage::command::PrivateEnvelopeCandidate, + ) -> radroots_transport::BoxFuture< + 'a, + Result< + radroots_storage::inventory::StockEnvelopeOutcome, + radroots_storage::command::OperationError, + >, + > { + Box::pin(async move { + self.worker + .capture_stock_envelope(request, proof, candidate.retained_copy()) + .await + }) + } + fn stock_envelope( + &self, + request: radroots_storage::inventory::StockEnvelopeRequest, + proof: AttachmentProof, + ) -> radroots_transport::BoxFuture< + '_, + Result< + Option<radroots_storage::inventory::StockEnvelopeSource>, + radroots_storage::command::OperationError, + >, + > { + Box::pin(self.worker.stock_envelope(request, proof)) + } fn prepare_stock_acceptance( &self, request: StockAcceptanceRequest, diff --git a/crates/storage_sqlite/src/workspace/stock/consent.rs b/crates/storage_sqlite/src/workspace/stock/consent.rs @@ -422,7 +422,7 @@ pub(in crate::workspace) async fn issue( proof: AttachmentProof, ) -> Result<StockConsentOutcome, OperationError> { let access = turn.access(&binding, minimum, &proof).await?; - if !matches!(access.authority.catalog, 7..=15) { + if !matches!(access.authority.catalog, 7..=16) { return Err(OperationError::UnsupportedOperation); } if !access.record.administrator() @@ -683,7 +683,7 @@ pub(in crate::workspace) async fn query( proof: AttachmentProof, ) -> Result<Option<StockConsentReceipt>, OperationError> { let access = turn.access(&binding, minimum, &proof).await?; - if !matches!(access.authority.catalog, 7..=15) { + if !matches!(access.authority.catalog, 7..=16) { return Err(OperationError::UnsupportedOperation); } if !access.record.administrator() && access.current.input().grant_id() != subject { diff --git a/crates/storage_sqlite/src/workspace/stock/consent/test_provider.rs b/crates/storage_sqlite/src/workspace/stock/consent/test_provider.rs @@ -74,7 +74,7 @@ impl KeyWrapping for Provider { fn wrap<'a>(&'a self, request: WrapRequest<'a>) -> BoxFuture<'a, Result<WrappedSecret, Error>> { Box::pin(async move { let result = self.inner.wrap(request).await; - if request.context().payload_schema().as_str() == "radroots.seller_stock.authority.v15" + if request.context().payload_schema().as_str() == "radroots.seller_stock.authority.v16" { self.gate.pause(); } diff --git a/crates/storage_sqlite/src/workspace/stock/envelope.rs b/crates/storage_sqlite/src/workspace/stock/envelope.rs @@ -0,0 +1,348 @@ +//! Obtained original outers on the stock owner. Capture grants no transport right. +use super::*; +use inventory::{Turn, checkpoint}; +use radroots_domain::{CheckpointDigest, EnvelopeEventId, OperationId, PublicKey}; +use radroots_storage::{ + command::{CommitAttempt, OperationError, PrivateEnvelopeCandidate}, + inventory::{ + InventoryCheckpointRef, StockEnvelopeOutcome, StockEnvelopeReceipt, StockEnvelopeRequest, + StockEnvelopeSource, + }, +}; +use sha2::{Digest, Sha256}; +use sqlx::Row; +use zeroize::Zeroizing; +mod codec; +mod projection; +pub(super) use codec::read_entry; +use codec::{Original, Retained}; +pub(super) use projection::{Projection, raw_projection}; + +async fn selected_entry( + connection: &mut SqliteConnection, + actor: PublicKey, + operation: OperationId, + recipient: PublicKey, +) -> Result<Option<OperationId>, OperationError> { + let row: Option<Vec<u8>> = sqlx::query_scalar( + "SELECT entry_id FROM stock_envelopes WHERE actor=? AND operation_id=? AND recipient=?", + ) + .bind(actor.as_bytes().as_slice()) + .bind(operation.as_bytes().as_slice()) + .bind(recipient.as_bytes().as_slice()) + .fetch_optional(connection) + .await + .map_err(|_| OperationError::RecoveryRequired)?; + row.map(|bytes| { + bytes + .try_into() + .map(OperationId::from_bytes) + .map_err(|_| OperationError::RecoveryRequired) + }) + .transpose() +} +async fn lookup( + turn: &mut Turn<'_>, + request: &StockEnvelopeRequest, +) -> Result<Option<Retained>, OperationError> { + let Some(entry) = selected_entry( + turn.connection, + turn.selected.participant(), + request.operation_id(), + request.recipient(), + ) + .await? + else { + return Ok(None); + }; + let retained = Box::pin(read_entry( + turn.connection, + turn.selected, + turn.crypto, + request.binding(), + entry, + )) + .await? + .ok_or(OperationError::RecoveryRequired)?; + let receipt = retained.source.receipt(); + if receipt.attempt().operation_id() != request.operation_id() + || receipt.attempt().command_digest() != request.command_digest() + || receipt.recipient() != request.recipient() + { + return Err(OperationError::RecoveryRequired); + } + Ok(Some(retained)) +} +async fn full_proof( + turn: &mut Turn<'_>, + request: &StockEnvelopeRequest, + authority: &Authority, +) -> Result<checkpoint::MinimumProof, OperationError> { + let requested = Box::pin(checkpoint::authenticate_minimum( + turn.connection, + turn.selected, + turn.crypto, + request.binding(), + authority, + request.minimum_checkpoint(), + )) + .await?; + if request.minimum_checkpoint().revision().get() == 0 { + return Ok(requested); + } + let zero = InventoryCheckpointRef::from_host_metadata( + turn.selected.authority(), + Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?, + None, + )?; + Box::pin(checkpoint::authenticate_minimum( + turn.connection, + turn.selected, + turn.crypto, + request.binding(), + authority, + zero, + )) + .await +} + +pub(in crate::workspace) async fn capture( + turn: &mut Turn<'_>, + request: StockEnvelopeRequest, + proof: AttachmentProof, + candidate: PrivateEnvelopeCandidate, +) -> Result<StockEnvelopeOutcome, OperationError> { + if *turn.uncertain { + return Err(OperationError::RecoveryRequired); + } + turn.namespace.validate()?; + let input = request.binding().input(); + if !SellerAuthoritySelection::from_attachment(turn.selected).matches_attachment(input) { + return Err(OperationError::Unauthorized); + } + let (authority, record, root, grant) = super::read(turn.connection, input, turn.crypto).await?; + if authority.catalog != 16 { + return Err(OperationError::UnsupportedOperation); + } + record.authenticate_retained_capture(input, &proof)?; + let witness = Box::pin(full_proof(turn, &request, &authority)).await?; + let original = Box::pin(Original::load(turn, &request)).await?; + if original.evidence.frame.header.actor != request.binding().actor() + || candidate.recipient() != request.recipient() + || original + .installation + .receipt + .publication_work() + .iter() + .any(|work| work.recipient() == candidate.author()) + { + return Err(OperationError::InvalidEvidence); + } + candidate.with_wrapping_key(|key| { + original.evidence.value.with_json(|seal| { + super::super::delivery::containment::verify( + candidate.raw_json(), + candidate.author(), + key, + request.recipient(), + seal, + original.evidence.value.receipt().digest(), + ) + }) + })?; + // The complete protected chain proves natural index absence as well as + // presence. A missing projection is recovery evidence, never recapture. + let existing = Box::pin(lookup(turn, &request)).await?; + let before = authority + .inventory + .ok_or(OperationError::RecoveryRequired)?; + let (frame, updated, receipt) = if let Some(old) = &existing { + if old.source.with_json(|raw| raw != candidate.raw_json()) + || old.source.receipt().evidence() != original.evidence.value.receipt() + || old.source.receipt().installation_transaction_id() + != original + .installation + .receipt + .attempt() + .transaction_id() + .ok_or(OperationError::RecoveryRequired)? + { + return Err(OperationError::IdempotencyConflict); + } + (None, None, old.source.receipt()) + } else { + let revision = Sequence::new( + before + .revision + .get() + .checked_add(1) + .ok_or(WorkspaceError::Capacity)?, + ) + .map_err(|_| WorkspaceError::Capacity)?; + let body = codec::encode(&original, &request, &candidate); + let mut header = ledger::Header { + version: 5, + family: ledger::Family::Envelope, + namespace: SellerAuthoritySelection::from_attachment(turn.selected), + actor: original.evidence.frame.header.actor, + operation: OperationId::from_bytes(super::super::random()?), + digest: request.command_digest(), + transaction: TransactionId::from_bytes(super::super::random()?), + revision, + previous: before.head, + head: CheckpointDigest::from_bytes([0; 32]), + pool: None, + order: original.installation.frame.header.order, + related_operation: Some(request.operation_id()), + subject_grant: None, + bytes: 0, + parts: 0, + body_digest: [0; 32], + }; + header.head = codec::head(&header, &body); + let receipt = StockEnvelopeReceipt::from_committed_store( + &request, + CommitAttempt::new( + input.workspace_id(), + request.operation_id(), + request.command_digest(), + Some(header.transaction), + ), + original.evidence.value.receipt(), + original.installation.frame.header.transaction, + candidate.event_id(), + *candidate.digest(), + InventoryCheckpointRef::from_host_metadata( + input.authority(), + revision, + Some(header.head), + )?, + )?; + let frame = ledger::prepare(turn.crypto, turn.selected, header, &body).await?; + let mut next = authority.clone(); + next.inventory = Some(InventoryCheckpoint { + revision, + head: Some(frame.header.head), + ..before + }); + let updated = turn + .crypto + .seal_stock_root(turn.selected, next.catalog, &next.encode()?) + .await?; + (Some(frame), Some(updated), receipt) + }; + let mut tx = turn + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let changed: Result<(), OperationError> = async { + turn.namespace.validate()?; + super::unchanged(&mut tx, input, authority.catalog, &root, &grant).await?; + record.authenticate_retained_capture(input, &proof)?; + witness.confirm(&mut tx).await?; + if let Some(old) = &existing { + old.confirm(&mut tx).await?; + } + if let Some(frame) = &frame { + frame.insert_record(&mut tx).await?; + Projection { + operation: request.operation_id(), + recipient: request.recipient(), + } + .insert(&mut tx, input.participant(), frame.header.operation) + .await?; + let result = sqlx::query( + "UPDATE stock_authority SET protected=? WHERE singleton=1 AND protected=?", + ) + .bind(updated.as_ref().ok_or(OperationError::RecoveryRequired)?) + .bind(&root) + .execute(&mut *tx) + .await + .map_err(|_| OperationError::StorageFailure)?; + if result.rows_affected() != 1 { + return Err(OperationError::RecoveryRequired); + } + } + turn.namespace.validate()?; + Ok(()) + } + .await; + if let Err(error) = changed { + if tx.rollback().await.is_err() { + *turn.uncertain = true; + return Ok(StockEnvelopeOutcome::Unknown(receipt.attempt())); + } + return Err(error); + } + let done = if frame.is_some() { + tx.commit().await + } else { + tx.rollback().await + }; + if done.is_err() { + *turn.uncertain = true; + return Ok(StockEnvelopeOutcome::Unknown(receipt.attempt())); + } + Ok(StockEnvelopeOutcome::Committed(Box::new(receipt))) +} + +pub(in crate::workspace) async fn read( + turn: &mut Turn<'_>, + request: StockEnvelopeRequest, + proof: AttachmentProof, +) -> Result<Option<StockEnvelopeSource>, OperationError> { + if *turn.uncertain { + return Err(OperationError::RecoveryRequired); + } + turn.namespace.validate()?; + let catalog: i64 = sqlx::query_scalar("PRAGMA user_version") + .fetch_one(&mut *turn.connection) + .await + .map_err(|_| OperationError::RecoveryRequired)?; + if catalog != 16 { + return Err(OperationError::UnsupportedOperation); + } + let auth = Box::pin(super::signing_plan::load( + turn, + request.binding(), + request.minimum_checkpoint(), + request.operation_id(), + request.command_digest(), + &proof, + )) + .await?; + if auth.access.authority.catalog != 16 { + return Err(OperationError::UnsupportedOperation); + } + let witness = Box::pin(full_proof(turn, &request, &auth.access.authority)).await?; + let retained = Box::pin(lookup(turn, &request)).await?; + let mut tx = turn + .connection + .begin_with("BEGIN IMMEDIATE") + .await + .map_err(|_| OperationError::StorageFailure)?; + let checked: Result<(), OperationError> = async { + turn.namespace.validate()?; + auth.confirm(&mut tx, turn.namespace, turn.clock, &proof, false) + .await?; + witness.confirm(&mut tx).await?; + if let Some(retained) = &retained { + retained.confirm(&mut tx).await?; + } + turn.namespace.validate()?; + Ok(()) + } + .await; + if let Err(error) = checked { + if tx.rollback().await.is_err() { + *turn.uncertain = true; + } + return Err(error); + } + if tx.rollback().await.is_err() { + *turn.uncertain = true; + return Err(OperationError::RecoveryRequired); + } + Ok(retained.map(|value| value.source)) +} diff --git a/crates/storage_sqlite/src/workspace/stock/envelope/codec.rs b/crates/storage_sqlite/src/workspace/stock/envelope/codec.rs @@ -0,0 +1,342 @@ +//! Fixed original source references and bounded exact outer bytes. +use super::*; +use nostr::JsonUtil; +use std::num::NonZeroU64; +const MAGIC: &[u8; 8] = b"RDSTE01\0"; +const PREFIX: usize = 213; +const MAX_RAW: usize = 65536; + +pub(super) struct Original { + pub installation: inventory::installation::Retained, + pub evidence: issuance::evidence::Retained, +} +impl Original { + pub(super) async fn load( + turn: &mut Turn<'_>, + request: &StockEnvelopeRequest, + ) -> Result<Self, OperationError> { + let entry:Option<Vec<u8>>=sqlx::query_scalar("SELECT installation_entry FROM stock_publication_work WHERE actor=? AND operation_id=? AND recipient=?") + .bind(turn.selected.participant().as_bytes().as_slice()).bind(request.operation_id().as_bytes().as_slice()).bind(request.recipient().as_bytes().as_slice()) + .fetch_optional(&mut *turn.connection).await.map_err(|_|OperationError::RecoveryRequired)?; + let entry = OperationId::from_bytes( + entry + .ok_or(OperationError::MissingHistory)? + .try_into() + .map_err(|_| OperationError::RecoveryRequired)?, + ); + let installation = Box::pin(inventory::installation::read_entry( + turn.connection, + turn.selected, + turn.crypto, + entry, + )) + .await? + .ok_or(OperationError::RecoveryRequired)?; + let summary = Box::pin(issuance::evidence::selection::Summary::read( + turn.connection, + turn.selected.participant(), + request.operation_id(), + request.recipient(), + )) + .await?; + let selected = summary.first.ok_or(OperationError::MissingHistory)?; + let evidence = Box::pin(issuance::evidence::selection::open( + turn.connection, + turn.selected, + turn.crypto, + request.operation_id(), + request.recipient(), + selected, + )) + .await?; + let value = Self { + installation, + evidence, + }; + value.validate()?; + if value.installation.receipt.attempt().operation_id() != request.operation_id() + || value.evidence.value.receipt().attempt().command_digest() != request.command_digest() + { + return Err(OperationError::InvalidEvidence); + } + Ok(value) + } + fn validate(&self) -> Result<(), OperationError> { + let installed = &self.installation; + let evidence = &self.evidence; + let source = evidence.value.receipt(); + if installed.receipt.attempt().operation_id() != source.attempt().operation_id() + || installed.frame.header.digest != source.attempt().command_digest() + || installed.frame.header.order != evidence.frame.header.order + || installed.frame.header.actor.participant() + != evidence.frame.header.actor.participant() + { + return Err(OperationError::RecoveryRequired); + } + let work = installed + .receipt + .publication_work() + .iter() + .find(|work| work.recipient() == source.recipient()) + .ok_or(OperationError::RecoveryRequired)?; + let event = evidence + .value + .with_json(|raw| nostr::Event::from_json(raw)) + .map_err(|_| OperationError::RecoveryRequired)?; + if work.seal_id() != &event.id.to_bytes() + || work + .evidence_digest() + .is_some_and(|digest| digest != source.digest()) + { + return Err(OperationError::RecoveryRequired); + } + Ok(()) + } +} +fn reference(out: &mut Vec<u8>, header: &ledger::Header) { + out.extend_from_slice(header.operation.as_bytes()); + out.extend_from_slice(header.transaction.as_bytes()); + out.extend_from_slice(header.head.as_bytes()); +} +pub(super) fn encode( + original: &Original, + request: &StockEnvelopeRequest, + candidate: &PrivateEnvelopeCandidate, +) -> Zeroizing<Vec<u8>> { + let mut bytes = Zeroizing::new(Vec::with_capacity(PREFIX + candidate.raw_json().len())); + bytes.extend_from_slice(MAGIC); + reference(&mut bytes, &original.installation.frame.header); + reference(&mut bytes, &original.evidence.frame.header); + bytes.push(u8::from(request.observed_at_unix_ms().is_some())); + bytes.extend_from_slice( + &request + .observed_at_unix_ms() + .map(NonZeroU64::get) + .unwrap_or(0) + .to_be_bytes(), + ); + bytes.extend_from_slice(&(candidate.raw_json().len() as u32).to_be_bytes()); + bytes.extend_from_slice(candidate.raw_json().as_bytes()); + bytes +} +pub(super) fn head(header: &ledger::Header, body: &[u8]) -> CheckpointDigest { + let mut hash = Sha256::new(); + hash.update(b"radroots.seller_stock.envelope_chain.v1\0"); + hash.update(header.namespace.workspace_id().as_bytes()); + hash.update(header.namespace.authority().executor_id().as_bytes()); + hash.update( + header + .namespace + .authority() + .generation() + .get() + .to_be_bytes(), + ); + hash.update(header.namespace.route_digest().as_bytes()); + hash.update(header.actor.participant().as_bytes()); + hash.update( + header + .actor + .grant_id() + .map(|v| *v.as_bytes()) + .unwrap_or([0; 32]), + ); + hash.update(header.operation.as_bytes()); + hash.update(header.digest.as_bytes()); + hash.update(header.transaction.as_bytes()); + hash.update(header.revision.get().to_be_bytes()); + hash.update([u8::from(header.previous.is_some())]); + hash.update(header.previous.map(|v| *v.as_bytes()).unwrap_or([0; 32])); + hash.update(Sha256::digest(body)); + CheckpointDigest::from_bytes(hash.finalize().into()) +} +pub(in crate::workspace::stock) struct Retained { + frame: ledger::Retained, + pub source: StockEnvelopeSource, + projection: Projection, +} +impl Retained { + pub(super) async fn confirm( + &self, + connection: &mut SqliteConnection, + ) -> Result<(), OperationError> { + ledger::confirm_header(connection, &self.frame.header, &self.frame.protected).await?; + ledger::confirm_parts( + connection, + self.frame.header.actor, + self.frame.header.operation, + self.frame.parts_witness, + self.frame.header.parts, + ) + .await?; + self.projection + .confirm( + connection, + self.frame.header.actor.participant(), + self.frame.header.operation, + ) + .await?; + let count: i64 = sqlx::query_scalar( + "SELECT count(*) FROM stock_movements WHERE actor=? AND operation_id=?", + ) + .bind(self.frame.header.actor.participant().as_bytes().as_slice()) + .bind(self.frame.header.operation.as_bytes().as_slice()) + .fetch_one(connection) + .await + .map_err(|_| OperationError::RecoveryRequired)?; + if count != 0 { + return Err(OperationError::RecoveryRequired); + } + Ok(()) + } + pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { + ledger::HistoryEntry { + envelope: Some(self.projection), + aborted: None, + preparation: None, + header: self.frame.header, + protected: self.frame.protected, + parts_witness: self.frame.parts_witness, + pools: vec![], + mapping: None, + movements: vec![], + signing: None, + installation: None, + local_record: None, + } + } +} +pub(in crate::workspace::stock) async fn read_entry( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + binding: &WorkspaceBinding, + entry: OperationId, +) -> Result<Option<Retained>, OperationError> { + let Some(frame) = Box::pin(ledger::read(connection, input, crypto, entry)).await? else { + return Ok(None); + }; + Ok(Some( + Box::pin(decode_entry(connection, input, crypto, binding, frame)).await?, + )) +} +async fn decode_entry( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + binding: &WorkspaceBinding, + frame: ledger::Retained, +) -> Result<Retained, OperationError> { + let body = frame.body.as_slice(); + if frame.header.family != ledger::Family::Envelope + || !(PREFIX..=PREFIX + MAX_RAW).contains(&body.len()) + || &body[..8] != MAGIC + { + return Err(OperationError::RecoveryRequired); + } + let array = |start, end| { + body[start..end] + .try_into() + .map_err(|_| OperationError::RecoveryRequired) + }; + let installation = Box::pin(inventory::installation::read_entry( + connection, + input, + crypto, + OperationId::from_bytes(array(8, 40)?), + )) + .await? + .ok_or(OperationError::RecoveryRequired)?; + let evidence = Box::pin(issuance::evidence::read_entry( + connection, + input, + crypto, + OperationId::from_bytes(array(104, 136)?), + )) + .await? + .ok_or(OperationError::RecoveryRequired)?; + let original = Original { + installation, + evidence, + }; + original.validate()?; + let h = &frame.header; + if original.installation.frame.header.transaction != TransactionId::from_bytes(array(40, 72)?) + || original.installation.frame.header.head != CheckpointDigest::from_bytes(array(72, 104)?) + || original.evidence.frame.header.transaction != TransactionId::from_bytes(array(136, 168)?) + || original.evidence.frame.header.head != CheckpointDigest::from_bytes(array(168, 200)?) + || h.actor != original.evidence.frame.header.actor + || h.digest != original.evidence.frame.header.digest + || h.related_operation != original.evidence.frame.header.related_operation + || h.order != original.evidence.frame.header.order + || h.revision <= original.evidence.frame.header.revision + || h.revision <= original.installation.frame.header.revision + || head(h, body) != h.head + { + return Err(OperationError::RecoveryRequired); + } + let at = u64::from_be_bytes( + body[201..209] + .try_into() + .map_err(|_| OperationError::RecoveryRequired)?, + ); + let observed = match (body[200], at) { + (0, 0) => None, + (1, n) if n > 0 => NonZeroU64::new(n), + _ => return Err(OperationError::RecoveryRequired), + }; + let length = u32::from_be_bytes( + body[209..213] + .try_into() + .map_err(|_| OperationError::RecoveryRequired)?, + ) as usize; + if length != body.len() - PREFIX { + return Err(OperationError::RecoveryRequired); + } + let raw = std::str::from_utf8(&body[PREFIX..]).map_err(|_| OperationError::RecoveryRequired)?; + let event = nostr::Event::from_json(raw).map_err(|_| OperationError::RecoveryRequired)?; + let evidence = original.evidence.value.receipt(); + let operation = h + .related_operation + .ok_or(OperationError::RecoveryRequired)?; + // This is actual caller context plus decoded selectors, never a fabricated + // authenticated binding. The public receipt remains metadata, not authority. + let report = StockEnvelopeRequest::new( + binding.clone(), + InventoryCheckpointRef::from_host_metadata( + input.authority(), + Sequence::new(0).map_err(|_| OperationError::RecoveryRequired)?, + None, + )?, + operation, + h.digest, + evidence.recipient(), + observed, + )?; + let receipt = StockEnvelopeReceipt::from_committed_store( + &report, + CommitAttempt::new( + input.workspace_id(), + operation, + h.digest, + Some(h.transaction), + ), + evidence, + original.installation.frame.header.transaction, + EnvelopeEventId::from_bytes(event.id.to_bytes()), + Sha256::digest(raw.as_bytes()).into(), + InventoryCheckpointRef::from_host_metadata(input.authority(), h.revision, Some(h.head))?, + )?; + let source = StockEnvelopeSource::from_retained_wire(receipt, raw) + .map_err(|_| OperationError::RecoveryRequired)?; + let retained = Retained { + projection: Projection { + operation, + recipient: receipt.recipient(), + }, + frame, + source, + }; + retained.confirm(connection).await?; + Ok(retained) +} diff --git a/crates/storage_sqlite/src/workspace/stock/envelope/projection.rs b/crates/storage_sqlite/src/workspace/stock/envelope/projection.rs @@ -0,0 +1,83 @@ +//! Exact body-derived cache witness. It cannot authorize capture or transport. +use super::*; +#[derive(Clone, Copy)] +pub(in crate::workspace::stock) struct Projection { + pub operation: OperationId, + pub recipient: PublicKey, +} +impl Projection { + pub(in crate::workspace::stock) fn digest( + self, + actor: PublicKey, + entry: OperationId, + ) -> [u8; 32] { + let mut hash = Sha256::new(); + hash.update(b"radroots.seller_stock.envelope_projection.v1\0"); + hash.update(actor.as_bytes()); + hash.update(entry.as_bytes()); + hash.update(self.operation.as_bytes()); + hash.update(self.recipient.as_bytes()); + hash.finalize().into() + } + pub(super) async fn insert( + self, + connection: &mut SqliteConnection, + actor: PublicKey, + entry: OperationId, + ) -> Result<(), OperationError> { + sqlx::query( + "INSERT INTO stock_envelopes(actor,operation_id,recipient,entry_id) VALUES(?,?,?,?)", + ) + .bind(actor.as_bytes().as_slice()) + .bind(self.operation.as_bytes().as_slice()) + .bind(self.recipient.as_bytes().as_slice()) + .bind(entry.as_bytes().as_slice()) + .execute(connection) + .await + .map_err(|_| OperationError::StorageFailure)?; + Ok(()) + } + pub(super) async fn confirm( + self, + connection: &mut SqliteConnection, + actor: PublicKey, + entry: OperationId, + ) -> Result<(), OperationError> { + if raw_projection(connection, actor.as_bytes(), entry.as_bytes()).await? + != self.digest(actor, entry) + { + return Err(OperationError::RecoveryRequired); + } + Ok(()) + } +} +pub(in crate::workspace::stock) async fn raw_projection( + connection: &mut SqliteConnection, + actor: &[u8; 32], + entry: &[u8; 32], +) -> Result<[u8; 32], OperationError> { + let row = sqlx::query( + "SELECT operation_id,recipient FROM stock_envelopes WHERE actor=? AND entry_id=?", + ) + .bind(actor.as_slice()) + .bind(entry.as_slice()) + .fetch_optional(connection) + .await + .map_err(|_| OperationError::RecoveryRequired)? + .ok_or(OperationError::RecoveryRequired)?; + let field = |index| { + row.try_get::<Vec<u8>, _>(index) + .map_err(|_| OperationError::RecoveryRequired)? + .try_into() + .map_err(|_| OperationError::RecoveryRequired) + }; + Ok(Projection { + operation: OperationId::from_bytes(field(0)?), + recipient: PublicKey::from_bytes(field(1)?) + .map_err(|_| OperationError::RecoveryRequired)?, + } + .digest( + PublicKey::from_bytes(*actor).map_err(|_| OperationError::RecoveryRequired)?, + OperationId::from_bytes(*entry), + )) +} diff --git a/crates/storage_sqlite/src/workspace/stock/grants.rs b/crates/storage_sqlite/src/workspace/stock/grants.rs @@ -324,7 +324,7 @@ pub(in crate::workspace) async fn change( } let (mut authority, record, old_root, old_grant) = super::read(turn.connection, change.caller.input(), turn.crypto).await?; - if !(4..=15).contains(&authority.catalog) { + if !(4..=16).contains(&authority.catalog) { return Err(WorkspaceError::Unauthorized); } let catalog = authority.catalog; diff --git a/crates/storage_sqlite/src/workspace/stock/inventory.rs b/crates/storage_sqlite/src/workspace/stock/inventory.rs @@ -189,14 +189,14 @@ impl Turn<'_> { return Err(OperationError::UnsupportedOperation); } - let minimum_proof = authenticate_minimum( + let minimum_proof = Box::pin(authenticate_minimum( self.connection, self.selected, self.crypto, ¤t, &authority, minimum, - ) + )) .await?; self.namespace.validate()?; Ok(Access { @@ -452,7 +452,7 @@ impl Turn<'_> { access.authority.authorization, mapping.as_ref().map(|value| value.effect), )?; - let framed = if matches!(catalog, 7..=15) { + let framed = if matches!(catalog, 7..=16) { Some( super::ledger::prepare( self.crypto, @@ -866,7 +866,7 @@ pub(super) async fn read_operation( .fetch_one(&mut *connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - let (frame, request, decoded) = if matches!(catalog, 7..=15) { + let (frame, request, decoded) = if matches!(catalog, 7..=16) { let retained = super::ledger::read(connection, input, crypto, operation) .await? .ok_or(OperationError::RecoveryRequired)?; diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/abort.rs b/crates/storage_sqlite/src/workspace/stock/inventory/abort.rs @@ -160,7 +160,7 @@ async fn abort_authorized( proof: AttachmentProof, auth: signing_plan::Authorized, ) -> Result<StockAbortOutcome, OperationError> { - if !matches!(auth.access.authority.catalog, 13..=15) { + if !matches!(auth.access.authority.catalog, 13..=16) { return Err(OperationError::UnsupportedOperation); } let (old, identity) = Box::pin(existing(turn, &auth.access, request.operation_id())).await?; @@ -381,7 +381,7 @@ pub(in crate::workspace) async fn read( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 13..=15) { + if !matches!(auth.access.authority.catalog, 13..=16) { return Err(OperationError::UnsupportedOperation); } let (old, identity) = Box::pin(existing(turn, &auth.access, request.operation_id())).await?; diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/abort/codec.rs b/crates/storage_sqlite/src/workspace/stock/inventory/abort/codec.rs @@ -305,6 +305,7 @@ impl Retained { } pub fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, preparation: None, local_record: None, installation: None, diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/abort/proof.rs b/crates/storage_sqlite/src/workspace/stock/inventory/abort/proof.rs @@ -150,7 +150,7 @@ pub(super) async fn prepare( recipient: *recipient, }), Some(original) => { - if auth.access.authority.catalog != 15 { + if !matches!(auth.access.authority.catalog, 15..=16) { return Err(OperationError::RecoveryRequired); } if at < original.report.claimed_at() { @@ -180,7 +180,7 @@ pub(super) async fn prepare( } } } - let payload = if auth.access.authority.catalog == 15 { + let payload = if matches!(auth.access.authority.catalog, 15..=16) { Payload::Current(Box::new( encoded .try_into() @@ -217,7 +217,7 @@ pub(super) async fn read( .fetch_one(&mut *connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - if catalog != 15 { + if !matches!(catalog, 15..=16) { return Err(OperationError::RecoveryRequired); } for (proof, expected) in proofs.iter().zip(recipients) { diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/acceptance.rs b/crates/storage_sqlite/src/workspace/stock/inventory/acceptance.rs @@ -236,7 +236,7 @@ pub(in crate::workspace) async fn admit( let access = turn .access(request.binding(), request.minimum_checkpoint(), &proof) .await?; - if !matches!(access.authority.catalog, 9..=15) { + if !matches!(access.authority.catalog, 9..=16) { return Err(OperationError::UnsupportedOperation); } let command = request.command(); diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/acceptance/codec.rs b/crates/storage_sqlite/src/workspace/stock/inventory/acceptance/codec.rs @@ -104,6 +104,7 @@ impl Retained { ) }); ledger::HistoryEntry { + envelope: None, aborted: None, preparation, local_record: None, @@ -323,7 +324,7 @@ pub(in crate::workspace::stock) async fn read( .map_err(|_| OperationError::RecoveryRequired)?; let operation_keyed = match catalog { 1..=12 => false, - 13..=15 => true, + 13..=16 => true, _ => return Err(OperationError::RecoveryRequired), }; let actor = header.actor.participant(); diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/checkpoint.rs b/crates/storage_sqlite/src/workspace/stock/inventory/checkpoint.rs @@ -68,7 +68,9 @@ impl MinimumProof { let related: Option<Vec<u8>> = row .try_get(2) .map_err(|_| OperationError::RecoveryRequired)?; - if !(0..=if catalog >= 15 { + if !(0..=if catalog >= 16 { + 10 + } else if catalog >= 15 { 9 } else if catalog >= 14 { 8 @@ -90,6 +92,12 @@ impl MinimumProof { super::super::ledger::raw_parts_witness(connection, &actor, &operation).await?; hash.update([parts]); hash.update(witness); + if family == 10 { + hash.update( + super::super::envelope::raw_projection(connection, &actor, &operation) + .await?, + ); + } if family == 7 { hash.update( super::abort::raw_projection(connection, &actor, &operation).await?, @@ -151,7 +159,7 @@ pub(in crate::workspace::stock) async fn authenticate_order_state( authority: &Authority, order: radroots_domain::OrderId, ) -> Result<super::super::order_state::Checked, OperationError> { - if !matches!(authority.catalog, 13..=15) { + if !matches!(authority.catalog, 13..=16) { return Err(OperationError::UnsupportedOperation); } let current = authority @@ -220,7 +228,7 @@ pub(super) async fn authenticate_order_absence( let current = authority .inventory .ok_or(OperationError::UnsupportedOperation)?; - if !matches!(authority.catalog, 9..=15) { + if !matches!(authority.catalog, 9..=16) { return Err(OperationError::UnsupportedOperation); } let upper = current.revision.get(); @@ -302,7 +310,7 @@ pub(in crate::workspace::stock) async fn authenticate_identity( let current = authority .inventory .ok_or(OperationError::UnsupportedOperation)?; - let multipart = matches!(authority.catalog, 7..=15); + let multipart = matches!(authority.catalog, 7..=16); let upper = current.revision.get(); let mut previous = current.head; let mut hash = history_hash(multipart); @@ -479,7 +487,7 @@ pub(in crate::workspace::stock) async fn authenticate_minimum_with_local( minimum.revision().get().max(1) }; let upper = current.revision.get(); - let multipart = matches!(authority.catalog, 7..=15); + let multipart = matches!(authority.catalog, 7..=16); let mut hash = history_hash(multipart); let mut expected = current.head; for revision in (lower..=upper).rev() { @@ -743,7 +751,7 @@ async fn authenticate_projected( .ok_or(OperationError::UnsupportedOperation)?; let upper = current.revision.get(); let mut expected = current.head; - let multipart = matches!(authority.catalog, 7..=15); + let multipart = matches!(authority.catalog, 7..=16); let mut hash = history_hash(multipart); for revision in (1..=upper).rev() { let id:Vec<u8>=sqlx::query_scalar("SELECT operation_id FROM stock_operations WHERE revision=? AND length(operation_id)=32").bind(revision as i64).fetch_optional(&mut *connection).await.map_err(|_|OperationError::RecoveryRequired)?.ok_or(OperationError::RecoveryRequired)?; diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/codec.rs b/crates/storage_sqlite/src/workspace/stock/inventory/codec.rs @@ -120,7 +120,7 @@ pub(super) fn encode_operation( String::from_utf8(clear.to_vec()).map_err(|_| OperationError::StorageFailure)?, ) .map_err(|_| OperationError::StorageFailure)?; - if matches!(catalog, 5..=15) { + if matches!(catalog, 5..=16) { return super::encode(&OperationV4 { version: 4, authorization: authorization.ok_or(OperationError::RecoveryRequired)?, @@ -158,7 +158,7 @@ fn decode_inner( catalog: i64, allow_mapping: bool, ) -> Result<(InventoryRequest, OperationData), OperationError> { - if matches!(catalog, 5..=15) { + if matches!(catalog, 5..=16) { let decoded: OperationV4<'_> = super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?; if decoded.version != 4 { diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/installation.rs b/crates/storage_sqlite/src/workspace/stock/inventory/installation.rs @@ -5,7 +5,7 @@ use checkpoint::{EntryIdentity, IdentityTarget}; use radroots_domain::{OrderId, PublicKey, RecordId, stock::StockTicks}; mod codec; mod projection; -pub(in crate::workspace::stock) use codec::read_entry; +pub(in crate::workspace::stock) use codec::{Retained, read_entry}; pub(in crate::workspace::stock) use projection::{Projection, raw_projection}; /// Captured original-operation selector; does not establish permission or evidence. @@ -222,7 +222,7 @@ pub(in crate::workspace) async fn install( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 12..=15) { + if !matches!(auth.access.authority.catalog, 12..=16) { return Err(OperationError::UnsupportedOperation); } let (old, identity) = Box::pin(existing(turn, &auth, request.operation)).await?; @@ -481,7 +481,7 @@ pub(in crate::workspace) async fn read( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 12..=15) { + if !matches!(auth.access.authority.catalog, 12..=16) { return Err(OperationError::UnsupportedOperation); } let (retained, identity) = Box::pin(existing(turn, &auth, request.operation)).await?; diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/installation/codec.rs b/crates/storage_sqlite/src/workspace/stock/inventory/installation/codec.rs @@ -91,6 +91,16 @@ pub(in crate::workspace::stock) async fn read_entry( let Some(frame) = ledger::read(connection, input, crypto, operation).await? else { return Ok(None); }; + Ok(Some( + Box::pin(read_frame(connection, input, crypto, frame)).await?, + )) +} +async fn read_frame( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + frame: ledger::Retained, +) -> Result<Retained, OperationError> { if frame.header.family != ledger::Family::Installation { return Err(OperationError::RecoveryRequired); } @@ -248,7 +258,7 @@ pub(in crate::workspace::stock) async fn read_entry( movements, }; retained.confirm(connection).await?; - Ok(Some(retained)) + Ok(retained) } impl Retained { pub(in crate::workspace::stock) async fn confirm( @@ -292,6 +302,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, aborted: None, preparation: None, local_record: None, diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/mapping.rs b/crates/storage_sqlite/src/workspace/stock/inventory/mapping.rs @@ -58,7 +58,7 @@ impl MappingRead { } } pub(super) fn require(access: &Access, key: OptionMappingKey) -> Result<(), OperationError> { - if !(5..=15).contains(&access.authority.catalog) { + if !(5..=16).contains(&access.authority.catalog) { return Err(OperationError::UnsupportedOperation); } if !access.record.administrator() diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/plan.rs b/crates/storage_sqlite/src/workspace/stock/inventory/plan.rs @@ -3,7 +3,7 @@ use super::*; pub(super) fn record_version(catalog: i64) -> Result<u8, OperationError> { match catalog { 2 => Ok(1), - 3..=15 => Ok(2), + 3..=16 => Ok(2), _ => Err(OperationError::RecoveryRequired), } } diff --git a/crates/storage_sqlite/src/workspace/stock/issuance.rs b/crates/storage_sqlite/src/workspace/stock/issuance.rs @@ -46,6 +46,12 @@ pub(super) async fn check_counts(connection: &mut SqliteConnection) -> Result<() return Err(WorkspaceError::RecoveryRequired); } } + if catalog >= 16 { + let (rows, entries): (i64,i64) = sqlx::query_as("SELECT (SELECT count(*) FROM stock_envelopes),(SELECT count(*) FROM stock_operations WHERE family=10)").fetch_one(&mut *connection).await.map_err(|_|WorkspaceError::RecoveryRequired)?; + if rows != entries { + return Err(WorkspaceError::RecoveryRequired); + } + } if catalog >= 15 { let (rows,entries):(i64,i64)=sqlx::query_as("SELECT (SELECT count(*) FROM stock_replay_admissions),(SELECT count(*) FROM stock_operations WHERE family=9)") .fetch_one(&mut *connection).await.map_err(|_|WorkspaceError::RecoveryRequired)?; diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/claim.rs b/crates/storage_sqlite/src/workspace/stock/issuance/claim.rs @@ -256,14 +256,14 @@ pub(in crate::workspace) async fn claim( request: StockClaimRequest, proof: AttachmentProof, ) -> Result<StockClaimOutcome, OperationError> { - let mut auth = signing_plan::load( + let mut auth = Box::pin(signing_plan::load( turn, &request.binding, request.minimum, request.operation, request.digest, &proof, - ) + )) .await?; if auth.access.authority.catalog < 11 { return Err(OperationError::UnsupportedOperation); @@ -424,14 +424,14 @@ pub(in crate::workspace) async fn read( request: StockClaimRequest, proof: AttachmentProof, ) -> Result<Option<StockSigningAttempt>, OperationError> { - let auth = signing_plan::load( + let auth = Box::pin(signing_plan::load( turn, &request.binding, request.minimum, request.operation, request.digest, &proof, - ) + )) .await?; if auth.access.authority.catalog < 11 { return Err(OperationError::UnsupportedOperation); diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/claim/codec.rs b/crates/storage_sqlite/src/workspace/stock/issuance/claim/codec.rs @@ -147,6 +147,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, aborted: None, preparation: None, local_record: None, diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/evidence.rs b/crates/storage_sqlite/src/workspace/stock/issuance/evidence.rs @@ -9,7 +9,7 @@ use std::num::NonZeroU64; use zeroize::Zeroizing; mod codec; pub(in crate::workspace::stock) mod selection; -pub(in crate::workspace::stock) use codec::read_entry; +pub(in crate::workspace::stock) use codec::{Retained, read_entry}; const MAX_RAW: usize = 64 * 1024; /// Independently checked obtained bytes. A store error leaves the caller's @@ -114,15 +114,25 @@ pub(in crate::workspace) async fn page( after: Option<Sequence>, proof: AttachmentProof, ) -> Result<StockEvidencePage, OperationError> { - let auth = super::super::signing_plan::load( + let auth = Box::pin(super::super::signing_plan::load( turn, &request.binding, request.minimum, request.operation, request.digest, &proof, - ) + )) .await?; + Box::pin(page_authorized(turn, request, after, proof, auth)).await +} + +async fn page_authorized( + turn: &mut Turn<'_>, + request: StockClaimRequest, + after: Option<Sequence>, + proof: AttachmentProof, + auth: super::super::signing_plan::Authorized, +) -> Result<StockEvidencePage, OperationError> { if auth.access.authority.catalog < 11 { return Err(OperationError::UnsupportedOperation); } @@ -131,14 +141,14 @@ pub(in crate::workspace) async fn page( Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?, None, )?; - let witness = checkpoint::authenticate_minimum( + let witness = Box::pin(checkpoint::authenticate_minimum( turn.connection, turn.selected, turn.crypto, &auth.access.current, &auth.access.authority, minimum, - ) + )) .await?; let selected = selection::page( turn.connection, @@ -220,12 +230,12 @@ pub(in crate::workspace) async fn capture( return Err(OperationError::UnsupportedOperation); } record.authenticate_retained_capture(input, &proof)?; - let original = claim::read_entry( + let original = Box::pin(claim::read_entry( turn.connection, turn.selected, turn.crypto, OperationId::from_bytes(*supplied.signing_attempt_id().as_bytes()), - ) + )) .await? .ok_or(OperationError::RecoveryRequired)?; if original.frame.header.actor != supplied.binding().actor() @@ -259,14 +269,14 @@ pub(in crate::workspace) async fn capture( Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?, None, )?; - let witness = checkpoint::authenticate_minimum( + let witness = Box::pin(checkpoint::authenticate_minimum( turn.connection, turn.selected, turn.crypto, supplied.binding(), &authority, minimum, - ) + )) .await?; let existing = lookup( turn.connection, @@ -416,15 +426,25 @@ pub(in crate::workspace) async fn read( digest: [u8; 32], proof: AttachmentProof, ) -> Result<Option<StockCapturedEvidence>, OperationError> { - let auth = super::super::signing_plan::load( + let auth = Box::pin(super::super::signing_plan::load( turn, &request.binding, request.minimum, request.operation, request.digest, &proof, - ) + )) .await?; + Box::pin(read_authorized(turn, request, digest, proof, auth)).await +} + +async fn read_authorized( + turn: &mut Turn<'_>, + request: StockClaimRequest, + digest: [u8; 32], + proof: AttachmentProof, + auth: super::super::signing_plan::Authorized, +) -> Result<Option<StockCapturedEvidence>, OperationError> { if auth.access.authority.catalog < 11 { return Err(OperationError::UnsupportedOperation); } @@ -447,14 +467,14 @@ pub(in crate::workspace) async fn read( Sequence::new(0).map_err(|_| WorkspaceError::InvalidInput)?, None, )?; - let witness = checkpoint::authenticate_minimum( + let witness = Box::pin(checkpoint::authenticate_minimum( turn.connection, turn.selected, turn.crypto, &auth.access.current, &auth.access.authority, minimum, - ) + )) .await?; let mut tx = turn .connection diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/evidence/codec.rs b/crates/storage_sqlite/src/workspace/stock/issuance/evidence/codec.rs @@ -74,6 +74,16 @@ pub(in crate::workspace::stock) async fn read_entry( let Some(frame) = ledger::read(connection, input, crypto, entry).await? else { return Ok(None); }; + Ok(Some( + Box::pin(read_frame(connection, input, crypto, frame)).await?, + )) +} +async fn read_frame( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + frame: ledger::Retained, +) -> Result<Retained, OperationError> { let body = frame.body.as_slice(); if frame.header.family != ledger::Family::SigningEvidence || !(PREFIX..=PREFIX + MAX_RAW).contains(&body.len()) @@ -173,7 +183,7 @@ pub(in crate::workspace::stock) async fn read_entry( local_record: Box::new(local_record), }; retained.confirm(connection).await?; - Ok(Some(retained)) + Ok(retained) } impl Retained { pub(in crate::workspace::stock) async fn confirm( @@ -211,6 +221,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, aborted: None, preparation: None, installation: None, diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/recovery.rs b/crates/storage_sqlite/src/workspace/stock/issuance/recovery.rs @@ -26,7 +26,7 @@ pub struct StockExactReplayPermit { admission: Option<StockReplayAdmissionReceipt>, } impl StockExactReplayPermit { - /// Confirmed first replay admission in current catalog15. Retained13/14 + /// Confirmed first replay admission in tracked catalogs15–16. Retained13/14 /// permits are explicitly untracked and return None; no report authorizes /// stock release, a fresh first dispatch or another replay invocation. pub const fn admission(&self) -> Option<StockReplayAdmissionReceipt> { @@ -81,7 +81,7 @@ pub(in crate::workspace) async fn authorize( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 13..=15) { + if !matches!(auth.access.authority.catalog, 13..=16) { return Err(OperationError::UnsupportedOperation); } let plan = auth.plan.as_ref().ok_or(OperationError::StaleState)?; diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/replay_admission.rs b/crates/storage_sqlite/src/workspace/stock/issuance/replay_admission.rs @@ -163,6 +163,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, aborted: None, preparation: None, local_record: None, diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/unissued.rs b/crates/storage_sqlite/src/workspace/stock/issuance/unissued.rs @@ -224,6 +224,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, aborted: None, preparation: None, installation: None, @@ -337,7 +338,7 @@ pub(in crate::workspace) async fn capture( } let (authority, record, root, grant) = super::super::read(turn.connection, input, turn.crypto).await?; - if !matches!(authority.catalog, 14..=15) { + if !matches!(authority.catalog, 14..=16) { return Err(OperationError::UnsupportedOperation); } record.authenticate_retained_capture(input, &proof)?; @@ -491,7 +492,7 @@ pub(in crate::workspace) async fn read( &proof, ) .await?; - if !matches!(auth.access.authority.catalog, 14..=15) { + if !matches!(auth.access.authority.catalog, 14..=16) { return Err(OperationError::UnsupportedOperation); } let witness = Box::pin(checkpoint::authenticate_minimum( diff --git a/crates/storage_sqlite/src/workspace/stock/ledger.rs b/crates/storage_sqlite/src/workspace/stock/ledger.rs @@ -25,6 +25,7 @@ pub(super) enum Family { AbortedPreparation, OriginalUnissued, ReplayAdmission, + Envelope, } #[derive(Serialize, Deserialize)] @@ -81,6 +82,7 @@ impl Header { | Family::AbortedPreparation | Family::OriginalUnissued | Family::ReplayAdmission + | Family::Envelope ) != self.related_operation.is_some() || (self.family == Family::Consent) != self.subject_grant.is_some() || self.bytes == 0 @@ -337,6 +339,7 @@ impl Family { Self::AbortedPreparation => 7, Self::OriginalUnissued => 8, Self::ReplayAdmission => 9, + Self::Envelope => 10, } } } @@ -444,6 +447,7 @@ pub(super) struct MovementWitness { /// A completely decoded original effect. Only the owning family decoder /// supplies its pool effects and movement witnesses; SQL indexes do not. pub(super) struct HistoryEntry { + pub envelope: Option<super::envelope::Projection>, pub aborted: Option<super::inventory::abort::Projection>, pub preparation: Option<[u8; 32]>, pub header: Header, @@ -492,6 +496,7 @@ pub(super) async fn history( _ => return Err(OperationError::RecoveryRequired), }; Ok(Some(HistoryEntry { + envelope: None, aborted: None, preparation: None, local_record: None, @@ -527,6 +532,7 @@ pub(super) async fn history( return Err(OperationError::RecoveryRequired); } Ok(Some(HistoryEntry { + envelope: None, aborted: None, preparation: None, local_record: None, @@ -540,7 +546,7 @@ pub(super) async fn history( movements: vec![], })) } - Some(2) if matches!(authority.catalog, 9..=15) => Ok(Some( + Some(2) if matches!(authority.catalog, 9..=16) => Ok(Some( Box::pin(super::inventory::acceptance::codec::read( connection, input, crypto, operation, )) @@ -549,7 +555,7 @@ pub(super) async fn history( .into_history(), )), Some(2) => Err(OperationError::UnsupportedOperation), - Some(3) if matches!(authority.catalog, 10..=15) => { + Some(3) if matches!(authority.catalog, 10..=16) => { let retained = Box::pin(super::signing_plan::read_entry( connection, input, crypto, operation, )) @@ -573,7 +579,7 @@ pub(super) async fn history( .ok_or(OperationError::RecoveryRequired)? .into_history(), )), - Some(6) if matches!(authority.catalog, 12..=15) => Ok(Some( + Some(6) if matches!(authority.catalog, 12..=16) => Ok(Some( Box::pin(super::inventory::installation::read_entry( connection, input, crypto, operation, )) @@ -605,6 +611,14 @@ pub(super) async fn history( .ok_or(OperationError::RecoveryRequired)? .into_history(), )), + Some(10) if authority.catalog >= 16 => Ok(Some( + Box::pin(super::envelope::read_entry( + connection, input, crypto, binding, operation, + )) + .await? + .ok_or(OperationError::RecoveryRequired)? + .into_history(), + )), Some(_) => Err(OperationError::RecoveryRequired), } } @@ -700,6 +714,15 @@ impl HistoryEntry { } (_, None) => {} } + match (header.family, self.envelope) { + (Family::Envelope, Some(projection)) => { + hash.update(projection.digest(header.actor.participant(), header.operation)) + } + (Family::Envelope, None) | (_, Some(_)) => { + return Err(OperationError::RecoveryRequired); + } + (_, None) => {} + } if (header.family == Family::SigningEvidence) != self.local_record.is_some() { return Err(OperationError::RecoveryRequired); } diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/abort_failures_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/abort_failures_tests.rs @@ -122,7 +122,7 @@ async fn late_order_grant_pool_and_unrelated_preparation_changes_never_commit_a_ .unwrap(); provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .abort_proven_unissued(selected, r.enrollment.proof()), @@ -279,7 +279,7 @@ async fn losing_abort_observer_drains_original_admitted_work_before_reopen() { let selected = abort_request(&r, &receipt).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .abort_proven_unissued(selected, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/acceptance_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/acceptance_tests.rs @@ -450,7 +450,7 @@ async fn lost_observer_retains_actual_two_pool_preparation_and_drain_precedes_re let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .prepare_stock_acceptance(request, r.enrollment.proof()), @@ -504,7 +504,7 @@ async fn source_mapping_pool_order_and_grant_rows_are_rechecked_after_final_prov let mut raw = database(&r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .prepare_stock_acceptance(request, r.enrollment.proof()), @@ -541,7 +541,7 @@ async fn final_clock_recheck_refuses_expiry_and_rollback_after_protection_withou let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .prepare_stock_acceptance(request, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/claim_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/claim_tests.rs @@ -329,7 +329,7 @@ async fn lost_claim_observer_drains_and_reopens_as_existing_without_a_permit() { let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .claim_stock_signing_attempt(request, r.enrollment.proof()), @@ -406,7 +406,7 @@ async fn claim_rechecks_original_plan_projection_grant_root_and_clock_after_prov let mut raw = database(&r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .claim_stock_signing_attempt(request, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/claimed_abort_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/claimed_abort_tests.rs @@ -157,12 +157,8 @@ async fn current_claimed_cleanup_releases_exact_originals_once_and_preserves_eve } #[tokio::test] async fn unrecorded_original_refusal_and_untracked_retained_claims_never_authorize_cleanup() { - for catalog in [14, 15] { - let r = if catalog == 14 { - Rig::with_catalog(catalog).await - } else { - Rig::new().await - }; + for catalog in [14, 15, 16] { + let r = Rig::with_catalog(catalog).await; let receipt = Box::pin(replay_ready(&r)).await; refused(&r, &receipt, peer(&r, &receipt), catalog == 14).await; let before = inventory_minimum(&r).await; @@ -404,7 +400,7 @@ async fn lost_claimed_cleanup_waiter_drains_before_reopen_and_keeps_original_pro let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin(r.store.abort_proven_unissued(request, r.enrollment.proof())); assert!(futures::poll!(pending.as_mut()).is_pending()); provider.gate.entered(); diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/envelope_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/envelope_tests.rs @@ -0,0 +1,481 @@ +//! Actual protected capture, original containment, reopen and transaction faults. +use super::*; +use nostr::JsonUtil; +use radroots_nostr::signing::{OsNonceSource, gift_wrap::wrap_retained_seal}; +use radroots_storage::{ + command::PrivateEnvelopeCandidate, + inventory::{InventoryStore, StockEnvelopeOutcome, StockEnvelopeReceipt, StockEnvelopeRequest}, +}; +use std::num::NonZeroU64; +fn proof(r: &Rig) -> AttachmentProof { + r.enrollment + .proof() + .with_secret(|bytes| AttachmentProof::from_protected_input(bytes)) + .unwrap() +} +async fn envelope_request( + r: &Rig, + original: &StockAcceptanceReceipt, + recipient: PublicKey, + observed: Option<NonZeroU64>, +) -> StockEnvelopeRequest { + StockEnvelopeRequest::new( + r.binding.clone(), + inventory_minimum(r).await, + original.attempt().operation_id(), + original.attempt().command_digest(), + recipient, + observed, + ) + .unwrap() +} +fn wrapped(raw: &str, recipient: PublicKey) -> PrivateEnvelopeCandidate { + let pending = wrap_retained_seal( + raw, + recipient, + NonZeroU64::new(150000).unwrap(), + &OsNonceSource, + ) + .unwrap(); + pending.with_wrapping_key(|key| { + PrivateEnvelopeCandidate::from_obtained(recipient, pending.signed_json(), key).unwrap() + }) +} +async fn setup( + catalog: i64, +) -> ( + Rig, + StockAcceptanceReceipt, + StockInstallationReceipt, + PrivateEnvelopeCandidate, +) { + let r = Rig::with_catalog(catalog).await; + let original = ready(&r).await; + let recipient = peer(&r, &original); + let (claim, raw) = obtained(&r, &original, recipient).await; + let capture = StockEvidenceCapture::verify(r.binding.clone(), &claim, &raw, None).unwrap(); + r.store + .capture_stock_signing_evidence(&capture, r.enrollment.proof()) + .await + .unwrap(); + let installed = installed(install(&r, &original).await.unwrap()); + let candidate = wrapped(&raw, recipient); + (r, original, installed, candidate) +} +fn committed_envelope(value: StockEnvelopeOutcome) -> StockEnvelopeReceipt { + let StockEnvelopeOutcome::Committed(value) = value else { + panic!("confirmed original outer required") + }; + *value +} +async fn capture( + r: &Rig, + original: &StockAcceptanceReceipt, + candidate: &PrivateEnvelopeCandidate, + at: Option<NonZeroU64>, +) -> Result<StockEnvelopeOutcome, OperationError> { + r.store + .capture_stock_envelope( + envelope_request(r, original, candidate.recipient(), at).await, + proof(r), + candidate, + ) + .await +} +#[tokio::test] +async fn original_peer_and_self_outers_retain_exact_bytes_once_and_reopen_without_new_stock_effects() + { + let (mut r, original, installed, peer_outer) = setup(16).await; + let before = inventory_minimum(&r).await; + let first = committed_envelope(capture(&r, &original, &peer_outer, None).await.unwrap()); + assert_eq!( + first.installation_transaction_id(), + installed.attempt().transaction_id().unwrap() + ); + assert_eq!(first.event_id(), peer_outer.event_id()); + assert!(first.observed_at_unix_ms().is_none()); + assert_eq!( + first.checkpoint().revision().get(), + before.revision().get() + 1 + ); + assert_eq!( + committed_envelope( + capture(&r, &original, &peer_outer, NonZeroU64::new(151000)) + .await + .unwrap() + ), + first + ); + let self_recipient = r.enrollment.input().participant(); + let (claim, raw) = obtained(&r, &original, self_recipient).await; + let signed = StockEvidenceCapture::verify(r.binding.clone(), &claim, &raw, None).unwrap(); + r.store + .capture_stock_signing_evidence(&signed, r.enrollment.proof()) + .await + .unwrap(); + let self_outer = wrapped(&raw, self_recipient); + let second = committed_envelope( + capture(&r, &original, &self_outer, NonZeroU64::new(151000)) + .await + .unwrap(), + ); + assert_eq!(second.observed_at_unix_ms(), NonZeroU64::new(151000)); + let checkpoint = inventory_minimum(&r).await; + reopen_plain(&mut r).await; + for (outer, receipt) in [(&peer_outer, first), (&self_outer, second)] { + let source = r + .store + .stock_envelope( + envelope_request(&r, &original, outer.recipient(), None).await, + proof(&r), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(source.receipt(), receipt); + source.with_json(|raw| assert_eq!(raw, outer.raw_json())); + } + assert_eq!(inventory_minimum(&r).await, checkpoint); + balances(&r, 80, 9, 0, 1, 3).await; + balances(&r, 81, 8, 0, 2, 3).await; + r.store.close().await.unwrap(); +} +#[tokio::test] +async fn invalid_key_wrong_original_seal_and_rewrapping_cannot_replace_the_original_outer() { + let (r, original, _, outer) = setup(16).await; + let before = inventory_minimum(&r).await; + let wrong = + PrivateEnvelopeCandidate::from_obtained(outer.recipient(), outer.raw_json(), &[9; 32]) + .unwrap(); + assert!(matches!( + capture(&r, &original, &wrong, None).await, + Err(OperationError::InvalidEvidence) + )); + let self_recipient = r.enrollment.input().participant(); + let (claim, raw) = obtained(&r, &original, self_recipient).await; + let signed = StockEvidenceCapture::verify(r.binding.clone(), &claim, &raw, None).unwrap(); + r.store + .capture_stock_signing_evidence(&signed, r.enrollment.proof()) + .await + .unwrap(); + // A valid peer carrier containing the other recipient's actual seal. + let keys = nostr::Keys::new(nostr::SecretKey::from_slice(&[7; 32]).unwrap()); + let recipient = nostr::PublicKey::from_slice(outer.recipient().as_bytes()).unwrap(); + let event = nostr::UnsignedEvent { + id: None, + pubkey: keys.public_key(), + created_at: nostr::Timestamp::from_secs(150), + kind: nostr::Kind::Custom(1059), + tags: nostr::Tags::from_list(vec![nostr::Tag::public_key(recipient)]), + content: nostr::nips::nip44::encrypt( + keys.secret_key(), + &recipient, + &raw, + nostr::nips::nip44::Version::V2, + ) + .unwrap(), + } + .sign_with_keys(&keys) + .unwrap(); + let foreign = + PrivateEnvelopeCandidate::from_obtained(outer.recipient(), &event.as_json(), &[7; 32]) + .unwrap(); + assert!(matches!( + capture(&r, &original, &foreign, None).await, + Err(OperationError::InvalidEvidence) + )); + assert!(inventory_minimum(&r).await.revision() > before.revision()); + let first = committed_envelope(capture(&r, &original, &outer, None).await.unwrap()); + let evidence = r + .store + .stock_signing_evidence_page( + crate::workspace::StockClaimRequest::new( + r.binding.clone(), + inventory_minimum(&r).await, + original.attempt().operation_id(), + original.attempt().command_digest(), + outer.recipient(), + ), + None, + r.enrollment.proof(), + ) + .await + .unwrap(); + let source = r + .store + .stock_signing_evidence( + crate::workspace::StockClaimRequest::new( + r.binding.clone(), + inventory_minimum(&r).await, + original.attempt().operation_id(), + original.attempt().command_digest(), + outer.recipient(), + ), + *evidence.receipts()[0].digest(), + r.enrollment.proof(), + ) + .await + .unwrap() + .unwrap(); + let replacement = source.with_json(|raw| wrapped(raw, outer.recipient())); + assert!(matches!( + capture(&r, &original, &replacement, None).await, + Err(OperationError::IdempotencyConflict) + )); + assert_eq!( + committed_envelope(capture(&r, &original, &outer, None).await.unwrap()), + first + ); + r.store.close().await.unwrap(); +} +#[tokio::test] +async fn original_credential_captures_after_clock_loss_expiry_or_revocation_but_never_grants_read_access() + { + for mode in ["clock_unavailable", "expired", "revoked"] { + let (r, original, _, outer) = setup(16).await; + let selected = envelope_request(&r, &original, outer.recipient(), None).await; + let read = envelope_request(&r, &original, outer.recipient(), None).await; + match mode { + "clock_unavailable" => r.clock.0.store(u64::MAX, Ordering::SeqCst), + "expired" => r.clock.0.store(350, Ordering::SeqCst), + "revoked" => { + r.store + .revoke_stock_grant( + r.binding.clone(), + inventory_minimum(&r).await, + r.enrollment.proof(), + radroots_domain::TransactionId::from_bytes([99; 32]), + r.enrollment.input().grant_id(), + ) + .await + .unwrap(); + } + _ => unreachable!(), + }; + let saved = committed_envelope( + r.store + .capture_stock_envelope(selected, proof(&r), &outer) + .await + .unwrap(), + ); + assert!(saved.observed_at_unix_ms().is_none()); + assert!(r.store.stock_envelope(read, proof(&r)).await.is_err()); + r.clock.0.store(150, Ordering::SeqCst); + if mode != "revoked" { + let found = r + .store + .stock_envelope( + envelope_request(&r, &original, outer.recipient(), None).await, + proof(&r), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(found.receipt(), saved); + } + r.store.close().await.unwrap(); + } +} +#[tokio::test] +async fn late_original_or_projection_damage_cannot_commit_a_partially_authenticated_outer() { + for statement in [ + "UPDATE stock_grants SET protected=randomblob(length(protected))", + "UPDATE stock_signing_evidence SET evidence_digest=randomblob(32)", + "UPDATE stock_publication_work SET recipient=randomblob(32)", + "UPDATE stock_operations SET protected=randomblob(length(protected)) WHERE family=3", + ] { + let (mut r, original, _, outer) = setup(16).await; + let selected = envelope_request(&r, &original, outer.recipient(), None).await; + let provider = reopen(&mut r).await; + let mut db = database(&r).await; + provider + .gate + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); + let mut pending = Box::pin(r.store.capture_stock_envelope(selected, proof(&r), &outer)); + assert!(futures::poll!(pending.as_mut()).is_pending()); + provider.gate.entered(); + sqlx::raw_sql(statement).execute(&mut db).await.unwrap(); + provider.gate.release(); + assert!(pending.await.is_err(), "{statement}"); + let rows: i64 = sqlx::query_scalar("SELECT count(*) FROM stock_envelopes") + .fetch_one(&mut db) + .await + .unwrap(); + assert_eq!(rows, 0); + db.close().await.unwrap(); + r.store.close().await.unwrap(); + } +} +#[tokio::test] +async fn absent_installation_is_not_outer_capture_authority_and_retained15_never_upgrades() { + let r = Rig::new().await; + let original = ready(&r).await; + let recipient = peer(&r, &original); + let (claim, raw) = obtained(&r, &original, recipient).await; + let signed = StockEvidenceCapture::verify(r.binding.clone(), &claim, &raw, None).unwrap(); + r.store + .capture_stock_signing_evidence(&signed, r.enrollment.proof()) + .await + .unwrap(); + let outer = wrapped(&raw, recipient); + assert!(matches!( + capture(&r, &original, &outer, None).await, + Err(OperationError::MissingHistory) + )); + r.store.close().await.unwrap(); + let (mut r, original, _, outer) = setup(15).await; + assert!(matches!( + capture(&r, &original, &outer, None).await, + Err(OperationError::UnsupportedOperation) + )); + assert!(matches!( + r.store + .stock_envelope( + envelope_request(&r, &original, outer.recipient(), None).await, + proof(&r) + ) + .await, + Err(OperationError::UnsupportedOperation) + )); + reopen_plain(&mut r).await; + let catalog: i64 = sqlx::query_scalar("PRAGMA user_version") + .fetch_one(&mut database(&r).await) + .await + .unwrap(); + assert_eq!(catalog, 15); + r.store.close().await.unwrap(); +} +#[tokio::test] +async fn every_outer_write_failure_retains_caller_bytes_and_no_partial_row_or_stock_effect() { + for statement in [ + "CREATE TRIGGER fault BEFORE INSERT ON stock_operations WHEN NEW.family=10 BEGIN SELECT RAISE(ABORT,'fixture'); END", + "CREATE TRIGGER fault BEFORE INSERT ON stock_operation_parts BEGIN SELECT RAISE(ABORT,'fixture'); END", + "CREATE TRIGGER fault BEFORE INSERT ON stock_envelopes BEGIN SELECT RAISE(ABORT,'fixture'); END", + "CREATE TRIGGER fault BEFORE UPDATE ON stock_authority BEGIN SELECT RAISE(ABORT,'fixture'); END", + ] { + let (r, original, _, outer) = setup(16).await; + let before = inventory_minimum(&r).await; + let raw = outer.raw_json().to_owned(); + let mut db = database(&r).await; + sqlx::raw_sql(statement).execute(&mut db).await.unwrap(); + assert_eq!( + capture(&r, &original, &outer, None) + .await + .unwrap_err() + .code(), + "storage_failure" + ); + assert_eq!(outer.raw_json(), raw); + assert_eq!(inventory_minimum(&r).await, before); + let rows: i64 = sqlx::query_scalar("SELECT count(*) FROM stock_envelopes") + .fetch_one(&mut db) + .await + .unwrap(); + assert_eq!(rows, 0); + sqlx::raw_sql("DROP TRIGGER fault") + .execute(&mut db) + .await + .unwrap(); + committed_envelope(capture(&r, &original, &outer, None).await.unwrap()); + balances(&r, 80, 9, 0, 1, 3).await; + db.close().await.unwrap(); + r.store.close().await.unwrap(); + } +} +#[tokio::test] +async fn uncertain_outer_commit_fences_owner_and_original_bytes_can_be_retained_after_reopen() { + let (mut r, original, _, outer) = setup(16).await; + let before = inventory_minimum(&r).await; + let mut db = database(&r).await; + sqlx::raw_sql("CREATE TABLE deferred_fault(value INTEGER REFERENCES stock_authority(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fault AFTER INSERT ON stock_envelopes BEGIN INSERT INTO deferred_fault VALUES(2); END").execute(&mut db).await.unwrap(); + let StockEnvelopeOutcome::Unknown(anchor) = capture(&r, &original, &outer, None).await.unwrap() + else { + panic!("unknown COMMIT") + }; + assert!(anchor.transaction_id().is_some()); + let selected = StockEnvelopeRequest::new( + r.binding.clone(), + before, + original.attempt().operation_id(), + original.attempt().command_digest(), + outer.recipient(), + None, + ) + .unwrap(); + assert_eq!( + r.store + .stock_envelope(selected, proof(&r)) + .await + .unwrap_err() + .code(), + "recovery_required" + ); + r.store.close().await.unwrap(); + sqlx::raw_sql("DROP TRIGGER fault;DROP TABLE deferred_fault") + .execute(&mut db) + .await + .unwrap(); + db.close().await.unwrap(); + reopen_plain(&mut r).await; + assert_eq!(inventory_minimum(&r).await, before); + committed_envelope(capture(&r, &original, &outer, None).await.unwrap()); + r.store.close().await.unwrap(); +} +#[tokio::test] +async fn lost_outer_observer_is_drained_and_one_exact_record_survives_reopen() { + let (mut r, original, _, outer) = setup(16).await; + let selected = envelope_request(&r, &original, outer.recipient(), None).await; + let provider = reopen(&mut r).await; + provider + .gate + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); + let mut pending = Box::pin(r.store.capture_stock_envelope(selected, proof(&r), &outer)); + assert!(futures::poll!(pending.as_mut()).is_pending()); + provider.gate.entered(); + drop(pending); + provider.gate.release(); + reopen_plain(&mut r).await; + let found = r + .store + .stock_envelope( + envelope_request(&r, &original, outer.recipient(), None).await, + proof(&r), + ) + .await + .unwrap() + .unwrap(); + found.with_json(|raw| assert_eq!(raw, outer.raw_json())); + assert_eq!( + committed_envelope(capture(&r, &original, &outer, None).await.unwrap()), + found.receipt() + ); + r.store.close().await.unwrap(); +} +#[tokio::test] +async fn protected_outer_or_cache_damage_is_recovery_evidence_not_missing_work() { + for statement in [ + "DELETE FROM stock_envelopes", + "UPDATE stock_envelopes SET recipient=randomblob(32)", + "UPDATE stock_operation_parts SET protected=randomblob(length(protected)) WHERE operation_id IN(SELECT operation_id FROM stock_operations WHERE family=10)", + "UPDATE stock_operations SET protected=randomblob(length(protected)) WHERE family=10", + "DELETE FROM stock_publication_work", + "UPDATE stock_signing_evidence SET evidence_digest=randomblob(32)", + ] { + let (r, original, _, outer) = setup(16).await; + committed_envelope(capture(&r, &original, &outer, None).await.unwrap()); + let selected = envelope_request(&r, &original, outer.recipient(), None).await; + sqlx::raw_sql(statement) + .execute(&mut database(&r).await) + .await + .unwrap(); + assert_eq!( + r.store + .stock_envelope(selected, proof(&r)) + .await + .unwrap_err() + .code(), + "recovery_required" + ); + r.store.close().await.unwrap(); + } +} diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/evidence_reconciliation_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/evidence_reconciliation_tests.rs @@ -128,13 +128,13 @@ async fn refused_stock_recovery_preflight_keeps_writer_and_cached_original_autho remove_fault(&mut database, true).await; provider .gate - .arm(Direction::Unwrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Unwrap, "radroots.seller_stock.authority.v16", 0); let mut recovery = Box::pin(reconcile(&r, &request)); assert!(futures::poll!(recovery.as_mut()).is_pending()); provider.gate.entered(); // The authorized old snapshot has already been read. Future schema then // refuses read-only preflight after close; no migration or repair occurs. - sqlx::raw_sql("PRAGMA user_version=16") + sqlx::raw_sql("PRAGMA user_version=17") .execute(&mut database) .await .unwrap(); @@ -146,7 +146,7 @@ async fn refused_stock_recovery_preflight_keeps_writer_and_cached_original_autho .fetch_one(&mut database) .await .unwrap(), - 16 + 17 ); let wrong = AttachmentProof::from_protected_input(&[99; 32]).unwrap(); assert!( @@ -155,7 +155,7 @@ async fn refused_stock_recovery_preflight_keeps_writer_and_cached_original_autho .is_err() ); request.with_json(|value| assert_eq!(value, raw)); - sqlx::raw_sql("PRAGMA user_version=15") + sqlx::raw_sql("PRAGMA user_version=16") .execute(&mut database) .await .unwrap(); @@ -184,7 +184,7 @@ async fn lost_reconciliation_observer_drains_the_actual_replacement_and_original remove_fault(&mut database, true).await; provider .gate - .arm(Direction::Unwrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Unwrap, "radroots.seller_stock.authority.v16", 0); let mut recovery = Box::pin(reconcile(&r, &request)); assert!(futures::poll!(recovery.as_mut()).is_pending()); provider.gate.entered(); diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/evidence_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/evidence_tests.rs @@ -446,7 +446,7 @@ async fn lost_capture_observer_drains_owned_input_and_reopens_the_exact_original let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .capture_stock_signing_evidence(&evidence, r.enrollment.proof()), @@ -534,7 +534,7 @@ async fn capture_rechecks_original_claim_plan_root_and_grant_after_provider_io() let mut rawdb = database(&r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .capture_stock_signing_evidence(&evidence, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/exact_recovery_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/exact_recovery_tests.rs @@ -437,7 +437,7 @@ async fn replay_authorization_rechecks_actual_late_root_grant_plan_claim_backing let mut raw = database(&r).await; provider .gate - .arm(Direction::Unwrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Unwrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .authorize_stock_exact_replay(request, r.enrollment.proof()), @@ -491,7 +491,7 @@ async fn observer_loss_drains_authorization_without_signing_or_a_new_claim_and_r let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Unwrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Unwrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .authorize_stock_exact_replay(request, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/installation_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/installation_tests.rs @@ -1,6 +1,8 @@ //! Real T2 transactions over original T0/T1 and authenticated peer evidence. #[path = "abort_tests.rs"] mod aborts; +#[path = "envelope_tests.rs"] +mod envelopes; #[path = "local_history_tests.rs"] mod local_history; #[cfg(unix)] @@ -412,7 +414,7 @@ async fn lost_t2_observer_drains_and_reopens_one_atomic_installation() { let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .install_stock_agreement(selected, r.enrollment.proof()), @@ -451,7 +453,7 @@ async fn late_t2_backing_or_grant_or_evidence_change_never_commits_partial_insta let mut db = database(&r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .install_stock_agreement(selected, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/process_death_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/process_death_tests.rs @@ -1,4 +1,4 @@ -//! Private process-death qualification of S, T2 and proven-unissued abort. +//! Private process-death qualification of S, T2, original outer retention and abort. use super::*; use radroots_domain::{CommandDigest, OperationId}; use radroots_secrets::{ @@ -22,6 +22,7 @@ enum Edge { Capture, Install, Abort, + Envelope, } #[derive(serde::Serialize, serde::Deserialize)] #[serde(deny_unknown_fields)] @@ -32,6 +33,7 @@ struct Control { digest: CommandDigest, recipient: PublicKey, raw: String, + wrapping_key: Option<[u8; 32]>, edge: Edge, before: bool, } @@ -142,7 +144,7 @@ async fn stock_process_child() { .unwrap() .inventory_checkpoint() .unwrap(); - let capture = if matches!(control.edge, Edge::Abort) { + let capture = if matches!(control.edge, Edge::Abort | Edge::Envelope) { None } else { let report = store @@ -164,7 +166,7 @@ async fn stock_process_child() { if control.before { provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); } let work = async { match control.edge { @@ -192,6 +194,36 @@ async fn stock_process_child() { .unwrap(), ); } + Edge::Envelope => { + let candidate = radroots_storage::command::PrivateEnvelopeCandidate::from_obtained( + control.recipient, + &control.raw, + &control.wrapping_key.unwrap(), + ) + .unwrap(); + let request = radroots_storage::inventory::StockEnvelopeRequest::new( + binding, + minimum, + control.operation, + control.digest, + control.recipient, + None, + ) + .unwrap(); + assert!(matches!( + radroots_storage::inventory::InventoryStore::capture_stock_envelope( + &store, + request, + proof + .with_secret(|bytes| AttachmentProof::from_protected_input(bytes)) + .unwrap(), + &candidate, + ) + .await + .unwrap(), + radroots_storage::inventory::StockEnvelopeOutcome::Committed(_), + )); + } Edge::Abort => { aborts::released( store @@ -225,9 +257,9 @@ async fn stock_process_child() { } #[tokio::test] -async fn process_death_before_and_after_capture_install_or_abort_preserves_original_stock_and_evidence() +async fn process_death_before_and_after_capture_install_abort_or_envelope_preserves_original_stock_and_evidence() { - for edge in [Edge::Capture, Edge::Install, Edge::Abort] { + for edge in [Edge::Capture, Edge::Install, Edge::Abort, Edge::Envelope] { for before in [true, false] { let mut r = Rig::new().await; let receipt = if matches!(edge, Edge::Abort) { @@ -236,13 +268,13 @@ async fn process_death_before_and_after_capture_install_or_abort_preserves_origi Box::pin(ready(&r)).await }; let recipient = peer(&r, &receipt); - let (report, raw) = if matches!(edge, Edge::Abort) { + let (report, mut raw) = if matches!(edge, Edge::Abort) { (None, String::new()) } else { let (report, raw) = Box::pin(obtained(&r, &receipt, recipient)).await; (Some(report), raw) }; - if matches!(edge, Edge::Install) { + if matches!(edge, Edge::Install | Edge::Envelope) { let capture = StockEvidenceCapture::verify( r.binding.clone(), report.as_ref().unwrap(), @@ -257,6 +289,20 @@ async fn process_death_before_and_after_capture_install_or_abort_preserves_origi .unwrap(), ); } + let wrapping_key = if matches!(edge, Edge::Envelope) { + installed(install(&r, &receipt).await.unwrap()); + let pending = radroots_nostr::signing::gift_wrap::wrap_retained_seal( + &raw, + recipient, + std::num::NonZeroU64::new(150000).unwrap(), + &radroots_nostr::signing::OsNonceSource, + ) + .unwrap(); + raw = pending.signed_json().to_owned(); + Some(pending.with_wrapping_key(|key| *key)) + } else { + None + }; let minimum = inventory_minimum(&r).await; let control = Control { input: r.enrollment.input().clone(), @@ -265,6 +311,7 @@ async fn process_death_before_and_after_capture_install_or_abort_preserves_origi digest: receipt.attempt().command_digest(), recipient, raw, + wrapping_key, edge, before, }; @@ -311,10 +358,11 @@ async fn process_death_before_and_after_capture_install_or_abort_preserves_origi .iter() .find(|value| value.recipient() == recipient) .unwrap(); - let has_evidence = - matches!(edge, Edge::Install) || (matches!(edge, Edge::Capture) && !before); + let has_evidence = matches!(edge, Edge::Install | Edge::Envelope) + || (matches!(edge, Edge::Capture) && !before); assert_eq!(peer.first_evidence().is_some(), has_evidence); - let has_installation = matches!(edge, Edge::Install) && !before; + let has_installation = + matches!(edge, Edge::Envelope) || (matches!(edge, Edge::Install) && !before); assert_eq!(snapshot.installation().is_some(), has_installation); let has_abort = matches!(edge, Edge::Abort) && !before; assert_eq!(snapshot.aborted().is_some(), has_abort); @@ -367,6 +415,64 @@ async fn process_death_before_and_after_capture_install_or_abort_preserves_origi .unwrap(), ); } + if matches!(edge, Edge::Envelope) { + let request = radroots_storage::inventory::StockEnvelopeRequest::new( + r.binding.clone(), + inventory_minimum(&r).await, + control.operation, + control.digest, + control.recipient, + None, + ) + .unwrap(); + let found = radroots_storage::inventory::InventoryStore::stock_envelope( + &r.store, + request, + r.enrollment + .proof() + .with_secret(|bytes| AttachmentProof::from_protected_input(bytes)) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(found.is_some(), !before); + if let Some(found) = &found { + found.with_json(|raw| assert_eq!(raw, control.raw)); + } + let candidate = radroots_storage::command::PrivateEnvelopeCandidate::from_obtained( + control.recipient, + &control.raw, + &control.wrapping_key.unwrap(), + ) + .unwrap(); + let request = radroots_storage::inventory::StockEnvelopeRequest::new( + r.binding.clone(), + inventory_minimum(&r).await, + control.operation, + control.digest, + control.recipient, + None, + ) + .unwrap(); + let radroots_storage::inventory::StockEnvelopeOutcome::Committed(saved) = + radroots_storage::inventory::InventoryStore::capture_stock_envelope( + &r.store, + request, + r.enrollment + .proof() + .with_secret(|bytes| AttachmentProof::from_protected_input(bytes)) + .unwrap(), + &candidate, + ) + .await + .unwrap() + else { + panic!("original outer must reconcile"); + }; + if let Some(found) = found { + assert_eq!(*saved, found.receipt()); + } + } let final_installation = installed(install(&r, &receipt).await.unwrap()); assert_eq!(final_installation.record_id(), receipt.rumor().record_id()); balances(&r, 80, 9, 0, 1, 3).await; diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/signing_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/signing_tests.rs @@ -354,7 +354,7 @@ async fn dropped_t1_observer_keeps_owned_work_and_close_drains_before_original_r let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .bind_stock_signing_intents(request, r.enrollment.proof()), @@ -408,7 +408,7 @@ async fn t1_final_permission_order_root_and_clock_witnesses_refuse_changes_after let mut raw = database(&r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin( r.store .bind_stock_signing_intents(request, r.enrollment.proof()), diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/test_provider.rs b/crates/storage_sqlite/src/workspace/stock/public_source/test_provider.rs @@ -157,7 +157,7 @@ async fn public_source_close_drains_admitted_work_after_observer_loss() { let minimum = r.minimum().await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut future = Box::pin(r.store.retain_stock_listing( r.binding.clone(), minimum, @@ -213,7 +213,7 @@ async fn public_source_writes_recheck_expiry_and_head_rows_after_actual_protecti let mut raw = database(&r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut future = Box::pin(r.store.retain_stock_listing( r.binding.clone(), minimum, diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/transaction.rs b/crates/storage_sqlite/src/workspace/stock/public_source/transaction.rs @@ -31,7 +31,7 @@ async fn access( } let (authority, record, root, grant) = super::super::read(turn.connection, binding.input(), turn.crypto).await?; - if !matches!(authority.catalog, 8..=15) || authority.public_sources.is_none() { + if !matches!(authority.catalog, 8..=16) || authority.public_sources.is_none() { return Err(OperationError::UnsupportedOperation); } let current = record.authenticate(binding.input(), proof, turn.clock.now()?)?; diff --git a/crates/storage_sqlite/src/workspace/stock/public_source/unissued_tests.rs b/crates/storage_sqlite/src/workspace/stock/public_source/unissued_tests.rs @@ -364,7 +364,7 @@ async fn lost_original_negative_append_waiter_drains_and_reopens_the_same_fact() let provider = reopen(&mut r).await; provider .gate - .arm(Direction::Wrap, "radroots.seller_stock.authority.v15", 0); + .arm(Direction::Wrap, "radroots.seller_stock.authority.v16", 0); let mut pending = Box::pin(capture(&r, &value)); assert!(futures::poll!(pending.as_mut()).is_pending()); provider.gate.entered(); diff --git a/crates/storage_sqlite/src/workspace/stock/schema.rs b/crates/storage_sqlite/src/workspace/stock/schema.rs @@ -1,7 +1,7 @@ //! Exact stock catalogs. Enrollment-only version1 is retained without upgrade. use super::WorkspaceError; use sqlx::SqliteConnection; -pub(super) const CURRENT: i64 = 15; +pub(super) const CURRENT: i64 = 16; const AUTHORITY: &str = "CREATE TABLE stock_authority (singleton INTEGER PRIMARY KEY CHECK(singleton=1), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT"; const GRANTS: &str = "CREATE TABLE stock_grants (grant_id BLOB PRIMARY KEY CHECK(length(grant_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID"; const POOLS: &str = "CREATE TABLE stock_pools (pool_id BLOB PRIMARY KEY CHECK(length(pool_id)=32), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID"; @@ -19,6 +19,7 @@ const OPERATIONS_V12: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL const OPERATIONS_V13: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 7), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 7 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; const OPERATIONS_V14: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 8), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 8 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; const OPERATIONS_V15: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 9), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 9 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; +const OPERATIONS_V16: &str = "CREATE TABLE stock_operations (actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL REFERENCES stock_grants(grant_id), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), command_digest BLOB NOT NULL CHECK(length(command_digest)=32), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), pool_id BLOB REFERENCES stock_pools(pool_id) DEFERRABLE INITIALLY DEFERRED, revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), family INTEGER NOT NULL DEFAULT 0 CHECK(family BETWEEN 0 AND 10), order_id BLOB CHECK(order_id IS NULL OR length(order_id)=32), related_operation BLOB CHECK(related_operation IS NULL OR length(related_operation)=32), subject_grant BLOB REFERENCES stock_grants(grant_id) CHECK(subject_grant IS NULL OR length(subject_grant)=32), PRIMARY KEY(actor,grant_id,operation_id), CHECK((family=0 AND pool_id IS NOT NULL AND order_id IS NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family=1 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NOT NULL) OR (family=2 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NULL AND subject_grant IS NULL) OR (family BETWEEN 3 AND 10 AND pool_id IS NULL AND order_id IS NOT NULL AND related_operation IS NOT NULL AND subject_grant IS NULL))) STRICT, WITHOUT ROWID"; const MOVEMENTS: &str = "CREATE TABLE stock_movements (movement_id BLOB PRIMARY KEY CHECK(length(movement_id)=32), actor BLOB NOT NULL CHECK(length(actor)=32), grant_id BLOB NOT NULL CHECK(length(grant_id)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), FOREIGN KEY(actor,grant_id,operation_id) REFERENCES stock_operations(actor,grant_id,operation_id)) STRICT, WITHOUT ROWID"; pub(super) fn objects( version: i64, @@ -29,13 +30,15 @@ pub(super) fn objects( ]; match version { 1 => {} - 2..=15 => objects.extend([ + 2..=16 => objects.extend([ ("table", "stock_pools", "stock_pools", POOLS), ( "table", "stock_operations", "stock_operations", - if version >= 15 { + if version >= 16 { + OPERATIONS_V16 + } else if version >= 15 { OPERATIONS_V15 } else if version >= 14 { OPERATIONS_V14 @@ -88,7 +91,7 @@ pub(super) fn objects( if version >= 5 { objects.push(("table","stock_mappings","stock_mappings","CREATE TABLE stock_mappings (listing_author BLOB NOT NULL CHECK(length(listing_author)=32), listing_d BLOB NOT NULL CHECK(length(listing_d)=16), option_id BLOB NOT NULL CHECK(length(option_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144), PRIMARY KEY(listing_author,listing_d,option_id)) STRICT, WITHOUT ROWID")); } - if (6..=15).contains(&version) { + if (6..=16).contains(&version) { objects.push(("table","stock_source_records","stock_source_records","CREATE TABLE stock_source_records (record_id BLOB PRIMARY KEY CHECK(length(record_id)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); objects.push(("table","stock_source_evidence","stock_source_evidence","CREATE TABLE stock_source_evidence (envelope_id BLOB PRIMARY KEY CHECK(length(envelope_id)=32), record_id BLOB REFERENCES stock_source_records(record_id), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); objects.push(("index","stock_source_transaction_identity","stock_source_evidence","CREATE UNIQUE INDEX stock_source_transaction_identity ON stock_source_evidence(transaction_id)")); @@ -124,7 +127,7 @@ pub(super) fn objects( objects.push(("table","stock_orders","stock_orders","CREATE TABLE stock_orders (order_id BLOB PRIMARY KEY CHECK(length(order_id)=32), revision INTEGER NOT NULL CHECK(revision>0), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); } } - if matches!(version, 8..=15) { + if matches!(version, 8..=16) { objects.push(("table","stock_public_events","stock_public_events","CREATE TABLE stock_public_events (event_id BLOB PRIMARY KEY CHECK(length(event_id)=32), listing_author BLOB NOT NULL CHECK(length(listing_author)=32), listing_d BLOB NOT NULL CHECK(length(listing_d)=16), created_at BLOB NOT NULL CHECK(length(created_at)=8), transaction_id BLOB NOT NULL CHECK(length(transaction_id)=32), revision INTEGER NOT NULL CHECK(revision>0), chain_head BLOB NOT NULL CHECK(length(chain_head)=32), protected BLOB NOT NULL CHECK(length(protected)>0 AND length(protected)<=262144)) STRICT, WITHOUT ROWID")); objects.push(("index","stock_public_transaction_identity","stock_public_events","CREATE UNIQUE INDEX stock_public_transaction_identity ON stock_public_events(transaction_id)")); objects.push(( @@ -186,6 +189,16 @@ pub(super) fn objects( objects.push(("table","stock_replay_admissions","stock_replay_admissions","CREATE TABLE stock_replay_admissions (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_signing_claims(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); objects.push(("index","stock_replay_admission_entry","stock_replay_admissions","CREATE UNIQUE INDEX stock_replay_admission_entry ON stock_replay_admissions(actor,entry_id)")); } + + if version >= 16 { + objects.push(("table","stock_envelopes","stock_envelopes","CREATE TABLE stock_envelopes (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), recipient BLOB NOT NULL CHECK(length(recipient)=32), entry_id BLOB NOT NULL CHECK(length(entry_id)=32), PRIMARY KEY(actor,operation_id,recipient), FOREIGN KEY(actor,operation_id,recipient) REFERENCES stock_publication_work(actor,operation_id,recipient), FOREIGN KEY(actor,entry_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); + objects.push(( + "index", + "stock_envelope_entry", + "stock_envelopes", + "CREATE UNIQUE INDEX stock_envelope_entry ON stock_envelopes(actor,entry_id)", + )); + } Ok(objects) } pub(super) async fn admit(connection: &mut SqliteConnection) -> Result<i64, WorkspaceError> { diff --git a/crates/storage_sqlite/src/workspace/stock/signing_plan.rs b/crates/storage_sqlite/src/workspace/stock/signing_plan.rs @@ -147,8 +147,8 @@ pub(in crate::workspace::stock) async fn load( digest: CommandDigest, proof: &AttachmentProof, ) -> Result<Authorized, OperationError> { - let access = turn.access(binding, minimum, proof).await?; - if !matches!(access.authority.catalog, 10..=15) { + let access = Box::pin(turn.access(binding, minimum, proof)).await?; + if !matches!(access.authority.catalog, 10..=16) { return Err(OperationError::UnsupportedOperation); } let original = Box::pin(acceptance::codec::read( @@ -162,17 +162,18 @@ pub(in crate::workspace::stock) async fn load( if original.frame.header.digest != digest { return Err(OperationError::IdempotencyConflict); } - let original_proof = checkpoint::authenticate_identity( + let original_proof = Box::pin(checkpoint::authenticate_identity( turn.connection, turn.selected, turn.crypto, &access.current, &access.authority, IdentityTarget::Operation(operation, Some(original.identity())), - ) + )) .await?; let command = original.command()?; - let (consent, consent_proof) = acceptance::current_consent(turn, &access, &command).await?; + let (consent, consent_proof) = + Box::pin(acceptance::current_consent(turn, &access, &command)).await?; acceptance::check_consent( &access, &consent, @@ -207,7 +208,7 @@ pub(in crate::workspace::stock) async fn load( } else { None }; - let plan_proof = checkpoint::authenticate_identity( + let plan_proof = Box::pin(checkpoint::authenticate_identity( turn.connection, turn.selected, turn.crypto, @@ -217,7 +218,7 @@ pub(in crate::workspace::stock) async fn load( operation, expected: plan.as_ref().map(codec::Retained::identity), }, - ) + )) .await?; Ok(Authorized { access, diff --git a/crates/storage_sqlite/src/workspace/stock/signing_plan/codec.rs b/crates/storage_sqlite/src/workspace/stock/signing_plan/codec.rs @@ -191,6 +191,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + envelope: None, aborted: None, preparation: None, local_record: None, diff --git a/crates/storage_sqlite/src/workspace/stock/source/transaction.rs b/crates/storage_sqlite/src/workspace/stock/source/transaction.rs @@ -28,7 +28,7 @@ async fn access( } let (authority, record, root, grant) = super::super::read(turn.connection, binding.input(), turn.crypto).await?; - if !(6..=15).contains(&authority.catalog) { + if !(6..=16).contains(&authority.catalog) { return Err(OperationError::UnsupportedOperation); } let current = record.authenticate(binding.input(), proof, turn.clock.now()?)?; @@ -114,7 +114,7 @@ pub(in crate::workspace) async fn history( proof: AttachmentProof, ) -> Result<StockHistorySnapshot, OperationError> { let access = access(turn, &binding, minimum, &proof).await?; - if !matches!(access.authority.catalog, 8..=15) { + if !matches!(access.authority.catalog, 8..=16) { return Err(OperationError::UnsupportedOperation); } let prepared = Box::pin(super::history::prepare( @@ -175,7 +175,7 @@ pub(in crate::workspace) async fn inspect_request( proof: AttachmentProof, ) -> Result<StockRequestSnapshot, OperationError> { let access = access(turn, &binding, minimum, &proof).await?; - if !matches!(access.authority.catalog, 8..=15) { + if !matches!(access.authority.catalog, 8..=16) { return Err(OperationError::UnsupportedOperation); } let prepared = Box::pin(super::request::prepare( diff --git a/crates/storage_sqlite/src/workspace/stock/tests.rs b/crates/storage_sqlite/src/workspace/stock/tests.rs @@ -109,7 +109,7 @@ async fn current_grant_set_reopens_with_original_namespace_after_one_grant_chang ) .await .unwrap(); - assert_eq!(schema::admit(&mut connection).await, Ok(15)); + assert_eq!(schema::admit(&mut connection).await, Ok(16)); assert_eq!(schema::integrity(&mut connection).await, Ok(())); let root: Vec<u8> = sqlx::query_scalar("SELECT protected FROM stock_authority") .fetch_one(&mut connection) @@ -117,18 +117,18 @@ async fn current_grant_set_reopens_with_original_namespace_after_one_grant_chang .unwrap(); let clear = fixture .crypto() - .open_stock_root(enrollment.input(), 15, &root) + .open_stock_root(enrollment.input(), 16, &root) .await .unwrap(); let authority = clear - .expose_secret(|bytes| Authority::decode(bytes, 15)) + .expose_secret(|bytes| Authority::decode(bytes, 16)) .unwrap(); clear.expose_secret(|bytes| { let original:serde_json::Value=serde_json::from_slice(bytes).unwrap(); assert_eq!(original["inventory"]["mappings"],"0"); for bad in [serde_json::json!([]),serde_json::json!({"revision":"1","pools":"1","movements":"1","head":original["inventory"]["head"]})] { let mut value=original.clone();value["inventory"]=bad; - assert!(Authority::decode(&serde_json::to_vec(&value).unwrap(),15).is_err()); + assert!(Authority::decode(&serde_json::to_vec(&value).unwrap(),16).is_err()); } for bad in [ serde_json::json!([]), @@ -139,7 +139,7 @@ async fn current_grant_set_reopens_with_original_namespace_after_one_grant_chang serde_json::json!({"revision":"1","records":"2","parts":"3","head":"01".repeat(32)}), ] { let mut value=original.clone();value["sources"]=bad; - assert!(Authority::decode(&serde_json::to_vec(&value).unwrap(),15).is_err()); + assert!(Authority::decode(&serde_json::to_vec(&value).unwrap(),16).is_err()); } }); assert_eq!(authority.input, *enrollment.input()); @@ -189,7 +189,7 @@ async fn current_and_retained_roots_bind_the_complete_namespace_with_distinct_ca let caller = namespace.attachment(GrantId::from_bytes([9; 32])).unwrap(); let crypto = fixture.crypto(); let clear = b"synthetic context payload"; - for version in [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] { + for version in 4..=16 { let current = crypto .seal_stock_root(&owner, version, clear) .await @@ -277,7 +277,7 @@ async fn retained_v10_preserves_prior_families_and_signing_plan_retention() { } #[tokio::test] async fn retained_v11_preserves_prior_families_and_adds_claims_and_obtained_evidence() { - assert_eq!(schema::CURRENT, 15); + assert_eq!(schema::CURRENT, 16); retained_inventory_catalog(11).await; } // Exact catalog fixtures exercise retained/successor codecs; they are not @@ -322,7 +322,7 @@ pub(in crate::workspace::stock) async fn literal_enrollment_catalog( catalog: i64, enrollment: &StockEnrollment, ) { - assert!(matches!(catalog, 13 | 14)); + assert!(matches!(catalog, 13..=15)); let record = enrollment.record.lock().unwrap().take().unwrap(); literal_catalog_contents( fixture, @@ -956,24 +956,24 @@ async fn retained_enrollment_v1_authenticates_but_never_auto_creates_inventory() #[tokio::test] async fn retained_v12_preserves_initial_installation_catalog_without_upgrade() { - assert_eq!(schema::CURRENT, 15); + assert_eq!(schema::CURRENT, 16); retained_inventory_catalog(12).await; } #[tokio::test] async fn retained_v13_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 15); + assert_eq!(schema::CURRENT, 16); retained_inventory_catalog(13).await; } #[tokio::test] async fn retained_v14_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 15); + assert_eq!(schema::CURRENT, 16); retained_inventory_catalog(14).await; } #[tokio::test] -async fn current_v15_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 15); +async fn retained_v15_authenticates_literal_catalog_and_original_inventory_records() { + assert_eq!(schema::CURRENT, 16); retained_inventory_catalog(15).await; } diff --git a/crates/storage_sqlite/src/workspace/worker.rs b/crates/storage_sqlite/src/workspace/worker.rs @@ -89,6 +89,17 @@ enum Job { AttachmentProof, Reply<Option<stock::StockUnissuedReceipt>, OperationError>, ), + CaptureStockEnvelope( + Box<radroots_storage::inventory::StockEnvelopeRequest>, + AttachmentProof, + Box<radroots_storage::command::PrivateEnvelopeCandidate>, + Reply<radroots_storage::inventory::StockEnvelopeOutcome, OperationError>, + ), + StockEnvelope( + Box<radroots_storage::inventory::StockEnvelopeRequest>, + AttachmentProof, + Reply<Option<radroots_storage::inventory::StockEnvelopeSource>, OperationError>, + ), CaptureStockEvidence( Box<radroots_storage::inventory::StockEvidenceRequest>, AttachmentProof, @@ -358,6 +369,12 @@ impl Job { Self::CaptureStockEvidence(_, _, reply) | Self::RecoverStockEvidence(_, _, reply) => { let _ = reply.send(Err(OperationError::RecoveryRequired)); } + Self::CaptureStockEnvelope(_, _, _, reply) => { + let _ = reply.send(Err(OperationError::RecoveryRequired)); + } + Self::StockEnvelope(_, _, reply) => { + let _ = reply.send(Err(OperationError::RecoveryRequired)); + } Self::StockEvidence(_, _, _, reply) => { let _ = reply.send(Err(OperationError::RecoveryRequired)); } @@ -633,6 +650,8 @@ impl Handle { | Job::CaptureStockUnissued(..) | Job::RecoverStockUnissued(..) | Job::StockUnissued(..) + | Job::CaptureStockEnvelope(..) + | Job::StockEnvelope(..) | Job::CaptureStockEvidence(..) | Job::RecoverStockEvidence(..) | Job::StockEvidence(..) @@ -1055,6 +1074,47 @@ impl Handle { }; let _ = reply.send(result); } + Job::CaptureStockEnvelope(request, proof, candidate, reply) => { + let result = if kind == Kind::SellerStock { + Box::pin(stock::envelope::capture( + &mut stock::inventory::Turn { + connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + }, + *request, + proof, + *candidate, + )) + .await + } else { + Err(OperationError::UnsupportedOperation) + }; + let _ = reply.send(result); + } + Job::StockEnvelope(request, proof, reply) => { + let result = if kind == Kind::SellerStock { + Box::pin(stock::envelope::read( + &mut stock::inventory::Turn { + connection, + namespace: &namespace, + selected: &input, + crypto: &crypto, + clock: clock.as_ref(), + uncertain: &mut uncertain, + }, + *request, + proof, + )) + .await + } else { + Err(OperationError::UnsupportedOperation) + }; + let _ = reply.send(result); + } Job::CaptureStockEvidence(evidence, proof, reply) => { let result = if kind == Kind::SellerStock { Box::pin(stock::issuance::evidence::capture( @@ -1948,6 +2008,31 @@ impl Handle { self.send(Job::RecoverStockEvidence(Box::new(request), proof, reply))?; result.await.unwrap_or(Ok(unknown)) } + pub(super) async fn capture_stock_envelope( + &self, + request: radroots_storage::inventory::StockEnvelopeRequest, + proof: AttachmentProof, + candidate: radroots_storage::command::PrivateEnvelopeCandidate, + ) -> Result<radroots_storage::inventory::StockEnvelopeOutcome, OperationError> { + let unknown = radroots_storage::inventory::StockEnvelopeOutcome::Unknown(request.attempt()); + let (reply, result) = oneshot::channel(); + self.send(Job::CaptureStockEnvelope( + Box::new(request), + proof, + Box::new(candidate), + reply, + ))?; + result.await.unwrap_or(Ok(unknown)) + } + pub(super) async fn stock_envelope( + &self, + request: radroots_storage::inventory::StockEnvelopeRequest, + proof: AttachmentProof, + ) -> Result<Option<radroots_storage::inventory::StockEnvelopeSource>, OperationError> { + let (reply, result) = oneshot::channel(); + self.send(Job::StockEnvelope(Box::new(request), proof, reply))?; + result.await.map_err(|_| OperationError::RecoveryRequired)? + } pub(super) async fn capture_stock_signing_evidence( &self, evidence: radroots_storage::inventory::StockEvidenceRequest, @@ -2133,7 +2218,9 @@ impl Handle { | Job::RecoverStockUnissued(..) | Job::CaptureStockEvidence(..) | Job::RecoverStockEvidence(..) => 256 * 1024, - Job::StockEvidence(..) + Job::CaptureStockEnvelope(..) + | Job::StockEnvelope(..) + | Job::StockEvidence(..) | Job::StockUnissued(..) | Job::StockEvidencePage(..) | Job::StockSnapshot(..) diff --git a/crates/storage_sqlite/tests/support/stock_authority.rs b/crates/storage_sqlite/tests/support/stock_authority.rs @@ -602,7 +602,7 @@ async fn changed_catalog_after_protected_reads_is_refused_before_authenticated_c clock.release(); assert_eq!(operation.await, Err(WorkspaceError::RecoveryRequired)); store.close().await.unwrap(); - sqlx::query("PRAGMA user_version=15") + sqlx::query("PRAGMA user_version=16") .execute(&mut connection) .await .unwrap(); diff --git a/crates/storage_sqlite/tests/support/stock_envelope_port.rs b/crates/storage_sqlite/tests/support/stock_envelope_port.rs @@ -1,4 +1,4 @@ -//! This checkpoint exports descriptors, not a stock-envelope backend. +//! Actual stock refusal and shared report/carrier boundaries. use super::*; use nostr::JsonUtil; use radroots_domain::{ @@ -32,7 +32,7 @@ fn minimum(input: &AttachmentInput, revision: u64) -> InventoryCheckpointRef { fn request(binding: &WorkspaceBinding) -> StockEnvelopeRequest { StockEnvelopeRequest::new( binding.clone(), - minimum(binding.input(), 0), + minimum(binding.input(), 0).with_optional_authorization(binding.stock_authorization()), OperationId::from_bytes([80; 32]), CommandDigest::from_bytes([81; 32]), key(), @@ -82,26 +82,36 @@ async fn stock(f: &Fixture) -> (NativeWorkspace, StockEnrollment, WorkspaceBindi } #[tokio::test] -async fn envelope_ports_explicitly_refuse_on_current_stock_and_ordinary_outbox_without_writes() { +async fn envelope_ports_refuse_stock_without_original_work_and_ordinary_outbox_without_writes() { let f = Fixture::new(); let (store, enrollment, binding) = stock(&f).await; let outbox_fixture = Fixture::new(); let (outbox, _, _) = outbox_fixture.create().await; let candidate = PrivateEnvelopeCandidate::from_obtained(key(), &carrier(), &[3; 32]).unwrap(); - for port in [ - &store as &dyn InventoryStore, - &outbox as &dyn InventoryStore, + for (port, capture_error, query_error) in [ + ( + &store as &dyn InventoryStore, + OperationError::MissingHistory, + OperationError::StaleState, + ), + ( + &outbox as &dyn InventoryStore, + OperationError::UnsupportedOperation, + OperationError::UnsupportedOperation, + ), ] { - assert!(matches!( + assert_eq!( port.capture_stock_envelope(request(&binding), copy(enrollment.proof()), &candidate) - .await, - Err(OperationError::UnsupportedOperation) - )); - assert!(matches!( + .await + .unwrap_err(), + capture_error + ); + assert_eq!( port.stock_envelope(request(&binding), copy(enrollment.proof())) - .await, - Err(OperationError::UnsupportedOperation) - )); + .await + .unwrap_err(), + query_error + ); } // Current catalog and authoritative history remain unchanged by refusal. let mut connection = sqlx::SqliteConnection::connect_with( @@ -119,7 +129,7 @@ async fn envelope_ports_explicitly_refuse_on_current_stock_and_ordinary_outbox_w .fetch_one(&mut connection) .await .unwrap(); - assert_eq!(catalog, 15); + assert_eq!(catalog, 16); assert_eq!(entries, 0); connection.close().await.unwrap(); assert!( diff --git a/tools/xtask/src/architecture/decision_package.rs b/tools/xtask/src/architecture/decision_package.rs @@ -102,7 +102,34 @@ struct StockCapabilityContract { #[derive(Deserialize)] #[serde(deny_unknown_fields)] +struct StockEnvelopeRetention { + journal_family: u32, + payload_version: u32, + payload_prefix_bytes: u32, + supported_catalogs: Vec<u32>, + retained_unsupported_catalogs: Vec<u32>, + raw_max_bytes: u32, + worker_charge_bytes: u32, + capture_port: String, + query_port: String, + original_preparation_plan_claim_seal_installation_required: bool, + temporary_key_derivation_and_exact_decryption_required: bool, + temporary_key_retained: bool, + original_party_as_outer_author_allowed: bool, + body_derived_projection_and_complete_ancestry_required: bool, + original_credential_capture_requires_live_clock_or_grant: bool, + query_requires_current_recovery_access: bool, + observation_nullable: bool, + existing_original_bytes_may_be_replaced: bool, + capture_grants_dispatch: bool, + capture_changes_physical_stock: bool, + provider_io_inside_sql: bool, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] struct NativeSellerStockAuthority { + envelope_retention: StockEnvelopeRetention, qualified_local_recovery: QualifiedLocalRecovery, source_payload_schemas: Vec<String>, source_profile_dispositions: Vec<String>, @@ -608,16 +635,43 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { "local outbox authority drift", )?; let stock = &p.native_seller_stock_authority; + let envelope = &stock.envelope_retention; + require( + envelope.journal_family == 10 + && envelope.payload_version == 1 + && envelope.payload_prefix_bytes == 213 + && envelope.supported_catalogs == [16] + && envelope.retained_unsupported_catalogs + == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + && envelope.raw_max_bytes == 65536 + && envelope.worker_charge_bytes == 1048576 + && envelope.capture_port == "InventoryStore.capture_stock_envelope" + && envelope.query_port == "InventoryStore.stock_envelope" + && envelope.original_preparation_plan_claim_seal_installation_required + && envelope.temporary_key_derivation_and_exact_decryption_required + && !envelope.temporary_key_retained + && !envelope.original_party_as_outer_author_allowed + && envelope.body_derived_projection_and_complete_ancestry_required + && !envelope.original_credential_capture_requires_live_clock_or_grant + && envelope.query_requires_current_recovery_access + && envelope.observation_nullable + && !envelope.existing_original_bytes_may_be_replaced + && !envelope.capture_grants_dispatch + && !envelope.capture_changes_physical_stock + && !envelope.provider_io_inside_sql, + "stock envelope retention drift", + )?; require( stock.capability_contract.id == "radroots.seller_stock.native_inventory" && stock.capability_contract.version == "1" && stock.scope == "seller_stock_owner" && stock.database == "stock_execution_v1.sqlite" && stock.lock == ".stock_execution_v1.lock" - && stock.schema_version == 15 + && stock.schema_version == 16 && stock.retained_enrollment_versions == [1] - && stock.retained_inventory_versions == [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] - && stock.authority_payload_schema == "radroots.seller_stock.authority.v15" + && stock.retained_inventory_versions + == [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + && stock.authority_payload_schema == "radroots.seller_stock.authority.v16" && stock.public_source_payload_schemas == [ "radroots.seller_stock.public_source_event.v1", @@ -896,7 +950,7 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { && stock.qualified_local_recovery.native_authorizer == "NativeWorkspace.authorize_stock_exact_replay" && stock.qualified_local_recovery.captured_replay == "local_replay_safe" - && stock.qualified_local_recovery.native_catalog == 15 + && stock.qualified_local_recovery.native_catalog == 16 && stock.qualified_local_recovery.worker_charge_bytes == 1048576 && stock .qualified_local_recovery @@ -959,7 +1013,7 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { && stock.preparation_abort.unsupported_hosts_return == "capability_unavailable" && stock.preparation_abort.retained_payload_versions == [1] && stock.preparation_abort.retained_v1_requires_absent_claims - && stock.preparation_abort.claimed_cleanup_catalogs == [15] + && stock.preparation_abort.claimed_cleanup_catalogs == [15, 16] && stock.preparation_abort.untracked_claimed_catalogs == [13, 14] && stock.preparation_abort.per_recipient_proof_count == 2 && stock @@ -2283,6 +2337,50 @@ mod tests { } #[test] + fn stock_envelope_retention_cannot_become_dispatch_permission_or_a_cache_authority() { + for (field, bad) in [ + ("journal_family", json!(9)), + ("payload_prefix_bytes", json!(212)), + ("supported_catalogs", json!([15, 16])), + ("raw_max_bytes", json!(65537)), + ("worker_charge_bytes", json!(65536)), + ( + "original_preparation_plan_claim_seal_installation_required", + json!(false), + ), + ( + "temporary_key_derivation_and_exact_decryption_required", + json!(false), + ), + ("temporary_key_retained", json!(true)), + ("original_party_as_outer_author_allowed", json!(true)), + ( + "body_derived_projection_and_complete_ancestry_required", + json!(false), + ), + ( + "original_credential_capture_requires_live_clock_or_grant", + json!(true), + ), + ("query_requires_current_recovery_access", json!(false)), + ("observation_nullable", json!(false)), + ("existing_original_bytes_may_be_replaced", json!(true)), + ("capture_grants_dispatch", json!(true)), + ("capture_changes_physical_stock", json!(true)), + ("provider_io_inside_sql", json!(true)), + ] { + assert!( + validate_bytes(&changed( + &format!("/native_seller_stock_authority/envelope_retention/{field}"), + bad + )) + .is_err(), + "{field}" + ); + } + } + + #[test] fn initial_limits_and_exact_binding_economics_require_explicit_amendment() { for (pointer, bad) in [ ("/limits/public_event_bytes", json!(65537)),