qualia_core_db/bundle/stream/protection/
publisher.rs1use super::geometry::{PreparedGeometry, EMPTY_REF, EMPTY_ROOT};
3use crate::bundle::stream::proof::{OutboardNode, TreeSpec};
4use crate::crypto::network::object_stream::{Context, ObjectScope, PageRef, RootRow};
5use crate::net::qdnf::types::StrongDigest;
6use crate::specialized_libs::cryptographic_library::ManagedObjectPublication;
7#[derive(Clone, Copy, Debug, PartialEq, Eq)]
8pub enum PublisherError {
9 Capacity,
10 State,
11 Source,
12 Entropy,
13 Crypto,
14 Codec,
15 Proof,
16 Sink,
17}
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum Artifact {
20 Context,
21 Geometry,
22 Roots,
23 Descriptor,
24 PublisherProof,
25}
26pub trait CandidateSink {
30 fn artifact(&mut self, kind: Artifact, page: u16, bytes: &[u8]) -> Result<(), ()>;
31 fn ciphertext(&mut self, scope: ObjectScope, leaf: u64, bytes: &[u8]) -> Result<(), ()>;
32 fn outboard(&mut self, spec: &TreeSpec, node: OutboardNode) -> Result<(), ()>;
33}
34pub struct ProtectedCandidate {
35 pub(super) descriptor: StrongDigest,
36 pub(super) context: StrongDigest,
37}
38impl ProtectedCandidate {
39 pub fn descriptor_id(&self) -> StrongDigest {
40 self.descriptor
41 }
42 pub fn context_id(&self) -> StrongDigest {
43 self.context
44 }
45}
46pub struct ProtectedPublisher<'a, 's, 'm, 'r> {
47 pub(super) geometry: PreparedGeometry<'a, 's>,
48 pub(super) publication: ManagedObjectPublication<'m>,
49 pub(super) context: Context<'m>,
50 pub(super) roots: &'r mut [RootRow],
51 pub(super) root_refs: &'r mut [PageRef],
52 pub(super) next: usize,
53 pub(super) metadata_root: Option<StrongDigest>,
54 pub(super) candidate: Option<StrongDigest>,
55 pub(super) proof_staged: bool,
56 pub(super) header_staged: bool,
57 poisoned: bool,
58}
59impl<'a, 's, 'm, 'r> ProtectedPublisher<'a, 's, 'm, 'r> {
60 pub fn bind(
63 geometry: PreparedGeometry<'a, 's>,
64 mut publication: ManagedObjectPublication<'m>,
65 context: Context<'m>,
66 roots: &'r mut [RootRow],
67 refs: &'r mut [PageRef],
68 ) -> Result<Self, PublisherError> {
69 geometry.check_context(context)?;
70 let roots = roots
71 .get_mut(..geometry.object_count())
72 .ok_or(PublisherError::Capacity)?;
73 let refs = refs
74 .get_mut(..context.page_count())
75 .ok_or(PublisherError::Capacity)?;
76 publication
77 .bind_context(context)
78 .map_err(|_| PublisherError::Crypto)?;
79 roots.fill(EMPTY_ROOT);
80 refs.fill(EMPTY_REF);
81 Ok(Self {
82 geometry,
83 publication,
84 context,
85 roots,
86 root_refs: refs,
87 next: 0,
88 metadata_root: None,
89 candidate: None,
90 proof_staged: false,
91 header_staged: false,
92 poisoned: false,
93 })
94 }
95 pub fn is_poisoned(&self) -> bool {
96 self.poisoned || self.publication.is_poisoned()
97 }
98 pub(super) fn attempt<T>(
99 &mut self,
100 work: impl FnOnce(&mut Self) -> Result<T, PublisherError>,
101 ) -> Result<T, PublisherError> {
102 if self.is_poisoned() {
103 return Err(PublisherError::State);
104 }
105 self.poisoned = true;
106 struct Guard<'p, 'a, 's, 'm, 'r> {
107 p: &'p mut ProtectedPublisher<'a, 's, 'm, 'r>,
108 armed: bool,
109 }
110 impl Drop for Guard<'_, '_, '_, '_, '_> {
111 fn drop(&mut self) {
112 if self.armed {
113 self.p.publication.cancel();
114 }
115 }
116 }
117 let mut guard = Guard {
118 p: self,
119 armed: true,
120 };
121 let result = work(guard.p);
122 if result.is_ok() {
123 guard.p.poisoned = false;
124 guard.armed = false;
125 }
126 result
127 }
128 pub fn stage_header(
129 &mut self,
130 sink: &mut impl CandidateSink,
131 ) -> Result<(), PublisherError> {
132 if self.header_staged {
133 return Err(PublisherError::State);
134 }
135 self.attempt(|p| {
136 let bytes = p.context.encoded();
137 p.publication
138 .with_current_effect(|| {
139 sink.artifact(Artifact::Context, 0, bytes)
140 .map_err(|_| crate::crypto::network::errors::CryptoError::CryptoFailure)
141 })
142 .map_err(|_| PublisherError::Sink)?;
143 let mut scratch = [0; 1560];
144 for page in 0..p.context.page_count() {
145 let r = p.geometry.encode_page(page, &mut scratch)?;
146 p.publication
147 .with_current_effect(|| {
148 sink.artifact(
149 Artifact::Geometry,
150 page as u16,
151 &scratch[..r.length as usize],
152 )
153 .map_err(|_| crate::crypto::network::errors::CryptoError::CryptoFailure)
154 })
155 .map_err(|_| PublisherError::Sink)?;
156 }
157 p.header_staged = true;
158 Ok(())
159 })
160 }
161}
162impl Drop for ProtectedPublisher<'_, '_, '_, '_> {
163 fn drop(&mut self) {
164 self.publication.cancel();
165 }
166}