From 1e9306f960d1e3099667184c537c4cb303ba93ef Mon Sep 17 00:00:00 2001 From: Patrick Beza Date: Thu, 10 Oct 2024 18:17:58 +0200 Subject: [PATCH] fix(tee-prover): simplify TeeProofGenerationDataResponse This PR addresses Alex's code review comment: https://github.com/matter-labs/zksync-era/pull/3017#discussion_r1795237929 and partially addresses this one too: https://github.com/matter-labs/zksync-era/pull/3017#discussion_r1795240750 --- core/bin/zksync_tee_prover/src/api_client.rs | 2 +- core/bin/zksync_tee_prover/src/config.rs | 2 +- core/bin/zksync_tee_prover/src/tee_prover.rs | 18 ++++++++++++------ core/lib/prover_interface/src/api.rs | 5 ++++- core/node/proof_data_handler/src/lib.rs | 10 ++++++++-- .../src/tee_request_processor.rs | 6 ++++-- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/core/bin/zksync_tee_prover/src/api_client.rs b/core/bin/zksync_tee_prover/src/api_client.rs index 13fbc1ba8868..860c102d5629 100644 --- a/core/bin/zksync_tee_prover/src/api_client.rs +++ b/core/bin/zksync_tee_prover/src/api_client.rs @@ -77,7 +77,7 @@ impl TeeApiClient { pub async fn get_job( &self, tee_type: TeeType, - ) -> Result>, TeeProverError> { + ) -> Result, TeeProverError> { let request = TeeProofGenerationDataRequest { tee_type }; let response = self .post::<_, TeeProofGenerationDataResponse, _>("/tee/proof_inputs", request) diff --git a/core/bin/zksync_tee_prover/src/config.rs b/core/bin/zksync_tee_prover/src/config.rs index 1c2eb229d616..9c07ef1cf5a7 100644 --- a/core/bin/zksync_tee_prover/src/config.rs +++ b/core/bin/zksync_tee_prover/src/config.rs @@ -47,7 +47,7 @@ impl FromEnv for TeeProverConfig { /// export TEE_PROVER_SIGNING_KEY="b50b38c8d396c88728fc032ece558ebda96907a0b1a9340289715eef7bf29deb" /// export TEE_PROVER_ATTESTATION_QUOTE_FILE_PATH="/tmp/test" # run `echo test > /tmp/test` beforehand /// export TEE_PROVER_TEE_TYPE="sgx" - /// export TEE_PROVER_API_URL="http://127.0.0.1:3320" + /// export TEE_PROVER_API_URL="http://127.0.0.1:3421" /// export TEE_PROVER_MAX_RETRIES=10 /// export TEE_PROVER_INITIAL_RETRY_BACKOFF_SEC=1 /// export TEE_PROVER_RETRY_BACKOFF_MULTIPLIER=2.0 diff --git a/core/bin/zksync_tee_prover/src/tee_prover.rs b/core/bin/zksync_tee_prover/src/tee_prover.rs index 1511f0c88e3d..7d40bd324319 100644 --- a/core/bin/zksync_tee_prover/src/tee_prover.rs +++ b/core/bin/zksync_tee_prover/src/tee_prover.rs @@ -1,5 +1,6 @@ use std::fmt; +use reqwest::StatusCode; use secp256k1::{ecdsa::Signature, Message, PublicKey, Secp256k1}; use zksync_basic_types::H256; use zksync_node_framework::{ @@ -90,8 +91,8 @@ impl TeeProver { } async fn step(&self, public_key: &PublicKey) -> Result, TeeProverError> { - match self.api_client.get_job(self.config.tee_type).await? { - Some(job) => { + match self.api_client.get_job(self.config.tee_type).await { + Ok(job) => { let (signature, batch_number, root_hash) = self.verify(*job)?; self.api_client .submit_proof( @@ -104,10 +105,15 @@ impl TeeProver { .await?; Ok(Some(batch_number)) } - None => { - tracing::trace!("There are currently no pending batches to be proven"); - Ok(None) - } + 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), + }, } } } diff --git a/core/lib/prover_interface/src/api.rs b/core/lib/prover_interface/src/api.rs index 776cd3141cbe..22bcc434fc53 100644 --- a/core/lib/prover_interface/src/api.rs +++ b/core/lib/prover_interface/src/api.rs @@ -31,7 +31,10 @@ pub enum ProofGenerationDataResponse { } #[derive(Debug, Serialize, Deserialize)] -pub struct TeeProofGenerationDataResponse(pub Option>); +pub enum TeeProofGenerationDataResponse { + VerifierInputReady(Box), + VerifierInputNotReady, +} #[derive(Debug, Serialize, Deserialize)] pub enum SubmitProofResponse { diff --git a/core/node/proof_data_handler/src/lib.rs b/core/node/proof_data_handler/src/lib.rs index 0f90d657be5d..371e1a3fbd89 100644 --- a/core/node/proof_data_handler/src/lib.rs +++ b/core/node/proof_data_handler/src/lib.rs @@ -109,8 +109,14 @@ fn create_proof_processing_router( .await; match result { - Ok(Json(TeeProofGenerationDataResponse(None))) => (StatusCode::NO_CONTENT, Json("No new TeeVerifierInputs are available yet")).into_response(), - Ok(data) => (StatusCode::OK, data).into_response(), + Ok(data) => match data { + Json(TeeProofGenerationDataResponse::VerifierInputReady(input)) => { + (StatusCode::OK, Json(TeeProofGenerationDataResponse::VerifierInputReady(input))).into_response() + } + Json(TeeProofGenerationDataResponse::VerifierInputNotReady) => { + (StatusCode::NO_CONTENT, Json(TeeProofGenerationDataResponse::VerifierInputNotReady)).into_response() + } + } Err(e) => e.into_response(), } }, diff --git a/core/node/proof_data_handler/src/tee_request_processor.rs b/core/node/proof_data_handler/src/tee_request_processor.rs index 5956be17e976..9d8b40f4882f 100644 --- a/core/node/proof_data_handler/src/tee_request_processor.rs +++ b/core/node/proof_data_handler/src/tee_request_processor.rs @@ -56,7 +56,7 @@ impl TeeRequestProcessor { .await? { Some(number) => number, - None => break Ok(Json(TeeProofGenerationDataResponse(None))), + None => break Ok(Json(TeeProofGenerationDataResponse::VerifierInputNotReady)), }; match self @@ -64,7 +64,9 @@ impl TeeRequestProcessor { .await { Ok(input) => { - break Ok(Json(TeeProofGenerationDataResponse(Some(Box::new(input))))); + break Ok(Json(TeeProofGenerationDataResponse::VerifierInputReady( + Box::new(input), + ))); } Err(RequestProcessorError::ObjectStore(ObjectStoreError::KeyNotFound(_))) => { missing_range = match missing_range {