lib

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

receipt_installation.rs (18087B)


      1 use super::issuance::{claim, obtain, prepare, selection};
      2 use super::*;
      3 use radroots_execution::{
      4     BusinessInstallation, Delivery, Issuance, OperationReceipt, RecoveryRequirement,
      5 };
      6 use radroots_storage::command::{
      7     ChatInstallation, ClaimAttemptOutcome, EvidenceCaptureOutcome, EvidenceReceipt,
      8     InstallationOutcome, InstallationRequest, OperationSnapshot, SigningAttempt,
      9 };
     10 #[path = "receipt_installation_refusal.rs"]
     11 mod refusal;
     12 #[path = "receipt_installation_reports.rs"]
     13 mod reports;
     14 
     15 fn request_install(
     16     binding: &WorkspaceBinding,
     17     prepared: &PreparedOperation,
     18     plan: [u8; 32],
     19 ) -> InstallationRequest {
     20     InstallationRequest::new(
     21         binding.clone(),
     22         prepared.identity().operation_id(),
     23         prepared.identity().command_digest(),
     24         plan,
     25     )
     26 }
     27 fn installed(outcome: InstallationOutcome) -> ChatInstallation {
     28     match outcome {
     29         InstallationOutcome::Committed(value) => *value,
     30         InstallationOutcome::ListingCommitted(_) => {
     31             panic!("chat fixture must not install a listing")
     32         }
     33         InstallationOutcome::Unknown(_) => panic!("fixture must observe confirmed T2"),
     34     }
     35 }
     36 async fn capture(
     37     store: &NativeWorkspace,
     38     input: &AttachmentInput,
     39     proof: &AttachmentProof,
     40     attempt: &SigningAttempt,
     41 ) -> EvidenceReceipt {
     42     let evidence = obtain(attempt, true).await;
     43     match store
     44         .capture_evidence(input.clone(), proof, &evidence)
     45         .await
     46         .unwrap()
     47     {
     48         EvidenceCaptureOutcome::Committed(value) => value,
     49         EvidenceCaptureOutcome::Unknown(_) => panic!("fixture must confirm original capture"),
     50     }
     51 }
     52 async fn snapshot(
     53     store: &NativeWorkspace,
     54     binding: &WorkspaceBinding,
     55     prepared: &PreparedOperation,
     56     proof: &AttachmentProof,
     57 ) -> OperationSnapshot {
     58     store
     59         .operation_snapshot(
     60             binding.clone(),
     61             prepared.identity().operation_id(),
     62             Some(prepared.identity().command_digest()),
     63             copy(proof),
     64         )
     65         .await
     66         .unwrap()
     67         .unwrap()
     68 }
     69 
     70 #[tokio::test]
     71 async fn existing_peer_installation_allows_first_self_claim_within_original_window() {
     72     let fixture = Fixture::new();
     73     let (store, input, proof, binding) = fixture.create().await;
     74     let (prepared, digest, recipients) =
     75         prepare(&fixture, &store, &binding, &proof, 225, true).await;
     76     let peer = *recipients
     77         .iter()
     78         .find(|recipient| **recipient != key())
     79         .unwrap();
     80     let peer_claim = claim(
     81         store
     82             .claim_attempt(selection(&binding, &prepared, digest, peer), copy(&proof))
     83             .await
     84             .unwrap(),
     85     );
     86     let peer_capture = capture(&store, &input, &proof, &peer_claim).await;
     87     let original = installed(
     88         store
     89             .install_transition_and_outbox(
     90                 request_install(&binding, &prepared, digest),
     91                 copy(&proof),
     92             )
     93             .await
     94             .unwrap(),
     95     );
     96     assert_eq!(original.basis(), peer_capture);
     97     let partial = snapshot(&store, &binding, &prepared, &proof).await;
     98     assert_eq!(partial.publications().len(), 1);
     99     assert!(
    100         partial
    101             .artifacts()
    102             .iter()
    103             .find(|value| value.recipient() == Ok(key()))
    104             .unwrap()
    105             .claim()
    106             .is_none()
    107     );
    108     fixture.clock.now.store(110, Ordering::SeqCst);
    109     let own = claim(
    110         store
    111             .claim_attempt(selection(&binding, &prepared, digest, key()), copy(&proof))
    112             .await
    113             .unwrap(),
    114     );
    115     capture(&store, &input, &proof, &own).await;
    116     let extended = installed(
    117         store
    118             .install_transition_and_outbox(
    119                 request_install(&binding, &prepared, digest),
    120                 copy(&proof),
    121             )
    122             .await
    123             .unwrap(),
    124     );
    125     assert_eq!(extended, original);
    126     let complete = snapshot(&store, &binding, &prepared, &proof).await;
    127     assert_eq!(complete.publications().len(), 2);
    128     assert_eq!(complete.installed_message(), partial.installed_message());
    129     store.close().await.unwrap();
    130 }
    131 
    132 #[tokio::test]
    133 async fn actual_peer_installation_and_late_self_capture_keep_one_original_message_and_distinct_work()
    134  {
    135     let fixture = Fixture::new();
    136     let (store, input, proof, binding) = fixture.create().await;
    137     let (prepared, digest, recipients) =
    138         prepare(&fixture, &store, &binding, &proof, 220, true).await;
    139     assert!(matches!(
    140         store
    141             .install_transition_and_outbox(
    142                 request_install(&binding, &prepared, digest),
    143                 copy(&proof)
    144             )
    145             .await,
    146         Err(OperationError::InvalidEvidence)
    147     ));
    148     let peer = *recipients
    149         .iter()
    150         .find(|recipient| **recipient != key())
    151         .unwrap();
    152     let peer_claim = claim(
    153         store
    154             .claim_attempt(selection(&binding, &prepared, digest, peer), copy(&proof))
    155             .await
    156             .unwrap(),
    157     );
    158     let self_claim = claim(
    159         store
    160             .claim_attempt(selection(&binding, &prepared, digest, key()), copy(&proof))
    161             .await
    162             .unwrap(),
    163     );
    164     let peer_capture = capture(&store, &input, &proof, &peer_claim).await;
    165     // Obtain within the original first-issue window, then delay only persistence.
    166     let self_bytes = obtain(&self_claim, true).await;
    167     let before = snapshot(&store, &binding, &prepared, &proof).await;
    168     assert!(before.installed_message().is_none());
    169     fixture.clock.now.store(110, Ordering::SeqCst);
    170     let first = installed(
    171         store
    172             .install_transition_and_outbox(
    173                 request_install(&binding, &prepared, digest),
    174                 copy(&proof),
    175             )
    176             .await
    177             .unwrap(),
    178     );
    179     assert_eq!(first.basis(), peer_capture);
    180     assert_eq!(first.installed_at().get(), 110);
    181     let state = snapshot(&store, &binding, &prepared, &proof).await;
    182     assert_eq!(state.installation(), Some(first));
    183     assert_eq!(state.publications().len(), 1);
    184     assert_eq!(state.publications()[0].evidence(), peer_capture);
    185     assert_ne!(before.checkpoint_digest(), state.checkpoint_digest());
    186     let view = OperationReceipt::<radroots_domain::ChatMessage>::try_from_snapshot(&state).unwrap();
    187     assert_eq!(
    188         view.business_installation(),
    189         BusinessInstallation::Installed
    190     );
    191     assert_eq!(view.issuance(), Issuance::Unknown);
    192     assert_eq!(view.delivery(), Delivery::Pending);
    193     assert!(view.delivery_observations().is_empty());
    194     assert_eq!(
    195         view.installation_transaction_id(),
    196         Some(first.transaction_id())
    197     );
    198     let message = view.business_result().unwrap().clone();
    199     assert_eq!(message.id(), first.message_id());
    200     assert_eq!(message.created_at().get(), 100);
    201     assert_eq!(message.text(), "\0".repeat(3500));
    202     assert_eq!(message.peer(), peer);
    203     assert_eq!(
    204         view.artifacts()
    205             .iter()
    206             .filter(|value| value.publication_transaction_id().is_some())
    207             .count(),
    208         1
    209     );
    210     store.close().await.unwrap();
    211     fixture.clock.now.store(151, Ordering::SeqCst);
    212     let reopened = fixture.reopen(input.clone()).await;
    213     let replay = installed(
    214         reopened
    215             .install_transition_and_outbox(
    216                 request_install(&binding, &prepared, digest),
    217                 copy(&proof),
    218             )
    219             .await
    220             .unwrap(),
    221     );
    222     assert_eq!(replay, first);
    223     assert_eq!(
    224         snapshot(&reopened, &binding, &prepared, &proof)
    225             .await
    226             .installed_message(),
    227         Some(&message)
    228     );
    229     let captured = reopened
    230         .capture_evidence(input, &proof, &self_bytes)
    231         .await
    232         .unwrap();
    233     assert!(matches!(captured, EvidenceCaptureOutcome::Committed(_)));
    234     let late = snapshot(&reopened, &binding, &prepared, &proof).await;
    235     let late = OperationReceipt::<radroots_domain::ChatMessage>::try_from_snapshot(&late).unwrap();
    236     assert_eq!(
    237         late.business_installation(),
    238         BusinessInstallation::Installed
    239     );
    240     assert_eq!(late.issuance(), Issuance::Obtained);
    241     assert!(
    242         late.recovery()
    243             .contains(&RecoveryRequirement::InstallObtainedEvidence)
    244     );
    245     assert!(
    246         late.recovery()
    247             .contains(&RecoveryRequirement::ResumeDelivery)
    248     );
    249     assert!(late.error().is_none());
    250     let extended = installed(
    251         reopened
    252             .install_transition_and_outbox(
    253                 request_install(&binding, &prepared, digest),
    254                 copy(&proof),
    255             )
    256             .await
    257             .unwrap(),
    258     );
    259     assert_eq!(extended, first);
    260     let complete = snapshot(&reopened, &binding, &prepared, &proof).await;
    261     assert_eq!(complete.publications().len(), 2);
    262     assert_eq!(complete.installed_message(), Some(&message));
    263     let peer_work = complete
    264         .publications()
    265         .iter()
    266         .find(|value| value.recipient() == peer)
    267         .unwrap();
    268     let self_work = complete
    269         .publications()
    270         .iter()
    271         .find(|value| value.recipient() == key())
    272         .unwrap();
    273     assert_eq!(peer_work.transaction_id(), first.transaction_id());
    274     assert_ne!(self_work.transaction_id(), first.transaction_id());
    275     assert_eq!(self_work.queued_at().get(), 151);
    276     let receipt =
    277         OperationReceipt::<radroots_domain::ChatMessage>::try_from_snapshot(&complete).unwrap();
    278     assert_eq!(receipt.installed_at(), Some(first.installed_at()));
    279     assert_eq!(receipt.business_result(), Some(&message));
    280     assert_eq!(receipt.recovery(), &[RecoveryRequirement::ResumeDelivery]);
    281     assert!(matches!(
    282         reopened
    283             .claim_attempt(selection(&binding, &prepared, digest, peer), copy(&proof))
    284             .await
    285             .unwrap(),
    286         ClaimAttemptOutcome::Retained(_)
    287     ));
    288     reopened.close().await.unwrap();
    289 }
    290 
    291 #[tokio::test]
    292 async fn actual_work_insert_failure_rolls_back_the_message_but_keeps_original_claim_and_evidence() {
    293     let fixture = Fixture::new();
    294     let (store, input, proof, binding) = fixture.create().await;
    295     let (prepared, digest, recipients) =
    296         prepare(&fixture, &store, &binding, &proof, 221, false).await;
    297     let attempt = claim(
    298         store
    299             .claim_attempt(
    300                 selection(&binding, &prepared, digest, recipients[0]),
    301                 copy(&proof),
    302             )
    303             .await
    304             .unwrap(),
    305     );
    306     let original = capture(&store, &input, &proof, &attempt).await;
    307     let mut database = fixture.database().await;
    308     sqlx::raw_sql("CREATE TRIGGER refuse_chat_work BEFORE INSERT ON chat_publication_work BEGIN SELECT RAISE(ABORT,'synthetic insert failure'); END").execute(&mut database).await.unwrap();
    309     assert!(matches!(
    310         store
    311             .install_transition_and_outbox(
    312                 request_install(&binding, &prepared, digest),
    313                 copy(&proof)
    314             )
    315             .await,
    316         Err(OperationError::StorageFailure)
    317     ));
    318     let counts: (i64, i64) = sqlx::query_as(
    319         "SELECT (SELECT count(*) FROM chat_messages),(SELECT count(*) FROM chat_publication_work)",
    320     )
    321     .fetch_one(&mut database)
    322     .await
    323     .unwrap();
    324     assert_eq!(counts, (0, 0));
    325     let state = snapshot(&store, &binding, &prepared, &proof).await;
    326     assert!(state.installation().is_none());
    327     assert_eq!(
    328         state
    329             .artifacts()
    330             .iter()
    331             .filter_map(|value| value.obtained())
    332             .collect::<Vec<_>>(),
    333         vec![original]
    334     );
    335     sqlx::query("DROP TRIGGER refuse_chat_work")
    336         .execute(&mut database)
    337         .await
    338         .unwrap();
    339     database.close().await.unwrap();
    340     installed(
    341         store
    342             .install_transition_and_outbox(
    343                 request_install(&binding, &prepared, digest),
    344                 copy(&proof),
    345             )
    346             .await
    347             .unwrap(),
    348     );
    349     assert!(
    350         snapshot(&store, &binding, &prepared, &proof)
    351             .await
    352             .installed_message()
    353             .is_some()
    354     );
    355     store.close().await.unwrap();
    356 }
    357 
    358 #[tokio::test]
    359 async fn actual_t2_commit_uncertainty_latches_and_reopen_reconciles_original_obtained_facts() {
    360     let fixture = Fixture::new();
    361     let (store, input, proof, binding) = fixture.create().await;
    362     let (prepared, digest, recipients) =
    363         prepare(&fixture, &store, &binding, &proof, 222, false).await;
    364     let attempt = claim(
    365         store
    366             .claim_attempt(
    367                 selection(&binding, &prepared, digest, recipients[0]),
    368                 copy(&proof),
    369             )
    370             .await
    371             .unwrap(),
    372     );
    373     let original = capture(&store, &input, &proof, &attempt).await;
    374     let mut database = fixture.database().await;
    375     sqlx::raw_sql("CREATE TABLE chat_commit_fault (parent INTEGER REFERENCES workspace_grant(singleton) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER chat_commit_fault_trigger AFTER INSERT ON chat_publication_work BEGIN INSERT INTO chat_commit_fault VALUES(2); END").execute(&mut database).await.unwrap();
    376     database.close().await.unwrap();
    377     let InstallationOutcome::Unknown(anchor) = store
    378         .install_transition_and_outbox(request_install(&binding, &prepared, digest), copy(&proof))
    379         .await
    380         .unwrap()
    381     else {
    382         panic!("actual failed commit must remain anchored unknown")
    383     };
    384     assert_eq!(anchor.operation_id(), prepared.identity().operation_id());
    385     assert_eq!(
    386         anchor.command_digest(),
    387         prepared.identity().command_digest()
    388     );
    389     assert!(anchor.transaction_id().is_some());
    390     assert!(matches!(
    391         store
    392             .operation_snapshot(
    393                 binding.clone(),
    394                 prepared.identity().operation_id(),
    395                 None,
    396                 copy(&proof)
    397             )
    398             .await,
    399         Err(OperationError::RecoveryRequired)
    400     ));
    401     store.close().await.unwrap();
    402     let mut database = fixture.database().await;
    403     sqlx::raw_sql("DROP TRIGGER chat_commit_fault_trigger; DROP TABLE chat_commit_fault")
    404         .execute(&mut database)
    405         .await
    406         .unwrap();
    407     database.close().await.unwrap();
    408     let reopened = fixture.reopen(input).await;
    409     let state = snapshot(&reopened, &binding, &prepared, &proof).await;
    410     assert!(state.installation().is_none());
    411     assert_eq!(
    412         state
    413             .artifacts()
    414             .iter()
    415             .filter_map(|value| value.obtained())
    416             .collect::<Vec<_>>(),
    417         vec![original]
    418     );
    419     installed(
    420         reopened
    421             .install_transition_and_outbox(
    422                 request_install(&binding, &prepared, digest),
    423                 copy(&proof),
    424             )
    425             .await
    426             .unwrap(),
    427     );
    428     assert!(
    429         snapshot(&reopened, &binding, &prepared, &proof)
    430             .await
    431             .installed_message()
    432             .is_some()
    433     );
    434     reopened.close().await.unwrap();
    435 }
    436 
    437 #[tokio::test]
    438 async fn accepted_installation_survives_lost_observer_and_safe_close_drains_the_original_work() {
    439     let fixture = Fixture::new();
    440     let (store, input, proof, binding) = fixture.create().await;
    441     let (prepared, digest, recipients) =
    442         prepare(&fixture, &store, &binding, &proof, 223, false).await;
    443     let attempt = claim(
    444         store
    445             .claim_attempt(
    446                 selection(&binding, &prepared, digest, recipients[0]),
    447                 copy(&proof),
    448             )
    449             .await
    450             .unwrap(),
    451     );
    452     capture(&store, &input, &proof, &attempt).await;
    453     let (arrived, resume) = fixture.clock.arm(1);
    454     let owner = store.clone();
    455     let selected = request_install(&binding, &prepared, digest);
    456     let secret = copy(&proof);
    457     let observer =
    458         tokio::spawn(async move { owner.install_transition_and_outbox(selected, secret).await });
    459     reached(arrived).await;
    460     observer.abort();
    461     assert!(observer.await.unwrap_err().is_cancelled());
    462     resume.send(()).unwrap();
    463     store.close().await.unwrap();
    464     let reopened = fixture.reopen(input).await;
    465     let state = snapshot(&reopened, &binding, &prepared, &proof).await;
    466     assert!(state.installed_message().is_some());
    467     assert_eq!(state.publications().len(), 1);
    468     reopened.close().await.unwrap();
    469 }
    470 
    471 #[tokio::test]
    472 async fn predecessor_schema_five_retains_actual_claims_and_obtained_bytes_without_automatic_upgrade()
    473  {
    474     let fixture = Fixture::new();
    475     let (store, input, proof, binding) = fixture.create().await;
    476     let (prepared, digest, recipients) =
    477         prepare(&fixture, &store, &binding, &proof, 224, false).await;
    478     let attempt = claim(
    479         store
    480             .claim_attempt(
    481                 selection(&binding, &prepared, digest, recipients[0]),
    482                 copy(&proof),
    483             )
    484             .await
    485             .unwrap(),
    486     );
    487     capture(&store, &input, &proof, &attempt).await;
    488     store.close().await.unwrap();
    489     let mut database = fixture.database().await;
    490     sqlx::raw_sql(
    491         "DROP TABLE public_publication_work; DROP TABLE listing_installations; DROP TABLE listing_creations; DROP TABLE artifact_delivery_observations; DROP TABLE artifact_delivery_parts; DROP TABLE artifact_delivery_attempts; DROP TABLE chat_publication_work; DROP TABLE chat_messages; PRAGMA user_version=5",
    492     )
    493     .execute(&mut database)
    494     .await
    495     .unwrap();
    496     let count: i64 = sqlx::query_scalar("SELECT count(*) FROM sqlite_schema")
    497         .fetch_one(&mut database)
    498         .await
    499         .unwrap();
    500     assert_eq!(count, 11);
    501     database.close().await.unwrap();
    502     let path = fixture.path.join("execution_v1.sqlite");
    503     let original = std::fs::read(&path).unwrap();
    504     assert!(matches!(
    505         NativeWorkspace::open_existing(
    506             &fixture.path,
    507             input,
    508             fixture.crypto(),
    509             fixture.clock.clone()
    510         )
    511         .await,
    512         Err(WorkspaceError::RecoveryRequired)
    513     ));
    514     assert_eq!(std::fs::read(path).unwrap(), original);
    515 }