Skip to main content

qualia_core_db/bundle/stream/mixed/
transfer.rs

1//! Production join: MixedHmc inventory → MuxTransfer + BoundEntry.
2//! Family is an assignment tag. Admit before tagging Qac/Emf/P64.
3use super::{mixed_plan, MixedError, MixedHmc, MixedPlan};
4use crate::audio::qac::QacStorageOptions;
5use crate::bundle::stream::descriptor::{EntryRecord, HASH_SCRATCH_BYTES};
6use crate::bundle::stream::formats::{
7    emf_present_id, p64_present_id, qac_present_id, AuthenticatedRange, EmfStreamView,
8    P64StreamView, QacStreamView,
9};
10use crate::bundle::stream::index::{
11    admit_header, admit_index, EntryScratch, ExtentLimit, IndexReadLimits,
12};
13use crate::bundle::stream::plan::{
14    PrincipalSelection, SelectionIntent, SelectionRequest, MAX_PLAN_ENTRIES,
15};
16use crate::bundle::stream::policy::{BoundClosure, LocalWholeSourceGrant};
17use crate::bundle::stream::proof::{
18    verify_leaf_span, OutboardNode, SpanLimits, TreeBuilder, LEAF_BYTES,
19};
20use crate::bundle::stream::publish::PublicationClass;
21use crate::bundle::stream::selection::SelectedPlainEntry;
22use crate::crypto::network::authority::PinAuthority;
23use crate::net::peer::replication::qbdl::provider::{OutboardCatalog, OutboardLimits};
24use crate::net::peer::replication::qbdl::service::{
25    transfer_scheduled, BoundEntry, Error as MuxError, MuxReport, MuxTransfer, PlainClient,
26    PlainProvider, ServiceIo,
27};
28use crate::net::qdnf::types::StrongDigest;
29use crate::q42::field_envelope::Limits as EmfLimits;
30const POLICY_GENERATION: u64 = 1;
31fn map_mux(e: MuxError) -> MixedError {
32    match e {
33        MuxError::Limit | MuxError::Capacity | MuxError::Overflow => MixedError::Limit,
34        MuxError::Unsupported | MuxError::Admission => MixedError::Unsupported,
35        MuxError::Identity | MuxError::Malformed => MixedError::Identity,
36        _ => MixedError::Identity,
37    }
38}
39/// Scene intents for the listed assignment ids, in HMC order.
40pub fn mixed_intents(hmc: &MixedHmc, ids: &[u32]) -> Result<Vec<SelectionIntent>, MixedError> {
41    if ids.is_empty() || ids.len() > MAX_PLAN_ENTRIES {
42        return Err(MixedError::Limit);
43    }
44    let mut out = Vec::with_capacity(ids.len());
45    for id in ids {
46        let i = hmc.index_of(*id).ok_or(MixedError::Identity)?;
47        out.push(
48            SelectionIntent::scene(*id, hmc.family(i), None, None)
49                .map_err(|_| MixedError::Unsupported)?,
50        );
51    }
52    Ok(out)
53}
54/// One principal over the listed assignment ids.
55pub fn mixed_selection<'a>(
56    intents: &'a [SelectionIntent],
57) -> Result<PrincipalSelection<'a>, MixedError> {
58    let request = SelectionRequest::scene(intents)?;
59    Ok(PrincipalSelection::new(1, request)?)
60}
61/// Select every published assignment, ordinal order.
62pub fn select_mixed<'a>(
63    hmc: &'a MixedHmc,
64    bound: &BoundClosure<'_>,
65) -> Result<Vec<SelectedPlainEntry<'a>>, MixedError> {
66    let header = admit_header(
67        &hmc.source()[..64],
68        hmc.source().len() as u64,
69        ExtentLimit::default(),
70        IndexReadLimits::default(),
71    )
72    .map_err(|_| MixedError::Identity)?;
73    let mut index_scratch = vec![EntryScratch::EMPTY; hmc.len()];
74    let start = usize::try_from(header.index_offset()).map_err(|_| MixedError::Limit)?;
75    let index = admit_index(
76        &header,
77        &hmc.source()[start..],
78        IndexReadLimits::default(),
79        &mut index_scratch,
80    )
81    .map_err(|_| MixedError::Identity)?;
82    let mut rows = [EntryRecord::EMPTY; 128];
83    let mut framing = vec![0; HASH_SCRATCH_BYTES];
84    let mut out = Vec::with_capacity(hmc.len());
85    for ordinal in 0..hmc.len() as u32 {
86        out.push(hmc.select(bound, ordinal, &index, &mut rows, &mut framing)?);
87    }
88    Ok(out)
89}
90/// Cold outboard for one selected entry. Caller sorts once, then lends.
91pub fn mixed_outboard(
92    source: &[u8],
93    selected: SelectedPlainEntry<'_>,
94) -> Result<(Vec<OutboardNode>, StrongDigest), MixedError> {
95    let start = usize::try_from(selected.offset()).map_err(|_| MixedError::Limit)?;
96    let length = usize::try_from(selected.logical_length()).map_err(|_| MixedError::Limit)?;
97    let end = start.checked_add(length).ok_or(MixedError::Limit)?;
98    if end > source.len() {
99        return Err(MixedError::Identity);
100    }
101    let entry = &source[start..end];
102    let spec = selected.tree_spec().map_err(|_| MixedError::Identity)?;
103    let mut builder = TreeBuilder::new(spec);
104    let mut nodes = Vec::new();
105    for leaf in entry.chunks(LEAF_BYTES as usize) {
106        builder
107            .append(leaf, |node| {
108                nodes.push(node);
109                Ok(())
110            })
111            .map_err(|_| MixedError::Identity)?;
112    }
113    let root = builder
114        .finish(|node| {
115            nodes.push(node);
116            Ok(())
117        })
118        .map_err(|_| MixedError::Identity)?;
119    nodes.sort_unstable_by_key(|node| (node.start_leaf, node.leaf_count));
120    Ok((nodes, root))
121}
122fn catalog<'p, 'n>(
123    selected: SelectedPlainEntry<'p>,
124    nodes: &'n [OutboardNode],
125    root: StrongDigest,
126) -> Result<OutboardCatalog<'p, 'n>, MixedError> {
127    let spec = selected.tree_spec().map_err(|_| MixedError::Identity)?;
128    OutboardCatalog::new(
129        selected,
130        spec,
131        root,
132        nodes,
133        OutboardLimits {
134            max_nodes: nodes.len(),
135            max_bytes: nodes.len() * core::mem::size_of::<OutboardNode>(),
136        },
137    )
138    .map_err(|_| MixedError::Identity)
139}
140/// Bind one selected publication entry to its plain provider.
141pub fn bind_mixed_entry<'src, 'sel, 'nodes>(
142    source: &'src [u8],
143    selected: SelectedPlainEntry<'sel>,
144    nodes: &'nodes [OutboardNode],
145    root: StrongDigest,
146) -> Result<BoundEntry<'src, 'sel, 'nodes>, MixedError> {
147    Ok(BoundEntry {
148        entry_id: selected.entry_id(),
149        provider: PlainProvider::bind(
150            PublicationClass::PlainCandidate,
151            source,
152            catalog(selected, nodes, root)?,
153            POLICY_GENERATION,
154        )
155        .map_err(map_mux)?,
156    })
157}
158/// Bind every selected entry. `nodes`/`roots` must be 1:1 with `selected`.
159pub fn bind_mixed_entries<'src, 'sel, 'nodes>(
160    source: &'src [u8],
161    selected: &[SelectedPlainEntry<'sel>],
162    nodes: &'nodes [Vec<OutboardNode>],
163    roots: &[StrongDigest],
164) -> Result<Vec<BoundEntry<'src, 'sel, 'nodes>>, MixedError> {
165    if selected.len() != nodes.len() || selected.len() != roots.len() {
166        return Err(MixedError::Identity);
167    }
168    let mut out = Vec::with_capacity(selected.len());
169    for i in 0..selected.len() {
170        out.push(bind_mixed_entry(source, selected[i], &nodes[i], roots[i])?);
171    }
172    Ok(out)
173}
174/// Bind clients 1:1 with selected entries.
175pub fn bind_mixed_clients<'p>(
176    selected: &[SelectedPlainEntry<'p>],
177) -> Result<Vec<PlainClient<'p>>, MixedError> {
178    let mut out = Vec::with_capacity(selected.len());
179    for sel in selected {
180        out.push(PlainClient::bind(*sel, POLICY_GENERATION, 0).map_err(map_mux)?);
181    }
182    Ok(out)
183}
184/// Compile the fair schedule from a mixed plan. Incomplete/Refused stay on
185/// `MuxTransfer` and refuse at `transfer_mixed` before any leaf.
186pub fn compile_mixed_mux(
187    plan: &MixedPlan,
188    selections: &[PrincipalSelection<'_>],
189    present: &[u32],
190) -> Result<MuxTransfer, MixedError> {
191    let inventory = plan.inventory()?;
192    MuxTransfer::compile(&inventory, selections, present).map_err(map_mux)
193}
194fn with_complete_range<R>(
195    selected: SelectedPlainEntry<'_>,
196    payload: &[u8],
197    run: impl FnOnce(AuthenticatedRange<'_>) -> Result<R, MixedError>,
198) -> Result<R, MixedError> {
199    let spec = selected.tree_spec().map_err(|_| MixedError::Identity)?;
200    let mut siblings = [StrongDigest::ZERO; 32];
201    let span = verify_leaf_span(
202        &spec,
203        selected.root(),
204        0,
205        payload,
206        &[&[0]],
207        &mut siblings,
208        SpanLimits {
209            max_leaves: 1,
210            max_encoded_bytes: LEAF_BYTES as usize,
211        },
212    )
213    .map_err(|_| MixedError::Identity)?;
214    let range = AuthenticatedRange::new(selected, &span, 0, payload.len())
215        .map_err(|_| MixedError::Identity)?;
216    run(range)
217}
218/// Present ids after family admit. Opaque is never present. Qac/Emf/P64 require
219/// their stream view. TenD/Q42/M64 stay present after select (their views are
220/// split-range, not complete-entry).
221pub fn mixed_present_ids(
222    hmc: &MixedHmc,
223    bound: &BoundClosure<'_>,
224) -> Result<Vec<u32>, MixedError> {
225    use crate::bundle::stream::plan::EntryFamily;
226    let selected = select_mixed(hmc, bound)?;
227    let mut present = Vec::new();
228    for (i, sel) in selected.iter().enumerate() {
229        let id = hmc.entry_id(i);
230        if sel.entry_id() != id {
231            return Err(MixedError::Identity);
232        }
233        let payload = hmc.payload(i);
234        match hmc.family(i) {
235            EntryFamily::Opaque => {}
236            EntryFamily::Qac => {
237                if with_complete_range(*sel, payload, |range| {
238                    let view = QacStreamView::admit(range, QacStorageOptions::default())
239                        .map_err(|_| MixedError::Unsupported)?;
240                    Ok(qac_present_id(id, &view).is_some())
241                })? {
242                    present.push(id);
243                }
244            }
245            EntryFamily::Emf => {
246                if with_complete_range(*sel, payload, |range| {
247                    let view = EmfStreamView::admit(range, EmfLimits::default())
248                        .map_err(|_| MixedError::Unsupported)?;
249                    Ok(emf_present_id(id, &view).is_some())
250                })? {
251                    present.push(id);
252                }
253            }
254            EntryFamily::P64 => {
255                if with_complete_range(*sel, payload, |range| {
256                    let view = P64StreamView::admit(range).map_err(|_| MixedError::Unsupported)?;
257                    Ok(p64_present_id(id, &view).is_some())
258                })? {
259                    present.push(id);
260                }
261            }
262            EntryFamily::TenD | EntryFamily::Q42 | EntryFamily::M64 => {
263                present.push(id);
264            }
265        }
266    }
267    Ok(present)
268}
269/// Drive `next` until empty over the compiled mixed schedule.
270pub fn transfer_mixed<PS: ServiceIo, CS: ServiceIo>(
271    mux: &mut MuxTransfer,
272    entries: &[BoundEntry<'_, '_, '_>],
273    clients: &mut [PlainClient<'_>],
274    provider_session: &mut PS,
275    client_session: &mut CS,
276    grant: &mut LocalWholeSourceGrant<'_, '_>,
277    pins: &PinAuthority,
278    clock: impl Fn() -> u64,
279    payload_out: &mut [u8],
280) -> Result<MuxReport, MixedError> {
281    transfer_scheduled(
282        mux,
283        entries,
284        clients,
285        provider_session,
286        client_session,
287        grant,
288        pins,
289        clock,
290        payload_out,
291    )
292    .map_err(map_mux)
293}
294/// Convenience: plan + present + compile from one published HMC.
295pub fn compile_from_mixed(
296    hmc: &MixedHmc,
297    bound: &BoundClosure<'_>,
298    selections: &[PrincipalSelection<'_>],
299) -> Result<(MixedPlan, Vec<u32>, MuxTransfer), MixedError> {
300    let plan = mixed_plan(hmc)?;
301    let present = mixed_present_ids(hmc, bound)?;
302    let mux = compile_mixed_mux(&plan, selections, &present)?;
303    Ok((plan, present, mux))
304}
305#[cfg(all(test, not(target_arch = "wasm32")))]
306#[path = "transfer_tests.rs"]
307mod transfer_tests;