OXYGENHUB

Coverage / crates/oxy-core/src/repo/receipts.rs 82.5%

99 of 120 lines run, 15 of 26 functions · run #309 on main · run missed

1
//! Execution receipts in a repository (spec §14.6): kept beside the state
2
//! they were made on, with the runner's identity and signature.
3
4
use super::*;
5
use crate::receipt::SignedReceipt;
6
7
/// A stored receipt and what verifying it found.
8
#[derive(Clone, Debug)]
9
pub struct ReceiptInfo {
10
    /// The receipt attestation's ObjectRef, which names it.
11
    pub attest: ObjectRef,
12
    pub signed: SignedReceipt,
13
    /// Why it does not verify, if it does not.
14
    pub problem: Option<String>,
15
    /// The runner's key was revoked (spec §14.2).
16
    pub revoked: bool,
17
}
18
19
impl Repo {
20
    /// Verify `sr` and keep it: the receipt, the attestation with its
21
    /// signatures, and the runner's identity. Returns the attestation's
22
    /// ObjectRef. Receipts for another repository are refused.
235×
    pub fn add_receipt(&self, sr: &SignedReceipt) -> Result<ObjectRef> {
245×
        sr.verify()
255×
            .map_err(|e| RepoError::Msg(format!("receipt does not verify: {e}")))?;
263×
        if sr.attest.repo != self.id()? {
271×
            return fail("that receipt is for another repository");
282×
        }
292×
        self.store.put_identity(&sr.runner)?;
302×
        self.store.put_receipt(&sr.receipt)?;
312×
        let r = self.store.put_attest(&sr.attest)?;
322×
        for sig in &sr.sigs {
332×
            self.store.put_signature(&r, sig)?;
34
        }
352×
        self.store.index_receipt(&sr.state(), &r)?;
362×
        Ok(r)
375×
    }
38
39
    /// The receipt named by attestation `r`, verified again from the store.
407×
    pub fn receipt(&self, r: &ObjectRef) -> Result<ReceiptInfo> {
417×
        let attest = self.store.get_attest(r)?;
427×
        let AttestBody::Receipt { receipt, .. } = &attest.body else {
430×
            return fail(format!("#{} is not a receipt", r.short(12)));
44
        };
457×
        let receipt = self.store.get_receipt(receipt)?;
467×
        let sigs = self.store.signatures(r)?;
477×
        let Some(key) = sigs.first().map(|s| s.key) else {
480×
            return fail(format!("receipt #{} has no signature", r.short(12)));
49
        };
507×
        let runner = self.store.get_identity(&key)?;
517×
        let signed = SignedReceipt {
527×
            receipt,
537×
            attest,
547×
            sigs,
557×
            runner,
567×
        };
577×
        let problem = signed.verify().err().map(|e| e.to_string());
58
        Ok(ReceiptInfo {
597×
            attest: *r,
607×
            revoked: self.is_revoked(&key)?,
617×
            signed,
627×
            problem,
63
        })
647×
    }
65
66
    /// Every receipt kept for the state of `heads`, oldest first.
679×
    pub fn receipts(&self, heads: &[ChangeHash]) -> Result<Vec<ReceiptInfo>> {
689×
        let state = hash_object(
699×
            ObjectType::State,
709×
            &StateObject::new(heads.to_vec()).encode(),
71
        );
729×
        let mut out = self
739×
            .store
749×
            .receipts_for(&state)?
759×
            .iter()
769×
            .map(|r| self.receipt(r))
779×
            .collect::<Result<Vec<_>>>()?;
789×
        out.sort_by_key(|i| i.signed.attest.hlc);
799×
        Ok(out)
809×
    }
81
82
    /// Receipts to keep through garbage collection: those whose state's
83
    /// heads `keep` holds, with their receipts, runners and outputs.
84
    /// Keep a signed approval (oxygenhub#235): verified, for this repository,
85
    /// with the signer's identity and indexed by the state it approves, so
86
    /// garbage collection keeps it while that state lives. Who may approve is
87
    /// the caller's rule.
883×
    pub fn add_approval(&self, sa: &crate::approval::SignedApproval) -> Result<ObjectRef> {
893×
        sa.verify()
903×
            .map_err(|e| RepoError::Msg(format!("approval does not verify: {e}")))?;
912×
        if sa.attest.repo != self.id()? {
921×
            return fail("that approval is for another repository");
931×
        }
941×
        let state = sa
951×
            .statement()
961×
            .map_err(|e| RepoError::Msg(e.to_string()))?
97
            .state;
981×
        self.store.put_identity(&sa.signer)?;
991×
        let r = self.store.put_attest(&sa.attest)?;
1001×
        for sig in &sa.sigs {
1011×
            self.store.put_signature(&r, sig)?;
102
        }
1031×
        self.store.index_approval(&state, &r)?;
1041×
        Ok(r)
1053×
    }
106
107
    /// Approvals of states garbage collection keeps are kept, with their
108
    /// signatures and signers.
10917×
    pub(crate) fn reachable_approvals(&self, keep: &mut HashSet<ObjectRef>) -> Result<()> {
11017×
        for state in self.store.approval_states()? {
1111×
            if !keep.contains(&state) {
1120×
                let heads = match self.store.get_state(&state) {
1130×
                    Ok(s) => s.heads,
1140×
                    Err(StoreError::NotFound(_)) => vec![],
1150×
                    Err(e) => return Err(e.into()),
116
                };
1170×
                if !heads.iter().all(|h| keep.contains(h)) {
1180×
                    continue;
1190×
                }
1200×
                keep.insert(state);
1211×
            }
1221×
            for a in self.store.approvals_for(&state)? {
1231×
                keep.insert(a);
1241×
                for sig in self.store.signatures(&a)? {
1251×
                    keep.insert(sig.key);
1261×
                }
127
            }
128
        }
12917×
        Ok(())
13017×
    }
131
13217×
    pub(crate) fn reachable_receipts(&self, keep: &mut HashSet<ObjectRef>) -> Result<()> {
13317×
        for state in self.store.receipt_states()? {
1341×
            let heads = match self.store.get_state(&state) {
1351×
                Ok(s) => s.heads,
136
                // A receipt may arrive before its state object; its heads
137
                // are unknown, so keep it.
1380×
                Err(StoreError::NotFound(_)) => vec![],
1390×
                Err(e) => return Err(e.into()),
140
            };
1411×
            if !heads.iter().all(|h| keep.contains(h)) {
1420×
                continue;
1431×
            }
1441×
            keep.insert(state);
1451×
            for a in self.store.receipts_for(&state)? {
1461×
                keep.insert(a);
1471×
                for sig in self.store.signatures(&a)? {
1481×
                    keep.insert(sig.key);
1491×
                }
1501×
                let Ok(attest) = self.store.get_attest(&a) else {
1510×
                    continue;
152
                };
1531×
                let AttestBody::Receipt { receipt, .. } = attest.body else {
1540×
                    continue;
155
                };
1561×
                keep.insert(receipt);
1571×
                let Ok(r) = self.store.get_receipt(&receipt) else {
1580×
                    continue;
159
                };
1601×
                for b in r.outputs.iter().chain(&r.inputs) {
1611×
                    if keep.insert(*b) {
1621×
                        if let Ok(bytes) = self.store.get_typed(b, ObjectType::Blob) {
1631×
                            keep.extend(Blob::decode(&bytes)?.chunks);
1640×
                        }
1650×
                    }
166
                }
167
            }
168
        }
16917×
        Ok(())
17017×
    }
171
}