From b460f1701a9e536bab10618af62465f28daf6793 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 5 Apr 2023 15:34:28 -0700 Subject: [PATCH] rename revert_handler to error_handler --- web3_proxy/src/rpcs/many.rs | 4 +-- web3_proxy/src/rpcs/one.rs | 8 ++--- web3_proxy/src/rpcs/request.rs | 56 ++++++++++++++++++---------------- 3 files changed, 35 insertions(+), 33 deletions(-) diff --git a/web3_proxy/src/rpcs/many.rs b/web3_proxy/src/rpcs/many.rs index 4c86f2a6..5998e269 100644 --- a/web3_proxy/src/rpcs/many.rs +++ b/web3_proxy/src/rpcs/many.rs @@ -2,7 +2,7 @@ use super::blockchain::{BlocksByHashCache, Web3ProxyBlock}; use super::consensus::ConsensusWeb3Rpcs; use super::one::Web3Rpc; -use super::request::{OpenRequestHandle, OpenRequestResult, RequestRevertHandler}; +use super::request::{OpenRequestHandle, OpenRequestResult, RequestErrorHandler}; use crate::app::{flatten_handle, AnyhowJoinHandle, Web3ProxyApp}; ///! Load balanced communication with a group of web3 providers use crate::config::{BlockAndRpc, TxHashAndRpc, Web3RpcConfig}; @@ -877,7 +877,7 @@ impl Web3Rpcs { .request( &request.method, &json!(request.params), - RequestRevertHandler::Save, + RequestErrorHandler::Save, None, ) .await; diff --git a/web3_proxy/src/rpcs/one.rs b/web3_proxy/src/rpcs/one.rs index 6d896abb..ca369585 100644 --- a/web3_proxy/src/rpcs/one.rs +++ b/web3_proxy/src/rpcs/one.rs @@ -6,7 +6,7 @@ use crate::app::{flatten_handle, AnyhowJoinHandle}; use crate::config::{BlockAndRpc, Web3RpcConfig}; use crate::frontend::authorization::Authorization; use crate::frontend::errors::{Web3ProxyError, Web3ProxyResult}; -use crate::rpcs::request::RequestRevertHandler; +use crate::rpcs::request::RequestErrorHandler; use anyhow::{anyhow, Context}; use ethers::prelude::{Bytes, Middleware, ProviderError, TxHash, H256, U64}; use ethers::types::{Address, Transaction, U256}; @@ -716,9 +716,9 @@ impl Web3Rpc { tx_id_sender: Option)>>, ) -> anyhow::Result<()> { let error_handler = if self.backup { - RequestRevertHandler::DebugLevel + RequestErrorHandler::DebugLevel } else { - RequestRevertHandler::ErrorLevel + RequestErrorHandler::ErrorLevel }; let mut delay_start = false; @@ -1331,7 +1331,7 @@ impl Web3Rpc { self: &Arc, method: &str, params: &P, - revert_handler: RequestRevertHandler, + revert_handler: RequestErrorHandler, authorization: Arc, unlocked_provider: Option>, ) -> anyhow::Result diff --git a/web3_proxy/src/rpcs/request.rs b/web3_proxy/src/rpcs/request.rs index 2bf24b2f..abba4f4b 100644 --- a/web3_proxy/src/rpcs/request.rs +++ b/web3_proxy/src/rpcs/request.rs @@ -33,9 +33,9 @@ pub struct OpenRequestHandle { rpc: Arc, } -/// Depending on the context, RPC errors can require different handling. +/// Depending on the context, RPC errors require different handling. #[derive(Copy, Clone)] -pub enum RequestRevertHandler { +pub enum RequestErrorHandler { /// Log at the trace level. Use when errors are expected. TraceLevel, /// Log at the debug level. Use when errors are expected. @@ -58,13 +58,13 @@ struct EthCallFirstParams { data: Option, } -impl From for RequestRevertHandler { +impl From for RequestErrorHandler { fn from(level: Level) -> Self { match level { - Level::Trace => RequestRevertHandler::TraceLevel, - Level::Debug => RequestRevertHandler::DebugLevel, - Level::Error => RequestRevertHandler::ErrorLevel, - Level::Warn => RequestRevertHandler::WarnLevel, + Level::Trace => RequestErrorHandler::TraceLevel, + Level::Debug => RequestErrorHandler::DebugLevel, + Level::Error => RequestErrorHandler::ErrorLevel, + Level::Warn => RequestErrorHandler::WarnLevel, _ => unimplemented!("unexpected tracing Level"), } } @@ -80,7 +80,7 @@ impl Authorization { let rpc_key_id = match self.checks.rpc_secret_key_id { Some(rpc_key_id) => rpc_key_id.into(), None => { - // // trace!(?self, "cannot save revert without rpc_key_id"); + // trace!(?self, "cannot save revert without rpc_key_id"); return Ok(()); } }; @@ -150,7 +150,7 @@ impl OpenRequestHandle { self, method: &str, params: &P, - revert_handler: RequestRevertHandler, + mut error_handler: RequestErrorHandler, unlocked_provider: Option>, ) -> Result where @@ -229,44 +229,45 @@ impl OpenRequestHandle { if let Err(err) = &response { // only save reverts for some types of calls // TODO: do something special for eth_sendRawTransaction too - let error_handler = if let RequestRevertHandler::Save = revert_handler { + error_handler = if let RequestErrorHandler::Save = error_handler { // TODO: should all these be Trace or Debug or a mix? if !["eth_call", "eth_estimateGas"].contains(&method) { // trace!(%method, "skipping save on revert"); - RequestRevertHandler::TraceLevel + RequestErrorHandler::TraceLevel } else if self.authorization.db_conn.is_some() { let log_revert_chance = self.authorization.checks.log_revert_chance; if log_revert_chance == 0.0 { // trace!(%method, "no chance. skipping save on revert"); - RequestRevertHandler::TraceLevel + RequestErrorHandler::TraceLevel } else if log_revert_chance == 1.0 { // trace!(%method, "gaurenteed chance. SAVING on revert"); - revert_handler + error_handler } else if thread_fast_rng::thread_fast_rng().gen_range(0.0f64..=1.0) < log_revert_chance { // trace!(%method, "missed chance. skipping save on revert"); - RequestRevertHandler::TraceLevel + RequestErrorHandler::TraceLevel } else { // trace!("Saving on revert"); // TODO: is always logging at debug level fine? - revert_handler + error_handler } } else { // trace!(%method, "no database. skipping save on revert"); - RequestRevertHandler::TraceLevel + RequestErrorHandler::TraceLevel } } else { - revert_handler + error_handler }; // TODO: simple enum -> string derive? + // TODO: if ProviderError::UnsupportedRpc, we should retry on another server #[derive(Debug)] enum ResponseTypes { Revert, RateLimit, - Ok, + Error, } // check for "execution reverted" here @@ -317,17 +318,18 @@ impl OpenRequestHandle { trace!("rate limit from {}", self.rpc); ResponseTypes::RateLimit } else { - ResponseTypes::Ok + ResponseTypes::Error } } else { - ResponseTypes::Ok + ResponseTypes::Error } } else { - ResponseTypes::Ok + ResponseTypes::Error }; if matches!(response_type, ResponseTypes::RateLimit) { if let Some(hard_limit_until) = self.rpc.hard_limit_until.as_ref() { + // TODO: how long should we actually wait? different providers have different times let retry_at = Instant::now() + Duration::from_secs(1); trace!("retry {} at: {:?}", self.rpc, retry_at); @@ -337,8 +339,8 @@ impl OpenRequestHandle { } // TODO: think more about the method and param logs. those can be sensitive information - match revert_handler { - RequestRevertHandler::DebugLevel => { + match error_handler { + RequestErrorHandler::DebugLevel => { // TODO: think about this revert check more. sometimes we might want reverts logged so this needs a flag if matches!(response_type, ResponseTypes::Revert) { debug!( @@ -347,7 +349,7 @@ impl OpenRequestHandle { ); } } - RequestRevertHandler::TraceLevel => { + RequestErrorHandler::TraceLevel => { trace!( "bad response from {}! method={} params={:?} err={:?}", self.rpc, @@ -356,21 +358,21 @@ impl OpenRequestHandle { err ); } - RequestRevertHandler::ErrorLevel => { + RequestErrorHandler::ErrorLevel => { // TODO: include params if not running in release mode error!( "bad response from {}! method={} err={:?}", self.rpc, method, err ); } - RequestRevertHandler::WarnLevel => { + RequestErrorHandler::WarnLevel => { // TODO: include params if not running in release mode warn!( "bad response from {}! method={} err={:?}", self.rpc, method, err ); } - RequestRevertHandler::Save => { + RequestErrorHandler::Save => { trace!( "bad response from {}! method={} params={:?} err={:?}", self.rpc,