Skip to content

Commit

Permalink
Fix DB transaction error in verify REST endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiimk committed Dec 28, 2024
1 parent 1df9088 commit 9b8c645
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 51 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Recommendation: for ease of reading, use the following order:
## [Unreleased]
### Fixed
- GraphQL: in a multi-tenant workspace, `datasets.createEmpty` and `datasets.createFromSnapshot` mutations now return dataset aliases prefixed with account name.
- Fix DB transaction error in `/verify` REST endpoint (cherry-picked from `0.214.1`)

## [0.214.1] - 2024-12-28 (HOT FIX)
### Fixed
- Transaction error on `/verify` REST endpoint

## [0.215.0] - 2024-12-27
### Added
Expand Down
60 changes: 35 additions & 25 deletions src/adapter/http/src/data/query_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,41 @@ use super::query_types::{QueryResponse, *};
#[transactional_handler]
pub async fn query_handler_post(
Extension(catalog): Extension<Catalog>,
Json(mut body): Json<QueryRequest>,
Json(body): Json<QueryRequest>,
) -> Result<Json<QueryResponse>, ApiError> {
query_handler_impl(catalog, body).await
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// Execute a batch query
///
/// Functions exactly like the [POST version](#tag/odf-query/POST/query) of the
/// endpoint with all parameters passed in the query string instead of the body.
#[utoipa::path(
get,
path = "/query",
params(QueryParams),
responses((status = OK, body = QueryResponse)),
tag = "odf-query",
security(
(),
("api_key" = [])
),
)]
#[transactional_handler]
pub async fn query_handler(
Extension(catalog): Extension<Catalog>,
Query(params): Query<QueryParams>,
) -> Result<Json<QueryResponse>, ApiError> {
query_handler_impl(catalog, params.into()).await
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

pub async fn query_handler_impl(
catalog: Catalog,
mut body: QueryRequest,
) -> Result<Json<QueryResponse>, ApiError> {
tracing::debug!(request = ?body, "Query");

Expand Down Expand Up @@ -254,30 +288,6 @@ pub async fn query_handler_post(

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/// Execute a batch query
///
/// Functions exactly like the [POST version](#tag/odf-query/POST/query) of the
/// endpoint with all parameters passed in the query string instead of the body.
#[utoipa::path(
get,
path = "/query",
params(QueryParams),
responses((status = OK, body = QueryResponse)),
tag = "odf-query",
security(
(),
("api_key" = [])
),
)]
pub async fn query_handler(
catalog: Extension<Catalog>,
Query(params): Query<QueryParams>,
) -> Result<Json<QueryResponse>, ApiError> {
query_handler_post(catalog, Json(params.into())).await
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#[derive(Debug, thiserror::Error)]
#[error("Response signing is not enabled by the node operator")]
struct ResponseSigningNotConfigured;
Expand Down
47 changes: 21 additions & 26 deletions src/adapter/http/src/data/verify_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,17 @@ pub async fn verify_handler(
Json(request): Json<VerifyRequest>,
) -> Result<VerifyResponse, ApiError> {
tracing::debug!(?request, "Verification request");

assert_eq!(
request.proof.r#type,
query::ProofType::Ed25519Signature2020,
"UnsupportedProofType",
);

let response = verify(catalog, request).await?;
tracing::debug!(?response, "Verification response");
Ok(response)
}

async fn verify(catalog: Catalog, request: VerifyRequest) -> Result<VerifyResponse, ApiError> {
// 1. Validate request
match request.proof.r#type {
query::ProofType::Ed25519Signature2020 => {}
}

if request.commitment.input_hash
!= odf::Multihash::from_digest_sha3_256(&query::to_canonical_json(&request.input))
{
Expand Down Expand Up @@ -172,25 +169,23 @@ async fn verify(catalog: Catalog, request: VerifyRequest) -> Result<VerifyRespon
// We are only interested in the output
data_request.include.clear();

let query_result =
match query_handler::query_handler_post(axum::Extension(catalog), Json(data_request)).await
{
Ok(Json(v)) => Ok(v),
Err(err) => match err.source().unwrap().downcast_ref::<QueryError>() {
Some(QueryError::DatasetNotFound(err)) => {
return Ok(VerifyResponse::from(DatasetNotFound::new(
err.dataset_ref.id().unwrap().clone(),
)));
}
Some(QueryError::DatasetBlockNotFound(err)) => {
return Ok(VerifyResponse::from(DatasetBlockNotFound::new(
err.dataset_id.clone(),
err.block_hash.clone(),
)));
}
_ => Err(err),
},
}?;
let query_result = match query_handler::query_handler_impl(catalog, data_request).await {
Ok(Json(v)) => Ok(v),
Err(err) => match err.source().unwrap().downcast_ref::<QueryError>() {
Some(QueryError::DatasetNotFound(err)) => {
return Ok(VerifyResponse::from(DatasetNotFound::new(
err.dataset_ref.id().unwrap().clone(),
)));
}
Some(QueryError::DatasetBlockNotFound(err)) => {
return Ok(VerifyResponse::from(DatasetBlockNotFound::new(
err.dataset_id.clone(),
err.block_hash.clone(),
)));
}
_ => Err(err),
},
}?;

let output_hash_actual =
odf::Multihash::from_digest_sha3_256(&query::to_canonical_json(&query_result.output));
Expand Down

0 comments on commit 9b8c645

Please sign in to comment.