From 5d58a3512b40816b908b09ebb2e40c44b09c7f4b Mon Sep 17 00:00:00 2001 From: Morty Date: Mon, 6 Jan 2025 19:48:23 +0800 Subject: [PATCH] remove unused files --- prover/src/coordinator_client/api.rs | 144 ---------------------- prover/src/coordinator_client/errors.rs | 65 ---------- prover/src/coordinator_client/listener.rs | 5 - prover/src/coordinator_client/types.rs | 87 ------------- 4 files changed, 301 deletions(-) delete mode 100644 prover/src/coordinator_client/api.rs delete mode 100644 prover/src/coordinator_client/errors.rs delete mode 100644 prover/src/coordinator_client/listener.rs delete mode 100644 prover/src/coordinator_client/types.rs diff --git a/prover/src/coordinator_client/api.rs b/prover/src/coordinator_client/api.rs deleted file mode 100644 index 905a1e61c5..0000000000 --- a/prover/src/coordinator_client/api.rs +++ /dev/null @@ -1,144 +0,0 @@ -use crate::{coordinator_client::ProofStatusNotOKError, types::ProofStatus}; - -use super::{errors::*, types::*}; -use anyhow::{bail, Result}; -use core::time::Duration; -use reqwest::{header::CONTENT_TYPE, Url}; -use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; -use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware}; -use serde::Serialize; - -pub struct Api { - url_base: Url, - send_timeout: Duration, - pub client: ClientWithMiddleware, -} - -impl Api { - pub fn new( - url_base: &str, - send_timeout: Duration, - retry_count: u32, - retry_wait_time_sec: u64, - ) -> Result { - let retry_wait_duration = core::time::Duration::from_secs(retry_wait_time_sec); - let retry_policy = ExponentialBackoff::builder() - .retry_bounds(retry_wait_duration / 2, retry_wait_duration) - .build_with_max_retries(retry_count); - - let client = ClientBuilder::new(reqwest::Client::new()) - .with(RetryTransientMiddleware::new_with_policy(retry_policy)) - .build(); - - Ok(Self { - url_base: Url::parse(url_base)?, - send_timeout, - client, - }) - } - - pub async fn challenge(&self) -> Result> { - let method = "/coordinator/v1/challenge"; - let url = self.build_url(method)?; - - let response = self - .client - .get(url) - .header(CONTENT_TYPE, "application/json") - .timeout(self.send_timeout) - .send() - .await?; - - let response_body = response.text().await?; - - serde_json::from_str(&response_body).map_err(|e| anyhow::anyhow!(e)) - } - - pub async fn login( - &self, - req: &LoginRequest, - token: &String, - ) -> Result> { - let method = "/coordinator/v1/login"; - self.post_with_token(method, req, token).await - } - - pub async fn get_task( - &self, - req: &GetTaskRequest, - token: &String, - ) -> Result> { - let method = "/coordinator/v1/get_task"; - self.post_with_token(method, req, token).await - } - - pub async fn submit_proof( - &self, - req: &SubmitProofRequest, - token: &String, - ) -> Result> { - let method = "/coordinator/v1/submit_proof"; - let response = self - .post_with_token::>( - method, req, token, - ) - .await?; - - // when req's status already not ok, we mark the error returned from coordinator and will - // ignore it later. - if response.errcode == ErrorCode::ErrCoordinatorHandleZkProofFailure - && req.status != ProofStatus::Ok - && response - .errmsg - .contains("validator failure proof msg status not ok") - { - return Err(anyhow::anyhow!(ProofStatusNotOKError)); - } - Ok(response) - } - - async fn post_with_token( - &self, - method: &str, - req: &Req, - token: &String, - ) -> Result - where - Req: ?Sized + Serialize, - Resp: serde::de::DeserializeOwned, - { - let url = self.build_url(method)?; - let request_body = serde_json::to_string(req)?; - - log::info!("[coordinator client], {method}, request: {request_body}"); - let response = self - .client - .post(url) - .header(CONTENT_TYPE, "application/json") - .bearer_auth(token) - .body(request_body) - .timeout(self.send_timeout) - .send() - .await?; - - if response.status() != http::status::StatusCode::OK { - log::error!( - "[coordinator client], {method}, status not ok: {}", - response.status() - ); - bail!( - "[coordinator client], {method}, status not ok: {}", - response.status() - ) - } - - let response_body = response.text().await?; - - log::info!("[coordinator client], {method}, response: {response_body}"); - serde_json::from_str(&response_body).map_err(|e| anyhow::anyhow!(e)) - } - - fn build_url(&self, method: &str) -> Result { - self.url_base.join(method).map_err(|e| anyhow::anyhow!(e)) - } -} diff --git a/prover/src/coordinator_client/errors.rs b/prover/src/coordinator_client/errors.rs deleted file mode 100644 index 9bad256fac..0000000000 --- a/prover/src/coordinator_client/errors.rs +++ /dev/null @@ -1,65 +0,0 @@ -use serde::{Deserialize, Deserializer}; -use std::fmt; - -#[derive(Debug, Clone, Copy, PartialEq)] -pub enum ErrorCode { - Success, - InternalServerError, - - ErrProverStatsAPIParameterInvalidNo, - ErrProverStatsAPIProverTaskFailure, - ErrProverStatsAPIProverTotalRewardFailure, - - ErrCoordinatorParameterInvalidNo, - ErrCoordinatorGetTaskFailure, - ErrCoordinatorHandleZkProofFailure, - ErrCoordinatorEmptyProofData, - - ErrJWTCommonErr, - ErrJWTTokenExpired, - - Undefined(i32), -} - -impl ErrorCode { - fn from_i32(v: i32) -> Self { - match v { - 0 => ErrorCode::Success, - 500 => ErrorCode::InternalServerError, - 10001 => ErrorCode::ErrProverStatsAPIParameterInvalidNo, - 10002 => ErrorCode::ErrProverStatsAPIProverTaskFailure, - 10003 => ErrorCode::ErrProverStatsAPIProverTotalRewardFailure, - 20001 => ErrorCode::ErrCoordinatorParameterInvalidNo, - 20002 => ErrorCode::ErrCoordinatorGetTaskFailure, - 20003 => ErrorCode::ErrCoordinatorHandleZkProofFailure, - 20004 => ErrorCode::ErrCoordinatorEmptyProofData, - 50000 => ErrorCode::ErrJWTCommonErr, - 50001 => ErrorCode::ErrJWTTokenExpired, - _ => { - log::error!("get unexpected error code from coordinator: {v}"); - ErrorCode::Undefined(v) - } - } - } -} - -impl<'de> Deserialize<'de> for ErrorCode { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let v: i32 = i32::deserialize(deserializer)?; - Ok(ErrorCode::from_i32(v)) - } -} - -// ==================================================== - -#[derive(Debug, Clone)] -pub struct ProofStatusNotOKError; - -impl fmt::Display for ProofStatusNotOKError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "proof status not ok") - } -} diff --git a/prover/src/coordinator_client/listener.rs b/prover/src/coordinator_client/listener.rs deleted file mode 100644 index 9f9f70f606..0000000000 --- a/prover/src/coordinator_client/listener.rs +++ /dev/null @@ -1,5 +0,0 @@ -use super::SubmitProofRequest; - -pub trait Listener { - fn on_proof_submitted(&self, req: &SubmitProofRequest); -} diff --git a/prover/src/coordinator_client/types.rs b/prover/src/coordinator_client/types.rs deleted file mode 100644 index 93eadd8616..0000000000 --- a/prover/src/coordinator_client/types.rs +++ /dev/null @@ -1,87 +0,0 @@ -use super::errors::ErrorCode; -use crate::types::{ProofFailureType, ProofStatus, ProverType}; -use rlp::{Encodable, RlpStream}; -use serde::{Deserialize, Serialize}; -use scroll_proving_sdk::prover::types::CircuitType; - -#[derive(Deserialize)] -pub struct Response { - pub errcode: ErrorCode, - pub errmsg: String, - pub data: Option, -} - -#[derive(Serialize, Deserialize)] -pub struct LoginMessage { - pub challenge: String, - pub prover_name: String, - pub prover_version: String, - pub prover_types: Vec, - pub vks: Vec, -} - -impl Encodable for LoginMessage { - fn rlp_append(&self, s: &mut RlpStream) { - let num_fields = 5; - s.begin_list(num_fields); - s.append(&self.challenge); - s.append(&self.prover_version); - s.append(&self.prover_name); - // The ProverType in go side is an type alias of uint8 - // A uint8 slice is treated as a string when doing the rlp encoding - let prover_types = self - .prover_types - .iter() - .map(|prover_type: &ProverType| prover_type.to_u8()) - .collect::>(); - s.append(&prover_types); - s.begin_list(self.vks.len()); - for vk in &self.vks { - s.append(vk); - } - } -} - -#[derive(Serialize, Deserialize)] -pub struct LoginRequest { - pub message: LoginMessage, - pub public_key: String, - pub signature: String, -} - -#[derive(Serialize, Deserialize)] -pub struct LoginResponseData { - pub time: String, - pub token: String, -} - -pub type ChallengeResponseData = LoginResponseData; - -#[derive(Default, Serialize, Deserialize)] -pub struct GetTaskRequest { - pub task_types: Vec, - pub prover_height: Option, -} - -#[derive(Serialize, Deserialize)] -pub struct GetTaskResponseData { - pub uuid: String, - pub task_id: String, - pub task_type: CircuitType, - pub task_data: String, - pub hard_fork_name: String, -} - -#[derive(Serialize, Deserialize, Default)] -pub struct SubmitProofRequest { - pub uuid: String, - pub task_id: String, - pub task_type: CircuitType, - pub status: ProofStatus, - pub proof: String, - pub failure_type: Option, - pub failure_msg: Option, -} - -#[derive(Serialize, Deserialize)] -pub struct SubmitProofResponseData {}