memory.rs (35321B)
1 #![cfg(feature = "memory")] 2 3 use futures_executor::block_on; 4 use radroots_event::{ 5 SignedEvent, 6 admission::{AdmissionPolicy, RawEvent, SignatureVerifier, VisibilityPolicy}, 7 envelope::EventEnvelope, 8 wire::Nip01EventWire, 9 }; 10 use radroots_protocol::runtime::v1::OperationId; 11 use radroots_storage::{ 12 Error, EventStore, Journal, Outbox, ProjectionStore, 13 atomic::{ 14 AtomicCommit, AtomicCommitDigest, AtomicCommitDisposition, AtomicCommitId, AtomicStorage, 15 AtomicWorkflow, CommitIngested, 16 }, 17 event::{ 18 EventAdmission, EventPosition, EventQuery, EventQueryBounds, EventSequence, 19 SourceGeneration, 20 }, 21 journal::{ 22 IdempotencyDigest, IdempotencyKey, JournalStage, OperationInstanceId, PrepareOperation, 23 RECOVERABLE_QUERY_LIMIT_MAX, 24 }, 25 memory::MemoryStorage, 26 outbox::{ 27 ClaimOutboxItems, DeliveryPlanDigest, EnqueueDisposition, EnqueueOutboxItem, LeaseId, 28 LeaseOwner, OutboxItemId, OutboxStage, 29 }, 30 private_artifact::{ 31 ArtifactCommitment, ArtifactKind, ArtifactSchemaId, DurableSecretReference, 32 EXPIRED_ARTIFACT_QUERY_LIMIT_MAX, PrivateArtifactId, PrivateArtifactMetadata, 33 PrivateArtifactRevision, PrivateArtifactStore, RetentionPolicy, 34 }, 35 projection::{ 36 InvalidationReason, ProjectionCheckpoint, ProjectionDocument, ProjectionGeneration, 37 ProjectionHealth, ProjectionId, ProjectionInvalidation, ProjectionRevision, 38 ProjectionSnapshot, RawSourceDigest, RebuildStage, RebuildTicket, RebuildTicketId, 39 RebuildTransition, 40 }, 41 }; 42 use radroots_transport::{ 43 DeliveryRequest, Target, TargetSet, TransportId, 44 policy::{SatisfactionClass, SatisfactionPolicy, TargetPolicy}, 45 sink::DeliveryPayload, 46 source::{EventProvenance, ObservedEvent}, 47 }; 48 49 fn signed_event() -> SignedEvent { 50 signed_event_with(1_800_000_100, 0, vec![], "memory-backend") 51 } 52 53 fn signed_event_with( 54 created_at: u64, 55 kind: u32, 56 tags: Vec<Vec<String>>, 57 content: &str, 58 ) -> SignedEvent { 59 let mut wire = Nip01EventWire { 60 id: "0".repeat(64), 61 pubkey: "585591529da0bab31b3b1b1f986611cf5f435dca84f978c89ee8a40cca7103df".to_owned(), 62 created_at, 63 kind, 64 tags, 65 content: content.to_owned(), 66 sig: "42".repeat(64), 67 extra: Default::default(), 68 }; 69 wire.id = wire.computed_event_id().expect("event id").to_hex(); 70 let raw = serde_json::json!({ 71 "id": &wire.id, 72 "pubkey": &wire.pubkey, 73 "created_at": wire.created_at, 74 "kind": wire.kind, 75 "tags": &wire.tags, 76 "content": &wire.content, 77 "sig": &wire.sig, 78 }) 79 .to_string(); 80 SignedEvent::from_wire_verified_id(wire, raw).expect("signed event") 81 } 82 83 struct Allow; 84 85 impl SignatureVerifier for Allow { 86 fn verify_signature(&self, _event: &EventEnvelope) -> Result<(), radroots_event::Error> { 87 Ok(()) 88 } 89 } 90 91 impl AdmissionPolicy for Allow { 92 type Error = core::convert::Infallible; 93 94 fn policy_id(&self) -> &'static str { 95 "test.storage-memory.admission.v1" 96 } 97 98 fn admit( 99 &self, 100 _event: &radroots_event::admission::ContractValidatedEvent, 101 ) -> Result<(), Self::Error> { 102 Ok(()) 103 } 104 } 105 106 impl VisibilityPolicy for Allow { 107 type Error = core::convert::Infallible; 108 109 fn policy_id(&self) -> &'static str { 110 "test.storage-memory.visibility.v1" 111 } 112 113 fn make_visible( 114 &self, 115 _event: &radroots_event::admission::AdmittedEvent, 116 ) -> Result<(), Self::Error> { 117 Ok(()) 118 } 119 } 120 121 fn admission(event: SignedEvent, at: u64) -> EventAdmission { 122 let target = Target::new(TransportId::NOSTR, "wss://relay.example").expect("target"); 123 let provenance = EventProvenance::new(TransportId::NOSTR, target.fingerprint().clone(), at) 124 .expect("provenance"); 125 EventAdmission::raw(ObservedEvent::new(event, provenance)) 126 } 127 128 fn visible_admission(event: SignedEvent, at: u64) -> EventAdmission { 129 let verified = RawEvent::new(event.envelope().clone()) 130 .verify_id() 131 .expect("event id") 132 .verify_signature(&Allow) 133 .expect("signature"); 134 let validated = if event.envelope().kind_u32() == 5 { 135 verified 136 .validate_contract_for_admission("radroots.social.deletion_request.v1") 137 .expect("admission-selected contract") 138 } else { 139 verified.validate_contract().expect("contract") 140 }; 141 let visible = validated 142 .admit_with(&Allow) 143 .expect("admission") 144 .make_visible_with(&Allow) 145 .expect("visibility"); 146 let target = Target::new(TransportId::NOSTR, "wss://relay.example").expect("target"); 147 let provenance = EventProvenance::new(TransportId::NOSTR, target.fingerprint().clone(), at) 148 .expect("provenance"); 149 EventAdmission::visible(ObservedEvent::new(event, provenance), visible) 150 .expect("visible admission") 151 } 152 153 #[test] 154 fn verified_replacement_changes_visibility_without_increasing_raw_count() { 155 let store = MemoryStorage::new(SourceGeneration::new([19; 32]).unwrap()); 156 let old = signed_event_with(10, 0, vec![], r#"{"display_name":"Old Farm","bot":false}"#); 157 let newer = signed_event_with(20, 0, vec![], "malformed profile"); 158 block_on(store.admit(visible_admission(old.clone(), 10))).unwrap(); 159 let raw = admission(newer.clone(), 20); 160 block_on(store.admit(raw.clone())).unwrap(); 161 let token_before = block_on(store.source_token()).unwrap(); 162 assert_eq!(token_before.epoch(), 2); 163 let before = block_on(store.rebuild_visibility()).unwrap(); 164 assert_eq!(before.visible_event_ids(), &[*old.id()]); 165 let verified = RawEvent::new(newer.envelope().clone()) 166 .verify_id() 167 .unwrap() 168 .verify_signature(&Allow) 169 .unwrap(); 170 let advanced = block_on( 171 store.admit( 172 EventAdmission::verified( 173 ObservedEvent::new(newer.clone(), raw.provenance().clone()), 174 verified, 175 ) 176 .unwrap(), 177 ), 178 ) 179 .unwrap(); 180 assert_eq!( 181 advanced.disposition(), 182 radroots_storage::event::AdmissionDisposition::Advanced 183 ); 184 assert_eq!( 185 block_on(store.source_token()).unwrap().epoch(), 186 token_before.epoch() + 1 187 ); 188 let after = block_on(store.rebuild_visibility()).unwrap(); 189 assert_ne!(before.digest(), after.digest()); 190 assert_eq!(after.current_heads()[0].event_id, *newer.id()); 191 assert!(after.visible_event_ids().is_empty()); 192 assert_eq!( 193 block_on(EventStore::status(&store)).unwrap().raw_events(), 194 2 195 ); 196 assert!( 197 block_on(store.query_visible(EventQuery::all(EventQueryBounds::first(10).unwrap()))) 198 .unwrap() 199 .items() 200 .is_empty() 201 ); 202 } 203 204 #[test] 205 fn memory_visibility_rebuild_is_current_delete_aware_and_atomic_parity_safe() { 206 let generation = SourceGeneration::new([17; 32]).expect("generation"); 207 let direct = MemoryStorage::new(generation); 208 let atomic_store = MemoryStorage::new(generation); 209 let old = signed_event_with( 210 1_800_000_100, 211 0, 212 vec![], 213 r#"{"display_name":"Old Farm","bot":false}"#, 214 ); 215 let current = signed_event_with( 216 1_800_000_200, 217 0, 218 vec![], 219 r#"{"display_name":"Current Farm","bot":false}"#, 220 ); 221 let deletion = signed_event_with( 222 1_800_000_300, 223 5, 224 vec![vec!["e".to_owned(), current.id().to_hex()]], 225 "retired profile", 226 ); 227 let admissions = [ 228 visible_admission(old.clone(), 100), 229 visible_admission(current.clone(), 200), 230 visible_admission(deletion.clone(), 300), 231 ]; 232 233 for admission in admissions.clone() { 234 block_on(direct.admit(admission)).expect("direct admission"); 235 } 236 for (index, admission) in admissions.into_iter().enumerate() { 237 let identity = u8::try_from(index).expect("commit identity") + 20; 238 block_on(atomic_store.commit(atomic( 239 identity, 240 identity, 241 AtomicWorkflow::Ingested(Box::new(CommitIngested::new(admission, None))), 242 ))) 243 .expect("atomic admission"); 244 } 245 246 let snapshot = block_on(direct.rebuild_visibility()).expect("visibility rebuild"); 247 let atomic_snapshot = 248 block_on(atomic_store.rebuild_visibility()).expect("atomic visibility rebuild"); 249 assert_eq!(snapshot, atomic_snapshot); 250 assert_eq!(snapshot.current_heads()[0].event_id, *current.id()); 251 assert_eq!(snapshot.visible_event_ids(), &[*deletion.id()]); 252 assert_eq!(snapshot.suppressed_event_ids(), &[*current.id()]); 253 assert_eq!(snapshot.superseded_event_ids(), &[*old.id()]); 254 let page = block_on(direct.query_visible(EventQuery::all( 255 EventQueryBounds::first(10).expect("bounds"), 256 ))) 257 .expect("visible page"); 258 assert_eq!(page.items().len(), 1); 259 assert_eq!(page.items()[0].event().id(), deletion.id()); 260 assert_eq!( 261 block_on(EventStore::status(&direct)) 262 .expect("status") 263 .visible_events(), 264 1 265 ); 266 } 267 268 fn prepare(instance: OperationInstanceId) -> PrepareOperation { 269 PrepareOperation::new( 270 instance, 271 OperationId::SyncPush, 272 IdempotencyKey::parse("memory-operation").expect("key"), 273 IdempotencyDigest::new([3; 32]), 274 100, 275 ) 276 .expect("prepare") 277 } 278 279 fn atomic(id: u8, digest: u8, workflow: AtomicWorkflow) -> AtomicCommit { 280 AtomicCommit::new( 281 AtomicCommitId::new([id; 16]).expect("commit id"), 282 AtomicCommitDigest::new([digest; 32]), 283 200, 284 workflow, 285 ) 286 .expect("atomic commit") 287 } 288 289 fn delivery_request(event: SignedEvent) -> DeliveryRequest { 290 DeliveryRequest::new( 291 "memory-delivery", 292 DeliveryPayload::new(event), 293 TargetSet::new(vec![ 294 Target::nostr_relay("wss://relay.example").expect("target"), 295 ]) 296 .expect("target set"), 297 SatisfactionPolicy::new(SatisfactionClass::Accepted, TargetPolicy::all()), 298 1_000, 299 ) 300 .expect("delivery request") 301 } 302 303 fn private_metadata() -> PrivateArtifactMetadata { 304 PrivateArtifactMetadata::new( 305 PrivateArtifactId::new([9; 16]).expect("artifact id"), 306 ArtifactKind::parse("memory.private").expect("kind"), 307 ArtifactSchemaId::parse("memory.private.v1").expect("schema"), 308 ArtifactCommitment::new([8; 32]), 309 64, 310 DurableSecretReference::new("memory", "caller-owned-key", 1).expect("secret reference"), 311 RetentionPolicy::new(None, Some(300)).expect("retention"), 312 100, 313 ) 314 .expect("metadata") 315 } 316 317 #[test] 318 fn memory_event_and_journal_implement_the_canonical_spis() { 319 let store = MemoryStorage::new(SourceGeneration::new([7; 32]).expect("generation")); 320 let event = signed_event(); 321 let event_id = *event.id(); 322 block_on(store.admit(admission(event, 10))).expect("admit"); 323 let raw = block_on(store.query_raw(EventQuery::all( 324 EventQueryBounds::first(10).expect("bounds"), 325 ))) 326 .expect("query"); 327 assert_eq!(raw.items().len(), 1); 328 assert_eq!(raw.items()[0].event().id(), &event_id); 329 assert_eq!( 330 block_on(EventStore::status(&store)) 331 .expect("status") 332 .raw_events(), 333 1 334 ); 335 336 let instance = OperationInstanceId::new([1; 16]).expect("instance"); 337 let prepared = block_on(store.prepare(prepare(instance))).expect("prepare"); 338 let signed = block_on( 339 store.transition(radroots_storage::journal::JournalTransition::signed( 340 instance, 341 prepared.record().revision(), 342 event_id, 343 )), 344 ) 345 .expect("signed"); 346 assert_eq!(signed.state().stage(), JournalStage::Signed); 347 assert_eq!( 348 block_on(store.by_idempotency_key( 349 OperationId::SyncPush, 350 IdempotencyKey::parse("memory-operation").expect("key"), 351 )) 352 .expect("lookup") 353 .expect("record") 354 .instance_id(), 355 instance 356 ); 357 } 358 359 #[test] 360 fn memory_atomic_ingest_commits_event_state_and_replays() { 361 let store = MemoryStorage::default(); 362 let event = signed_event(); 363 let ingest = atomic( 364 1, 365 1, 366 AtomicWorkflow::Ingested(Box::new(CommitIngested::new(admission(event, 20), None))), 367 ); 368 let committed = block_on(store.commit(ingest.clone())).expect("atomic ingest"); 369 assert_eq!(committed.disposition(), AtomicCommitDisposition::Committed); 370 assert_eq!( 371 block_on(store.commit(ingest)) 372 .expect("atomic replay") 373 .disposition(), 374 AtomicCommitDisposition::Replay 375 ); 376 assert_eq!( 377 block_on(EventStore::status(&store)) 378 .expect("status") 379 .raw_events(), 380 1 381 ); 382 } 383 384 #[test] 385 fn memory_outbox_uses_caller_supplied_time_and_lease_identity() { 386 let store = MemoryStorage::default(); 387 let item = EnqueueOutboxItem::new( 388 OutboxItemId::new([5; 16]).expect("item id"), 389 OperationInstanceId::new([5; 16]).expect("instance"), 390 DeliveryPlanDigest::new([5; 32]), 391 delivery_request(signed_event()), 392 100, 393 ) 394 .expect("enqueue"); 395 assert_eq!( 396 block_on(store.enqueue(item.clone())) 397 .expect("enqueue") 398 .disposition(), 399 EnqueueDisposition::Created 400 ); 401 assert_eq!( 402 block_on(store.enqueue(item)).expect("replay").disposition(), 403 EnqueueDisposition::Replay 404 ); 405 let claimed = block_on( 406 store.claim( 407 ClaimOutboxItems::new( 408 LeaseOwner::parse("memory-worker").expect("owner"), 409 LeaseId::new([6; 16]).expect("lease seed"), 410 200, 411 250, 412 1, 413 ) 414 .expect("claim"), 415 ), 416 ) 417 .expect("claim"); 418 assert_eq!(claimed.len(), 1); 419 assert_eq!(claimed[0].record().stage(), OutboxStage::Leased); 420 assert_eq!(block_on(Outbox::status(&store)).expect("status").leased, 1); 421 } 422 423 #[test] 424 fn memory_projection_rebuild_and_private_metadata_share_deterministic_state() { 425 let store = MemoryStorage::default(); 426 let projection_id = ProjectionId::parse("memory.test").expect("projection id"); 427 let initial_generation = ProjectionGeneration::new([4; 32]).expect("generation"); 428 let replacement_generation = ProjectionGeneration::new([5; 32]).expect("generation"); 429 let checkpoint = 430 ProjectionCheckpoint::new(projection_id.clone(), initial_generation, None, 0, 200) 431 .expect("checkpoint"); 432 assert_eq!( 433 block_on(store.checkpoint(checkpoint)) 434 .expect("checkpoint") 435 .health(), 436 ProjectionHealth::Ready 437 ); 438 let invalidation = ProjectionInvalidation::new( 439 projection_id, 440 initial_generation, 441 replacement_generation, 442 InvalidationReason::ProjectionGenerationChanged, 443 210, 444 ) 445 .expect("invalidation"); 446 assert_eq!( 447 block_on(store.invalidate(invalidation.clone())) 448 .expect("invalidate") 449 .health(), 450 ProjectionHealth::Invalidated 451 ); 452 let ticket_id = RebuildTicketId::new([7; 16]).expect("ticket id"); 453 block_on( 454 store.request_rebuild( 455 RebuildTicket::requested( 456 ticket_id, 457 invalidation, 458 store.generation(), 459 None, 460 RawSourceDigest::new([8; 32]), 461 ) 462 .expect("ticket"), 463 ), 464 ) 465 .expect("request rebuild"); 466 let running = block_on(store.transition_rebuild(RebuildTransition::start( 467 ticket_id, 468 ProjectionRevision::INITIAL, 469 220, 470 ))) 471 .expect("start rebuild"); 472 assert_eq!(running.stage(), RebuildStage::Running); 473 474 let metadata = private_metadata(); 475 block_on(store.put_metadata(metadata.clone())).expect("put metadata"); 476 assert_eq!( 477 block_on(store.expired(299, 1)).expect("not expired").len(), 478 0 479 ); 480 assert_eq!(block_on(store.expired(300, 1)).expect("expired").len(), 1); 481 block_on(store.mark_expired( 482 metadata.artifact_id(), 483 PrivateArtifactRevision::INITIAL, 484 300, 485 )) 486 .expect("mark expired"); 487 assert_eq!( 488 block_on(PrivateArtifactStore::status(&store)) 489 .expect("status") 490 .expired, 491 1 492 ); 493 } 494 495 #[test] 496 fn atomic_projection_failure_leaves_event_and_checkpoint_unchanged() { 497 let store = MemoryStorage::default(); 498 let projection_id = ProjectionId::parse("memory.atomic").expect("projection id"); 499 let generation = ProjectionGeneration::new([4; 32]).expect("projection generation"); 500 let first = ProjectionCheckpoint::new(projection_id.clone(), generation, None, 2, 200) 501 .expect("checkpoint"); 502 block_on(store.checkpoint(first)).expect("initial checkpoint"); 503 let regressing = ProjectionCheckpoint::new(projection_id.clone(), generation, None, 1, 201) 504 .expect("checkpoint"); 505 let request = atomic( 506 4, 507 4, 508 AtomicWorkflow::Ingested(Box::new(CommitIngested::new( 509 admission(signed_event(), 20), 510 Some(regressing), 511 ))), 512 ); 513 assert_eq!( 514 block_on(store.commit(request)), 515 Err(radroots_storage::Error::ProjectionCheckpointRegression) 516 ); 517 assert_eq!( 518 block_on(EventStore::status(&store)) 519 .expect("status") 520 .raw_events(), 521 0 522 ); 523 assert_eq!( 524 block_on(ProjectionStore::status(&store, projection_id)) 525 .expect("projection status") 526 .expect("projection") 527 .checkpoint() 528 .expect("checkpoint") 529 .projected_rows(), 530 2 531 ); 532 } 533 534 #[test] 535 fn memory_event_journal_and_closed_state_fail_closed() { 536 let generation = SourceGeneration::new([7; 32]).unwrap(); 537 let store = MemoryStorage::new(generation); 538 assert_eq!(store.generation(), generation); 539 let event = signed_event(); 540 let inserted = block_on(store.admit(admission(event.clone(), 10))).unwrap(); 541 assert_eq!( 542 block_on(store.admit(admission(event.clone(), 10))) 543 .unwrap() 544 .disposition(), 545 radroots_storage::event::AdmissionDisposition::Duplicate 546 ); 547 let wrong_cursor = EventPosition::new( 548 SourceGeneration::new([8; 32]).unwrap(), 549 EventSequence::new(1).unwrap(), 550 ); 551 let query = EventQuery::all(EventQueryBounds::first(1).unwrap().after(wrong_cursor)); 552 assert_eq!( 553 block_on(store.query_raw(query)), 554 Err(Error::SourceGenerationChanged) 555 ); 556 assert_eq!( 557 block_on(store.query_provenance( 558 *event.id(), 559 EventQueryBounds::first(1).unwrap().after(wrong_cursor), 560 )), 561 Err(Error::SourceGenerationChanged) 562 ); 563 let after = EventQueryBounds::first(1) 564 .unwrap() 565 .after(inserted.position()); 566 assert!( 567 block_on(store.query_provenance(*event.id(), after)) 568 .unwrap() 569 .items() 570 .is_empty() 571 ); 572 assert_eq!( 573 block_on(store.query_provenance( 574 radroots_event::EventId::parse("f".repeat(64)).unwrap(), 575 EventQueryBounds::first(1).unwrap(), 576 )), 577 Err(Error::EventNotFound) 578 ); 579 580 let instance = OperationInstanceId::new([1; 16]).unwrap(); 581 let operation = prepare(instance); 582 block_on(store.prepare(operation.clone())).unwrap(); 583 assert_eq!( 584 block_on( 585 store.prepare( 586 PrepareOperation::new( 587 instance, 588 OperationId::SyncPush, 589 IdempotencyKey::parse("memory-operation").unwrap(), 590 IdempotencyDigest::new([4; 32]), 591 100, 592 ) 593 .unwrap() 594 ) 595 ), 596 Err(Error::IdempotencyConflict) 597 ); 598 assert_eq!( 599 block_on( 600 store.prepare( 601 PrepareOperation::new( 602 instance, 603 OperationId::SyncPush, 604 IdempotencyKey::parse("different-key").unwrap(), 605 IdempotencyDigest::new([3; 32]), 606 100, 607 ) 608 .unwrap() 609 ) 610 ), 611 Err(Error::OperationIdentityMismatch) 612 ); 613 assert!( 614 block_on(store.operation(OperationInstanceId::new([2; 16]).unwrap())) 615 .unwrap() 616 .is_none() 617 ); 618 assert!( 619 block_on(store.by_idempotency_key( 620 OperationId::SyncPull, 621 IdempotencyKey::parse("memory-operation").unwrap() 622 )) 623 .unwrap() 624 .is_none() 625 ); 626 assert_eq!( 627 block_on(store.recoverable(0)), 628 Err(Error::InvalidJournalQueryLimit) 629 ); 630 631 block_on(radroots_storage::backup::StorageReliability::close(&store)).unwrap(); 632 assert_eq!( 633 block_on(EventStore::status(&store)), 634 Err(Error::BackendUnavailable) 635 ); 636 assert_eq!( 637 block_on(store.admit(admission(event, 11))), 638 Err(Error::BackendUnavailable) 639 ); 640 } 641 642 #[test] 643 fn memory_outbox_conflict_and_claim_matrix_is_complete() { 644 let store = MemoryStorage::default(); 645 let make_item = |item: u8, instance: u8, digest: u8, created: u64| { 646 EnqueueOutboxItem::new( 647 OutboxItemId::new([item; 16]).unwrap(), 648 OperationInstanceId::new([instance; 16]).unwrap(), 649 DeliveryPlanDigest::new([digest; 32]), 650 delivery_request(signed_event()), 651 created, 652 ) 653 .unwrap() 654 }; 655 block_on(store.enqueue(make_item(1, 1, 1, 100))).unwrap(); 656 assert_eq!( 657 block_on(store.enqueue(make_item(1, 1, 2, 100))), 658 Err(Error::OutboxPlanConflict) 659 ); 660 assert_eq!( 661 block_on(store.enqueue(make_item(2, 1, 1, 100))), 662 Err(Error::OutboxPlanConflict) 663 ); 664 assert!( 665 block_on(store.item(OutboxItemId::new([9; 16]).unwrap())) 666 .unwrap() 667 .is_none() 668 ); 669 let first_claim = ClaimOutboxItems::new( 670 LeaseOwner::parse("worker").unwrap(), 671 LeaseId::new([3; 16]).unwrap(), 672 200, 673 300, 674 1, 675 ) 676 .unwrap(); 677 assert_eq!(block_on(store.claim(first_claim)).unwrap().len(), 1); 678 let concurrent = ClaimOutboxItems::new( 679 LeaseOwner::parse("worker-two").unwrap(), 680 LeaseId::new([4; 16]).unwrap(), 681 250, 682 350, 683 1, 684 ) 685 .unwrap(); 686 assert!(block_on(store.claim(concurrent)).unwrap().is_empty()); 687 assert_eq!( 688 block_on(store.release( 689 OutboxItemId::new([9; 16]).unwrap(), 690 LeaseId::new([4; 16]).unwrap(), 691 radroots_storage::outbox::OutboxRevision::INITIAL, 692 260, 693 None, 694 )), 695 Err(Error::OutboxItemNotFound) 696 ); 697 } 698 699 #[test] 700 fn memory_projection_and_private_artifact_conflict_matrix_is_complete() { 701 let store = MemoryStorage::default(); 702 let projection_id = ProjectionId::parse("memory.matrix").unwrap(); 703 let initial = ProjectionGeneration::new([4; 32]).unwrap(); 704 let replacement = ProjectionGeneration::new([5; 32]).unwrap(); 705 let initial_checkpoint = 706 ProjectionCheckpoint::new(projection_id.clone(), initial, None, 1, 100).unwrap(); 707 block_on(store.checkpoint(initial_checkpoint.clone())).unwrap(); 708 assert_eq!( 709 block_on(store.checkpoint( 710 ProjectionCheckpoint::new(projection_id.clone(), replacement, None, 2, 101).unwrap() 711 )), 712 Err(Error::ProjectionCheckpointMismatch) 713 ); 714 assert_eq!( 715 block_on(store.checkpoint( 716 ProjectionCheckpoint::new(projection_id.clone(), initial, None, 0, 101).unwrap() 717 )), 718 Err(Error::ProjectionCheckpointRegression) 719 ); 720 let missing = ProjectionInvalidation::new( 721 ProjectionId::parse("missing").unwrap(), 722 initial, 723 replacement, 724 InvalidationReason::OperatorRequested, 725 110, 726 ) 727 .unwrap(); 728 assert_eq!( 729 block_on(store.invalidate(missing)), 730 Err(Error::ProjectionCheckpointMismatch) 731 ); 732 let wrong = ProjectionInvalidation::new( 733 projection_id.clone(), 734 replacement, 735 ProjectionGeneration::new([6; 32]).unwrap(), 736 InvalidationReason::OperatorRequested, 737 110, 738 ) 739 .unwrap(); 740 assert_eq!( 741 block_on(store.invalidate(wrong)), 742 Err(Error::ProjectionCheckpointMismatch) 743 ); 744 let invalidation = ProjectionInvalidation::new( 745 projection_id.clone(), 746 initial, 747 replacement, 748 InvalidationReason::OperatorRequested, 749 110, 750 ) 751 .unwrap(); 752 block_on(store.invalidate(invalidation.clone())).unwrap(); 753 assert_eq!( 754 block_on(store.invalidate(invalidation.clone())) 755 .unwrap() 756 .health(), 757 ProjectionHealth::Invalidated 758 ); 759 let conflicting_invalidation = ProjectionInvalidation::new( 760 projection_id.clone(), 761 initial, 762 ProjectionGeneration::new([6; 32]).unwrap(), 763 InvalidationReason::ProjectionGenerationChanged, 764 111, 765 ) 766 .unwrap(); 767 assert_eq!( 768 block_on(store.invalidate(conflicting_invalidation)), 769 Err(Error::ProjectionRevisionConflict) 770 ); 771 assert!( 772 block_on(store.invalidation(projection_id.clone(), replacement)) 773 .unwrap() 774 .is_some() 775 ); 776 assert!( 777 block_on(store.invalidation( 778 projection_id.clone(), 779 ProjectionGeneration::new([9; 32]).unwrap(), 780 )) 781 .unwrap() 782 .is_none() 783 ); 784 let ticket = RebuildTicket::requested( 785 RebuildTicketId::new([7; 16]).unwrap(), 786 invalidation.clone(), 787 store.generation(), 788 None, 789 RawSourceDigest::new([8; 32]), 790 ) 791 .unwrap(); 792 assert_eq!( 793 block_on(store.request_rebuild(ticket.clone())).unwrap(), 794 ticket 795 ); 796 let conflicting_ticket = RebuildTicket::requested( 797 ticket.ticket_id(), 798 invalidation, 799 store.generation(), 800 None, 801 RawSourceDigest::new([9; 32]), 802 ) 803 .unwrap(); 804 assert_eq!( 805 block_on(store.request_rebuild(conflicting_ticket)), 806 Err(Error::ProjectionRevisionConflict) 807 ); 808 assert_eq!( 809 block_on(store.request_rebuild(ticket.clone())).unwrap(), 810 ticket 811 ); 812 assert!( 813 block_on(store.rebuild(ticket.ticket_id())) 814 .unwrap() 815 .is_some() 816 ); 817 818 let metadata = private_metadata(); 819 assert_eq!( 820 block_on(store.put_metadata(metadata.clone())).unwrap(), 821 metadata 822 ); 823 assert_eq!( 824 block_on(store.put_metadata(metadata.clone())).unwrap(), 825 metadata 826 ); 827 let conflict = PrivateArtifactMetadata::new( 828 metadata.artifact_id(), 829 ArtifactKind::parse("memory.private").unwrap(), 830 ArtifactSchemaId::parse("memory.private.v1").unwrap(), 831 ArtifactCommitment::new([9; 32]), 832 64, 833 DurableSecretReference::new("memory", "caller-owned-key", 1).unwrap(), 834 RetentionPolicy::new(None, Some(300)).unwrap(), 835 100, 836 ) 837 .unwrap(); 838 assert_eq!( 839 block_on(store.put_metadata(conflict)), 840 Err(Error::PrivateArtifactConflict) 841 ); 842 assert!( 843 block_on(store.metadata(PrivateArtifactId::new([8; 16]).unwrap())) 844 .unwrap() 845 .is_none() 846 ); 847 assert_eq!( 848 block_on(store.expired(0, 1)), 849 Err(Error::InvalidExpiredArtifactQueryLimit) 850 ); 851 assert_eq!( 852 block_on(store.expired(1, 0)), 853 Err(Error::InvalidExpiredArtifactQueryLimit) 854 ); 855 } 856 857 #[test] 858 fn memory_branch_guards_distinguish_each_identity_and_bound() { 859 let store = MemoryStorage::new(SourceGeneration::new([7; 32]).unwrap()); 860 let event = signed_event(); 861 block_on(store.admit(admission(event.clone(), 10))).unwrap(); 862 let other_id = radroots_event::EventId::parse("f".repeat(64)).unwrap(); 863 let selected = block_on(store.query_raw( 864 EventQuery::for_ids(EventQueryBounds::first(10).unwrap(), vec![other_id]).unwrap(), 865 )) 866 .unwrap(); 867 assert!(selected.items().is_empty()); 868 869 let instance = OperationInstanceId::new([1; 16]).unwrap(); 870 block_on(store.prepare(prepare(instance))).unwrap(); 871 let different_operation = PrepareOperation::new( 872 OperationInstanceId::new([2; 16]).unwrap(), 873 OperationId::SyncPull, 874 IdempotencyKey::parse("memory-operation").unwrap(), 875 IdempotencyDigest::new([3; 32]), 876 100, 877 ) 878 .unwrap(); 879 assert_eq!( 880 block_on(store.prepare(different_operation)), 881 Err(Error::IdempotencyConflict) 882 ); 883 let different_instance = PrepareOperation::new( 884 OperationInstanceId::new([2; 16]).unwrap(), 885 OperationId::SyncPush, 886 IdempotencyKey::parse("memory-operation").unwrap(), 887 IdempotencyDigest::new([3; 32]), 888 100, 889 ) 890 .unwrap(); 891 assert_eq!( 892 block_on(store.prepare(different_instance)), 893 Err(Error::IdempotencyConflict) 894 ); 895 assert_eq!( 896 block_on(store.recoverable(RECOVERABLE_QUERY_LIMIT_MAX + 1)), 897 Err(Error::InvalidJournalQueryLimit) 898 ); 899 900 let private = MemoryStorage::default(); 901 block_on(private.put_metadata(private_metadata())).unwrap(); 902 assert_eq!( 903 block_on(private.expired(1, EXPIRED_ARTIFACT_QUERY_LIMIT_MAX + 1)), 904 Err(Error::InvalidExpiredArtifactQueryLimit) 905 ); 906 block_on(private.mark_expired( 907 PrivateArtifactId::new([9; 16]).unwrap(), 908 PrivateArtifactRevision::INITIAL, 909 300, 910 )) 911 .unwrap(); 912 assert!(block_on(private.expired(400, 1)).unwrap().is_empty()); 913 } 914 915 #[test] 916 fn memory_outbox_replay_checks_each_durable_plan_field_and_retry_window() { 917 let make_request = |id: &str| { 918 DeliveryRequest::new( 919 id, 920 DeliveryPayload::new(signed_event()), 921 TargetSet::new(vec![Target::nostr_relay("wss://relay.example").unwrap()]).unwrap(), 922 SatisfactionPolicy::new(SatisfactionClass::Accepted, TargetPolicy::all()), 923 1_000, 924 ) 925 .unwrap() 926 }; 927 let make_item = |instance: u8, digest: u8, request: DeliveryRequest, created: u64| { 928 EnqueueOutboxItem::new( 929 OutboxItemId::new([1; 16]).unwrap(), 930 OperationInstanceId::new([instance; 16]).unwrap(), 931 DeliveryPlanDigest::new([digest; 32]), 932 request, 933 created, 934 ) 935 .unwrap() 936 }; 937 938 for conflict in [ 939 make_item(2, 1, make_request("memory-delivery"), 100), 940 make_item(1, 2, make_request("memory-delivery"), 100), 941 make_item(1, 1, make_request("different-delivery"), 100), 942 make_item(1, 1, make_request("memory-delivery"), 101), 943 ] { 944 let store = MemoryStorage::default(); 945 block_on(store.enqueue(make_item(1, 1, make_request("memory-delivery"), 100))).unwrap(); 946 assert_eq!( 947 block_on(store.enqueue(conflict)), 948 Err(Error::OutboxPlanConflict) 949 ); 950 } 951 952 let store = MemoryStorage::default(); 953 block_on(store.enqueue(make_item(1, 1, make_request("memory-delivery"), 100))).unwrap(); 954 let lease_seed = LeaseId::new([3; 16]).unwrap(); 955 let claimed = block_on( 956 store.claim( 957 ClaimOutboxItems::new( 958 LeaseOwner::parse("worker").unwrap(), 959 lease_seed, 960 200, 961 250, 962 1, 963 ) 964 .unwrap(), 965 ), 966 ) 967 .unwrap(); 968 let record = &claimed[0]; 969 block_on(store.release( 970 record.record().item_id(), 971 record.lease().id(), 972 record.record().revision(), 973 210, 974 Some(300), 975 )) 976 .unwrap(); 977 let early = ClaimOutboxItems::new( 978 LeaseOwner::parse("worker").unwrap(), 979 LeaseId::new([4; 16]).unwrap(), 980 299, 981 350, 982 1, 983 ) 984 .unwrap(); 985 assert!(block_on(store.claim(early)).unwrap().is_empty()); 986 } 987 988 #[test] 989 fn memory_atomic_replay_rejects_a_changed_digest_without_mutation() { 990 let store = MemoryStorage::default(); 991 let event = signed_event(); 992 let committed = atomic( 993 1, 994 1, 995 AtomicWorkflow::Ingested(Box::new(CommitIngested::new( 996 admission(event.clone(), 20), 997 None, 998 ))), 999 ); 1000 block_on(store.commit(committed)).unwrap(); 1001 let conflict = atomic( 1002 1, 1003 2, 1004 AtomicWorkflow::Ingested(Box::new(CommitIngested::new(admission(event, 20), None))), 1005 ); 1006 assert_eq!( 1007 block_on(store.commit(conflict)), 1008 Err(Error::AtomicCommitConflict) 1009 ); 1010 assert_eq!( 1011 block_on(EventStore::status(&store)).unwrap().raw_events(), 1012 1 1013 ); 1014 } 1015 1016 #[test] 1017 fn memory_materialized_documents_replace_and_snapshots_remain_immutable() { 1018 let store = MemoryStorage::default(); 1019 let projection_id = ProjectionId::parse("memory.today").unwrap(); 1020 let generation = ProjectionGeneration::new([21; 32]).unwrap(); 1021 block_on(store.put_projection_document( 1022 projection_id.clone(), 1023 generation, 1024 ProjectionDocument::new("context.one".into(), vec![1]).unwrap(), 1025 )) 1026 .unwrap(); 1027 block_on(store.put_projection_document( 1028 projection_id.clone(), 1029 generation, 1030 ProjectionDocument::new("context.one".into(), vec![2]).unwrap(), 1031 )) 1032 .unwrap(); 1033 assert_eq!( 1034 block_on(store.projection_document( 1035 projection_id.clone(), 1036 generation, 1037 "context.one".into(), 1038 )) 1039 .unwrap() 1040 .unwrap() 1041 .value(), 1042 [2] 1043 ); 1044 for (candidate_id, candidate_generation, candidate_key) in [ 1045 ( 1046 ProjectionId::parse("memory.other").unwrap(), 1047 generation, 1048 "context.one".to_owned(), 1049 ), 1050 ( 1051 projection_id.clone(), 1052 ProjectionGeneration::new([23; 32]).unwrap(), 1053 "context.one".to_owned(), 1054 ), 1055 ( 1056 projection_id.clone(), 1057 generation, 1058 "context.other".to_owned(), 1059 ), 1060 ] { 1061 assert!( 1062 block_on(store.projection_document(candidate_id, candidate_generation, candidate_key,)) 1063 .unwrap() 1064 .is_none() 1065 ); 1066 } 1067 let snapshot = 1068 ProjectionSnapshot::new(projection_id.clone(), [22; 32], generation, 100, vec![3]).unwrap(); 1069 block_on(store.put_projection_snapshot(snapshot.clone())).unwrap(); 1070 block_on(store.put_projection_snapshot(snapshot.clone())).unwrap(); 1071 assert_eq!( 1072 block_on(store.projection_snapshot(projection_id.clone(), [22; 32])).unwrap(), 1073 Some(snapshot) 1074 ); 1075 assert_eq!( 1076 block_on(store.put_projection_snapshot( 1077 ProjectionSnapshot::new(projection_id, [22; 32], generation, 100, vec![4]).unwrap() 1078 )), 1079 Err(Error::CorruptProjectionDocument) 1080 ); 1081 assert!( 1082 block_on( 1083 store.projection_snapshot(ProjectionId::parse("memory.other").unwrap(), [22; 32],) 1084 ) 1085 .unwrap() 1086 .is_none() 1087 ); 1088 assert!( 1089 block_on( 1090 store.projection_snapshot(ProjectionId::parse("memory.today").unwrap(), [23; 32],) 1091 ) 1092 .unwrap() 1093 .is_none() 1094 ); 1095 }