Skip to content

Commit

Permalink
fix: use NO_CONTENT instead of NOT_FOUND for no job
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Hoyer <[email protected]>
  • Loading branch information
haraldh committed Oct 16, 2024
1 parent c3d0833 commit beb3eef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
36 changes: 17 additions & 19 deletions core/bin/zksync_tee_prover/src/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use reqwest::Client;
use reqwest::{Client, Response, StatusCode};
use secp256k1::{ecdsa::Signature, PublicKey};
use serde::{de::DeserializeOwned, Serialize};
use serde::Serialize;
use url::Url;
use zksync_basic_types::H256;
use zksync_prover_interface::{
api::{
RegisterTeeAttestationRequest, RegisterTeeAttestationResponse, SubmitTeeProofRequest,
SubmitTeeProofResponse, TeeProofGenerationDataRequest, TeeProofGenerationDataResponse,
},
api::{RegisterTeeAttestationRequest, SubmitTeeProofRequest, TeeProofGenerationDataRequest},
inputs::TeeVerifierInput,
outputs::L1BatchTeeProofForL1,
};
Expand All @@ -31,10 +28,9 @@ impl TeeApiClient {
}
}

async fn post<Req, Resp, S>(&self, endpoint: S, request: Req) -> Result<Resp, reqwest::Error>
async fn post<Req, S>(&self, endpoint: S, request: Req) -> Result<Response, reqwest::Error>
where
Req: Serialize + std::fmt::Debug,
Resp: DeserializeOwned,
S: AsRef<str>,
{
let url = self.api_base_url.join(endpoint.as_ref()).unwrap();
Expand All @@ -46,9 +42,7 @@ impl TeeApiClient {
.json(&request)
.send()
.await?
.error_for_status()?
.json::<Resp>()
.await
.error_for_status()
}

/// Registers the attestation quote with the TEE prover interface API, effectively proving that
Expand All @@ -63,8 +57,7 @@ impl TeeApiClient {
attestation: attestation_quote_bytes,
pubkey: public_key.serialize().to_vec(),
};
self.post::<_, RegisterTeeAttestationResponse, _>("/tee/register_attestation", request)
.await?;
self.post("/tee/register_attestation", request).await?;
tracing::info!(
"Attestation quote was successfully registered for the public key {}",
public_key
Expand All @@ -77,12 +70,17 @@ impl TeeApiClient {
pub async fn get_job(
&self,
tee_type: TeeType,
) -> Result<Box<TeeVerifierInput>, TeeProverError> {
) -> Result<Option<TeeVerifierInput>, TeeProverError> {
let request = TeeProofGenerationDataRequest { tee_type };
let response = self
.post::<_, TeeProofGenerationDataResponse, _>("/tee/proof_inputs", request)
.await?;
Ok(response.0)
let response = self.post("/tee/proof_inputs", request).await?;
match response.status() {
StatusCode::OK => Ok(Some(response.json::<TeeVerifierInput>().await?)),
StatusCode::NO_CONTENT => Ok(None),
_ => response
.json::<Option<TeeVerifierInput>>()
.await
.map_err(TeeProverError::Request),
}
}

/// Submits the successfully verified proof to the TEE prover interface API.
Expand All @@ -101,7 +99,7 @@ impl TeeApiClient {
tee_type,
}));
let observer = METRICS.proof_submitting_time.start();
self.post::<_, SubmitTeeProofResponse, _>(
self.post(
format!("/tee/submit_proofs/{batch_number}").as_str(),
request,
)
Expand Down
19 changes: 7 additions & 12 deletions core/bin/zksync_tee_prover/src/tee_prover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fmt;

use reqwest::StatusCode;
use secp256k1::{ecdsa::Signature, Message, PublicKey, Secp256k1};
use zksync_basic_types::H256;
use zksync_node_framework::{
Expand Down Expand Up @@ -92,8 +91,8 @@ impl TeeProver {

async fn step(&self, public_key: &PublicKey) -> Result<Option<L1BatchNumber>, TeeProverError> {
match self.api_client.get_job(self.config.tee_type).await {
Ok(job) => {
let (signature, batch_number, root_hash) = self.verify(*job)?;
Ok(Some(job)) => {
let (signature, batch_number, root_hash) = self.verify(job)?;
self.api_client
.submit_proof(
batch_number,
Expand All @@ -105,15 +104,11 @@ impl TeeProver {
.await?;
Ok(Some(batch_number))
}
Err(err) => match err {
TeeProverError::Request(req_err)
if req_err.status() == Some(StatusCode::NOT_FOUND) =>
{
tracing::trace!("There are currently no pending batches to be proven");
Ok(None)
}
_ => Err(err),
},
Ok(None) => {
tracing::trace!("There are currently no pending batches to be proven");
Ok(None)
}
Err(err) => Err(err),
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions core/node/proof_data_handler/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,35 @@ impl From<DalError> for RequestProcessorError {

impl IntoResponse for RequestProcessorError {
fn into_response(self) -> Response {
let (status_code, message) = match self {
match self {
RequestProcessorError::GeneralError(err) => {
tracing::error!("Error: {:?}", err);
(
StatusCode::INTERNAL_SERVER_ERROR,
"An internal error occurred".to_owned(),
)
.into_response()
}
RequestProcessorError::ObjectStore(err) => {
tracing::error!("GCS error: {:?}", err);
(
StatusCode::BAD_GATEWAY,
"Failed fetching/saving from GCS".to_owned(),
)
.into_response()
}
RequestProcessorError::Dal(err) => {
tracing::error!("Sqlx error: {:?}", err);
(
StatusCode::BAD_GATEWAY,
"Failed fetching/saving from db".to_owned(),
)
.into_response()
}
RequestProcessorError::NoJob => {
tracing::trace!("No job found");
(StatusCode::NOT_FOUND, "No job found".to_owned())
(StatusCode::NO_CONTENT, ()).into_response()
}
};
(status_code, message).into_response()
}
}
}

0 comments on commit beb3eef

Please sign in to comment.