lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

commit ba5d7f81afcf9f046cbaf6d82b6f39fc7e8fd236
parent aec064e9d967cc3b912443743737234a21d11302
Author: triesap <tyson@radroots.org>
Date:   Thu, 29 Oct 2026 22:41:53 +0000

inventory: retain obligations after committed stock loss

- Append bounded original-allocation deficits with atomic pool and movement effects.
- Preserve retained catalogs and add exact protected catalog 19 storage.
- Expose historical deficit metadata and enforce inventory queue reservations.
- Verify native and Linux rollback, uncertain commit, exact replay, and current rewrap.

Diffstat:
Mcontracts/api_baselines/radroots.txt | 1+
Mcontracts/api_baselines/radroots_storage.txt | 12++++++++++++
Mcontracts/architecture/decisions/shared_sdk_refactor.v1.json | 34+++++++++++++++++++++++++---------
Mcrates/radroots/src/native_commerce_tests.rs | 3+++
Acrates/radroots/src/native_stock_deficit_tests.rs | 528+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/radroots/src/stock.rs | 10+++++++---
Mcrates/storage/src/inventory.rs | 33+++++++++++++++++++++++++++++++++
Acrates/storage/src/inventory/deficit.rs | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/workspace/crypto.rs | 16++++++++++------
Mcrates/storage_sqlite/src/workspace/stock.rs | 8++++----
Mcrates/storage_sqlite/src/workspace/stock/consent.rs | 4++--
Mcrates/storage_sqlite/src/workspace/stock/delivery.rs | 3++-
Mcrates/storage_sqlite/src/workspace/stock/delivery/observation_operations.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/delivery/operations.rs | 4++--
Mcrates/storage_sqlite/src/workspace/stock/envelope.rs | 6+++---
Mcrates/storage_sqlite/src/workspace/stock/envelope/codec.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/envelope/rewrap.rs | 8++++----
Mcrates/storage_sqlite/src/workspace/stock/envelope/rewrap_admission.rs | 9+++++----
Mcrates/storage_sqlite/src/workspace/stock/envelope/rewrap_capture.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/grants.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/inventory.rs | 105+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Mcrates/storage_sqlite/src/workspace/stock/inventory/abort.rs | 4++--
Mcrates/storage_sqlite/src/workspace/stock/inventory/abort/codec.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/inventory/abort/proof.rs | 6+++---
Mcrates/storage_sqlite/src/workspace/stock/inventory/acceptance.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/inventory/acceptance/codec.rs | 10+++++++++-
Mcrates/storage_sqlite/src/workspace/stock/inventory/checkpoint.rs | 16+++++++++++-----
Mcrates/storage_sqlite/src/workspace/stock/inventory/codec.rs | 38++++++++++++++++++++++++++++++++++++++
Acrates/storage_sqlite/src/workspace/stock/inventory/deficit.rs | 333+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/storage_sqlite/src/workspace/stock/inventory/installation.rs | 4++--
Mcrates/storage_sqlite/src/workspace/stock/inventory/installation/codec.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/inventory/mapping.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/inventory/plan.rs | 56+++++++++++++++++++++++++++++++++++++++++++++++---------
Mcrates/storage_sqlite/src/workspace/stock/issuance/claim/codec.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/issuance/evidence/codec.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/issuance/recovery.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/issuance/replay_admission.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/issuance/unissued.rs | 5+++--
Mcrates/storage_sqlite/src/workspace/stock/ledger.rs | 16+++++++++++++---
Mcrates/storage_sqlite/src/workspace/stock/public_source/transaction.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/schema.rs | 11+++++++----
Mcrates/storage_sqlite/src/workspace/stock/signing_plan.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/signing_plan/codec.rs | 1+
Mcrates/storage_sqlite/src/workspace/stock/snapshot.rs | 2+-
Mcrates/storage_sqlite/src/workspace/stock/source/transaction.rs | 6+++---
Mcrates/storage_sqlite/src/workspace/stock/tests.rs | 33+++++++++++++++++++++------------
Mcrates/storage_sqlite/src/workspace/worker.rs | 8+++++++-
Mtools/xtask/src/architecture/decision_package.rs | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
48 files changed, 1366 insertions(+), 107 deletions(-)

diff --git a/contracts/api_baselines/radroots.txt b/contracts/api_baselines/radroots.txt @@ -199,6 +199,7 @@ pub use radroots::stock::InventoryOutcome pub use radroots::stock::InventoryReceipt pub use radroots::stock::MapOptionInput pub use radroots::stock::PoolCorrectionInput +pub use radroots::stock::StockDeficit pub use radroots::stock::StockEvidenceOutcome pub use radroots::stock::StockEvidenceReceipt pub use radroots::stock::StockEvidenceRequest diff --git a/contracts/api_baselines/radroots_storage.txt b/contracts/api_baselines/radroots_storage.txt @@ -1426,12 +1426,14 @@ pub struct radroots_storage::inventory::InventoryReceipt impl radroots_storage::inventory::InventoryReceipt pub const fn radroots_storage::inventory::InventoryReceipt::attempt(&self) -> radroots_storage::command::CommitAttempt pub const fn radroots_storage::inventory::InventoryReceipt::checkpoint(&self) -> radroots_storage::inventory::InventoryCheckpointRef +pub fn radroots_storage::inventory::InventoryReceipt::deficits(&self) -> &[radroots_storage::inventory::StockDeficit] pub fn radroots_storage::inventory::InventoryReceipt::from_committed_mapping(radroots_storage::command::CommitAttempt, radroots_domain::stock::PoolSnapshot, radroots_domain::stock::mapping::OptionPoolMapping, radroots_storage::inventory::InventoryCheckpointRef) -> core::result::Result<Self, radroots_storage::command::OperationError> pub fn radroots_storage::inventory::InventoryReceipt::from_committed_store(radroots_storage::command::CommitAttempt, radroots_protocol::command::inventory::InventoryCommandKind, radroots_domain::stock::PoolSnapshot, radroots_storage::inventory::InventoryCheckpointRef) -> core::result::Result<Self, radroots_storage::command::OperationError> pub const fn radroots_storage::inventory::InventoryReceipt::kind(&self) -> radroots_protocol::command::inventory::InventoryCommandKind pub const fn radroots_storage::inventory::InventoryReceipt::mapping(&self) -> core::option::Option<radroots_domain::stock::mapping::OptionPoolMapping> pub const fn radroots_storage::inventory::InventoryReceipt::pool(&self) -> radroots_domain::stock::PoolSnapshot pub const fn radroots_storage::inventory::InventoryReceipt::transaction_id(&self) -> core::option::Option<radroots_domain::identifier::TransactionId> +pub fn radroots_storage::inventory::InventoryReceipt::with_deficits(self, alloc::vec::Vec<radroots_storage::inventory::StockDeficit>) -> core::result::Result<Self, radroots_storage::command::OperationError> pub struct radroots_storage::inventory::InventoryRequest impl radroots_storage::inventory::InventoryRequest pub const fn radroots_storage::inventory::InventoryRequest::binding(&self) -> &radroots_storage::workspace::WorkspaceBinding @@ -1492,6 +1494,16 @@ pub const fn radroots_storage::inventory::StockAcceptanceRequest::minimum_checkp pub const fn radroots_storage::inventory::StockAcceptanceRequest::source_checkpoint(&self) -> radroots_storage::workspace::seller_stock::StockSourceCheckpointRef impl core::fmt::Debug for radroots_storage::inventory::StockAcceptanceRequest pub fn radroots_storage::inventory::StockAcceptanceRequest::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result +pub struct radroots_storage::inventory::StockDeficit +impl radroots_storage::inventory::StockDeficit +pub const fn radroots_storage::inventory::StockDeficit::acceptance(self) -> radroots_domain::identifier::OperationId +pub const fn radroots_storage::inventory::StockDeficit::backing(self) -> radroots_domain::stock::obligation::ObligationBacking +pub const fn radroots_storage::inventory::StockDeficit::from_host_metadata(radroots_domain::identifier::OperationId, radroots_domain::identifier::OperationId, radroots_domain::identifier::AgreementLineId, radroots_domain::identifier::PoolId, radroots_domain::stock::obligation::ObligationBacking) -> Self +pub const fn radroots_storage::inventory::StockDeficit::installation(self) -> radroots_domain::identifier::OperationId +pub const fn radroots_storage::inventory::StockDeficit::line(self) -> radroots_domain::identifier::AgreementLineId +pub const fn radroots_storage::inventory::StockDeficit::pool(self) -> radroots_domain::identifier::PoolId +impl core::fmt::Debug for radroots_storage::inventory::StockDeficit +pub fn radroots_storage::inventory::StockDeficit::fmt(&self, &mut core::fmt::Formatter<'_>) -> core::fmt::Result pub struct radroots_storage::inventory::StockDeliveryAttempt impl radroots_storage::inventory::StockDeliveryAttempt pub const fn radroots_storage::inventory::StockDeliveryAttempt::checkpoint(&self) -> radroots_storage::inventory::InventoryCheckpointRef 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": 18, + "native_catalog": 19, "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": 18, + "schema_version": 19, "retained_enrollment_versions": [ 1 ], @@ -131,9 +131,10 @@ 14, 15, 16, - 17 + 17, + 18 ], - "authority_payload_schema": "radroots.seller_stock.authority.v18", + "authority_payload_schema": "radroots.seller_stock.authority.v19", "public_source_payload_schemas": [ "radroots.seller_stock.public_source_event.v1", "radroots.seller_stock.public_source_head.v1", @@ -399,7 +400,8 @@ 15, 16, 17, - 18 + 18, + 19 ], "untracked_claimed_catalogs": [ 13, @@ -420,7 +422,8 @@ "supported_catalogs": [ 16, 17, - 18 + 18, + 19 ], "retained_unsupported_catalogs": [ 1, @@ -460,7 +463,8 @@ "delivery": { "supported_catalogs": [ 17, - 18 + 18, + 19 ], "retained_unsupported_catalogs": [ 1, @@ -534,7 +538,8 @@ "payload_version": 1, "payload_prefix_bytes": 117, "supported_catalogs": [ - 18 + 18, + 19 ], "retained_unsupported_catalogs": [ 1, @@ -1096,7 +1101,18 @@ "receipt_consumes_again": false, "released_deficit_adds_available": false, "loss_deletes_obligation": false, - "native_atomic_allocation_and_identity_required": true + "native_atomic_allocation_and_identity_required": true, + "native_correction": { + "catalog": 19, + "selection": "available_then_original_installation_and_allocation_order", + "max_affected_allocations": 32, + "protected_original_allocation_required": true, + "deficit_projection_in_final_transaction": true, + "quantity_storage": "protected_original_operation_body", + "worker_charge_bytes": 1048576, + "preparatory_loss_implicit": false, + "quarantine_loss_implicit": false + } }, "execution": { "ordinary_inventory_owner": { diff --git a/crates/radroots/src/native_commerce_tests.rs b/crates/radroots/src/native_commerce_tests.rs @@ -924,6 +924,9 @@ mod original_refusals; #[path = "native_stock_rewrap_tests.rs"] mod rewrap; +#[path = "native_stock_deficit_tests.rs"] +mod deficit; + #[tokio::test] async fn peer_installation_survives_self_refusal_and_resume_uses_original_exact_recovery() { let r = Rig::new().await; diff --git a/crates/radroots/src/native_stock_deficit_tests.rs b/crates/radroots/src/native_stock_deficit_tests.rs @@ -0,0 +1,528 @@ +//! Real signed/installed acceptance, owner-private loss and protected reopen. +use super::*; +use radroots_domain::{ + Sequence, + stock::{AvailableStockCorrection, StockTicks}, +}; +use radroots_storage::inventory::InventoryReceipt; + +async fn correct( + r: &Rig, + id: u8, + version: u64, + kind: AvailableStockCorrection, + amount: &str, + reason: &str, +) -> crate::Result<InventoryOutcome> { + let (_, _, minimum) = r.minima().await; + let context = r + .scope + .command_context( + OperationId::from_bytes([id; 32]), + None, + None, + UnixSeconds::new(230).unwrap(), + ) + .unwrap(); + let input = radroots_protocol::command::PoolCorrectionInput::new( + PoolId::from_bytes([80; 32]), + Sequence::new(version).unwrap(), + kind, + amount.parse().unwrap(), + reason, + ) + .unwrap(); + if kind == AvailableStockCorrection::Spoil { + r.client + .stock(&r.scope) + .spoil(context, minimum, input) + .await + } else { + r.client + .stock(&r.scope) + .adjust(context, minimum, input) + .await + } +} +fn committed(outcome: InventoryOutcome) -> InventoryReceipt { + let InventoryOutcome::Committed(receipt) = outcome else { + panic!("actual correction must confirm its transaction"); + }; + *receipt +} + +async fn failed_native_correction(uncertain_commit: bool) { + use sqlx::Connection; + let r = Rig::new().await; + let (_, installed) = r.accept().await; + let (_, _, minimum) = r.minima().await; + let before = r + .client + .stock(&r.scope) + .pool(minimum, PoolId::from_bytes([80; 32])) + .await + .unwrap() + .unwrap() + .snapshot(); + let mut fault = sqlx::SqliteConnection::connect(&format!( + "sqlite://{}", + r._temp + .path() + .join("stock/stock_execution_v1.sqlite") + .display() + )) + .await + .unwrap(); + let counts_sql = "SELECT (SELECT count(*) FROM stock_operations),(SELECT count(*) FROM stock_movements),(SELECT count(*) FROM stock_deficits)"; + let counts: (i64, i64, i64) = sqlx::query_as(counts_sql) + .fetch_one(&mut fault) + .await + .unwrap(); + if uncertain_commit { + sqlx::raw_sql("CREATE TABLE deficit_deferred_fault(value INTEGER REFERENCES stock_authority(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER controlled_deficit_failure AFTER INSERT ON stock_deficits BEGIN INSERT INTO deficit_deferred_fault VALUES(2); END").execute(&mut fault).await.unwrap(); + } else { + sqlx::query("CREATE TRIGGER controlled_deficit_failure BEFORE INSERT ON stock_deficits BEGIN SELECT RAISE(ABORT, 'controlled_deficit_failure'); END").execute(&mut fault).await.unwrap(); + } + let result = correct( + &r, + 120, + before.version().get(), + AvailableStockCorrection::Spoil, + "8", + "synthetic loss at actual failed transaction", + ) + .await; + if uncertain_commit { + let InventoryOutcome::Unknown(anchor) = result.unwrap() else { + panic!("actual COMMIT failure must remain unknown"); + }; + assert_eq!(anchor.operation_id(), OperationId::from_bytes([120; 32])); + assert!(anchor.transaction_id().is_some()); + assert_eq!( + r.native + .inventory_operation( + r.scope.binding().clone(), + minimum, + anchor.operation_id(), + Some(anchor.command_digest()), + proof(r.enrollment.proof()) + ) + .await + .unwrap_err(), + OperationError::RecoveryRequired + ); + } else { + assert_eq!(result.unwrap_err().code(), crate::Code::StorageFailure); + } + assert_eq!( + sqlx::query_as::<_, (i64, i64, i64)>(counts_sql) + .fetch_one(&mut fault) + .await + .unwrap(), + counts + ); + assert_eq!(r.control.signs.load(Ordering::SeqCst), 2); + assert_eq!(r.io.wraps.load(Ordering::SeqCst), 2); + assert_eq!(r.io.sent.lock().unwrap().len(), 2); + // No obtained material is pending for an owner-private correction. Close + // may release this original writer without a current clock or new effect. + r.clock.0.store(0, Ordering::SeqCst); + r.client.close().await.unwrap(); + sqlx::raw_sql( + "DROP TRIGGER controlled_deficit_failure; DROP TABLE IF EXISTS deficit_deferred_fault", + ) + .execute(&mut fault) + .await + .unwrap(); + fault.close().await.unwrap(); + r.clock.0.store(150, Ordering::SeqCst); + let crypto = WorkspaceCrypto::new( + r.vault.clone(), + SecretRef::new( + SecretId::parse("workspace-key").unwrap(), + BackendKind::File, + KeyVersion::new(1).unwrap(), + ), + SecretMaterial::from_slice(&[0x41; 32]).unwrap(), + AccessPolicy::standard(), + ) + .unwrap(); + let reopened = NativeWorkspace::open_seller_stock( + &r._temp.path().join("stock"), + r.enrollment.input().clone(), + crypto, + r.clock.clone(), + ) + .await + .unwrap(); + let binding = reopened + .authenticate(r.enrollment.input().clone(), r.enrollment.proof()) + .await + .unwrap(); + let pool = reopened + .pool( + binding.clone(), + minimum, + before.id(), + proof(r.enrollment.proof()), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(pool.snapshot(), before); + assert!( + reopened + .inventory_operation( + binding.clone(), + minimum, + OperationId::from_bytes([120; 32]), + None, + proof(r.enrollment.proof()) + ) + .await + .unwrap() + .is_none() + ); + let current = reopened + .stock_authority(r.enrollment.input().clone(), r.enrollment.proof()) + .await + .unwrap(); + assert_eq!(current.inventory_checkpoint(), Some(installed.checkpoint())); + reopened.close().await.unwrap(); +} + +#[tokio::test] +async fn native_inventory_proof_work_reserves_the_live_queue_budget_until_sql_confirmation() { + use sqlx::Connection; + let r = Rig::new().await; + let (_, _, minimum) = r.minima().await; + let pool = PoolId::from_bytes([80; 32]); + let mut blocker = sqlx::SqliteConnection::connect(&format!( + "sqlite://{}", + r._temp + .path() + .join("stock/stock_execution_v1.sqlite") + .display() + )) + .await + .unwrap(); + let transaction = blocker.begin_with("BEGIN IMMEDIATE").await.unwrap(); + let mut reads: Vec<_> = (0..4) + .map(|_| { + Box::pin(r.native.pool( + r.scope.binding().clone(), + minimum, + pool, + proof(r.enrollment.proof()), + )) + }) + .collect(); + for read in &mut reads { + assert!(futures::poll!(read.as_mut()).is_pending()); + } + let fifth = r.native.pool( + r.scope.binding().clone(), + minimum, + pool, + proof(r.enrollment.proof()), + ); + let error = tokio::time::timeout(std::time::Duration::from_secs(1), fifth) + .await + .expect("full queue must refuse without waiting for its blocked writer") + .unwrap_err(); + assert_eq!( + error, + OperationError::Workspace(WorkspaceError::Unavailable) + ); + transaction.rollback().await.unwrap(); + for result in futures::future::join_all(reads).await { + assert_eq!( + result + .unwrap() + .unwrap() + .snapshot() + .balances() + .physical() + .available + .get(), + 10 + ); + } + blocker.close().await.unwrap(); + r.client.close().await.unwrap(); +} + +#[tokio::test] +async fn native_deficit_statement_failure_rolls_back_pool_movement_obligation_and_root() { + failed_native_correction(false).await; +} + +#[tokio::test] +async fn native_deficit_commit_failure_stays_unknown_and_reopen_retains_original_backing() { + failed_native_correction(true).await; +} + +#[tokio::test] +async fn installed_backing_loss_retains_obligation_exact_replay_deficits_and_reopen() { + use AvailableStockCorrection::{Add, Remove, Spoil}; + let r = Rig::new().await; + let original_request = r.request().await; + let line = &original_request.terms().lines()[0]; + let key = + radroots_domain::stock::OptionMappingKey::new(line.source().coordinate(), line.option_id()); + let (acceptance, installed) = r.accept().await; + let (_, _, minimum) = r.minima().await; + let before = r + .client + .stock(&r.scope) + .pool(minimum, PoolId::from_bytes([80; 32])) + .await + .unwrap() + .unwrap() + .snapshot(); + assert_eq!(before.balances().physical().available.get(), 7); + assert_eq!(before.balances().physical().committed_hold.get(), 3); + // A later mapping cannot change the original three-tick obligation into + // a six-tick obligation or redirect this loss to a newly selected plan. + let (_, _, mapping_minimum) = r.minima().await; + let mapping = r + .client + .stock(&r.scope) + .option_mapping(mapping_minimum, key) + .await + .unwrap() + .unwrap(); + let context = r + .scope + .command_context( + OperationId::from_bytes([109; 32]), + None, + None, + UnixSeconds::new(230).unwrap(), + ) + .unwrap(); + let changed = radroots_protocol::command::MapOptionInput::new( + key, + before.id(), + radroots_domain::Quantity::new("2".parse().unwrap(), before.unit().unit()).unwrap(), + mapping.version(), + ) + .unwrap(); + r.client + .stock(&r.scope) + .map_option(context, mapping_minimum, changed) + .await + .unwrap(); + let first = committed( + correct( + &r, + 110, + before.version().get(), + Spoil, + "8", + "synthetic recorded spoilage", + ) + .await + .unwrap(), + ); + let physical = first.pool().balances().physical(); + assert_eq!(physical.available, StockTicks::ZERO); + assert_eq!(physical.committed_hold.get(), 2); + assert_eq!(physical.waste.get(), 8); + assert_eq!(first.pool().balances().introduced().get(), 10); + assert_eq!(first.deficits().len(), 1); + let deficit = first.deficits()[0]; + assert_eq!(deficit.acceptance(), acceptance.context().operation_id()); + assert_eq!(deficit.pool(), before.id()); + assert_eq!(deficit.backing().remaining().get(), 3); + assert_eq!(deficit.backing().backed().get(), 2); + assert_eq!(deficit.backing().deficit().get(), 1); + assert_eq!( + format!("{deficit:?}"), + "StockDeficit(<private effect metadata>)" + ); + let repeat = committed( + correct( + &r, + 110, + before.version().get(), + Spoil, + "8", + "synthetic recorded spoilage", + ) + .await + .unwrap(), + ); + assert_eq!(repeat, first); + assert_eq!( + correct( + &r, + 110, + before.version().get(), + Spoil, + "8", + "changed captured reason" + ) + .await + .unwrap_err() + .code(), + crate::Code::IdempotencyConflict + ); + assert_eq!( + correct(&r, 111, before.version().get(), Spoil, "1", "stale version") + .await + .unwrap_err() + .code(), + crate::Code::StaleState + ); + let second = committed( + correct( + &r, + 112, + first.pool().version().get(), + Remove, + "2", + "synthetic recorded removal", + ) + .await + .unwrap(), + ); + assert_eq!( + second.pool().balances().physical().committed_hold, + StockTicks::ZERO + ); + assert_eq!(second.pool().balances().physical().removed.get(), 2); + assert_eq!(second.deficits()[0].backing().remaining().get(), 3); + assert_eq!(second.deficits()[0].backing().backed(), StockTicks::ZERO); + assert_eq!(second.deficits()[0].backing().deficit().get(), 3); + assert_eq!( + correct( + &r, + 113, + second.pool().version().get(), + Spoil, + "1", + "cannot deplete an unbacked obligation" + ) + .await + .unwrap_err() + .code(), + crate::Code::StaleState + ); + let added = committed( + correct( + &r, + 114, + second.pool().version().get(), + Add, + "2", + "explicit new physical source", + ) + .await + .unwrap(), + ); + assert!(added.deficits().is_empty()); + assert_eq!(added.pool().balances().physical().available.get(), 2); + assert_eq!( + added.pool().balances().physical().committed_hold, + StockTicks::ZERO + ); + assert_eq!(added.pool().balances().introduced().get(), 12); + let (_, _, minimum) = r.minima().await; + let old = r + .client + .stock(&r.scope) + .operation( + minimum, + first.attempt().operation_id(), + Some(first.attempt().command_digest()), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(old, first); + // The current minimum lies after this original loss. It must not permit + // trusting a forged historical deficit projection beneath that minimum. + use sqlx::Connection; + let mut fault = sqlx::SqliteConnection::connect(&format!( + "sqlite://{}", + r._temp + .path() + .join("stock/stock_execution_v1.sqlite") + .display() + )) + .await + .unwrap(); + sqlx::query("UPDATE stock_deficits SET installation_id=acceptance_id WHERE operation_id=?") + .bind(first.attempt().operation_id().as_bytes().as_slice()) + .execute(&mut fault) + .await + .unwrap(); + assert_eq!( + r.client + .stock(&r.scope) + .operation(minimum, first.attempt().operation_id(), None) + .await + .unwrap_err() + .code(), + crate::Code::RecoveryRequired + ); + sqlx::query("UPDATE stock_deficits SET installation_id=? WHERE operation_id=?") + .bind(deficit.installation().as_bytes().as_slice()) + .bind(first.attempt().operation_id().as_bytes().as_slice()) + .execute(&mut fault) + .await + .unwrap(); + fault.close().await.unwrap(); + let status = r + .client + .orders(&r.scope) + .acceptance_status( + minimum, + acceptance.context().operation_id(), + acceptance.digest(), + ) + .await + .unwrap(); + assert_eq!(status.installation(), installed.installation()); + assert_eq!(r.control.signs.load(Ordering::SeqCst), 2); + assert_eq!(r.io.wraps.load(Ordering::SeqCst), 2); + assert_eq!(r.io.sent.lock().unwrap().len(), 2); + r.client.close().await.unwrap(); + let crypto = WorkspaceCrypto::new( + r.vault.clone(), + SecretRef::new( + SecretId::parse("workspace-key").unwrap(), + BackendKind::File, + KeyVersion::new(1).unwrap(), + ), + SecretMaterial::from_slice(&[0x41; 32]).unwrap(), + AccessPolicy::standard(), + ) + .unwrap(); + let reopened = NativeWorkspace::open_seller_stock( + &r._temp.path().join("stock"), + r.enrollment.input().clone(), + crypto, + r.clock.clone(), + ) + .await + .unwrap(); + let binding = reopened + .authenticate(r.enrollment.input().clone(), r.enrollment.proof()) + .await + .unwrap(); + let recovered = reopened + .inventory_operation( + binding, + minimum, + second.attempt().operation_id(), + Some(second.attempt().command_digest()), + proof(r.enrollment.proof()), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(recovered, second); + reopened.close().await.unwrap(); +} diff --git a/crates/radroots/src/stock.rs b/crates/radroots/src/stock.rs @@ -5,7 +5,9 @@ use radroots_domain::{ stock::{AvailableStockCorrection, OptionMappingKey, OptionPoolMapping, PoolView}, }; pub use radroots_protocol::command::{CreatePoolInput, MapOptionInput, PoolCorrectionInput}; -pub use radroots_storage::inventory::{InventoryCheckpointRef, InventoryOutcome, InventoryReceipt}; +pub use radroots_storage::inventory::{ + InventoryCheckpointRef, InventoryOutcome, InventoryReceipt, StockDeficit, +}; pub use radroots_storage::inventory::{ StockEvidenceOutcome, StockEvidenceReceipt, StockEvidenceRequest, }; @@ -55,7 +57,8 @@ impl<'a> Stock<'a> { ) .await } - /// Applies an explicit positive addition or available-stock removal. + /// Applies an explicit positive addition or physical-stock removal. + /// Qualified hosts retain deficits if installed original backing is lost. pub async fn adjust( &self, context: CommandContext, @@ -67,7 +70,8 @@ impl<'a> Stock<'a> { } self.correct(context, minimum, input).await } - /// Records physical spoilage without silently spending held quantities. + /// Records physical spoilage and retains affected installed obligations. + /// Preparatory and quarantine claims require their explicit resolution. pub async fn spoil( &self, context: CommandContext, diff --git a/crates/storage/src/inventory.rs b/crates/storage/src/inventory.rs @@ -9,7 +9,9 @@ pub use radroots_protocol::command::{CapturedInventoryCommand, InventoryCommandK use radroots_transport::BoxFuture; mod abort; mod acceptance; +mod deficit; mod delivery; +pub use deficit::StockDeficit; mod delivery_state; mod envelope; mod evidence; @@ -204,6 +206,7 @@ pub struct InventoryReceipt { attempt: CommitAttempt, pool: PoolSnapshot, kind: InventoryCommandKind, + deficits: Vec<StockDeficit>, mapping: Option<radroots_domain::stock::OptionPoolMapping>, checkpoint: InventoryCheckpointRef, } @@ -229,6 +232,7 @@ impl InventoryReceipt { kind, pool, mapping: None, + deficits: Vec::new(), checkpoint, }) } @@ -253,9 +257,38 @@ impl InventoryReceipt { pool, kind: InventoryCommandKind::MapOption, mapping: Some(mapping), + deficits: Vec::new(), checkpoint, }) } + /// Adds retained allocation metadata only; this creates no effect or authority. + pub fn with_deficits(mut self, deficits: Vec<StockDeficit>) -> Result<Self, OperationError> { + if deficits.len() > 32 + || deficits.iter().any(|value| { + value.pool() != self.pool.id() + || value.backing().deficit() == radroots_domain::stock::StockTicks::ZERO + }) + || deficits.iter().enumerate().any(|(index, value)| { + deficits[..index].iter().any(|prior| { + prior.acceptance() == value.acceptance() && prior.line() == value.line() + }) + }) + || (!deficits.is_empty() + && !matches!( + self.kind, + InventoryCommandKind::SpoilStock | InventoryCommandKind::RemoveStock + )) + { + return Err(OperationError::RecoveryRequired); + } + self.deficits = deficits; + Ok(self) + } + /// Deficits caused by this transaction at its retained checkpoint. + /// These are not a current full-obligation balance or disclosure grant. + pub fn deficits(&self) -> &[StockDeficit] { + &self.deficits + } /// Original private mapping effect; absent for physical inventory commands. pub const fn mapping(&self) -> Option<radroots_domain::stock::OptionPoolMapping> { self.mapping diff --git a/crates/storage/src/inventory/deficit.rs b/crates/storage/src/inventory/deficit.rs @@ -0,0 +1,51 @@ +use radroots_domain::{AgreementLineId, OperationId, PoolId, stock::ObligationBacking}; + +/// A retained correction's original-allocation deficit at that transaction. +/// This is historical effect metadata, not a current backing or authority proof. +#[derive(Clone, Copy, Eq, PartialEq)] +pub struct StockDeficit { + acceptance: OperationId, + installation: OperationId, + line: AgreementLineId, + pool: PoolId, + backing: ObligationBacking, +} +impl StockDeficit { + /// Reconstructs metadata only. The store must independently authenticate + /// the original allocation, correction, transaction and current access. + pub const fn from_host_metadata( + acceptance: OperationId, + installation: OperationId, + line: AgreementLineId, + pool: PoolId, + backing: ObligationBacking, + ) -> Self { + Self { + acceptance, + installation, + line, + pool, + backing, + } + } + pub const fn acceptance(self) -> OperationId { + self.acceptance + } + pub const fn installation(self) -> OperationId { + self.installation + } + pub const fn line(self) -> AgreementLineId { + self.line + } + pub const fn pool(self) -> PoolId { + self.pool + } + pub const fn backing(self) -> ObligationBacking { + self.backing + } +} +impl core::fmt::Debug for StockDeficit { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + f.write_str("StockDeficit(<private effect metadata>)") + } +} 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..=18) { + if matches!(version, 4..=19) { use sha2::{Digest, Sha256}; let namespace = radroots_storage::workspace::seller_stock::SellerAuthoritySelection::from_attachment( @@ -750,8 +750,10 @@ fn stock_root_context( b"radroots.seller_stock.authority_context.v16\0".as_slice() } else if version == 17 { b"radroots.seller_stock.authority_context.v17\0".as_slice() - } else { + } else if version == 18 { b"radroots.seller_stock.authority_context.v18\0".as_slice() + } else { + b"radroots.seller_stock.authority_context.v19\0".as_slice() }); hash.update(wire.as_slice()); let subject = radroots_domain::CommandDigest::from_bytes(hash.finalize().into()); @@ -788,8 +790,10 @@ fn stock_root_context( "radroots.seller_stock.authority.v16" } else if version == 17 { "radroots.seller_stock.authority.v17" - } else { + } else if version == 18 { "radroots.seller_stock.authority.v18" + } else { + "radroots.seller_stock.authority.v19" }) .map_err(|_| WorkspaceError::InvalidInput)?, )); @@ -907,12 +911,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..=18, "pool") => return Ok(original), + (2, _) | (3..=19, "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..=18, "operation") => "radroots.seller_stock.operation.v5", - (3..=18, "movement") => "radroots.seller_stock.movement.v2", + (7..=19, "operation") => "radroots.seller_stock.operation.v5", + (3..=19, "movement") => "radroots.seller_stock.movement.v2", _ => return Err(WorkspaceError::RecoveryRequired), }; Ok(EnvelopeContext::new( diff --git a/crates/storage_sqlite/src/workspace/stock.rs b/crates/storage_sqlite/src/workspace/stock.rs @@ -445,10 +445,10 @@ struct AuthorityV8 { } impl Authority { fn encode(&self) -> Result<zeroize::Zeroizing<Vec<u8>>, WorkspaceError> { - if !matches!(self.catalog, 8..=18) && self.public_sources.is_some() { + if !matches!(self.catalog, 8..=19) && self.public_sources.is_some() { return Err(WorkspaceError::RecoveryRequired); } - if matches!(self.catalog, 8..=18) { + if matches!(self.catalog, 8..=19) { let public_sources = self .public_sources .ok_or(WorkspaceError::RecoveryRequired)?; @@ -700,7 +700,7 @@ impl Authority { public_sources: None, } } - 8..=18 => { + 8..=19 => { let value: AuthorityV8 = closed_record(bytes)?; value.public_sources.validate()?; if i64::from(value.version) != catalog @@ -831,7 +831,7 @@ pub(super) async fn initialize( .await .map_err(|_| WorkspaceError::StorageFailure)?; } - sqlx::query("PRAGMA user_version=18") + sqlx::query("PRAGMA user_version=19") .execute(&mut *tx) .await .map_err(|_| WorkspaceError::StorageFailure)?; diff --git a/crates/storage_sqlite/src/workspace/stock/consent.rs b/crates/storage_sqlite/src/workspace/stock/consent.rs @@ -424,7 +424,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..=18) { + if !matches!(access.authority.catalog, 7..=19) { return Err(OperationError::UnsupportedOperation); } if !access.record.administrator() @@ -685,7 +685,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..=18) { + if !matches!(access.authority.catalog, 7..=19) { return Err(OperationError::UnsupportedOperation); } if !access.record.administrator() && access.current.input().grant_id() != subject { diff --git a/crates/storage_sqlite/src/workspace/stock/delivery.rs b/crates/storage_sqlite/src/workspace/stock/delivery.rs @@ -146,6 +146,7 @@ async fn confirm_record( } fn history(frame: ledger::Retained, projection: Projection) -> ledger::HistoryEntry { ledger::HistoryEntry { + deficit: None, delivery: Some(projection), envelope: None, aborted: None, @@ -181,7 +182,7 @@ impl Prepared { append: Append, body: &[u8], ) -> Result<Self, OperationError> { - if !matches!(authority.catalog, 17 | 18) { + if !matches!(authority.catalog, 17..=19) { return Err(OperationError::UnsupportedOperation); } let before = authority diff --git a/crates/storage_sqlite/src/workspace/stock/delivery/observation_operations.rs b/crates/storage_sqlite/src/workspace/stock/delivery/observation_operations.rs @@ -18,7 +18,7 @@ pub(in crate::workspace) async fn capture_observation( } let (authority, credential, root, grant) = super::super::read(turn.connection, input, turn.crypto).await?; - if !matches!(authority.catalog, 17 | 18) { + if !matches!(authority.catalog, 17..=19) { return Err(OperationError::UnsupportedOperation); } credential.authenticate_retained_capture(input, &proof)?; diff --git a/crates/storage_sqlite/src/workspace/stock/delivery/operations.rs b/crates/storage_sqlite/src/workspace/stock/delivery/operations.rs @@ -11,7 +11,7 @@ async fn authorized( .fetch_one(&mut *turn.connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - if !matches!(catalog, 17 | 18) { + if !matches!(catalog, 17..=19) { return Err(OperationError::UnsupportedOperation); } let loading = signing_plan::load( @@ -23,7 +23,7 @@ async fn authorized( proof, ); let auth = Box::pin(loading).await?; - if !matches!(auth.access.authority.catalog, 17 | 18) { + if !matches!(auth.access.authority.catalog, 17..=19) { return Err(OperationError::UnsupportedOperation); } if !auth diff --git a/crates/storage_sqlite/src/workspace/stock/envelope.rs b/crates/storage_sqlite/src/workspace/stock/envelope.rs @@ -188,7 +188,7 @@ pub(in crate::workspace) async fn capture( return Err(OperationError::Unauthorized); } let (authority, record, root, grant) = super::read(turn.connection, input, turn.crypto).await?; - if !matches!(authority.catalog, 16..=18) { + if !matches!(authority.catalog, 16..=19) { return Err(OperationError::UnsupportedOperation); } record.authenticate_retained_capture(input, &proof)?; @@ -368,7 +368,7 @@ pub(in crate::workspace) async fn read( .fetch_one(&mut *turn.connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - if !matches!(catalog, 16..=18) { + if !matches!(catalog, 16..=19) { return Err(OperationError::UnsupportedOperation); } let auth = Box::pin(super::signing_plan::load( @@ -380,7 +380,7 @@ pub(in crate::workspace) async fn read( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 16..=18) { + if !matches!(auth.access.authority.catalog, 16..=19) { return Err(OperationError::UnsupportedOperation); } let witness = Box::pin(full_proof(turn, &request, &auth.access.authority)).await?; diff --git a/crates/storage_sqlite/src/workspace/stock/envelope/codec.rs b/crates/storage_sqlite/src/workspace/stock/envelope/codec.rs @@ -195,6 +195,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + deficit: None, delivery: None, envelope: Some(self.projection), aborted: None, diff --git a/crates/storage_sqlite/src/workspace/stock/envelope/rewrap.rs b/crates/storage_sqlite/src/workspace/stock/envelope/rewrap.rs @@ -51,7 +51,7 @@ pub(super) async fn lookup( .fetch_one(&mut *turn.connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - if catalog != 18 { + if !matches!(catalog, 18 | 19) { return Err(OperationError::UnsupportedOperation); } let entry: Option<Vec<u8>> = sqlx::query_scalar("SELECT CASE WHEN length(entry_id)=32 THEN entry_id END FROM stock_rewraps WHERE actor=? AND operation_id=? AND recipient=? AND event_id=?") @@ -96,7 +96,7 @@ pub(super) async fn decode_entry( .await .map_err(|_| OperationError::RecoveryRequired)?; let body = frame.body.as_slice(); - if catalog != 18 + if !matches!(catalog, 18 | 19) || frame.header.family != ledger::Family::RewrappedEnvelope || !(PREFIX..=PREFIX + 65536).contains(&body.len()) || &body[..8] != MAGIC @@ -283,7 +283,7 @@ async fn query( .fetch_one(&mut *turn.connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - if catalog != 18 { + if !matches!(catalog, 18 | 19) { return Err(OperationError::UnsupportedOperation); } let auth = Box::pin(super::super::signing_plan::load( @@ -295,7 +295,7 @@ async fn query( proof, )) .await?; - if auth.access.authority.catalog != 18 + if !matches!(auth.access.authority.catalog, 18 | 19) || !auth .original .receipt diff --git a/crates/storage_sqlite/src/workspace/stock/envelope/rewrap_admission.rs b/crates/storage_sqlite/src/workspace/stock/envelope/rewrap_admission.rs @@ -86,7 +86,7 @@ pub(super) async fn read_entry( .ok_or(OperationError::RecoveryRequired)?; let (authority, _, _, _) = super::super::read(connection, input, crypto).await?; let body = frame.body.as_slice(); - if catalog != 18 + if !matches!(catalog, 18 | 19) || frame.header.family != ledger::Family::RewrapAdmission || body.len() != BYTES || &body[..8] != MAGIC @@ -223,6 +223,7 @@ impl Admitted { } pub(super) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + deficit: None, envelope: Some(self.projection), delivery: None, aborted: None, @@ -255,7 +256,7 @@ pub(in crate::workspace) async fn prepare( .fetch_one(&mut *turn.connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - if catalog != 18 { + if !matches!(catalog, 18 | 19) { return Err(OperationError::UnsupportedOperation); } let auth = Box::pin(super::super::signing_plan::load( @@ -267,7 +268,7 @@ pub(in crate::workspace) async fn prepare( &proof, )) .await?; - if auth.access.authority.catalog != 18 + if !matches!(auth.access.authority.catalog, 18 | 19) || !auth .original .receipt @@ -458,7 +459,7 @@ pub(in crate::workspace) async fn reconcile( } let (authority, record, root, grant) = super::super::read(turn.connection, input, turn.crypto).await?; - if authority.catalog != 18 { + if !matches!(authority.catalog, 18 | 19) { return Err(OperationError::UnsupportedOperation); } record.authenticate_retained_capture(input, &proof)?; diff --git a/crates/storage_sqlite/src/workspace/stock/envelope/rewrap_capture.rs b/crates/storage_sqlite/src/workspace/stock/envelope/rewrap_capture.rs @@ -25,7 +25,7 @@ pub(in crate::workspace::stock::envelope) async fn capture( } let (authority, record, root, grant) = super::super::super::read(turn.connection, input, turn.crypto).await?; - if authority.catalog != 18 { + if !matches!(authority.catalog, 18 | 19) { return Err(OperationError::UnsupportedOperation); } record.authenticate_retained_capture(input, &proof)?; 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..=18).contains(&authority.catalog) { + if !(4..=19).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 @@ -4,6 +4,7 @@ pub(in crate::workspace) mod acceptance; pub(in crate::workspace::stock) mod backing; pub(super) mod checkpoint; mod codec; +pub(in crate::workspace::stock) mod deficit; pub(in crate::workspace) mod installation; mod mapping; mod plan; @@ -40,6 +41,17 @@ pub(super) async fn check_head( checkpoint: InventoryCheckpoint, ) -> Result<(), WorkspaceError> { super::issuance::check_counts(connection).await?; + let catalog: i64 = sqlx::query_scalar("PRAGMA user_version") + .fetch_one(&mut *connection) + .await + .map_err(|_| WorkspaceError::RecoveryRequired)?; + if catalog == 19 { + let invalid: i64 = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM stock_deficits d LEFT JOIN stock_operations o ON o.actor=d.actor AND o.operation_id=d.operation_id WHERE o.family IS NULL OR o.family!=0) OR EXISTS(SELECT 1 FROM pragma_foreign_key_check('stock_deficits'))") + .fetch_one(&mut *connection).await.map_err(|_| WorkspaceError::RecoveryRequired)?; + if invalid != 0 { + return Err(WorkspaceError::RecoveryRequired); + } + } if let Some(expected) = checkpoint.orders { let count: i64 = sqlx::query_scalar("SELECT count(*) FROM stock_orders") .fetch_one(&mut *connection) @@ -189,7 +201,7 @@ impl Turn<'_> { return Err(OperationError::UnsupportedOperation); } - let minimum_proof = Box::pin(authenticate_minimum( + let mut minimum_proof = Box::pin(authenticate_minimum( self.connection, self.selected, self.crypto, @@ -199,6 +211,22 @@ impl Turn<'_> { )) .await?; self.namespace.validate()?; + if authority.catalog == 19 && minimum.revision().get() != 0 { + let genesis = radroots_storage::inventory::InventoryCheckpointRef::from_host_metadata( + self.selected.authority(), + Sequence::new(0).map_err(|_| OperationError::RecoveryRequired)?, + None, + )?; + minimum_proof = Box::pin(authenticate_minimum( + self.connection, + self.selected, + self.crypto, + &current, + &authority, + genesis, + )) + .await?; + } Ok(Access { authority, record, @@ -319,7 +347,28 @@ impl Turn<'_> { ) .await?, ); - let (pool, quantity, movement_kind) = plan_effect(request.command(), before)?; + let losses = if catalog == 19 { + Box::pin(deficit::plan( + self.connection, + self.selected, + self.crypto, + request.command(), + before, + access + .authority + .inventory + .ok_or(OperationError::UnsupportedOperation)? + .revision + .get() + .checked_add(1) + .ok_or(WorkspaceError::Capacity)?, + )) + .await? + } else { + Vec::new() + }; + let (pool, quantity, movement_kind) = + plan_effect(request.command(), before, deficit::quantity(&losses)?)?; let mapping = mapping::prepare(self, &access, &request, pool).await?; let label = if let Some(input) = request.command().creation_input() { input.label() @@ -386,6 +435,8 @@ impl Turn<'_> { receipt_checkpoint, )? }; + let receipt = + receipt.with_deficits(losses.iter().map(|loss| loss.metadata(pool.id())).collect())?; let protected_pool = if mapping.is_some() { None } else { @@ -451,8 +502,9 @@ impl Turn<'_> { catalog, access.authority.authorization, mapping.as_ref().map(|value| value.effect), + &losses, )?; - let framed = if matches!(catalog, 7..=18) { + let framed = if matches!(catalog, 7..=19) { Some( super::ledger::prepare( self.crypto, @@ -575,6 +627,7 @@ impl Turn<'_> { sqlx::query("INSERT INTO stock_operations(actor,grant_id,operation_id,command_digest,transaction_id,pool_id,revision,chain_head,protected) VALUES(?,?,?,?,?,?,?,?,?)") .bind(request.binding().actor().participant().as_bytes().as_slice()).bind(request.binding().input().grant_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(digest.as_bytes().as_slice()).bind(transaction.as_bytes().as_slice()).bind(pool.id().as_bytes().as_slice()).bind(revision.get() as i64).bind(head.as_bytes().as_slice()).bind(protected_operation).execute(&mut *tx).await.map_err(|_|OperationError::StorageFailure)?; if let Some(framed)=&framed {framed.insert_parts(&mut tx,request.binding().actor(),operation).await?;} + if catalog == 19 { deficit::insert(&mut tx, request.binding().actor().participant(), operation, pool.id(), &losses).await?; } if let (Some(id),Some(protected))=(movement,protected_movement) { sqlx::query("INSERT INTO stock_movements(movement_id,actor,grant_id,operation_id,pool_id,protected) VALUES(?,?,?,?,?,?)").bind(id.as_slice()).bind(request.binding().actor().participant().as_bytes().as_slice()).bind(request.binding().input().grant_id().as_bytes().as_slice()).bind(operation.as_bytes().as_slice()).bind(pool.id().as_bytes().as_slice()).bind(protected).execute(&mut *tx).await.map_err(|_|OperationError::StorageFailure)?; } @@ -825,6 +878,7 @@ async fn read_pool( })) } pub(super) struct Retained { + pub(super) deficit_projection: Option<[u8; 32]>, pub(super) frame: Option<(super::ledger::Header, [u8; 32])>, pub(super) original_grant: GrantId, pub(super) previous_head: Option<CheckpointDigest>, @@ -866,7 +920,7 @@ pub(super) async fn read_operation( .fetch_one(&mut *connection) .await .map_err(|_| OperationError::RecoveryRequired)?; - let (frame, request, decoded) = if matches!(catalog, 7..=18) { + let (frame, request, decoded) = if matches!(catalog, 7..=19) { let retained = super::ledger::read(connection, input, crypto, operation) .await? .ok_or(OperationError::RecoveryRequired)?; @@ -913,8 +967,41 @@ pub(super) async fn read_operation( if original_grant != request.binding().input().grant_id().as_bytes() { return Err(OperationError::RecoveryRequired); } - let (planned, quantity, movement_kind) = plan_effect(request.command(), decoded.before) + let deficit_projection = if catalog == 19 { + let expected = Box::pin(deficit::plan( + connection, + input, + crypto, + request.command(), + decoded.before, + decoded.revision.get(), + )) + .await .map_err(|_| OperationError::RecoveryRequired)?; + if expected != decoded.losses { + return Err(OperationError::RecoveryRequired); + } + let digest = deficit::digest(decoded.pool.id(), &decoded.losses)?; + if deficit::raw_projection( + connection, + binding.actor().participant().as_bytes(), + operation.as_bytes(), + ) + .await? + != digest + { + return Err(OperationError::RecoveryRequired); + } + Some(digest) + } else { + None + }; + let (planned, quantity, movement_kind) = plan_effect( + request.command(), + decoded.before, + deficit::quantity(&decoded.losses)?, + ) + .map_err(|_| OperationError::RecoveryRequired)?; let expected_mapping = mapping::effect( request.command(), decoded.pool, @@ -1020,7 +1107,15 @@ pub(super) async fn read_operation( checkpoint, )? }; + let receipt = receipt.with_deficits( + decoded + .losses + .iter() + .map(|loss| loss.metadata(decoded.pool.id())) + .collect(), + )?; Ok(Some(Retained { + deficit_projection, frame, original_grant: request.binding().input().grant_id(), previous_head: decoded.previous_head, 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..=18) { + if !matches!(auth.access.authority.catalog, 13..=19) { 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..=18) { + if !matches!(auth.access.authority.catalog, 13..=19) { 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 { + deficit: None, delivery: None, envelope: None, preparation: 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 !matches!(auth.access.authority.catalog, 15..=18) { + if !matches!(auth.access.authority.catalog, 15..=19) { return Err(OperationError::RecoveryRequired); } if at < original.report.claimed_at() { @@ -180,7 +180,7 @@ pub(super) async fn prepare( } } } - let payload = if matches!(auth.access.authority.catalog, 15..=18) { + let payload = if matches!(auth.access.authority.catalog, 15..=19) { 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 !matches!(catalog, 15..=18) { + if !matches!(catalog, 15..=19) { 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..=18) { + if !matches!(access.authority.catalog, 9..=19) { 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 @@ -29,6 +29,13 @@ impl Retained { .map(|effect| (effect.before().id(), effect.quantity())) .collect()) } + pub(in crate::workspace::stock) fn original_allocations( + &self, + ) -> Result<Vec<radroots_domain::stock::StockAllocation>, OperationError> { + let body: Body = super::super::super::closed_record(&self.frame.body) + .map_err(|_| OperationError::RecoveryRequired)?; + Ok(body.allocations) + } pub fn command(&self) -> Result<CapturedOrderAcceptance, OperationError> { let body: Body = super::super::super::closed_record(&self.frame.body) .map_err(|_| OperationError::RecoveryRequired)?; @@ -104,6 +111,7 @@ impl Retained { ) }); ledger::HistoryEntry { + deficit: None, delivery: None, envelope: None, aborted: None, @@ -325,7 +333,7 @@ pub(in crate::workspace::stock) async fn read( .map_err(|_| OperationError::RecoveryRequired)?; let operation_keyed = match catalog { 1..=12 => false, - 13..=18 => true, + 13..=19 => 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 @@ -96,6 +96,12 @@ impl MinimumProof { super::super::ledger::raw_parts_witness(connection, &actor, &operation).await?; hash.update([parts]); hash.update(witness); + if catalog == 19 && family == 0 { + hash.update(b"D19"); + hash.update( + super::deficit::raw_projection(connection, &actor, &operation).await?, + ); + } if matches!(family, 11 | 12) { hash.update( super::super::delivery::raw_projection( @@ -187,7 +193,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..=18) { + if !matches!(authority.catalog, 13..=19) { return Err(OperationError::UnsupportedOperation); } let current = authority @@ -256,7 +262,7 @@ pub(super) async fn authenticate_order_absence( let current = authority .inventory .ok_or(OperationError::UnsupportedOperation)?; - if !matches!(authority.catalog, 9..=18) { + if !matches!(authority.catalog, 9..=19) { return Err(OperationError::UnsupportedOperation); } let upper = current.revision.get(); @@ -338,7 +344,7 @@ pub(in crate::workspace::stock) async fn authenticate_identity( let current = authority .inventory .ok_or(OperationError::UnsupportedOperation)?; - let multipart = matches!(authority.catalog, 7..=18); + let multipart = matches!(authority.catalog, 7..=19); let upper = current.revision.get(); let mut previous = current.head; let mut hash = history_hash(multipart); @@ -515,7 +521,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..=18); + let multipart = matches!(authority.catalog, 7..=19); let mut hash = history_hash(multipart); let mut expected = current.head; for revision in (lower..=upper).rev() { @@ -779,7 +785,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..=18); + let multipart = matches!(authority.catalog, 7..=19); 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 @@ -106,11 +106,23 @@ struct OperationV4<'a> { #[serde(deserialize_with = "super::required_nullable")] mapping: Option<super::mapping::MappingEffect>, } +#[derive(Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +struct OperationV5<'a> { + version: u8, + authorization: StockAuthorizationRef, + #[serde(borrow)] + operation: &'a RawValue, + #[serde(deserialize_with = "super::required_nullable")] + mapping: Option<super::mapping::MappingEffect>, + losses: Vec<super::deficit::Loss>, +} pub(super) fn encode_operation( value: &OperationWire<'_>, catalog: i64, authorization: Option<StockAuthorizationRef>, mapping: Option<super::mapping::MappingEffect>, + losses: &[super::deficit::Loss], ) -> Result<zeroize::Zeroizing<Vec<u8>>, OperationError> { let clear = super::encode(value)?; if catalog < 4 { @@ -120,6 +132,18 @@ pub(super) fn encode_operation( String::from_utf8(clear.to_vec()).map_err(|_| OperationError::StorageFailure)?, ) .map_err(|_| OperationError::StorageFailure)?; + if catalog == 19 { + return super::encode(&OperationV5 { + version: 5, + authorization: authorization.ok_or(OperationError::RecoveryRequired)?, + operation: &raw, + mapping, + losses: losses.to_vec(), + }); + } + if !losses.is_empty() { + return Err(OperationError::UnsupportedOperation); + } if matches!(catalog, 5..=18) { return super::encode(&OperationV4 { version: 4, @@ -135,6 +159,7 @@ pub(super) fn encode_operation( }) } pub(super) struct OperationData { + pub(super) losses: Vec<super::deficit::Loss>, pub(super) mapping: Option<super::mapping::MappingEffect>, pub(super) authorization: Option<StockAuthorizationRef>, pub(super) version: u8, @@ -158,6 +183,18 @@ fn decode_inner( catalog: i64, allow_mapping: bool, ) -> Result<(InventoryRequest, OperationData), OperationError> { + if catalog == 19 { + let decoded: OperationV5<'_> = + super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?; + if decoded.version != 5 || decoded.losses.len() > super::deficit::MAX_LOSSES { + return Err(OperationError::RecoveryRequired); + } + let (request, mut data) = decode_inner(decoded.operation.get().as_bytes(), 3, true)?; + data.authorization = Some(decoded.authorization); + data.mapping = decoded.mapping; + data.losses = decoded.losses; + return Ok((request, data)); + } if matches!(catalog, 5..=18) { let decoded: OperationV4<'_> = super::closed_record(bytes).map_err(|_| OperationError::RecoveryRequired)?; @@ -213,6 +250,7 @@ fn decode_inner( Ok(( request, OperationData { + losses: Vec::new(), mapping: None, authorization: None, version: decoded.version, diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/deficit.rs b/crates/storage_sqlite/src/workspace/stock/inventory/deficit.rs @@ -0,0 +1,333 @@ +//! Catalog19 owner-private losses against retained, installed original allocations. +//! The SQL index is only a projection. Full protected ancestry authenticates its +//! complete contents and is reread under the same final transaction fence. +use super::*; +use radroots_domain::{ + AgreementLineId, PublicKey, + stock::{ObligationBacking, StockTicks}, +}; + +pub(super) const MAX_LOSSES: usize = 32; + +#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub(super) struct Loss { + acceptance: OperationId, + installation: OperationId, + line: AgreementLineId, + before: ObligationBacking, + after: ObligationBacking, +} +impl Loss { + fn quantity(&self) -> Result<StockTicks, OperationError> { + let quantity = self + .before + .backed() + .checked_sub(self.after.backed()) + .map_err(|_| OperationError::RecoveryRequired)?; + if quantity == StockTicks::ZERO + || self.before.lose_backing(quantity).ok() != Some(self.after) + { + return Err(OperationError::RecoveryRequired); + } + Ok(quantity) + } + pub(super) fn metadata(&self, pool: PoolId) -> radroots_storage::inventory::StockDeficit { + radroots_storage::inventory::StockDeficit::from_host_metadata( + self.acceptance, + self.installation, + self.line, + pool, + self.after, + ) + } +} + +/// Available stock is corrected first. Any remainder selects still-backed +/// installed allocations by original installation revision then original line +/// order. Preparatory and quarantined claims are never silently depleted. +pub(super) async fn plan( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + command: &radroots_storage::inventory::CapturedInventoryCommand, + before: Option<PoolSnapshot>, + before_revision: u64, +) -> Result<Vec<Loss>, OperationError> { + use radroots_domain::stock::AvailableStockCorrection; + let radroots_storage::inventory::CapturedInventoryCommand::CorrectPool(command) = command + else { + return Ok(Vec::new()); + }; + if command.input().kind() == AvailableStockCorrection::Add { + return Ok(Vec::new()); + } + let pool = before.ok_or(OperationError::StaleState)?; + if pool.id() != command.input().pool_id() + || pool.version() != command.input().expected_version() + { + return Err(OperationError::StaleState); + } + let quantity = pool + .unit() + .ticks_for( + radroots_domain::Quantity::new(command.input().amount().amount(), pool.unit().unit()) + .map_err(|_| OperationError::InvalidCommand)?, + ) + .map_err(|_| OperationError::InvalidCommand)?; + let physical = pool.balances().physical(); + if quantity <= physical.available { + return Ok(Vec::new()); + } + let mut remaining = quantity + .checked_sub(physical.available) + .map_err(|_| OperationError::InvalidCommand)?; + if remaining > physical.committed_hold { + return Err(OperationError::StaleState); + } + let mut cursor = 0_u64; + let mut losses = Vec::new(); + while remaining != StockTicks::ZERO { + let row = sqlx::query("SELECT revision,CASE WHEN length(operation_id)=32 THEN operation_id END FROM stock_operations WHERE family=6 AND revision>? AND revision<? ORDER BY revision LIMIT 1") + .bind(cursor as i64).bind(before_revision as i64).fetch_optional(&mut *connection).await + .map_err(|_| OperationError::RecoveryRequired)? + .ok_or(OperationError::RecoveryRequired)?; + let revision: i64 = row + .try_get(0) + .map_err(|_| OperationError::RecoveryRequired)?; + let entry: Vec<u8> = row + .try_get::<Option<Vec<u8>>, _>(1) + .map_err(|_| OperationError::RecoveryRequired)? + .ok_or(OperationError::RecoveryRequired)?; + if revision <= 0 { + return Err(OperationError::RecoveryRequired); + } + cursor = revision as u64; + let entry = OperationId::from_bytes( + entry + .try_into() + .map_err(|_| OperationError::RecoveryRequired)?, + ); + let installed = Box::pin(super::installation::read_entry( + connection, input, crypto, entry, + )) + .await? + .ok_or(OperationError::RecoveryRequired)?; + if installed.frame.header.revision.get() != cursor + || installed.frame.header.actor.participant() != input.participant() + { + return Err(OperationError::RecoveryRequired); + } + let acceptance = installed + .frame + .header + .related_operation + .ok_or(OperationError::RecoveryRequired)?; + let original = Box::pin(super::acceptance::codec::read( + connection, input, crypto, acceptance, + )) + .await? + .ok_or(OperationError::RecoveryRequired)?; + for allocation in original.original_allocations()? { + if allocation.pool_id() != pool.id() { + continue; + } + let prior = prior_quantity( + connection, + input, + crypto, + acceptance, + entry, + allocation.line_id(), + pool.id(), + before_revision, + ) + .await?; + let backed = allocation + .ticks() + .checked_sub(prior) + .map_err(|_| OperationError::RecoveryRequired)?; + if backed == StockTicks::ZERO { + continue; + } + if losses.len() == MAX_LOSSES { + return Err(WorkspaceError::Capacity.into()); + } + let quantity = remaining.min(backed); + let before = ObligationBacking::new(allocation.ticks(), backed, prior) + .map_err(|_| OperationError::RecoveryRequired)?; + let after = before + .lose_backing(quantity) + .map_err(|_| OperationError::RecoveryRequired)?; + losses.push(Loss { + acceptance, + installation: entry, + line: allocation.line_id(), + before, + after, + }); + remaining = remaining + .checked_sub(quantity) + .map_err(|_| OperationError::RecoveryRequired)?; + if remaining == StockTicks::ZERO { + break; + } + } + } + Ok(losses) +} + +pub(super) fn quantity(losses: &[Loss]) -> Result<StockTicks, OperationError> { + if losses.len() > MAX_LOSSES { + return Err(WorkspaceError::Capacity.into()); + } + let mut quantity = StockTicks::ZERO; + for (index, loss) in losses.iter().enumerate() { + if losses[..index] + .iter() + .any(|prior| prior.acceptance == loss.acceptance && prior.line == loss.line) + { + return Err(OperationError::RecoveryRequired); + } + quantity = quantity + .checked_add(loss.quantity()?) + .map_err(|_| OperationError::RecoveryRequired)?; + } + Ok(quantity) +} + +/// Index rows contain only identities. Actual prior quantities remain inside +/// bounded protected operation bodies; no plaintext numeric sum is authoritative. +#[expect( + clippy::too_many_arguments, + reason = "exact original allocation and revision boundary" +)] +async fn prior_quantity( + connection: &mut SqliteConnection, + input: &AttachmentInput, + crypto: &WorkspaceCrypto, + acceptance: OperationId, + installation: OperationId, + line: AgreementLineId, + pool: PoolId, + before_revision: u64, +) -> Result<StockTicks, OperationError> { + let mut quantity = StockTicks::ZERO; + let mut cursor = 0_u64; + loop { + let row = sqlx::query("SELECT o.revision,CASE WHEN length(o.operation_id)=32 THEN o.operation_id END FROM stock_deficits d JOIN stock_operations o ON o.actor=d.actor AND o.operation_id=d.operation_id WHERE d.actor=? AND d.acceptance_id=? AND d.installation_id=? AND d.line_id=? AND d.pool_id=? AND o.revision>? AND o.revision<? ORDER BY o.revision LIMIT 1") + .bind(input.participant().as_bytes().as_slice()).bind(acceptance.as_bytes().as_slice()) + .bind(installation.as_bytes().as_slice()).bind(line.as_bytes().as_slice()) + .bind(pool.as_bytes().as_slice()).bind(cursor as i64).bind(before_revision as i64) + .fetch_optional(&mut *connection).await.map_err(|_| OperationError::RecoveryRequired)?; + let Some(row) = row else { + return Ok(quantity); + }; + let revision: i64 = row + .try_get(0) + .map_err(|_| OperationError::RecoveryRequired)?; + let operation: Vec<u8> = row + .try_get::<Option<Vec<u8>>, _>(1) + .map_err(|_| OperationError::RecoveryRequired)? + .ok_or(OperationError::RecoveryRequired)?; + if revision <= 0 { + return Err(OperationError::RecoveryRequired); + } + cursor = revision as u64; + let operation = OperationId::from_bytes( + operation + .try_into() + .map_err(|_| OperationError::RecoveryRequired)?, + ); + let frame = super::super::ledger::read(connection, input, crypto, operation) + .await? + .ok_or(OperationError::RecoveryRequired)?; + let (request, data) = super::codec::decode_operation(&frame.body, 19)?; + if frame.header.family != super::super::ledger::Family::Inventory + || frame.header.actor.participant() != input.participant() + || frame.header.revision.get() != cursor + || frame.header.operation != operation + || request.command().context().operation_id() != operation + || request.command().digest() != frame.header.digest + || data.revision != frame.header.revision + || data.head != frame.header.head + || data.pool.id() != pool + { + return Err(OperationError::RecoveryRequired); + } + let mut matching = data.losses.iter().filter(|loss| { + loss.acceptance == acceptance && loss.installation == installation && loss.line == line + }); + let loss = matching.next().ok_or(OperationError::RecoveryRequired)?; + if matching.next().is_some() { + return Err(OperationError::RecoveryRequired); + } + quantity = quantity + .checked_add(loss.quantity()?) + .map_err(|_| OperationError::RecoveryRequired)?; + } +} + +fn projection_hash() -> Sha256 { + let mut hash = Sha256::new(); + hash.update(b"radroots.seller_stock.deficit_projection.v1\0"); + hash +} +pub(super) fn digest(pool: PoolId, losses: &[Loss]) -> Result<[u8; 32], OperationError> { + let mut ordered: Vec<_> = losses.iter().collect(); + ordered.sort_by_key(|loss| (loss.acceptance, loss.line)); + let mut hash = projection_hash(); + hash.update((ordered.len() as u64).to_be_bytes()); + for loss in ordered { + hash.update(loss.acceptance.as_bytes()); + hash.update(loss.installation.as_bytes()); + hash.update(loss.line.as_bytes()); + hash.update(pool.as_bytes()); + } + Ok(hash.finalize().into()) +} +pub(in crate::workspace::stock) async fn raw_projection( + connection: &mut SqliteConnection, + actor: &[u8; 32], + operation: &[u8; 32], +) -> Result<[u8; 32], OperationError> { + let rows = sqlx::query("SELECT CASE WHEN length(acceptance_id)=32 THEN acceptance_id END,CASE WHEN length(installation_id)=32 THEN installation_id END,CASE WHEN length(line_id)=32 THEN line_id END,CASE WHEN length(pool_id)=32 THEN pool_id END FROM stock_deficits WHERE actor=? AND operation_id=? ORDER BY acceptance_id,line_id LIMIT 33") + .bind(actor.as_slice()).bind(operation.as_slice()).fetch_all(connection).await + .map_err(|_| OperationError::RecoveryRequired)?; + if rows.len() > MAX_LOSSES { + return Err(OperationError::RecoveryRequired); + } + let mut hash = projection_hash(); + hash.update((rows.len() as u64).to_be_bytes()); + for row in rows { + for column in 0..4 { + let value: Vec<u8> = row + .try_get::<Option<Vec<u8>>, _>(column) + .map_err(|_| OperationError::RecoveryRequired)? + .ok_or(OperationError::RecoveryRequired)?; + hash.update(value); + } + } + Ok(hash.finalize().into()) +} +pub(super) async fn insert( + connection: &mut SqliteConnection, + actor: PublicKey, + operation: OperationId, + pool: PoolId, + losses: &[Loss], +) -> Result<(), OperationError> { + for loss in losses { + sqlx::query("INSERT INTO stock_deficits(actor,operation_id,acceptance_id,installation_id,line_id,pool_id) VALUES(?,?,?,?,?,?)") + .bind(actor.as_bytes().as_slice()).bind(operation.as_bytes().as_slice()) + .bind(loss.acceptance.as_bytes().as_slice()).bind(loss.installation.as_bytes().as_slice()) + .bind(loss.line.as_bytes().as_slice()).bind(pool.as_bytes().as_slice()) + .execute(&mut *connection).await.map_err(|_| OperationError::StorageFailure)?; + } + if raw_projection(connection, actor.as_bytes(), operation.as_bytes()).await? + != digest(pool, losses)? + { + return Err(OperationError::RecoveryRequired); + } + Ok(()) +} diff --git a/crates/storage_sqlite/src/workspace/stock/inventory/installation.rs b/crates/storage_sqlite/src/workspace/stock/inventory/installation.rs @@ -222,7 +222,7 @@ pub(in crate::workspace) async fn install( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 12..=18) { + if !matches!(auth.access.authority.catalog, 12..=19) { 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..=18) { + if !matches!(auth.access.authority.catalog, 12..=19) { 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 @@ -302,6 +302,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + deficit: None, delivery: None, envelope: None, aborted: 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..=18).contains(&access.authority.catalog) { + if !(5..=19).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,13 +3,14 @@ use super::*; pub(super) fn record_version(catalog: i64) -> Result<u8, OperationError> { match catalog { 2 => Ok(1), - 3..=18 => Ok(2), + 3..=19 => Ok(2), _ => Err(OperationError::RecoveryRequired), } } pub(super) fn plan_effect( command: &radroots_storage::inventory::CapturedInventoryCommand, before: Option<PoolSnapshot>, + held_loss: radroots_domain::stock::StockTicks, ) -> Result< ( PoolSnapshot, @@ -70,14 +71,51 @@ pub(super) fn plan_effect( .map_err(|_| OperationError::InvalidCommand)?, ) .map_err(|_| OperationError::InvalidCommand)?; - let balances = before - .balances() - .correct_available(input.kind(), quantity) - .map_err(|error| match error { - StockError::Underflow => OperationError::StaleState, - StockError::OutOfRange => WorkspaceError::Capacity.into(), - _ => OperationError::InvalidCommand, - })?; + let available_quantity = quantity + .checked_sub(held_loss) + .map_err(|_| OperationError::RecoveryRequired)?; + if held_loss != radroots_domain::stock::StockTicks::ZERO + && (input.kind() == AvailableStockCorrection::Add + || available_quantity != before.balances().physical().available) + { + return Err(OperationError::RecoveryRequired); + } + let balances = if available_quantity == radroots_domain::stock::StockTicks::ZERO { + before.balances() + } else { + before + .balances() + .correct_available(input.kind(), available_quantity) + .map_err(|error| match error { + StockError::Underflow => OperationError::StaleState, + StockError::OutOfRange => WorkspaceError::Capacity.into(), + _ => OperationError::InvalidCommand, + })? + }; + let balances = if held_loss == radroots_domain::stock::StockTicks::ZERO { + Ok(balances) + } else { + balances.deplete_held( + radroots_domain::stock::HeldStockCategory::Committed, + match input.kind() { + AvailableStockCorrection::Remove => { + radroots_domain::stock::HeldStockLoss::Removal + } + AvailableStockCorrection::Spoil => { + radroots_domain::stock::HeldStockLoss::Spoilage + } + AvailableStockCorrection::Add => { + return Err(OperationError::RecoveryRequired); + } + }, + held_loss, + ) + } + .map_err(|error| match error { + StockError::Underflow => OperationError::StaleState, + StockError::OutOfRange => WorkspaceError::Capacity.into(), + _ => OperationError::InvalidCommand, + })?; let version = Sequence::new( before .version() 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 { + deficit: None, delivery: None, envelope: None, aborted: None, diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/evidence/codec.rs b/crates/storage_sqlite/src/workspace/stock/issuance/evidence/codec.rs @@ -221,6 +221,7 @@ impl Retained { } pub(in crate::workspace::stock) fn into_history(self) -> ledger::HistoryEntry { ledger::HistoryEntry { + deficit: None, delivery: None, envelope: None, aborted: None, diff --git a/crates/storage_sqlite/src/workspace/stock/issuance/recovery.rs b/crates/storage_sqlite/src/workspace/stock/issuance/recovery.rs @@ -81,7 +81,7 @@ pub(in crate::workspace) async fn authorize( &proof, )) .await?; - if !matches!(auth.access.authority.catalog, 13..=18) { + if !matches!(auth.access.authority.catalog, 13..=19) { 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 { + deficit: None, delivery: None, envelope: None, aborted: 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 { + deficit: None, delivery: None, envelope: None, aborted: None, @@ -339,7 +340,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..=18) { + if !matches!(authority.catalog, 14..=19) { return Err(OperationError::UnsupportedOperation); } record.authenticate_retained_capture(input, &proof)?; @@ -493,7 +494,7 @@ pub(in crate::workspace) async fn read( &proof, ) .await?; - if !matches!(auth.access.authority.catalog, 14..=18) { + if !matches!(auth.access.authority.catalog, 14..=19) { 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 @@ -459,6 +459,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 deficit: Option<[u8; 32]>, pub delivery: Option<super::delivery::Projection>, pub envelope: Option<super::envelope::Projection>, pub aborted: Option<super::inventory::abort::Projection>, @@ -509,14 +510,14 @@ pub(super) async fn history( )) .await } - Some(2) if matches!(authority.catalog, 9..=18) => { + Some(2) if matches!(authority.catalog, 9..=19) => { Box::pin(history_family_2( connection, input, crypto, binding, authority, operation, )) .await } Some(2) => Err(OperationError::UnsupportedOperation), - Some(3) if matches!(authority.catalog, 10..=18) => { + Some(3) if matches!(authority.catalog, 10..=19) => { Box::pin(history_family_3( connection, input, crypto, binding, authority, operation, )) @@ -534,7 +535,7 @@ pub(super) async fn history( )) .await } - Some(6) if matches!(authority.catalog, 12..=18) => { + Some(6) if matches!(authority.catalog, 12..=19) => { Box::pin(history_family_6( connection, input, crypto, binding, authority, operation, )) @@ -606,6 +607,7 @@ async fn history_family_0( _ => return Err(OperationError::RecoveryRequired), }; Ok(Some(HistoryEntry { + deficit: retained.deficit_projection, delivery: None, envelope: None, aborted: None, @@ -652,6 +654,7 @@ async fn history_family_1( return Err(OperationError::RecoveryRequired); } Ok(Some(HistoryEntry { + deficit: None, delivery: None, envelope: None, aborted: None, @@ -909,6 +912,13 @@ impl HistoryEntry { )?; hash.update([header.parts]); hash.update(self.parts_witness); + if let Some(deficit) = self.deficit { + if header.family != Family::Inventory { + return Err(OperationError::RecoveryRequired); + } + hash.update(b"D19"); + hash.update(deficit); + } match (header.family, self.aborted) { (Family::AbortedPreparation, Some(projection)) => { hash.update(projection.digest(header.actor.participant(), header.operation)) 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..=18) || authority.public_sources.is_none() { + if !matches!(authority.catalog, 8..=19) || 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/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 = 18; +pub(super) const CURRENT: i64 = 19; 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"; @@ -32,7 +32,7 @@ pub(super) fn objects( ]; match version { 1 => {} - 2..=18 => objects.extend([ + 2..=19 => objects.extend([ ("table", "stock_pools", "stock_pools", POOLS), ( "table", @@ -97,7 +97,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..=18).contains(&version) { + if (6..=19).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)")); @@ -133,7 +133,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..=18) { + if matches!(version, 8..=19) { 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(( @@ -223,6 +223,9 @@ pub(super) fn objects( "CREATE UNIQUE INDEX stock_rewrap_entry ON stock_rewraps(actor,entry_id)", )); } + if version >= 19 { + objects.push(("table", "stock_deficits", "stock_deficits", "CREATE TABLE stock_deficits (actor BLOB NOT NULL CHECK(length(actor)=32), operation_id BLOB NOT NULL CHECK(length(operation_id)=32), acceptance_id BLOB NOT NULL CHECK(length(acceptance_id)=32), installation_id BLOB NOT NULL CHECK(length(installation_id)=32), line_id BLOB NOT NULL CHECK(length(line_id)=32), pool_id BLOB NOT NULL REFERENCES stock_pools(pool_id), PRIMARY KEY(actor,operation_id,acceptance_id,line_id), FOREIGN KEY(actor,operation_id) REFERENCES stock_operations(actor,operation_id), FOREIGN KEY(actor,acceptance_id) REFERENCES stock_orders(actor,operation_id), FOREIGN KEY(actor,installation_id) REFERENCES stock_operations(actor,operation_id)) STRICT, WITHOUT ROWID")); + } 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 @@ -148,7 +148,7 @@ pub(in crate::workspace::stock) async fn load( proof: &AttachmentProof, ) -> Result<Authorized, OperationError> { let access = Box::pin(turn.access(binding, minimum, proof)).await?; - if !matches!(access.authority.catalog, 10..=18) { + if !matches!(access.authority.catalog, 10..=19) { return Err(OperationError::UnsupportedOperation); } let original = Box::pin(acceptance::codec::read( 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 { + deficit: None, delivery: None, envelope: None, aborted: None, diff --git a/crates/storage_sqlite/src/workspace/stock/snapshot.rs b/crates/storage_sqlite/src/workspace/stock/snapshot.rs @@ -338,7 +338,7 @@ async fn read_authorized( .authority .inventory .ok_or(OperationError::RecoveryRequired)?; - let delivery = if matches!(auth.access.authority.catalog, 17 | 18) { + let delivery = if matches!(auth.access.authority.catalog, 17..=19) { Some(Box::new( Box::pin(super::delivery::snapshot( turn, 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..=18).contains(&authority.catalog) { + if !(6..=19).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..=18) { + if !matches!(access.authority.catalog, 8..=19) { 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..=18) { + if !matches!(access.authority.catalog, 8..=19) { 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 @@ -19,6 +19,7 @@ pub(in crate::workspace::stock) fn catalog_authority_schema(catalog: i64) -> &'s 16 => "radroots.seller_stock.authority.v16", 17 => "radroots.seller_stock.authority.v17", 18 => "radroots.seller_stock.authority.v18", + 19 => "radroots.seller_stock.authority.v19", _ => panic!("unsupported private stock fixture catalog"), } } @@ -300,7 +301,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, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(11).await; } // Exact catalog fixtures exercise retained/successor codecs; they are not @@ -345,7 +346,7 @@ pub(in crate::workspace::stock) async fn literal_enrollment_catalog( catalog: i64, enrollment: &StockEnrollment, ) { - assert!(matches!(catalog, 13..=17)); + assert!(matches!(catalog, 13..=18)); let record = enrollment.record.lock().unwrap().take().unwrap(); literal_catalog_contents( fixture, @@ -404,7 +405,7 @@ async fn literal_catalog_contents( if catalog >= 7 { value["inventory"]["orders"] = serde_json::json!("0"); } - if matches!(catalog, 8..=17) { + if matches!(catalog, 8..=18) { value["public_sources"] = serde_json::json!({"revision":"0","events":"0","heads":"0","parts":"0","head":null}); } @@ -454,6 +455,7 @@ async fn literal_catalog_contents( 15 => "PRAGMA user_version=15", 16 => "PRAGMA user_version=16", 17 => "PRAGMA user_version=17", + 18 => "PRAGMA user_version=18", _ => unreachable!(), }) .execute(&mut *tx) @@ -478,7 +480,7 @@ async fn retained_inventory_catalog(catalog: i64) { assert_eq!(authority.source_checkpoint().is_some(), catalog >= 6); assert_eq!( authority.listing_checkpoint().is_some(), - matches!(catalog, 8..=17) + matches!(catalog, 8..=18) ); let minimum = authority.inventory_checkpoint().unwrap(); let context = CommandContextData::new( @@ -721,7 +723,7 @@ async fn retained_inventory_catalog(catalog: i64) { .fetch_one(&mut connection) .await .unwrap(); - let old = if matches!(catalog, 7..=17) { + let old = if matches!(catalog, 7..=18) { let retained = ledger::read( &mut connection, &input, @@ -774,7 +776,7 @@ async fn retained_inventory_catalog(catalog: i64) { // inside a real SQL transaction. Row counts and the protected header // stay unchanged while one referenced ciphertext changes. for part in [false, true] { - if part && !matches!(catalog, 7..=17) { + if part && !matches!(catalog, 7..=18) { continue; } let (authority, _, _, _) = read(&mut connection, &input, &fixture.crypto()) @@ -854,6 +856,7 @@ async fn retained_inventory_catalog(catalog: i64) { 15 => "PRAGMA user_version=14", 16 => "PRAGMA user_version=15", 17 => "PRAGMA user_version=16", + 18 => "PRAGMA user_version=17", _ => unreachable!(), }) .execute(&mut connection) @@ -983,36 +986,42 @@ 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, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(12).await; } #[tokio::test] async fn retained_v13_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(13).await; } #[tokio::test] async fn retained_v14_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(14).await; } #[tokio::test] async fn retained_v15_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(15).await; } #[tokio::test] async fn retained_v16_authenticates_literal_catalog_and_original_inventory_records() { - assert_eq!(schema::CURRENT, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(16).await; } #[tokio::test] async fn retained_v17_authenticates_original_delivery_catalog_without_upgrade() { - assert_eq!(schema::CURRENT, 18); + assert_eq!(schema::CURRENT, 19); retained_inventory_catalog(17).await; } + +#[tokio::test] +async fn retained_v18_authenticates_original_rewrap_catalog_without_upgrade() { + assert_eq!(schema::CURRENT, 19); + retained_inventory_catalog(18).await; +} diff --git a/crates/storage_sqlite/src/workspace/worker.rs b/crates/storage_sqlite/src/workspace/worker.rs @@ -1595,6 +1595,12 @@ impl Handle { | Job::StockAgreementInstallation(..) | Job::AbortStockPreparation(..) | Job::StockPreparationAbort(..) => 1024 * 1024, + // Catalog19 inventory access authenticates original installed + // allocations and protected deficit history before its SQL fence. + Job::ExecuteInventory(..) + | Job::InventoryOperation(..) + | Job::Pool(..) + | Job::OptionMapping(..) => 1024 * 1024, Job::IssueStockConsent(..) | Job::PrepareStockAcceptance(..) => 512 * 1024, Job::BindStockSigningPlan(..) | Job::StockSigningPlan(..) @@ -1602,7 +1608,7 @@ impl Handle { | Job::StockSigningAttempt(..) | Job::AuthorizeStockExactReplay(..) => 1024 * 1024, Job::RetainStockSource(..) => 256 * 1024, - Job::ChangeStockGrant(..) | Job::ExecuteInventory(..) => 64 * 1024, + Job::ChangeStockGrant(..) => 64 * 1024, Job::CaptureDeliveryObservation(..) => 96 * 1024, Job::PrepareDelivery(..) => 1024 * 1024, Job::BindPlan(..) => 1024 * 1024, diff --git a/tools/xtask/src/architecture/decision_package.rs b/tools/xtask/src/architecture/decision_package.rs @@ -53,6 +53,21 @@ struct StockAccounting { released_deficit_adds_available: bool, loss_deletes_obligation: bool, native_atomic_allocation_and_identity_required: bool, + native_correction: NativeCorrection, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct NativeCorrection { + catalog: u8, + selection: String, + max_affected_allocations: u8, + protected_original_allocation_required: bool, + deficit_projection_in_final_transaction: bool, + quantity_storage: String, + worker_charge_bytes: u32, + preparatory_loss_implicit: bool, + quarantine_loss_implicit: bool, } #[derive(Deserialize)] @@ -729,6 +744,19 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { let p: Policy = serde_json::from_slice(raw) .map_err(|_| "shared SDK contract: invalid closed JSON shape".to_owned())?; let accounting = &p.stock_accounting; + let correction = &accounting.native_correction; + require( + correction.catalog == 19 + && correction.selection == "available_then_original_installation_and_allocation_order" + && correction.max_affected_allocations == 32 + && correction.protected_original_allocation_required + && correction.deficit_projection_in_final_transaction + && correction.quantity_storage == "protected_original_operation_body" + && correction.worker_charge_bytes == 1048576 + && !correction.preparatory_loss_implicit + && !correction.quarantine_loss_implicit, + "native correction original allocation, projection or category policy drift", + )?; require( accounting.value_owner == "radroots_domain.stock" && accounting.scope == "one_fixed_pool_unit" @@ -769,7 +797,7 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { envelope.journal_family == 10 && envelope.payload_version == 1 && envelope.payload_prefix_bytes == 213 - && envelope.supported_catalogs == [16, 17, 18] + && envelope.supported_catalogs == [16, 17, 18, 19] && envelope.retained_unsupported_catalogs == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] && envelope.raw_max_bytes == 65536 @@ -817,7 +845,7 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { && !rewrap.ordinary_status_is_atomic_absence_proof && rewrap.payload_version == 1 && rewrap.payload_prefix_bytes == 117 - && rewrap.supported_catalogs == [18] + && rewrap.supported_catalogs == [18, 19] && rewrap.retained_unsupported_catalogs == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] && rewrap.raw_max_bytes == 65536 @@ -845,7 +873,7 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { )?; let delivery = &stock.delivery; require( - delivery.supported_catalogs == [17, 18] + delivery.supported_catalogs == [17, 18, 19] && delivery.retained_unsupported_catalogs == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] && delivery.attempt_journal_family == 11 @@ -905,11 +933,11 @@ fn validate_bytes(raw: &[u8]) -> Result<(), String> { && stock.scope == "seller_stock_owner" && stock.database == "stock_execution_v1.sqlite" && stock.lock == ".stock_execution_v1.lock" - && stock.schema_version == 18 + && stock.schema_version == 19 && stock.retained_enrollment_versions == [1] && stock.retained_inventory_versions - == [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] - && stock.authority_payload_schema == "radroots.seller_stock.authority.v18" + == [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18] + && stock.authority_payload_schema == "radroots.seller_stock.authority.v19" && stock.public_source_payload_schemas == [ "radroots.seller_stock.public_source_event.v1", @@ -1188,7 +1216,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 == 18 + && stock.qualified_local_recovery.native_catalog == 19 && stock.qualified_local_recovery.worker_charge_bytes == 1048576 && stock .qualified_local_recovery @@ -1251,7 +1279,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, 16, 17, 18] + && stock.preparation_abort.claimed_cleanup_catalogs == [15, 16, 17, 18, 19] && stock.preparation_abort.untracked_claimed_catalogs == [13, 14] && stock.preparation_abort.per_recipient_proof_count == 2 && stock @@ -1804,6 +1832,29 @@ mod tests { } #[test] + fn native_corrections_require_original_allocations_and_atomic_deficits() { + for (field, value) in [ + ("catalog", json!(18)), + ("selection", json!("latest_mapping")), + ("max_affected_allocations", json!(33)), + ("protected_original_allocation_required", json!(false)), + ("deficit_projection_in_final_transaction", json!(false)), + ("quantity_storage", json!("plaintext_sql_index")), + ("worker_charge_bytes", json!(65536)), + ("preparatory_loss_implicit", json!(true)), + ("quarantine_loss_implicit", json!(true)), + ] { + assert!( + validate_bytes(&changed( + &format!("/stock_accounting/native_correction/{field}"), + value + )) + .is_err() + ); + } + } + + #[test] fn shared_consumers_cannot_silently_lose_buyer_or_seller_roles() { for host in ["cli", "browser", "generic_ffi"] { assert!(