receipt_workflow_listing.rs (27446B)
1 //! Ordinary public commands with a signing-only provider and measured relay reads. 2 #[path = "receipt_listing_economics.rs"] 3 mod economics; 4 #[path = "receipt_public_snapshots.rs"] 5 mod snapshots; 6 7 use super::*; 8 use radroots::listings::PublishInput; 9 use radroots::{BusinessInstallation, Delivery, Issuance, OperationKind}; 10 use radroots_execution::{Admission, Code}; 11 use radroots_signing::{ 12 issuance::{SigningIntent, SigningOutcome}, 13 provider::{ 14 PrivateCiphertext, PrivateCryptoCapability, PrivateText, ProviderCall, ProviderState, 15 }, 16 }; 17 18 type SigningPause = (oneshot::Sender<()>, oneshot::Receiver<()>); 19 struct PublicSigner { 20 local: LocalSigner, 21 calls: AtomicU64, 22 private_calls: AtomicU64, 23 unknown: AtomicBool, 24 pause: Mutex<Option<SigningPause>>, 25 } 26 impl PublicSigner { 27 fn new() -> Arc<Self> { 28 Arc::new(Self { 29 local: LocalSigner::new( 30 parse_secret_key( 31 "0000000000000000000000000000000000000000000000000000000000000001", 32 ) 33 .unwrap(), 34 ) 35 .unwrap() 36 .with_nonce_source(Arc::new(OsNonceSource)), 37 calls: AtomicU64::new(0), 38 private_calls: AtomicU64::new(0), 39 unknown: AtomicBool::new(false), 40 pause: Mutex::new(None), 41 }) 42 } 43 } 44 impl Signer for PublicSigner { 45 fn status( 46 &self, 47 ) -> BoxFuture<'_, Result<radroots_signing::SignerStatus, radroots_signing::Error>> { 48 self.local.status() 49 } 50 fn sign( 51 &self, 52 request: radroots_signing::SignRequest, 53 ) -> BoxFuture<'_, Result<radroots_signing::SignReceipt, radroots_signing::Error>> { 54 self.local.sign(request) 55 } 56 } 57 impl ParticipantProvider for PublicSigner { 58 fn provider_state(&self) -> BoxFuture<'_, Result<ProviderState, ProviderError>> { 59 Box::pin(async move { 60 let state = self.local.provider_state().await?; 61 Ok(ProviderState::new( 62 state.identity(), 63 PrivateCryptoCapability::Unavailable, 64 PrivateCryptoCapability::Unavailable, 65 state.signing_replay(), 66 ) 67 .with_exact_issuance(state.exact_issuance())) 68 }) 69 } 70 fn sign_exact(&self, intent: SigningIntent) -> BoxFuture<'_, SigningOutcome> { 71 Box::pin(async move { 72 assert_eq!(intent.recipient(), None); 73 self.calls.fetch_add(1, Ordering::SeqCst); 74 if self.unknown.load(Ordering::SeqCst) { 75 return SigningOutcome::unknown(intent.binding(), ProviderError::ProviderFailure); 76 } 77 let actual = self.local.sign_exact(intent).await; 78 let pause = self.pause.lock().unwrap().take(); 79 if let Some((reached, resume)) = pause { 80 reached.send(()).unwrap(); 81 resume.await.unwrap(); 82 } 83 actual 84 }) 85 } 86 fn encrypt_private( 87 &self, 88 _call: ProviderCall, 89 _recipient: PublicKey, 90 _text: PrivateText, 91 ) -> BoxFuture<'_, Result<PrivateCiphertext, ProviderError>> { 92 self.private_calls.fetch_add(1, Ordering::SeqCst); 93 Box::pin(async { Err(ProviderError::CapabilityUnavailable) }) 94 } 95 fn decrypt_private( 96 &self, 97 _call: ProviderCall, 98 _sender: PublicKey, 99 _text: PrivateCiphertext, 100 ) -> BoxFuture<'_, Result<PrivateText, ProviderError>> { 101 self.private_calls.fetch_add(1, Ordering::SeqCst); 102 Box::pin(async { Err(ProviderError::CapabilityUnavailable) }) 103 } 104 } 105 fn input(id: u8) -> PublishInput { 106 let wire = json!({"id":format!("{id:02x}").repeat(16),"title":" Native carrots 🥕 ","summary":"Quote required","description_markdown":"Exact public terms", 107 "published_at":now().to_string(),"profile":{"profile":"radroots.listing","version":1,"state":"active", 108 "product":{"name":"Carrots","description":"x".repeat(4096),"producer":null,"origin_summary":null}, 109 "default_option":"02".repeat(32),"options":[{"id":"02".repeat(32),"label":"Bunch","unit":{"type":"package","label":"bunch","contents":{"type":"unspecified"}}, 110 "increment":"1","minimum":"1","price":{"type":"quote_required"},"supply":{"type":"available","amount":"10"},"lead_seconds":"0"}], 111 "fulfillment":{"modes":["pickup"],"area":"Example","lead_seconds":"0"},"authority":null,"media":[]}}); 112 PublishInput::parse(&serde_json::to_vec(&wire).unwrap()).unwrap() 113 } 114 fn config(relay: &Relay) -> Config { 115 Config::from_profile( 116 RelayProfile::explicit( 117 RelayProfileKind::Simulator, 118 [ 119 RelayEndpoint::new(&relay.url, RelayUrlPolicy::Local, RelayAccess::ReadWrite) 120 .unwrap(), 121 ], 122 ) 123 .unwrap(), 124 ) 125 .with_timeouts(2000, 2000, 2000) 126 .unwrap() 127 } 128 struct Ready { 129 fixture: Fixture, 130 store: Arc<NativeWorkspace>, 131 client: Client, 132 scope: radroots::WorkspaceSession, 133 input: AttachmentInput, 134 proof: AttachmentProof, 135 signer: Arc<PublicSigner>, 136 } 137 async fn open( 138 fixture: &Fixture, 139 store: &NativeWorkspace, 140 relay: &Relay, 141 name: &str, 142 signer: Arc<PublicSigner>, 143 credential: (&AttachmentInput, &AttachmentProof), 144 mode: StartMode, 145 ) -> (Client, radroots::WorkspaceSession) { 146 let (input, proof) = credential; 147 let cache = fixture.path.parent().unwrap().join(name); 148 std::fs::create_dir(&cache).unwrap(); 149 let options = OpenOptions::new(Paths::from_directory(cache).unwrap(), OpenMode::Create) 150 .with_source_generation(SourceGeneration::new([41; 32]).unwrap(), 1) 151 .unwrap(); 152 let client = Client::open( 153 NativeConfig::public_read(options) 154 .with_workspace(store.clone()) 155 .with_nostr( 156 config(relay), 157 SatisfactionPolicy::new(SatisfactionClass::Accepted, TargetPolicy::all()), 158 ) 159 .unwrap(), 160 ) 161 .await 162 .unwrap(); 163 let scope = client.attach(input.clone(), copy(proof)).await.unwrap(); 164 client 165 .attach_signer(SignerHandle::new(signer, Arc::new(Callbacks))) 166 .await 167 .unwrap(); 168 client.start(mode).await.unwrap(); 169 (client, scope) 170 } 171 impl Ready { 172 async fn new(relay: &Relay, mode: StartMode, signer: Arc<PublicSigner>, seller: bool) -> Self { 173 let fixture = Fixture::new(); 174 fixture.clock.live.store(true, Ordering::SeqCst); 175 let cfg = if seller { 176 LocalOutboxConfig::seller( 177 key(), 178 key(), 179 "public-workflow", 180 UnixSeconds::new(now() + 600).unwrap(), 181 ) 182 } else { 183 LocalOutboxConfig::new( 184 key(), 185 key(), 186 "public-workflow", 187 UnixSeconds::new(now() + 600).unwrap(), 188 ) 189 } 190 .unwrap(); 191 let (store, input, proof) = NativeWorkspace::initialize_local_outbox( 192 &fixture.path, 193 cfg, 194 fixture.crypto(), 195 fixture.clock.clone(), 196 ) 197 .await 198 .unwrap(); 199 let store = Arc::new(store); 200 let (client, scope) = open( 201 &fixture, 202 &store, 203 relay, 204 "cache", 205 signer.clone(), 206 (&input, &proof), 207 mode, 208 ) 209 .await; 210 Self { 211 fixture, 212 store, 213 client, 214 scope, 215 input, 216 proof, 217 signer, 218 } 219 } 220 fn context(&self, id: u8, deadline: u64) -> radroots::CommandContext { 221 self.scope 222 .command_context( 223 OperationId::from_bytes([id; 32]), 224 None, 225 None, 226 UnixSeconds::new(deadline).unwrap(), 227 ) 228 .unwrap() 229 } 230 async fn snapshot(&self, id: u8) -> radroots_storage::command::OperationSnapshot { 231 self.store 232 .operation_snapshot( 233 self.scope.binding().clone(), 234 OperationId::from_bytes([id; 32]), 235 None, 236 copy(&self.proof), 237 ) 238 .await 239 .unwrap() 240 .unwrap() 241 } 242 } 243 async fn stop(relay: Relay) { 244 relay.stop.send(()).unwrap(); 245 relay.task.await.unwrap(); 246 } 247 248 #[tokio::test] 249 async fn ordinary_public_publish_uses_no_private_capability_and_queries_the_actual_listing_after_reopen() 250 { 251 let mut relay = Relay::new().await; 252 let ready = Ready::new(&relay, StartMode::Online, PublicSigner::new(), true).await; 253 assert_eq!(relay.count.load(Ordering::SeqCst), 0); 254 let captured = input(211); 255 let deadline = now() + 120; 256 let receipt = ready 257 .client 258 .listings() 259 .publish(ready.context(211, deadline), captured.clone()) 260 .await 261 .unwrap(); 262 assert_eq!(receipt.kind(), OperationKind::Listing); 263 assert_eq!( 264 receipt.business_installation(), 265 BusinessInstallation::Installed 266 ); 267 assert_eq!(receipt.issuance(), Issuance::Obtained); 268 assert_eq!(receipt.delivery(), Delivery::Observed); 269 assert_eq!(receipt.message_id(), None); 270 assert!(receipt.record_ids().is_empty()); 271 let listing = receipt.business_result().unwrap(); 272 assert_eq!(listing.id(), captured.id()); 273 assert_eq!(listing.author(), key()); 274 assert_eq!(listing.text(), captured.text()); 275 assert_eq!(listing.terms(), captured.terms()); 276 assert_eq!(listing.published_at(), captured.published_at()); 277 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 278 assert_eq!(ready.signer.private_calls.load(Ordering::SeqCst), 0); 279 assert_eq!(relay.requests.load(Ordering::SeqCst), 0); 280 assert_eq!(relay.count.load(Ordering::SeqCst), 1); 281 let raw = tokio::time::timeout(Duration::from_secs(5), relay.events.recv()) 282 .await 283 .unwrap() 284 .unwrap(); 285 let independent = nostr::Event::from_json(&raw).unwrap(); 286 independent.verify().unwrap(); 287 assert_eq!(independent.kind.as_u16(), 30402); 288 assert_eq!(independent.id.as_bytes(), listing.revision().as_bytes()); 289 assert!( 290 independent 291 .tags 292 .iter() 293 .any(|tag| tag.as_slice().iter().any(|v| v.len() > 4096)) 294 ); 295 let snapshot = ready.snapshot(211).await; 296 assert!(snapshot.publications().is_empty()); 297 assert!(snapshot.installed_message().is_none()); 298 assert!( 299 snapshot 300 .publication_accepted(snapshot.public_publications()[0]) 301 .unwrap() 302 ); 303 assert!(matches!( 304 radroots_execution::OperationReceipt::<radroots_domain::ChatMessage>::try_from_snapshot( 305 &snapshot 306 ), 307 Err(OperationError::UnsupportedOperation) 308 )); 309 let status = ready 310 .client 311 .operations(&ready.scope) 312 .get(OperationId::from_bytes([211; 32])) 313 .await 314 .unwrap() 315 .unwrap(); 316 assert_eq!(status.kind(), OperationKind::Listing); 317 assert_eq!(status.message_id(), None); 318 assert_eq!( 319 status.business_result().unwrap().as_listing().unwrap(), 320 listing 321 ); 322 assert!(status.business_result().unwrap().as_chat().is_none()); 323 let replay = ready 324 .client 325 .listings() 326 .publish(ready.context(211, deadline), captured) 327 .await 328 .unwrap(); 329 assert_eq!( 330 replay.operation_checkpoint(), 331 receipt.operation_checkpoint() 332 ); 333 assert_eq!(relay.count.load(Ordering::SeqCst), 1); 334 ready.client.close().await.unwrap(); 335 let store = ready.fixture.reopen(ready.input.clone()).await; 336 let (client, scope) = open( 337 &ready.fixture, 338 &store, 339 &relay, 340 "cache-reopened", 341 ready.signer.clone(), 342 (&ready.input, &ready.proof), 343 StartMode::Online, 344 ) 345 .await; 346 let restored = client 347 .operations(&scope) 348 .get(OperationId::from_bytes([211; 32])) 349 .await 350 .unwrap() 351 .unwrap(); 352 assert_eq!( 353 restored.operation_checkpoint(), 354 receipt.operation_checkpoint() 355 ); 356 assert_eq!( 357 restored.business_result().unwrap().as_listing().unwrap(), 358 listing 359 ); 360 let resumed = client 361 .operations(&scope) 362 .resume( 363 OperationId::from_bytes([211; 32]), 364 Some(receipt.command_digest()), 365 ) 366 .await 367 .unwrap(); 368 assert_eq!( 369 resumed.operation_checkpoint(), 370 receipt.operation_checkpoint() 371 ); 372 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 373 assert_eq!(relay.count.load(Ordering::SeqCst), 1); 374 client.close().await.unwrap(); 375 stop(relay).await; 376 } 377 378 #[tokio::test] 379 async fn ordinary_local_public_admission_returns_listing_family_without_signing_or_network() { 380 let relay = Relay::new().await; 381 let ready = Ready::new(&relay, StartMode::LocalOnly, PublicSigner::new(), true).await; 382 let receipt = ready 383 .client 384 .listings() 385 .publish(ready.context(212, now() + 120), input(212)) 386 .await 387 .unwrap(); 388 assert!(receipt.durable()); 389 assert_eq!(receipt.kind(), OperationKind::Listing); 390 assert!(receipt.business_result().is_none()); 391 assert_eq!(receipt.issuance(), Issuance::None); 392 assert_eq!(receipt.message_id(), None); 393 let status = ready 394 .client 395 .operations(&ready.scope) 396 .get(OperationId::from_bytes([212; 32])) 397 .await 398 .unwrap() 399 .unwrap(); 400 assert_eq!(status.kind(), OperationKind::Listing); 401 assert!(status.business_result().is_none()); 402 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 0); 403 assert_eq!(ready.signer.private_calls.load(Ordering::SeqCst), 0); 404 assert_eq!(relay.count.load(Ordering::SeqCst), 0); 405 ready.client.close().await.unwrap(); 406 stop(relay).await; 407 } 408 409 #[tokio::test] 410 async fn ordinary_public_explicit_retry_after_first_issue_expiry_preserves_original_bytes_and_policy() 411 { 412 let mut relay = Relay::new().await; 413 relay.accepting.store(false, Ordering::SeqCst); 414 let ready = Ready::new(&relay, StartMode::Online, PublicSigner::new(), true).await; 415 let deadline = now() + 10; 416 let captured = input(213); 417 let receipt = ready 418 .client 419 .listings() 420 .publish(ready.context(213, deadline), captured.clone()) 421 .await 422 .unwrap(); 423 assert!(receipt.business_result().is_some()); 424 let original = relay.events.recv().await.unwrap(); 425 let before = ready.snapshot(213).await; 426 let policy = *before.delivery_attempts()[0] 427 .intent() 428 .publication_policy_digest(); 429 assert!( 430 !before 431 .publication_accepted(before.public_publications()[0]) 432 .unwrap() 433 ); 434 let replay = ready 435 .client 436 .listings() 437 .publish(ready.context(213, deadline), captured) 438 .await 439 .unwrap(); 440 assert_eq!( 441 replay.operation_checkpoint(), 442 receipt.operation_checkpoint() 443 ); 444 assert_eq!(relay.count.load(Ordering::SeqCst), 1); 445 tokio::time::sleep(Duration::from_secs(deadline.saturating_sub(now()) + 1)).await; 446 relay.accepting.store(true, Ordering::SeqCst); 447 let resumed = ready 448 .client 449 .operations(&ready.scope) 450 .resume( 451 OperationId::from_bytes([213; 32]), 452 Some(receipt.command_digest()), 453 ) 454 .await 455 .unwrap(); 456 assert_eq!(resumed.issuance_window(), receipt.issuance_window()); 457 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 458 assert_eq!( 459 tokio::time::timeout(Duration::from_secs(5), relay.events.recv()) 460 .await 461 .unwrap() 462 .unwrap(), 463 original 464 ); 465 let after = ready.snapshot(213).await; 466 assert_eq!(after.delivery_attempts().len(), 2); 467 assert!( 468 after 469 .publication_accepted(after.public_publications()[0]) 470 .unwrap() 471 ); 472 assert!( 473 after 474 .delivery_attempts() 475 .iter() 476 .all(|v| v.intent().publication_policy_digest() == &policy) 477 ); 478 assert!( 479 after 480 .delivery_attempts() 481 .iter() 482 .any(|attempt| attempt.target_ordinals() == [2]) 483 ); 484 ready.client.close().await.unwrap(); 485 stop(relay).await; 486 } 487 488 #[tokio::test] 489 async fn ordinary_public_resume_requires_original_targets_to_remain_configured() { 490 let mut original_relay = Relay::new().await; 491 original_relay.accepting.store(false, Ordering::SeqCst); 492 let replacement = Relay::new().await; 493 let ready = Ready::new( 494 &original_relay, 495 StartMode::Online, 496 PublicSigner::new(), 497 true, 498 ) 499 .await; 500 let receipt = ready 501 .client 502 .listings() 503 .publish(ready.context(218, now() + 120), input(218)) 504 .await 505 .unwrap(); 506 let original_bytes = original_relay.events.recv().await.unwrap(); 507 let before = ready.snapshot(218).await; 508 assert_eq!(before.delivery_attempts().len(), 1); 509 ready.client.close().await.unwrap(); 510 511 let store = ready.fixture.reopen(ready.input.clone()).await; 512 let (client, scope) = open( 513 &ready.fixture, 514 &store, 515 &replacement, 516 "cache-reconfigured", 517 ready.signer.clone(), 518 (&ready.input, &ready.proof), 519 StartMode::Online, 520 ) 521 .await; 522 let observed = client 523 .operations(&scope) 524 .get(OperationId::from_bytes([218; 32])) 525 .await 526 .unwrap() 527 .unwrap(); 528 assert_eq!( 529 observed.operation_checkpoint(), 530 receipt.operation_checkpoint() 531 ); 532 let error = client 533 .operations(&scope) 534 .resume( 535 OperationId::from_bytes([218; 32]), 536 Some(receipt.command_digest()), 537 ) 538 .await 539 .unwrap_err(); 540 assert_eq!(error.code(), Code::Unauthorized); 541 assert_eq!(error.context().admission(), Admission::Confirmed); 542 assert_eq!( 543 error.context().operation_id(), 544 Some(OperationId::from_bytes([218; 32])) 545 ); 546 let after = store 547 .operation_snapshot( 548 scope.binding().clone(), 549 OperationId::from_bytes([218; 32]), 550 Some(receipt.command_digest()), 551 copy(&ready.proof), 552 ) 553 .await 554 .unwrap() 555 .unwrap(); 556 assert_eq!(after.checkpoint_digest(), before.checkpoint_digest()); 557 assert_eq!(after.delivery_attempts().len(), 1); 558 assert_eq!(replacement.count.load(Ordering::SeqCst), 0); 559 assert_eq!(replacement.requests.load(Ordering::SeqCst), 0); 560 assert_eq!(original_relay.count.load(Ordering::SeqCst), 1); 561 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 562 client.close().await.unwrap(); 563 564 original_relay.accepting.store(true, Ordering::SeqCst); 565 let store = ready.fixture.reopen(ready.input.clone()).await; 566 let (client, scope) = open( 567 &ready.fixture, 568 &store, 569 &original_relay, 570 "cache-original-restored", 571 ready.signer.clone(), 572 (&ready.input, &ready.proof), 573 StartMode::Online, 574 ) 575 .await; 576 let resumed = client 577 .operations(&scope) 578 .resume( 579 OperationId::from_bytes([218; 32]), 580 Some(receipt.command_digest()), 581 ) 582 .await 583 .unwrap(); 584 assert_eq!(resumed.business_result(), observed.business_result()); 585 assert!( 586 resumed 587 .delivery_observations() 588 .iter() 589 .any(|observation| { observation.outcome() == radroots::TargetObservation::Accepted }) 590 ); 591 assert_eq!( 592 tokio::time::timeout(Duration::from_secs(5), original_relay.events.recv()) 593 .await 594 .unwrap() 595 .unwrap(), 596 original_bytes 597 ); 598 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 599 assert_eq!(ready.signer.private_calls.load(Ordering::SeqCst), 0); 600 client.close().await.unwrap(); 601 stop(replacement).await; 602 stop(original_relay).await; 603 } 604 605 #[tokio::test] 606 async fn public_snapshot_cannot_remove_installation_beneath_retained_delivery() { 607 let relay = Relay::new().await; 608 let ready = Ready::new(&relay, StartMode::Online, PublicSigner::new(), true).await; 609 ready 610 .client 611 .listings() 612 .publish(ready.context(219, now() + 120), input(219)) 613 .await 614 .unwrap(); 615 let snapshot = ready.snapshot(219).await; 616 assert!(snapshot.listing_installation().is_some()); 617 assert!(!snapshot.delivery_attempts().is_empty()); 618 assert!(!snapshot.delivery_observations().is_empty()); 619 let changed = snapshot.with_public_installation(None, vec![]); 620 ready.client.close().await.unwrap(); 621 stop(relay).await; 622 assert!(matches!(changed, Err(OperationError::RecoveryRequired))); 623 } 624 625 #[tokio::test] 626 async fn ordinary_unknown_public_issuance_is_never_reissued_by_query_or_explicit_resume() { 627 let relay = Relay::new().await; 628 let signer = PublicSigner::new(); 629 signer.unknown.store(true, Ordering::SeqCst); 630 let ready = Ready::new(&relay, StartMode::Online, signer, true).await; 631 let receipt = ready 632 .client 633 .listings() 634 .publish(ready.context(214, now() + 120), input(214)) 635 .await 636 .unwrap(); 637 assert_eq!(receipt.issuance(), Issuance::Unknown); 638 assert!(receipt.business_result().is_none()); 639 assert_eq!(receipt.message_id(), None); 640 let resumed = ready 641 .client 642 .operations(&ready.scope) 643 .resume( 644 OperationId::from_bytes([214; 32]), 645 Some(receipt.command_digest()), 646 ) 647 .await 648 .unwrap(); 649 assert_eq!(resumed.issuance(), Issuance::Unknown); 650 assert!(resumed.business_result().is_none()); 651 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 652 assert_eq!(relay.count.load(Ordering::SeqCst), 0); 653 ready.client.close().await.unwrap(); 654 stop(relay).await; 655 } 656 657 #[tokio::test] 658 async fn ordinary_public_observer_loss_retains_actual_obtained_output_until_close_drains() { 659 let relay = Relay::new().await; 660 let signer = PublicSigner::new(); 661 let (reached, produced) = oneshot::channel(); 662 let (resume, released) = oneshot::channel(); 663 *signer.pause.lock().unwrap() = Some((reached, released)); 664 let ready = Ready::new(&relay, StartMode::Online, signer, true).await; 665 let context = ready.context(215, now() + 120); 666 let client = ready.client.clone(); 667 let value = input(215); 668 let observer = tokio::spawn(async move { client.listings().publish(context, value).await }); 669 tokio::time::timeout(Duration::from_secs(5), produced) 670 .await 671 .unwrap() 672 .unwrap(); 673 observer.abort(); 674 assert!(observer.await.unwrap_err().is_cancelled()); 675 { 676 let mut close = Box::pin(ready.client.close()); 677 assert!(futures::poll!(&mut close).is_pending()); 678 } 679 assert!(matches!( 680 NativeWorkspace::open_existing( 681 &ready.fixture.path, 682 ready.input.clone(), 683 ready.fixture.crypto(), 684 ready.fixture.clock.clone() 685 ) 686 .await, 687 Err(WorkspaceError::WriterActive) 688 )); 689 resume.send(()).unwrap(); 690 tokio::time::timeout(Duration::from_secs(5), ready.client.close()) 691 .await 692 .unwrap() 693 .unwrap(); 694 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 695 let reopened = ready.fixture.reopen(ready.input.clone()).await; 696 let snapshot = reopened 697 .operation_snapshot( 698 ready.scope.binding().clone(), 699 OperationId::from_bytes([215; 32]), 700 None, 701 copy(&ready.proof), 702 ) 703 .await 704 .unwrap() 705 .unwrap(); 706 assert!(snapshot.artifacts()[0].obtained().is_some()); 707 assert!(snapshot.listing_installation().is_some()); 708 assert!(snapshot.installed_message().is_none()); 709 reopened.close_workspace().await.unwrap(); 710 stop(relay).await; 711 } 712 713 #[tokio::test] 714 async fn ordinary_public_capture_failure_retains_writer_and_recovers_original_without_new_signature() 715 { 716 let relay = Relay::new().await; 717 let ready = Ready::new(&relay, StartMode::Online, PublicSigner::new(), true).await; 718 let mut db = ready.fixture.database().await; 719 sqlx::query("CREATE TRIGGER public_capture_fault BEFORE INSERT ON artifact_evidence BEGIN SELECT RAISE(ABORT,'public_capture_fault'); END").execute(&mut db).await.unwrap(); 720 assert!( 721 ready 722 .client 723 .listings() 724 .publish(ready.context(216, now() + 120), input(216)) 725 .await 726 .is_err() 727 ); 728 assert!(ready.client.close().await.is_err()); 729 assert!(matches!( 730 NativeWorkspace::open_existing( 731 &ready.fixture.path, 732 ready.input.clone(), 733 ready.fixture.crypto(), 734 ready.fixture.clock.clone() 735 ) 736 .await, 737 Err(WorkspaceError::WriterActive) 738 )); 739 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 740 assert_eq!(relay.count.load(Ordering::SeqCst), 0); 741 sqlx::query("DROP TRIGGER public_capture_fault") 742 .execute(&mut db) 743 .await 744 .unwrap(); 745 db.close().await.unwrap(); 746 ready.client.close().await.unwrap(); 747 let reopened = ready.fixture.reopen(ready.input.clone()).await; 748 let snapshot = reopened 749 .operation_snapshot( 750 ready.scope.binding().clone(), 751 OperationId::from_bytes([216; 32]), 752 None, 753 copy(&ready.proof), 754 ) 755 .await 756 .unwrap() 757 .unwrap(); 758 assert!(snapshot.artifacts()[0].obtained().is_some()); 759 assert!(snapshot.listing_installation().is_none()); 760 let (client, scope) = open( 761 &ready.fixture, 762 &reopened, 763 &relay, 764 "cache-recovery", 765 ready.signer.clone(), 766 (&ready.input, &ready.proof), 767 StartMode::Online, 768 ) 769 .await; 770 let resumed = client 771 .operations(&scope) 772 .resume( 773 OperationId::from_bytes([216; 32]), 774 Some(snapshot.prepared().identity().command_digest()), 775 ) 776 .await 777 .unwrap(); 778 assert_eq!( 779 resumed.business_installation(), 780 BusinessInstallation::Installed 781 ); 782 assert!(resumed.business_result().unwrap().as_listing().is_some()); 783 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 1); 784 assert_eq!(relay.count.load(Ordering::SeqCst), 1); 785 client.close().await.unwrap(); 786 stop(relay).await; 787 } 788 789 #[tokio::test] 790 async fn ordinary_publication_never_upgrades_an_actual_buyer_outbox_to_seller_authority() { 791 let relay = Relay::new().await; 792 let ready = Ready::new(&relay, StartMode::Online, PublicSigner::new(), false).await; 793 assert!( 794 ready 795 .client 796 .listings() 797 .publish(ready.context(217, now() + 120), input(217)) 798 .await 799 .is_err() 800 ); 801 assert_eq!(ready.signer.calls.load(Ordering::SeqCst), 0); 802 assert_eq!(relay.count.load(Ordering::SeqCst), 0); 803 assert!( 804 ready 805 .store 806 .operation( 807 ready.scope.binding().clone(), 808 OperationId::from_bytes([217; 32]), 809 None, 810 copy(&ready.proof) 811 ) 812 .await 813 .unwrap() 814 .is_none() 815 ); 816 ready.client.close().await.unwrap(); 817 stop(relay).await; 818 }