From bfef7a3eb0dcdecca0cc9c97e3c56dd7b7619005 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 8 Nov 2023 12:01:26 -0800 Subject: [PATCH] well that option wasnt as clean as i wanted --- web3_proxy/src/errors.rs | 10 +++++++--- web3_proxy/src/frontend/authorization.rs | 2 +- web3_proxy/src/frontend/rpc_proxy_http.rs | 20 ++++++++++---------- web3_proxy/src/frontend/rpc_proxy_ws.rs | 6 +++--- web3_proxy/src/jsonrpc/request.rs | 4 ++-- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/web3_proxy/src/errors.rs b/web3_proxy/src/errors.rs index 3b8f64e1..db7715d0 100644 --- a/web3_proxy/src/errors.rs +++ b/web3_proxy/src/errors.rs @@ -1441,7 +1441,7 @@ impl IntoResponse for Web3ProxyError { #[inline] /// TODO: maybe we don't want this anymore. maybe we want to require a web3_request? fn into_response(self) -> Response { - self.into_response_with_id(Default::default(), RequestForError::None) + self.into_response_with_id(Default::default(), None::) } } @@ -1465,11 +1465,15 @@ where } impl Web3ProxyError { - pub fn into_message<'a, R>(self, id: Option>, web3_request: R) -> Message + pub fn into_message<'a, R>( + self, + id: Option>, + request_for_error: Option, + ) -> Message where R: Into>, { - let (_, err) = self.as_response_parts(web3_request); + let (_, err) = self.as_response_parts(request_for_error); let id = id.unwrap_or_default(); diff --git a/web3_proxy/src/frontend/authorization.rs b/web3_proxy/src/frontend/authorization.rs index 9103e7d1..abffe00b 100644 --- a/web3_proxy/src/frontend/authorization.rs +++ b/web3_proxy/src/frontend/authorization.rs @@ -192,7 +192,7 @@ impl ResponseOrBytes<'_> { Self::Response(x) => x.num_bytes(), Self::Bytes(num_bytes) => *num_bytes, Self::Error(x) => { - let (_, x) = x.as_response_parts(RequestForError::None); + let (_, x) = x.as_response_parts(None::); x.num_bytes() } diff --git a/web3_proxy/src/frontend/rpc_proxy_http.rs b/web3_proxy/src/frontend/rpc_proxy_http.rs index 24d36ba0..fe10939e 100644 --- a/web3_proxy/src/frontend/rpc_proxy_http.rs +++ b/web3_proxy/src/frontend/rpc_proxy_http.rs @@ -3,7 +3,7 @@ use super::authorization::{ip_is_authorized, key_is_authorized}; use super::request_id::RequestId; use super::rpc_proxy_ws::ProxyMode; -use crate::errors::Web3ProxyError; +use crate::errors::{RequestForError, Web3ProxyError}; use crate::{app::App, jsonrpc::JsonRpcRequestEnum}; use axum::extract::rejection::JsonRejection; use axum::extract::{Path, State}; @@ -92,14 +92,14 @@ async fn _proxy_web3_rpc( ) -> Result { // TODO: create a stat if they error. (but we haven't parsed rpc_key yet, so it needs some thought) let payload = payload - .map_err(|e| Web3ProxyError::from(e).into_response_with_id(None, None))? + .map_err(|e| Web3ProxyError::from(e).into_response_with_id(None, None::))? .0; let first_id = payload.first_id(); let authorization = ip_is_authorized(&app, ip, origin, proxy_mode) .await - .map_err(|e| e.into_response_with_id(first_id.clone(), None))?; + .map_err(|e| e.into_response_with_id(first_id.clone(), None::))?; let authorization = Arc::new(authorization); @@ -114,7 +114,7 @@ async fn _proxy_web3_rpc( let (status_code, response, rpcs) = app .proxy_web3_rpc(authorization, payload, Some(request_id)) .await - .map_err(|e| e.into_response_with_id(first_id, None))?; + .map_err(|e| e.into_response_with_id(first_id, None::))?; let mut response = (status_code, response).into_response(); @@ -300,19 +300,19 @@ async fn _proxy_web3_rpc_with_key( // TODO: DRY w/ proxy_web3_rpc // TODO: create a stat if they error. (but we haven't parsed rpc_key yet, so it needs some thought) let payload = payload - .map_err(|e| Web3ProxyError::from(e).into_response_with_id(None, None))? + .map_err(|e| Web3ProxyError::from(e).into_response_with_id(None, None::))? .0; let first_id = payload.first_id(); - let rpc_key = rpc_key - .parse() - .map_err(|e: Web3ProxyError| e.into_response_with_id(first_id.clone(), None))?; + let rpc_key = rpc_key.parse().map_err(|e: Web3ProxyError| { + e.into_response_with_id(first_id.clone(), None::) + })?; let authorization = key_is_authorized(&app, &rpc_key, ip, origin, proxy_mode, referer, user_agent) .await - .map_err(|e| e.into_response_with_id(first_id.clone(), None))?; + .map_err(|e| e.into_response_with_id(first_id.clone(), None::))?; let authorization = Arc::new(authorization); @@ -326,7 +326,7 @@ async fn _proxy_web3_rpc_with_key( let (status_code, response, rpcs) = app .proxy_web3_rpc(authorization, payload, Some(request_id)) .await - .map_err(|e| e.into_response_with_id(first_id, None))?; + .map_err(|e| e.into_response_with_id(first_id, None::))?; let mut response = (status_code, response).into_response(); diff --git a/web3_proxy/src/frontend/rpc_proxy_ws.rs b/web3_proxy/src/frontend/rpc_proxy_ws.rs index 297ba66a..be45a0b5 100644 --- a/web3_proxy/src/frontend/rpc_proxy_ws.rs +++ b/web3_proxy/src/frontend/rpc_proxy_ws.rs @@ -448,7 +448,7 @@ async fn handle_socket_payload( let response_str = match response { Ok(x) => x.to_json_string().await?, Err(err) => { - let (_, response_data) = err.as_response_parts(RequestForError::None); + let (_, response_data) = err.as_response_parts(None::); let response = ParsedResponse::from_response_data(response_data, response_id); @@ -498,7 +498,7 @@ async fn read_web3_socket( Ok((m, s)) => (m, Some(s)), Err(err) => { // TODO: how can we get the id out of the payload? - let m = err.into_message(None, None); + let m = err.into_message(None, None::); (m, None) } } @@ -532,7 +532,7 @@ async fn read_web3_socket( Ok((m, s)) => (m, Some(s)), Err(err) => { // TODO: how can we get the id out of the payload? - let m = err.into_message(None, None); + let m = err.into_message(None, None::); (m, None) } }; diff --git a/web3_proxy/src/jsonrpc/request.rs b/web3_proxy/src/jsonrpc/request.rs index 4b279193..b2b045fc 100644 --- a/web3_proxy/src/jsonrpc/request.rs +++ b/web3_proxy/src/jsonrpc/request.rs @@ -1,6 +1,6 @@ use super::LooseId; use crate::app::App; -use crate::errors::{Web3ProxyError, RequestForError}; +use crate::errors::{RequestForError, Web3ProxyError}; use crate::frontend::authorization::{Authorization, RequestOrMethod}; use crate::jsonrpc::ValidatedRequest; use axum::response::Response as AxumResponse; @@ -142,7 +142,7 @@ impl JsonRpcRequestEnum { request.add_response(&response); - let response = response.into_response_with_id(Some(err_id), RequestForError::None); + let response = response.into_response_with_id(Some(err_id), None::); // TODO: variable duration depending on the IP sleep(duration).await;