lib

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

option_mapping.rs (28542B)


      1 use super::*;
      2 use radroots_domain::{
      3     ListingOptionId, Sequence,
      4     listing::{ListingCoordinate, ListingId},
      5     stock::OptionMappingKey,
      6 };
      7 use radroots_protocol::command::{CapturedOptionMapping, MapOptionInput};
      8 
      9 #[tokio::test]
     10 async fn admitted_mapping_survives_observer_loss_and_close_drains_before_original_recovery() {
     11     let f = Fixture::new();
     12     let clock = Arc::new(PausedClock {
     13         state: std::sync::Mutex::new((false, false, false)),
     14         changed: std::sync::Condvar::new(),
     15     });
     16     let e = StockEnrollment::prepare(
     17         SellerStockConfig::new(key(), key(), "owner", UnixSeconds::new(200).unwrap()).unwrap(),
     18         clock.as_ref(),
     19     )
     20     .unwrap();
     21     let store = NativeWorkspace::initialize_seller_stock(&f.path, &e, f.crypto(), clock.clone())
     22         .await
     23         .unwrap();
     24     let binding = store
     25         .authenticate(e.input().clone(), e.proof())
     26         .await
     27         .unwrap();
     28     let pool = committed(
     29         store
     30             .execute_inventory(request(&binding, 1, 2, "Carrots"), proof(e.proof()))
     31             .await
     32             .unwrap(),
     33     );
     34     let key = mapping_key(5, 6);
     35     let original = mapping_request(&binding, 7, key, 2, 0, grams("250"));
     36     let digest = original.command().digest();
     37     clock.arm();
     38     let mut future = store.execute_inventory(original, proof(e.proof()));
     39     assert!(futures::poll!(future.as_mut()).is_pending());
     40     clock.entered();
     41     drop(future);
     42     let mut closing = Box::pin(store.close());
     43     assert!(futures::poll!(closing.as_mut()).is_pending());
     44     drop(closing);
     45     assert!(matches!(
     46         NativeWorkspace::open_seller_stock(&f.path, e.input().clone(), f.crypto(), f.clock.clone())
     47             .await,
     48         Err(WorkspaceError::WriterActive)
     49     ));
     50     clock.release();
     51     store.close().await.unwrap();
     52     let store =
     53         NativeWorkspace::open_seller_stock(&f.path, e.input().clone(), f.crypto(), f.clock.clone())
     54             .await
     55             .unwrap();
     56     let receipt = store
     57         .inventory_operation(
     58             binding.clone(),
     59             pool.checkpoint(),
     60             OperationId::from_bytes([7; 32]),
     61             Some(digest),
     62             proof(e.proof()),
     63         )
     64         .await
     65         .unwrap()
     66         .unwrap();
     67     assert_eq!(receipt.pool(), pool.pool());
     68     assert_eq!(receipt.mapping().unwrap().version().get(), 1);
     69     assert_eq!(
     70         store
     71             .option_mapping(binding.clone(), receipt.checkpoint(), key, proof(e.proof()))
     72             .await
     73             .unwrap(),
     74         receipt.mapping()
     75     );
     76     assert_eq!(
     77         committed(
     78             store
     79                 .execute_inventory(
     80                     mapping_request(&binding, 7, key, 2, 0, grams("250")),
     81                     proof(e.proof())
     82                 )
     83                 .await
     84                 .unwrap()
     85         ),
     86         receipt
     87     );
     88     store.close().await.unwrap();
     89 }
     90 
     91 #[tokio::test]
     92 async fn mapping_rechecks_clock_deadline_and_credential_expiry_inside_its_actual_transaction() {
     93     for next in [0, 99, 150, 200] {
     94         let f = Fixture::new();
     95         let clock = Arc::new(AdmissionClock {
     96             armed: std::sync::atomic::AtomicBool::new(false),
     97             calls: AtomicU64::new(0),
     98             next,
     99         });
    100         let e = StockEnrollment::prepare(
    101             SellerStockConfig::new(key(), key(), "owner", UnixSeconds::new(200).unwrap()).unwrap(),
    102             clock.as_ref(),
    103         )
    104         .unwrap();
    105         let store =
    106             NativeWorkspace::initialize_seller_stock(&f.path, &e, f.crypto(), clock.clone())
    107                 .await
    108                 .unwrap();
    109         let binding = store
    110             .authenticate(e.input().clone(), e.proof())
    111             .await
    112             .unwrap();
    113         let pool = committed(
    114             store
    115                 .execute_inventory(request(&binding, 1, 2, "Carrots"), proof(e.proof()))
    116                 .await
    117                 .unwrap(),
    118         );
    119         clock.armed.store(true, Ordering::SeqCst);
    120         let result = store
    121             .execute_inventory(
    122                 mapping_request(&binding, 7, mapping_key(5, 6), 2, 0, grams("250")),
    123                 proof(e.proof()),
    124             )
    125             .await;
    126         match next {
    127             0 | 99 => assert_eq!(
    128                 result,
    129                 Err(OperationError::Workspace(WorkspaceError::Unavailable))
    130             ),
    131             150 => assert_eq!(result, Err(OperationError::DeadlineExceeded)),
    132             200 => assert_eq!(
    133                 result,
    134                 Err(OperationError::Workspace(WorkspaceError::GrantExpired))
    135             ),
    136             _ => unreachable!(),
    137         }
    138         clock.armed.store(false, Ordering::SeqCst);
    139         assert_eq!(
    140             store
    141                 .option_mapping(
    142                     binding,
    143                     pool.checkpoint(),
    144                     mapping_key(5, 6),
    145                     proof(e.proof())
    146                 )
    147                 .await
    148                 .unwrap(),
    149             None
    150         );
    151         assert_eq!(
    152             store
    153                 .stock_authority(e.input().clone(), e.proof())
    154                 .await
    155                 .unwrap()
    156                 .inventory_checkpoint(),
    157             Some(pool.checkpoint())
    158         );
    159         store.close().await.unwrap();
    160     }
    161 }
    162 
    163 fn mapping_key(listing: u8, option: u8) -> OptionMappingKey {
    164     OptionMappingKey::new(
    165         ListingCoordinate::new(key(), ListingId::from_bytes([listing; 16])),
    166         ListingOptionId::from_bytes([option; 32]),
    167     )
    168 }
    169 fn mapping_request(
    170     binding: &radroots_storage::workspace::WorkspaceBinding,
    171     operation: u8,
    172     key: OptionMappingKey,
    173     pool: u8,
    174     version: u64,
    175     quantity: Quantity,
    176 ) -> InventoryRequest {
    177     let selected = binding.input();
    178     let context = CommandContextData::new(
    179         OperationId::from_bytes([operation; 32]),
    180         binding.actor(),
    181         selected.workspace_id(),
    182         selected.authority(),
    183         selected.route_digest(),
    184         None,
    185         None,
    186         UnixSeconds::new(150).unwrap(),
    187         None,
    188     )
    189     .unwrap();
    190     InventoryRequest::new(
    191         binding.clone(),
    192         minimum(binding),
    193         CapturedOptionMapping::new(
    194             context,
    195             MapOptionInput::new(
    196                 key,
    197                 PoolId::from_bytes([pool; 32]),
    198                 quantity,
    199                 Sequence::new(version).unwrap(),
    200             )
    201             .unwrap(),
    202         )
    203         .unwrap(),
    204     )
    205     .unwrap()
    206 }
    207 fn grams(amount: &str) -> Quantity {
    208     Quantity::new(amount.parse().unwrap(), MeasurementUnit::Gram).unwrap()
    209 }
    210 #[tokio::test]
    211 async fn mapping_queries_and_original_replay_refuse_initial_and_confirmation_clock_regressions() {
    212     let f = Fixture::new();
    213     let clock = Arc::new(AdmissionClock {
    214         armed: std::sync::atomic::AtomicBool::new(false),
    215         calls: AtomicU64::new(0),
    216         next: 99,
    217     });
    218     let e = StockEnrollment::prepare(
    219         SellerStockConfig::new(key(), key(), "owner", UnixSeconds::new(200).unwrap()).unwrap(),
    220         clock.as_ref(),
    221     )
    222     .unwrap();
    223     let store = NativeWorkspace::initialize_seller_stock(&f.path, &e, f.crypto(), clock.clone())
    224         .await
    225         .unwrap();
    226     let binding = store
    227         .authenticate(e.input().clone(), e.proof())
    228         .await
    229         .unwrap();
    230     store
    231         .execute_inventory(request(&binding, 1, 2, "Carrots"), proof(e.proof()))
    232         .await
    233         .unwrap();
    234     let key = mapping_key(5, 6);
    235     let receipt = committed(
    236         store
    237             .execute_inventory(
    238                 mapping_request(&binding, 7, key, 2, 0, grams("250")),
    239                 proof(e.proof()),
    240             )
    241             .await
    242             .unwrap(),
    243     );
    244     for initial in [false, true] {
    245         for choice in 0..3 {
    246             clock.calls.store(u64::from(initial), Ordering::SeqCst);
    247             clock.armed.store(true, Ordering::SeqCst);
    248             let error = match choice {
    249                 0 => store
    250                     .option_mapping(binding.clone(), receipt.checkpoint(), key, proof(e.proof()))
    251                     .await
    252                     .unwrap_err(),
    253                 1 => store
    254                     .inventory_operation(
    255                         binding.clone(),
    256                         receipt.checkpoint(),
    257                         OperationId::from_bytes([7; 32]),
    258                         None,
    259                         proof(e.proof()),
    260                     )
    261                     .await
    262                     .unwrap_err(),
    263                 _ => store
    264                     .execute_inventory(
    265                         mapping_request(&binding, 7, key, 2, 0, grams("250")),
    266                         proof(e.proof()),
    267                     )
    268                     .await
    269                     .unwrap_err(),
    270             };
    271             assert_eq!(
    272                 error,
    273                 OperationError::Workspace(WorkspaceError::Unavailable)
    274             );
    275             clock.armed.store(false, Ordering::SeqCst);
    276         }
    277     }
    278     assert_eq!(
    279         store
    280             .option_mapping(binding, receipt.checkpoint(), key, proof(e.proof()))
    281             .await
    282             .unwrap(),
    283         receipt.mapping()
    284     );
    285     store.close().await.unwrap();
    286 }
    287 async fn seeded(
    288     f: &Fixture,
    289 ) -> (
    290     NativeWorkspace,
    291     StockEnrollment,
    292     radroots_storage::workspace::WorkspaceBinding,
    293     InventoryReceipt,
    294 ) {
    295     let (store, e, binding) = create(f).await;
    296     let pool = committed(
    297         store
    298             .execute_inventory(request(&binding, 1, 2, "Carrots"), proof(e.proof()))
    299             .await
    300             .unwrap(),
    301     );
    302     (store, e, binding, pool)
    303 }
    304 #[tokio::test]
    305 async fn actual_mapping_create_update_remap_retains_original_effects_and_leaves_physical_stock_unchanged()
    306  {
    307     let f = Fixture::new();
    308     let (store, e, binding, pool) = seeded(&f).await;
    309     let other = committed(
    310         store
    311             .execute_inventory(request(&binding, 3, 4, "Beets"), proof(e.proof()))
    312             .await
    313             .unwrap(),
    314     );
    315     let key = mapping_key(5, 6);
    316     let foreign = OptionMappingKey::new(
    317         ListingCoordinate::new(
    318             PublicKey::from_hex("c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5")
    319                 .unwrap(),
    320             key.listing().d(),
    321         ),
    322         key.option_id(),
    323     );
    324     assert_eq!(
    325         store
    326             .execute_inventory(
    327                 mapping_request(&binding, 6, foreign, 2, 0, grams("1")),
    328                 proof(e.proof())
    329             )
    330             .await,
    331         Err(OperationError::Unauthorized)
    332     );
    333     let original = mapping_request(&binding, 7, key, 2, 0, grams("250"));
    334     let wire = original.command().protected_wire().to_vec();
    335     let first = committed(
    336         store
    337             .execute_inventory(original, proof(e.proof()))
    338             .await
    339             .unwrap(),
    340     );
    341     assert_eq!(first.pool(), pool.pool());
    342     assert_eq!(first.mapping().unwrap().per_sale_ticks().get(), 250);
    343     let second_key = mapping_key(5, 8);
    344     let second = committed(
    345         store
    346             .execute_inventory(
    347                 mapping_request(&binding, 9, second_key, 2, 0, grams("125")),
    348                 proof(e.proof()),
    349             )
    350             .await
    351             .unwrap(),
    352     );
    353     assert_eq!(second.pool(), pool.pool());
    354     let updated = committed(
    355         store
    356             .execute_inventory(
    357                 mapping_request(&binding, 10, key, 2, 1, grams("500")),
    358                 proof(e.proof()),
    359             )
    360             .await
    361             .unwrap(),
    362     );
    363     assert_eq!(updated.mapping().unwrap().version().get(), 2);
    364     let remapped = committed(
    365         store
    366             .execute_inventory(
    367                 mapping_request(&binding, 11, key, 4, 2, grams("1000")),
    368                 proof(e.proof()),
    369             )
    370             .await
    371             .unwrap(),
    372     );
    373     assert_eq!(remapped.pool(), other.pool());
    374     assert_eq!(remapped.mapping().unwrap().version().get(), 3);
    375     // The complete listing coordinate, not just the option ID, forms the key.
    376     let distinct = mapping_key(12, 6);
    377     store
    378         .execute_inventory(
    379             mapping_request(&binding, 13, distinct, 2, 0, grams("10")),
    380             proof(e.proof()),
    381         )
    382         .await
    383         .unwrap();
    384     assert_eq!(
    385         store
    386             .option_mapping(
    387                 binding.clone(),
    388                 remapped.checkpoint(),
    389                 key,
    390                 proof(e.proof())
    391             )
    392             .await
    393             .unwrap(),
    394         remapped.mapping()
    395     );
    396     assert_eq!(
    397         store
    398             .option_mapping(
    399                 binding.clone(),
    400                 remapped.checkpoint(),
    401                 second_key,
    402                 proof(e.proof())
    403             )
    404             .await
    405             .unwrap(),
    406         second.mapping()
    407     );
    408     assert_eq!(
    409         store
    410             .inventory_operation(
    411                 binding.clone(),
    412                 remapped.checkpoint(),
    413                 OperationId::from_bytes([7; 32]),
    414                 None,
    415                 proof(e.proof())
    416             )
    417             .await
    418             .unwrap(),
    419         Some(first.clone())
    420     );
    421     assert_eq!(
    422         store
    423             .pool(
    424                 binding.clone(),
    425                 remapped.checkpoint(),
    426                 PoolId::from_bytes([2; 32]),
    427                 proof(e.proof())
    428             )
    429             .await
    430             .unwrap()
    431             .unwrap()
    432             .snapshot(),
    433         pool.pool()
    434     );
    435     assert_eq!(
    436         store
    437             .pool(
    438                 binding.clone(),
    439                 remapped.checkpoint(),
    440                 PoolId::from_bytes([4; 32]),
    441                 proof(e.proof())
    442             )
    443             .await
    444             .unwrap()
    445             .unwrap()
    446             .snapshot(),
    447         other.pool()
    448     );
    449     f.clock.0.store(175, Ordering::SeqCst);
    450     assert_eq!(
    451         committed(
    452             store
    453                 .execute_inventory(
    454                     InventoryRequest::from_protected_wire(
    455                         binding.clone(),
    456                         remapped.checkpoint(),
    457                         &wire
    458                     )
    459                     .unwrap(),
    460                     proof(e.proof())
    461                 )
    462                 .await
    463                 .unwrap()
    464         ),
    465         first
    466     );
    467     store.close().await.unwrap();
    468     let store =
    469         NativeWorkspace::open_seller_stock(&f.path, e.input().clone(), f.crypto(), f.clock.clone())
    470             .await
    471             .unwrap();
    472     assert_eq!(
    473         store
    474             .option_mapping(
    475                 binding.clone(),
    476                 remapped.checkpoint(),
    477                 key,
    478                 proof(e.proof())
    479             )
    480             .await
    481             .unwrap(),
    482         remapped.mapping()
    483     );
    484     assert_eq!(
    485         store
    486             .inventory_operation(
    487                 binding,
    488                 remapped.checkpoint(),
    489                 OperationId::from_bytes([7; 32]),
    490                 None,
    491                 proof(e.proof())
    492             )
    493             .await
    494             .unwrap(),
    495         Some(first)
    496     );
    497     store.close().await.unwrap();
    498     let mut db = database(&f).await;
    499     for (sql, expected) in [
    500         ("SELECT count(*) FROM stock_pools", 2),
    501         ("SELECT count(*) FROM stock_movements", 2),
    502         ("SELECT count(*) FROM stock_operations", 7),
    503         ("SELECT count(*) FROM stock_mappings", 3),
    504     ] {
    505         let count: i64 = sqlx::query_scalar(sql).fetch_one(&mut db).await.unwrap();
    506         assert_eq!(count, expected);
    507     }
    508     db.close().await.unwrap();
    509 }
    510 
    511 #[tokio::test]
    512 async fn mapping_versions_precision_units_and_global_operation_identity_are_enforced() {
    513     let f = Fixture::new();
    514     let (store, e, binding, _) = seeded(&f).await;
    515     let key = mapping_key(5, 6);
    516     for quantity in [
    517         grams("0.1"),
    518         Quantity::new("1".parse().unwrap(), MeasurementUnit::Litre).unwrap(),
    519     ] {
    520         assert_eq!(
    521             store
    522                 .execute_inventory(
    523                     mapping_request(&binding, 7, key, 2, 0, quantity),
    524                     proof(e.proof())
    525                 )
    526                 .await,
    527             Err(OperationError::InvalidCommand)
    528         );
    529     }
    530     assert_eq!(
    531         store
    532             .execute_inventory(
    533                 mapping_request(&binding, 7, key, 2, 1, grams("1")),
    534                 proof(e.proof())
    535             )
    536             .await,
    537         Err(OperationError::StaleState)
    538     );
    539     assert_eq!(
    540         store
    541             .execute_inventory(
    542                 mapping_request(&binding, 7, key, 20, 0, grams("1")),
    543                 proof(e.proof())
    544             )
    545             .await,
    546         Err(OperationError::StaleState)
    547     );
    548     let first = committed(
    549         store
    550             .execute_inventory(
    551                 mapping_request(&binding, 7, key, 2, 0, grams("250")),
    552                 proof(e.proof()),
    553             )
    554             .await
    555             .unwrap(),
    556     );
    557     assert_eq!(
    558         store
    559             .execute_inventory(
    560                 mapping_request(&binding, 8, key, 2, 0, grams("1")),
    561                 proof(e.proof())
    562             )
    563             .await,
    564         Err(OperationError::StaleState)
    565     );
    566     assert_eq!(
    567         store
    568             .execute_inventory(
    569                 mapping_request(&binding, 7, key, 2, 0, grams("500")),
    570                 proof(e.proof())
    571             )
    572             .await,
    573         Err(OperationError::IdempotencyConflict)
    574     );
    575     assert_eq!(
    576         store
    577             .execute_inventory(
    578                 request(&binding, 7, 10, "duplicate physical identity"),
    579                 proof(e.proof())
    580             )
    581             .await,
    582         Err(OperationError::IdempotencyConflict)
    583     );
    584     assert_eq!(
    585         store
    586             .execute_inventory(
    587                 mapping_request(&binding, 1, key, 2, 0, grams("1")),
    588                 proof(e.proof())
    589             )
    590             .await,
    591         Err(OperationError::IdempotencyConflict)
    592     );
    593     assert_eq!(
    594         store
    595             .option_mapping(
    596                 binding.clone(),
    597                 first.checkpoint(),
    598                 mapping_key(5, 9),
    599                 proof(e.proof())
    600             )
    601             .await
    602             .unwrap(),
    603         None
    604     );
    605     store.close().await.unwrap();
    606 }
    607 
    608 #[tokio::test]
    609 async fn delegated_full_inventory_does_not_confer_mapping_configuration_read_or_write() {
    610     use radroots_storage::workspace::seller_stock::{SellerAuthoritySelection, StockGrantScope};
    611     use radroots_storage_sqlite::workspace::{StockGrantEnrollment, StockGrantOutcome};
    612     let f = Fixture::new();
    613     let (store, e, owner, _) = seeded(&f).await;
    614     let key = mapping_key(5, 6);
    615     let original = committed(
    616         store
    617             .execute_inventory(
    618                 mapping_request(&owner, 7, key, 2, 0, grams("250")),
    619                 proof(e.proof()),
    620             )
    621             .await
    622             .unwrap(),
    623     );
    624     let child = StockGrantEnrollment::prepare(
    625         &SellerAuthoritySelection::from_attachment(owner.input()),
    626         "inventory-only".into(),
    627         UnixSeconds::new(200).unwrap(),
    628         StockGrantScope::full_inventory(),
    629     )
    630     .unwrap();
    631     let state = match store
    632         .enroll_stock_grant(owner.clone(), original.checkpoint(), e.proof(), &child)
    633         .await
    634         .unwrap()
    635     {
    636         StockGrantOutcome::Committed(v) => v.authorization(),
    637         _ => panic!("expected grant"),
    638     };
    639     let binding = store
    640         .authenticate(child.input().clone(), child.proof())
    641         .await
    642         .unwrap();
    643     let current = original.checkpoint().with_authorization(state);
    644     assert_eq!(
    645         store
    646             .option_mapping(binding.clone(), current, key, proof(child.proof()))
    647             .await,
    648         Err(OperationError::Unauthorized)
    649     );
    650     assert_eq!(
    651         store
    652             .execute_inventory(
    653                 mapping_request(&binding, 8, key, 2, 1, grams("500")),
    654                 proof(child.proof())
    655             )
    656             .await,
    657         Err(OperationError::Unauthorized)
    658     );
    659     assert_eq!(
    660         store
    661             .inventory_operation(
    662                 binding.clone(),
    663                 current,
    664                 OperationId::from_bytes([7; 32]),
    665                 None,
    666                 proof(child.proof())
    667             )
    668             .await,
    669         Err(OperationError::Unauthorized)
    670     );
    671     let replay = InventoryRequest::replay_from_protected_wire(
    672         binding,
    673         current,
    674         mapping_request(&owner, 7, key, 2, 0, grams("250"))
    675             .command()
    676             .protected_wire(),
    677     )
    678     .unwrap();
    679     assert_eq!(
    680         store.execute_inventory(replay, proof(child.proof())).await,
    681         Err(OperationError::Unauthorized)
    682     );
    683     let owner = store
    684         .authenticate(e.input().clone(), e.proof())
    685         .await
    686         .unwrap();
    687     assert_eq!(
    688         store
    689             .option_mapping(owner, current, key, proof(e.proof()))
    690             .await
    691             .unwrap(),
    692         original.mapping()
    693     );
    694     store.close().await.unwrap();
    695 }
    696 
    697 #[tokio::test]
    698 async fn every_mapping_operation_and_root_write_failure_rolls_back_the_whole_effect() {
    699     for updating in [false, true] {
    700         for (table, event) in [
    701             ("stock_mappings", if updating { "UPDATE" } else { "INSERT" }),
    702             ("stock_operations", "INSERT"),
    703             ("stock_authority", "UPDATE"),
    704         ] {
    705             let f = Fixture::new();
    706             let (store, e, binding, pool) = seeded(&f).await;
    707             let key = mapping_key(5, 6);
    708             let previous = if updating {
    709                 Some(committed(
    710                     store
    711                         .execute_inventory(
    712                             mapping_request(&binding, 7, key, 2, 0, grams("250")),
    713                             proof(e.proof()),
    714                         )
    715                         .await
    716                         .unwrap(),
    717                 ))
    718             } else {
    719                 None
    720             };
    721             let minimum = previous.as_ref().unwrap_or(&pool).checkpoint();
    722             let mut db = database(&f).await;
    723             let trigger = format!(
    724                 "CREATE TRIGGER mapping_fault BEFORE {event} ON {table} BEGIN SELECT RAISE(ABORT,'synthetic mapping failure'); END"
    725             );
    726             sqlx::raw_sql(sqlx::AssertSqlSafe(trigger))
    727                 .execute(&mut db)
    728                 .await
    729                 .unwrap();
    730             assert_eq!(
    731                 store
    732                     .execute_inventory(
    733                         mapping_request(&binding, 8, key, 2, u64::from(updating), grams("500")),
    734                         proof(e.proof())
    735                     )
    736                     .await,
    737                 Err(OperationError::StorageFailure),
    738                 "{table}/{updating}"
    739             );
    740             sqlx::query("DROP TRIGGER mapping_fault")
    741                 .execute(&mut db)
    742                 .await
    743                 .unwrap();
    744             assert_eq!(
    745                 store
    746                     .stock_authority(e.input().clone(), e.proof())
    747                     .await
    748                     .unwrap()
    749                     .inventory_checkpoint(),
    750                 Some(minimum)
    751             );
    752             assert_eq!(
    753                 store
    754                     .option_mapping(binding.clone(), minimum, key, proof(e.proof()))
    755                     .await
    756                     .unwrap(),
    757                 previous.as_ref().and_then(|value| value.mapping())
    758             );
    759             assert_eq!(
    760                 store
    761                     .inventory_operation(
    762                         binding.clone(),
    763                         minimum,
    764                         OperationId::from_bytes([8; 32]),
    765                         None,
    766                         proof(e.proof())
    767                     )
    768                     .await
    769                     .unwrap(),
    770                 None
    771             );
    772             assert_eq!(
    773                 store
    774                     .pool(
    775                         binding.clone(),
    776                         minimum,
    777                         PoolId::from_bytes([2; 32]),
    778                         proof(e.proof())
    779                     )
    780                     .await
    781                     .unwrap()
    782                     .unwrap()
    783                     .snapshot(),
    784                 pool.pool()
    785             );
    786             db.close().await.unwrap();
    787             store.close().await.unwrap();
    788         }
    789     }
    790 }
    791 
    792 #[tokio::test]
    793 async fn failed_mapping_commit_reports_unknown_fences_the_owner_and_reopens_without_a_partial_effect()
    794  {
    795     let f = Fixture::new();
    796     let (store, e, binding, pool) = seeded(&f).await;
    797     let key = mapping_key(5, 6);
    798     let original = mapping_request(&binding, 7, key, 2, 0, grams("250"));
    799     let digest = original.command().digest();
    800     let mut db = database(&f).await;
    801     sqlx::raw_sql("CREATE TABLE mapping_commit_fault(value INTEGER REFERENCES stock_authority(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER mapping_commit_failure AFTER INSERT ON stock_mappings BEGIN INSERT INTO mapping_commit_fault VALUES(2); END").execute(&mut db).await.unwrap();
    802     let anchor = match store
    803         .execute_inventory(original, proof(e.proof()))
    804         .await
    805         .unwrap()
    806     {
    807         InventoryOutcome::Unknown(value) => value,
    808         _ => panic!("commit must be unknown"),
    809     };
    810     assert_eq!(anchor.operation_id(), OperationId::from_bytes([7; 32]));
    811     assert_eq!(anchor.command_digest(), digest);
    812     assert!(anchor.transaction_id().is_some());
    813     assert_eq!(
    814         store
    815             .option_mapping(binding.clone(), pool.checkpoint(), key, proof(e.proof()))
    816             .await,
    817         Err(OperationError::RecoveryRequired)
    818     );
    819     store.close().await.unwrap();
    820     sqlx::raw_sql("DROP TRIGGER mapping_commit_failure; DROP TABLE mapping_commit_fault")
    821         .execute(&mut db)
    822         .await
    823         .unwrap();
    824     db.close().await.unwrap();
    825     let store =
    826         NativeWorkspace::open_seller_stock(&f.path, e.input().clone(), f.crypto(), f.clock.clone())
    827             .await
    828             .unwrap();
    829     assert_eq!(
    830         store
    831             .inventory_operation(
    832                 binding.clone(),
    833                 pool.checkpoint(),
    834                 anchor.operation_id(),
    835                 Some(digest),
    836                 proof(e.proof())
    837             )
    838             .await
    839             .unwrap(),
    840         None
    841     );
    842     assert_eq!(
    843         store
    844             .option_mapping(binding.clone(), pool.checkpoint(), key, proof(e.proof()))
    845             .await
    846             .unwrap(),
    847         None
    848     );
    849     let result = committed(
    850         store
    851             .execute_inventory(
    852                 mapping_request(&binding, 7, key, 2, 0, grams("250")),
    853                 proof(e.proof()),
    854             )
    855             .await
    856             .unwrap(),
    857     );
    858     assert_eq!(result.mapping().unwrap().version().get(), 1);
    859     assert_eq!(result.pool(), pool.pool());
    860     store.close().await.unwrap();
    861 }
    862 
    863 #[tokio::test]
    864 async fn older_authentic_mapping_projection_cannot_replace_the_latest_protected_configuration() {
    865     let f = Fixture::new();
    866     let (store, e, binding, _) = seeded(&f).await;
    867     let key = mapping_key(5, 6);
    868     store
    869         .execute_inventory(
    870             mapping_request(&binding, 7, key, 2, 0, grams("250")),
    871             proof(e.proof()),
    872         )
    873         .await
    874         .unwrap();
    875     let mut db = database(&f).await;
    876     let old: Vec<u8> = sqlx::query_scalar("SELECT protected FROM stock_mappings")
    877         .fetch_one(&mut db)
    878         .await
    879         .unwrap();
    880     let latest = committed(
    881         store
    882             .execute_inventory(
    883                 mapping_request(&binding, 8, key, 2, 1, grams("500")),
    884                 proof(e.proof()),
    885             )
    886             .await
    887             .unwrap(),
    888     );
    889     sqlx::query("UPDATE stock_mappings SET revision=1,protected=?")
    890         .bind(old)
    891         .execute(&mut db)
    892         .await
    893         .unwrap();
    894     assert_eq!(
    895         store
    896             .option_mapping(binding.clone(), latest.checkpoint(), key, proof(e.proof()))
    897             .await,
    898         Err(OperationError::RecoveryRequired)
    899     );
    900     assert_eq!(
    901         store
    902             .execute_inventory(
    903                 mapping_request(&binding, 9, key, 2, 1, grams("1000")),
    904                 proof(e.proof())
    905             )
    906             .await,
    907         Err(OperationError::RecoveryRequired)
    908     );
    909     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM stock_operations")
    910         .fetch_one(&mut db)
    911         .await
    912         .unwrap();
    913     assert_eq!(count, 3);
    914     db.close().await.unwrap();
    915     store.close().await.unwrap();
    916 }