From cac03a585226109ae0b45b2b5e14edb0c3fced30 Mon Sep 17 00:00:00 2001 From: Patrick Beza Date: Thu, 10 Oct 2024 18:17:58 +0200 Subject: [PATCH 1/2] 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 | 5 ++--- core/bin/zksync_tee_prover/src/config.rs | 2 +- core/bin/zksync_tee_prover/src/tee_prover.rs | 6 +++--- core/lib/prover_interface/src/api.rs | 5 ++++- core/node/proof_data_handler/src/lib.rs | 10 ++++++++-- .../proof_data_handler/src/tee_request_processor.rs | 6 ++++-- 6 files changed, 22 insertions(+), 12 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..a203a4c5edad 100644 --- a/core/bin/zksync_tee_prover/src/api_client.rs +++ b/core/bin/zksync_tee_prover/src/api_client.rs @@ -8,7 +8,6 @@ use zksync_prover_interface::{ RegisterTeeAttestationRequest, RegisterTeeAttestationResponse, SubmitTeeProofRequest, SubmitTeeProofResponse, TeeProofGenerationDataRequest, TeeProofGenerationDataResponse, }, - inputs::TeeVerifierInput, outputs::L1BatchTeeProofForL1, }; use zksync_types::{tee_types::TeeType, L1BatchNumber}; @@ -77,12 +76,12 @@ impl TeeApiClient { pub async fn get_job( &self, tee_type: TeeType, - ) -> Result>, TeeProverError> { + ) -> Result { let request = TeeProofGenerationDataRequest { tee_type }; let response = self .post::<_, TeeProofGenerationDataResponse, _>("/tee/proof_inputs", request) .await?; - Ok(response.0) + Ok(response) } /// Submits the successfully verified proof to the TEE prover interface API. 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..b3a3ed0b3438 100644 --- a/core/bin/zksync_tee_prover/src/tee_prover.rs +++ b/core/bin/zksync_tee_prover/src/tee_prover.rs @@ -8,7 +8,7 @@ use zksync_node_framework::{ wiring_layer::{WiringError, WiringLayer}, IntoContext, }; -use zksync_prover_interface::inputs::TeeVerifierInput; +use zksync_prover_interface::{api::TeeProofGenerationDataResponse, inputs::TeeVerifierInput}; use zksync_tee_verifier::Verify; use zksync_types::L1BatchNumber; @@ -91,7 +91,7 @@ impl TeeProver { async fn step(&self, public_key: &PublicKey) -> Result, TeeProverError> { match self.api_client.get_job(self.config.tee_type).await? { - Some(job) => { + TeeProofGenerationDataResponse::VerifierInputReady(job) => { let (signature, batch_number, root_hash) = self.verify(*job)?; self.api_client .submit_proof( @@ -104,7 +104,7 @@ impl TeeProver { .await?; Ok(Some(batch_number)) } - None => { + TeeProofGenerationDataResponse::VerifierInputNotReady => { tracing::trace!("There are currently no pending batches to be proven"); Ok(None) } 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 { From cfef33a868a1e4a5f74cc17ef1eea1420cd1eec8 Mon Sep 17 00:00:00 2001 From: Patrick Beza Date: Mon, 14 Oct 2024 18:13:53 +0200 Subject: [PATCH 2/2] Fix the issue with handling the 402 status code in the API client --- core/bin/zksync_tee_prover/src/api_client.rs | 34 +++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/core/bin/zksync_tee_prover/src/api_client.rs b/core/bin/zksync_tee_prover/src/api_client.rs index a203a4c5edad..e054768d698c 100644 --- a/core/bin/zksync_tee_prover/src/api_client.rs +++ b/core/bin/zksync_tee_prover/src/api_client.rs @@ -1,12 +1,12 @@ -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, + RegisterTeeAttestationRequest, SubmitTeeProofRequest, TeeProofGenerationDataRequest, + TeeProofGenerationDataResponse, }, outputs::L1BatchTeeProofForL1, }; @@ -30,10 +30,9 @@ impl TeeApiClient { } } - async fn post(&self, endpoint: S, request: Req) -> Result + async fn post(&self, endpoint: S, request: Req) -> Result where Req: Serialize + std::fmt::Debug, - Resp: DeserializeOwned, S: AsRef, { let url = self.api_base_url.join(endpoint.as_ref()).unwrap(); @@ -45,9 +44,7 @@ impl TeeApiClient { .json(&request) .send() .await? - .error_for_status()? - .json::() - .await + .error_for_status() } /// Registers the attestation quote with the TEE prover interface API, effectively proving that @@ -62,8 +59,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 @@ -78,10 +74,16 @@ impl TeeApiClient { tee_type: TeeType, ) -> Result { let request = TeeProofGenerationDataRequest { tee_type }; - let response = self - .post::<_, TeeProofGenerationDataResponse, _>("/tee/proof_inputs", request) - .await?; - Ok(response) + let response = self.post("/tee/proof_inputs", request).await?; + + match response.status() { + StatusCode::OK => Ok(response.json::().await?), + StatusCode::NO_CONTENT => Ok(TeeProofGenerationDataResponse::VerifierInputNotReady), + _ => response + .json::() + .await + .map_err(TeeProverError::Request), + } } /// Submits the successfully verified proof to the TEE prover interface API. @@ -100,7 +102,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, )