lib

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

workspace_acceptance.rs (9684B)


      1 //! Controlled accepted-task ownership, not native consent/stock qualification.
      2 use super::*;
      3 use radroots_domain::{ExpectedOrder, OrderId, RecordId, TermsDigest};
      4 use radroots_protocol::command::{AcceptOrderInput, AllocationSelection};
      5 use radroots_storage::workspace::seller_stock::{
      6     StockListingCheckpointRef, StockSourceCheckpointRef,
      7 };
      8 
      9 fn selected_store() -> Shared<Store> {
     10     let store = store();
     11     *store.model.binding.lock().unwrap() = WorkspaceBinding::from_authenticated_host(
     12         input(5),
     13         radroots_storage::workspace::WorkspaceScope::SellerStockOwner,
     14         ActorRef::new(key(), ActorRole::Seller, None, Some(input(5).grant_id())),
     15         "device",
     16         Sequence::new(1).unwrap(),
     17         UnixSeconds::new(200).unwrap(),
     18         UnixSeconds::new(100).unwrap(),
     19     )
     20     .unwrap();
     21     store
     22 }
     23 fn context(scope: &WorkspaceSession) -> radroots_execution::workspace::CommandContext {
     24     scope
     25         .command_context(
     26             operation(),
     27             Some(ExpectedOrder::new(
     28                 OrderId::from_bytes([84; 32]),
     29                 None,
     30                 Sequence::new(0).unwrap(),
     31                 None,
     32                 input(5).checkpoint().clone(),
     33             )),
     34             None,
     35             UnixSeconds::new(150).unwrap(),
     36         )
     37         .unwrap()
     38 }
     39 fn source(scope: &WorkspaceSession) -> StockSourceCheckpointRef {
     40     StockSourceCheckpointRef::from_host_metadata(
     41         scope.binding().input().authority(),
     42         Sequence::new(2).unwrap(),
     43         Some(CheckpointDigest::from_bytes([22; 32])),
     44     )
     45     .unwrap()
     46 }
     47 fn listing(scope: &WorkspaceSession) -> StockListingCheckpointRef {
     48     StockListingCheckpointRef::from_host_metadata(
     49         scope.binding().input().authority(),
     50         Sequence::new(3).unwrap(),
     51         Some(CheckpointDigest::from_bytes([33; 32])),
     52     )
     53     .unwrap()
     54 }
     55 fn acceptance() -> AcceptOrderInput {
     56     AcceptOrderInput::new(
     57         RecordId::from_bytes([85; 32]),
     58         TermsDigest::from_bytes([86; 32]),
     59         AllocationSelection::automatic(),
     60     )
     61 }
     62 
     63 #[test]
     64 fn accepted_acceptance_keeps_inputs_budget_and_permit_after_observer_loss_until_settlement() {
     65     block_on(async {
     66         let store = selected_store();
     67         let tasks = Shared::new(Tasks::default());
     68         let client = ClientSession::new_with_task_host(
     69             Host,
     70             core::num::NonZeroU16::new(8).unwrap(),
     71             tasks.clone(),
     72         );
     73         let scope = client
     74             .attach_workspace(store.clone(), input(5), proof())
     75             .await
     76             .unwrap();
     77         client.start().await.unwrap();
     78         drop(client.prepare_stock_acceptance(
     79             store.clone(),
     80             &scope,
     81             context(&scope),
     82             minimum(&scope),
     83             source(&scope),
     84             listing(&scope),
     85             acceptance(),
     86         ));
     87         assert!(tasks.queued.lock().unwrap().is_empty());
     88         let mut first = Box::pin(client.prepare_stock_acceptance(
     89             store.clone(),
     90             &scope,
     91             context(&scope),
     92             minimum(&scope),
     93             source(&scope),
     94             listing(&scope),
     95             acceptance(),
     96         ));
     97         let mut second = Box::pin(client.prepare_stock_acceptance(
     98             store.clone(),
     99             &scope,
    100             context(&scope),
    101             minimum(&scope),
    102             source(&scope),
    103             listing(&scope),
    104             acceptance(),
    105         ));
    106         assert!(futures::poll!(first.as_mut()).is_pending());
    107         assert!(futures::poll!(second.as_mut()).is_pending());
    108         assert_eq!(tasks.queued.lock().unwrap().len(), 2);
    109         assert!(store.calls.lock().unwrap().is_empty());
    110         assert_eq!(
    111             client
    112                 .prepare_stock_acceptance(
    113                     store.clone(),
    114                     &scope,
    115                     context(&scope),
    116                     minimum(&scope),
    117                     source(&scope),
    118                     listing(&scope),
    119                     acceptance()
    120                 )
    121                 .await
    122                 .unwrap_err()
    123                 .code(),
    124             Code::CapabilityUnavailable
    125         );
    126         drop(first);
    127         drop(second);
    128         let mut close = Box::pin(client.close());
    129         assert!(futures::poll!(close.as_mut()).is_pending());
    130         let mut jobs = core::mem::take(&mut *tasks.queued.lock().unwrap());
    131         store.waiting.store(true, Ordering::SeqCst);
    132         assert!(futures::poll!(jobs[0].as_mut()).is_pending());
    133         assert!(futures::poll!(close.as_mut()).is_pending());
    134         store.waiting.store(false, Ordering::SeqCst);
    135         store.waker.wake();
    136         for task in jobs {
    137             task.await;
    138         }
    139         let calls = store.calls.lock().unwrap().clone();
    140         assert_eq!(calls.len(), 2);
    141         assert_eq!(calls[0], calls[1]);
    142         assert_eq!(calls[0].0, operation());
    143         close.await.unwrap();
    144     });
    145 }
    146 
    147 #[test]
    148 fn host_and_foreign_context_scope_or_closed_scope_refuse_without_dispatch() {
    149     block_on(async {
    150         let store = selected_store();
    151         let tasks = Shared::new(Tasks::default());
    152         let client = ClientSession::new_with_task_host(
    153             Host,
    154             core::num::NonZeroU16::new(8).unwrap(),
    155             tasks.clone(),
    156         );
    157         let scope = client
    158             .attach_workspace(store.clone(), input(5), proof())
    159             .await
    160             .unwrap();
    161         let foreign = WorkspaceSession::attach(store.clone(), input(5), proof())
    162             .await
    163             .unwrap();
    164         client.start().await.unwrap();
    165         for (selected, captured) in [(&foreign, context(&foreign)), (&scope, context(&foreign))] {
    166             assert_eq!(
    167                 client
    168                     .prepare_stock_acceptance(
    169                         store.clone(),
    170                         selected,
    171                         captured,
    172                         minimum(&scope),
    173                         source(&scope),
    174                         listing(&scope),
    175                         acceptance()
    176                     )
    177                     .await
    178                     .unwrap_err()
    179                     .code(),
    180                 Code::Unauthorized
    181             );
    182         }
    183         tasks.refuse.store(true, Ordering::SeqCst);
    184         assert_eq!(
    185             client
    186                 .prepare_stock_acceptance(
    187                     store.clone(),
    188                     &scope,
    189                     context(&scope),
    190                     minimum(&scope),
    191                     source(&scope),
    192                     listing(&scope),
    193                     acceptance()
    194                 )
    195                 .await
    196                 .unwrap_err()
    197                 .code(),
    198             Code::CapabilityUnavailable
    199         );
    200         tasks.refuse.store(false, Ordering::SeqCst);
    201         let captured = context(&scope);
    202         scope.close();
    203         assert_eq!(
    204             client
    205                 .prepare_stock_acceptance(
    206                     store.clone(),
    207                     &scope,
    208                     captured,
    209                     minimum(&scope),
    210                     source(&scope),
    211                     listing(&scope),
    212                     acceptance()
    213                 )
    214                 .await
    215                 .unwrap_err()
    216                 .code(),
    217             Code::CapabilityUnavailable
    218         );
    219         client.close().await.unwrap();
    220         let no_host = ClientSession::new(Host, core::num::NonZeroU16::MIN);
    221         let scope = no_host
    222             .attach_workspace(store.clone(), input(5), proof())
    223             .await
    224             .unwrap();
    225         no_host.start().await.unwrap();
    226         assert_eq!(
    227             no_host
    228                 .prepare_stock_acceptance(
    229                     store.clone(),
    230                     &scope,
    231                     context(&scope),
    232                     minimum(&scope),
    233                     source(&scope),
    234                     listing(&scope),
    235                     acceptance()
    236                 )
    237                 .await
    238                 .unwrap_err()
    239                 .code(),
    240             Code::CapabilityUnavailable
    241         );
    242         no_host.close().await.unwrap();
    243         assert!(store.calls.lock().unwrap().is_empty());
    244         assert!(tasks.queued.lock().unwrap().is_empty());
    245     });
    246 }
    247 
    248 #[test]
    249 fn lost_accepted_acceptance_task_preserves_original_identity_and_unknown_admission() {
    250     block_on(async {
    251         let store = selected_store();
    252         let tasks = Shared::new(Tasks::default());
    253         let client =
    254             ClientSession::new_with_task_host(Host, core::num::NonZeroU16::MIN, tasks.clone());
    255         let scope = client
    256             .attach_workspace(store.clone(), input(5), proof())
    257             .await
    258             .unwrap();
    259         client.start().await.unwrap();
    260         let mut observed = Box::pin(client.prepare_stock_acceptance(
    261             store.clone(),
    262             &scope,
    263             context(&scope),
    264             minimum(&scope),
    265             source(&scope),
    266             listing(&scope),
    267             acceptance(),
    268         ));
    269         assert!(futures::poll!(observed.as_mut()).is_pending());
    270         // Deliberately nonconforming host: silence cannot prove no T0 commit.
    271         drop(tasks.queued.lock().unwrap().pop().unwrap());
    272         let error = observed.await.unwrap_err();
    273         assert_eq!(error.code(), Code::CommitUnknown);
    274         assert_eq!(
    275             error.context().admission(),
    276             radroots_execution::Admission::Unknown
    277         );
    278         assert_eq!(error.context().operation_id(), Some(operation()));
    279         assert_eq!(error.retry(), RetryClass::ManualReconciliation);
    280         assert!(store.calls.lock().unwrap().is_empty());
    281         client.close().await.unwrap();
    282     });
    283 }