|
| 1 | +// Copyright (c) 2025 Cloudflare, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +use crate::App; |
| 5 | +use axum::{async_trait, extract::FromRequest, response::IntoResponse, routing::post}; |
| 6 | +use daphne::{error::DapAbort, InitializedReport}; |
| 7 | +use daphne_service_utils::{ |
| 8 | + capnproto::{CapnprotoPayloadDecode, CapnprotoPayloadDecodeExt, CapnprotoPayloadEncodeExt}, |
| 9 | + compute_offload::{InitializeReports, InitializedReports}, |
| 10 | +}; |
| 11 | +use http::StatusCode; |
| 12 | +use prio::codec::ParameterizedDecode; |
| 13 | +use rayon::iter::{IntoParallelIterator as _, ParallelIterator}; |
| 14 | + |
| 15 | +pub(super) fn add_routes(router: super::Router<App>) -> super::Router<App> { |
| 16 | + router.route( |
| 17 | + "/compute_offload/initialize_reports", |
| 18 | + post(initialize_reports), |
| 19 | + ) |
| 20 | +} |
| 21 | + |
| 22 | +struct CapnprotoExtractor<T>(T); |
| 23 | + |
| 24 | +#[async_trait] |
| 25 | +impl<S, T> FromRequest<S> for CapnprotoExtractor<T> |
| 26 | +where |
| 27 | + T: CapnprotoPayloadDecode, |
| 28 | +{ |
| 29 | + type Rejection = StatusCode; |
| 30 | + |
| 31 | + async fn from_request( |
| 32 | + req: http::Request<axum::body::Body>, |
| 33 | + _state: &S, |
| 34 | + ) -> Result<Self, Self::Rejection> { |
| 35 | + let bytes = axum::body::to_bytes(req.into_body(), usize::MAX) |
| 36 | + .await |
| 37 | + .map_err(|_| StatusCode::BAD_REQUEST)?; |
| 38 | + let t = T::decode_from_bytes(&bytes).map_err(|_| StatusCode::BAD_REQUEST)?; |
| 39 | + |
| 40 | + Ok(CapnprotoExtractor(t)) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[tracing::instrument(skip_all, fields(%task_id, report_count = prep_inits.len()))] |
| 45 | +async fn initialize_reports( |
| 46 | + CapnprotoExtractor(InitializeReports { |
| 47 | + hpke_keys, |
| 48 | + valid_report_range, |
| 49 | + task_id, |
| 50 | + task_config, |
| 51 | + agg_param, |
| 52 | + prep_inits, |
| 53 | + }): CapnprotoExtractor<InitializeReports<'static>>, |
| 54 | +) -> impl IntoResponse { |
| 55 | + tracing::info!("initializing reports"); |
| 56 | + let initialized_reports = prep_inits |
| 57 | + .into_par_iter() |
| 58 | + .map(|prep_init| { |
| 59 | + InitializedReport::from_leader( |
| 60 | + &hpke_keys.as_ref(), |
| 61 | + valid_report_range.clone(), |
| 62 | + &task_id, |
| 63 | + &task_config, |
| 64 | + prep_init.report_share, |
| 65 | + prep_init.payload, |
| 66 | + &daphne::DapAggregationParam::get_decoded_with_param(&task_config.vdaf, &agg_param) |
| 67 | + .map_err(|e| DapAbort::from_codec_error(e, task_id))?, |
| 68 | + ) |
| 69 | + }) |
| 70 | + .collect::<Result<Vec<_>, _>>(); |
| 71 | + |
| 72 | + match initialized_reports { |
| 73 | + Ok(reports) => { |
| 74 | + let body = InitializedReports { |
| 75 | + vdaf: task_config.vdaf.into_owned(), |
| 76 | + reports, |
| 77 | + } |
| 78 | + .encode_to_bytes(); |
| 79 | + |
| 80 | + (StatusCode::OK, body).into_response() |
| 81 | + } |
| 82 | + Err(error) => (StatusCode::BAD_REQUEST, axum::Json(error)).into_response(), |
| 83 | + } |
| 84 | +} |
0 commit comments