inventory_abort.rs (4478B)
1 use super::*; 2 use radroots_domain::{ 3 CommandDigest, OperationId, OrderId, PoolId, TransactionId, 4 listing::MeasurementUnit, 5 stock::{PhysicalBalances, PoolBalances, PoolSnapshot, PoolUnit, StockTicks}, 6 }; 7 use radroots_storage::{ 8 command::{CommitAttempt, OperationError}, 9 inventory::{InventoryCheckpointRef, StockAbortReceipt}, 10 workspace::seller_stock::StockAuthorizationRef, 11 }; 12 13 fn checkpoint( 14 namespace: &AttachmentInput, 15 revision: u64, 16 authorization: bool, 17 ) -> InventoryCheckpointRef { 18 InventoryCheckpointRef::from_host_metadata( 19 namespace.authority(), 20 Sequence::new(revision).unwrap(), 21 (revision != 0).then_some(CheckpointDigest::from_bytes([7; 32])), 22 ) 23 .unwrap() 24 .with_optional_authorization(authorization.then(|| { 25 StockAuthorizationRef::from_host_metadata( 26 Sequence::new(1).unwrap(), 27 CheckpointDigest::from_bytes([8; 32]), 28 ) 29 .unwrap() 30 })) 31 } 32 fn attempt(namespace: &AttachmentInput, committed: bool) -> CommitAttempt { 33 CommitAttempt::new( 34 namespace.workspace_id(), 35 OperationId::from_bytes([70; 32]), 36 CommandDigest::from_bytes([71; 32]), 37 committed.then_some(TransactionId::from_bytes([72; 32])), 38 ) 39 } 40 fn report( 41 namespace: &AttachmentInput, 42 attempt: CommitAttempt, 43 checkpoint: InventoryCheckpointRef, 44 pools: Vec<PoolSnapshot>, 45 ) -> Result<StockAbortReceipt, OperationError> { 46 StockAbortReceipt::from_committed_store( 47 namespace, 48 attempt, 49 OrderId::from_bytes([73; 32]), 50 UnixSeconds::new(111).unwrap(), 51 checkpoint, 52 pools, 53 ) 54 } 55 fn pool(id: u8, revision: u64) -> PoolSnapshot { 56 PoolSnapshot::new( 57 PoolId::from_bytes([id; 32]), 58 PoolUnit::new(MeasurementUnit::Each, 0).unwrap(), 59 PoolBalances::new(StockTicks::ZERO, PhysicalBalances::ZERO).unwrap(), 60 Sequence::new(revision).unwrap(), 61 ) 62 } 63 64 #[test] 65 fn shared_cleanup_metadata_refuses_missing_commit_progress_authorization_and_foreign_namespace() { 66 let namespace = input(); 67 let valid = checkpoint(&namespace, 1, true); 68 for (anchor, progress) in [ 69 (attempt(&namespace, false), valid), 70 (attempt(&namespace, true), checkpoint(&namespace, 0, true)), 71 (attempt(&namespace, true), checkpoint(&namespace, 1, false)), 72 ( 73 CommitAttempt::new( 74 WorkspaceId::from_bytes([99; 32]), 75 OperationId::from_bytes([70; 32]), 76 CommandDigest::from_bytes([71; 32]), 77 Some(TransactionId::from_bytes([72; 32])), 78 ), 79 valid, 80 ), 81 ( 82 attempt(&namespace, true), 83 InventoryCheckpointRef::from_host_metadata( 84 AuthorityRef::new( 85 ExecutorId::from_bytes([99; 32]), 86 Generation::new(1).unwrap(), 87 ), 88 valid.revision(), 89 valid.head(), 90 ) 91 .unwrap() 92 .with_optional_authorization(valid.authorization()), 93 ), 94 ] { 95 assert_eq!( 96 report(&namespace, anchor, progress, vec![]).unwrap_err(), 97 OperationError::RecoveryRequired 98 ); 99 } 100 let value = report(&namespace, attempt(&namespace, true), valid, vec![]).unwrap(); 101 assert_eq!(value.attempt(), attempt(&namespace, true)); 102 assert_eq!(value.checkpoint(), valid); 103 assert!(value.released_pools().is_empty()); 104 assert_eq!(format!("{value:?}"), "StockAbortReceipt(<redacted>)"); 105 } 106 107 #[test] 108 fn shared_cleanup_pool_reports_are_bounded_canonical_and_keep_the_original_after_images() { 109 let namespace = input(); 110 let selected = |pools| { 111 report( 112 &namespace, 113 attempt(&namespace, true), 114 checkpoint(&namespace, 1, true), 115 pools, 116 ) 117 }; 118 for invalid in [ 119 vec![pool(1, 3), pool(1, 3)], 120 vec![pool(2, 3), pool(1, 3)], 121 vec![pool(1, 0)], 122 vec![pool(1, 1)], 123 vec![pool(1, 2)], 124 (0..65).map(|id| pool(id, 3)).collect(), 125 ] { 126 assert_eq!( 127 selected(invalid).unwrap_err(), 128 OperationError::RecoveryRequired 129 ); 130 } 131 let expected: Vec<_> = (0..64).map(|id| pool(id, 3)).collect(); 132 let value = selected(expected.clone()).unwrap(); 133 assert!(value.released_pools() == expected); 134 assert!(value.into_released_pools() == expected); 135 }