Skip to main content

coven_protocol/provider/
test_fixtures.rs

1//! Provider values every layer's tests build against: the bindings a store and
2//! a device are probed under, and a receipt that verifies against both.
3
4use super::*;
5use crate::store_commit::ObjectHash;
6
7pub fn test_store_binding() -> crate::objects::StoreProviderBinding {
8    crate::objects::StoreProviderBinding::S3 {
9        endpoint: crate::objects::S3EndpointBinding::Aws {
10            partition: "aws".to_string(),
11        },
12        region: "us-east-1".to_string(),
13        bucket: "bucket".to_string(),
14        key_prefix: None,
15    }
16}
17
18pub fn test_device_binding() -> crate::objects::ProviderDeviceBinding {
19    crate::objects::ProviderDeviceBinding {
20        principal: crate::objects::ProviderPrincipalId::Aws {
21            account_id: "123456789012".to_string(),
22            principal: crate::objects::AwsPrincipal::Root,
23        },
24    }
25}
26
27pub fn test_exact_receipt() -> ExactSlotProbeReceipt {
28    let probe_id = ProviderProbeId::from_bytes([7; 32]);
29    let slot = crate::objects::ObjectSlot::logical("store-v1/probes/exact".to_string()).unwrap();
30    let first = probe_payload(&probe_id, ProbePayloadLabel::ExactCreateFirst);
31    let second = probe_payload(&probe_id, ProbePayloadLabel::ExactCreateSecond);
32    let accepted = crate::objects::ExactObjectRef::new(
33        slot.clone(),
34        first.len() as u64,
35        ObjectHash::digest(&first),
36    );
37    let lost_slot =
38        crate::objects::ObjectSlot::logical("store-v1/probes/lost".to_string()).unwrap();
39    let lost_payload = probe_payload(&probe_id, ProbePayloadLabel::LostResponse);
40    let lost_ref = crate::objects::ExactObjectRef::new(
41        lost_slot.clone(),
42        lost_payload.len() as u64,
43        ObjectHash::digest(&lost_payload),
44    );
45    let conditional_slot =
46        crate::objects::ObjectSlot::logical("store-v1/probes/conditional".to_string()).unwrap();
47    let transcript = ExactSlotProbeTranscript {
48        probe_id,
49        logical_key: slot.logical_key().to_string(),
50        slot,
51        contenders: [
52            ProbeCreateAttempt {
53                payload_hash: ObjectHash::digest(&first),
54                outcome: ProbeCreateOutcome::Created,
55            },
56            ProbeCreateAttempt {
57                payload_hash: ObjectHash::digest(&second),
58                outcome: ProbeCreateOutcome::RejectedOccupied,
59            },
60        ],
61        accepted: accepted.clone(),
62        full_read_hash: accepted.stored_hash(),
63        range: ProbeRangeReceipt {
64            start: PROBE_RANGE_START,
65            end: PROBE_RANGE_END,
66            bytes_hash: ObjectHash::digest(
67                &first[PROBE_RANGE_START as usize..PROBE_RANGE_END as usize],
68            ),
69        },
70        conditional: test_conditional_receipt(probe_id, conditional_slot),
71        lost_response: LostResponseProbeReceipt {
72            logical_key: "store-v1/probes/lost".to_string(),
73            slot: lost_slot,
74            payload_hash: ObjectHash::digest(&lost_payload),
75            settled: lost_ref,
76            readback_hash: ObjectHash::digest(&lost_payload),
77        },
78    };
79    ExactSlotProbeReceipt::from_transcript(
80        transcript,
81        &test_store_binding(),
82        &test_device_binding(),
83    )
84}
85
86pub fn test_conditional_receipt(
87    probe_id: ProviderProbeId,
88    slot: crate::objects::ObjectSlot,
89) -> ConditionalUpdateProbeReceipt {
90    let initial = probe_payload(&probe_id, ProbePayloadLabel::ConditionalInitial);
91    let first = probe_payload(&probe_id, ProbePayloadLabel::ConditionalFirst);
92    let second = probe_payload(&probe_id, ProbePayloadLabel::ConditionalSecond);
93    ConditionalUpdateProbeReceipt {
94        logical_key: slot.logical_key().to_string(),
95        slot,
96        starting_payload_hash: ObjectHash::digest(&initial),
97        contenders: [
98            ProbeConditionalAttempt {
99                payload_hash: ObjectHash::digest(&first),
100                outcome: ProbeConditionalOutcome::Replaced,
101            },
102            ProbeConditionalAttempt {
103                payload_hash: ObjectHash::digest(&second),
104                outcome: ProbeConditionalOutcome::RejectedRevision,
105            },
106        ],
107        accepted_payload_hash: ObjectHash::digest(&first),
108    }
109}