diff --git a/web3_proxy/src/errors.rs b/web3_proxy/src/errors.rs index ecc0f66b..ce9c461e 100644 --- a/web3_proxy/src/errors.rs +++ b/web3_proxy/src/errors.rs @@ -1013,9 +1013,12 @@ impl Web3ProxyError { (code, JsonRpcResponseEnum::from(err)) } - pub fn into_response_with_id(self, id: Box) -> Response { + #[inline] + pub fn into_response_with_id(self, id: Option>) -> Response { let (status_code, response_data) = self.as_response_parts(); + let id = id.unwrap_or_default(); + let response = JsonRpcForwardedResponse::from_response_data(response_data, id); (status_code, Json(response)).into_response() diff --git a/web3_proxy/src/frontend/rpc_proxy_http.rs b/web3_proxy/src/frontend/rpc_proxy_http.rs index 6c6a8516..92dab682 100644 --- a/web3_proxy/src/frontend/rpc_proxy_http.rs +++ b/web3_proxy/src/frontend/rpc_proxy_http.rs @@ -60,20 +60,21 @@ async fn _proxy_web3_rpc( // TODO: do we care about keeping the TypedHeader wrapper? let origin = origin.map(|x| x.0); - let first_id = payload.first_id().map_err(|e| e.into_response())?; + let first_id = payload.first_id(); let (authorization, _semaphore) = ip_is_authorized(&app, ip, origin, proxy_mode) .await - .map_err(|e| e.into_response_with_id(first_id.to_owned()))?; + .map_err(|e| e.into_response_with_id(first_id.clone()))?; let authorization = Arc::new(authorization); // TODO: calculate payload bytes here (before turning into serde_json::Value). that will save serializing later + // TODO: is first_id the right thing to attach to this error? let (status_code, response, rpcs) = app .proxy_web3_rpc(authorization, payload) .await - .map_err(|e| e.into_response_with_id(first_id.to_owned()))?; + .map_err(|e| e.into_response_with_id(first_id))?; let mut response = (status_code, Json(response)).into_response(); @@ -220,11 +221,11 @@ async fn _proxy_web3_rpc_with_key( ) -> Result { // TODO: DRY w/ proxy_web3_rpc - let first_id = payload.first_id().map_err(|e| e.into_response())?; + let first_id = payload.first_id(); let rpc_key = rpc_key .parse() - .map_err(|e: Web3ProxyError| e.into_response_with_id(first_id.to_owned()))?; + .map_err(|e: Web3ProxyError| e.into_response_with_id(first_id.clone()))?; let (authorization, _semaphore) = key_is_authorized( &app, @@ -236,7 +237,7 @@ async fn _proxy_web3_rpc_with_key( user_agent.map(|x| x.0), ) .await - .map_err(|e| e.into_response_with_id(first_id.to_owned()))?; + .map_err(|e| e.into_response_with_id(first_id.clone()))?; let authorization = Arc::new(authorization); @@ -245,7 +246,7 @@ async fn _proxy_web3_rpc_with_key( let (status_code, response, rpcs) = app .proxy_web3_rpc(authorization, payload) .await - .map_err(|e| e.into_response_with_id(first_id.to_owned()))?; + .map_err(|e| e.into_response_with_id(first_id))?; let mut response = (status_code, Json(response)).into_response(); diff --git a/web3_proxy/src/jsonrpc.rs b/web3_proxy/src/jsonrpc.rs index 42fcc2ae..9cccfa23 100644 --- a/web3_proxy/src/jsonrpc.rs +++ b/web3_proxy/src/jsonrpc.rs @@ -80,15 +80,13 @@ pub enum JsonRpcRequestEnum { } impl JsonRpcRequestEnum { - pub fn first_id(&self) -> Web3ProxyResult> { + pub fn first_id(&self) -> Option> { match self { Self::Batch(x) => match x.first() { - Some(x) => Ok(x.id.clone()), - None => Err(Web3ProxyError::BadRequest( - "no requests in the batch".into(), - )), + Some(x) => Some(x.id.clone()), + None => None, }, - Self::Single(x) => Ok(x.id.clone()), + Self::Single(x) => Some(x.id.clone()), } } }