rename revert_handler to error_handler

This commit is contained in:
Bryan Stitt 2023-04-05 15:34:28 -07:00
parent 072119901e
commit b460f1701a
3 changed files with 35 additions and 33 deletions

@ -2,7 +2,7 @@
use super::blockchain::{BlocksByHashCache, Web3ProxyBlock}; use super::blockchain::{BlocksByHashCache, Web3ProxyBlock};
use super::consensus::ConsensusWeb3Rpcs; use super::consensus::ConsensusWeb3Rpcs;
use super::one::Web3Rpc; use super::one::Web3Rpc;
use super::request::{OpenRequestHandle, OpenRequestResult, RequestRevertHandler}; use super::request::{OpenRequestHandle, OpenRequestResult, RequestErrorHandler};
use crate::app::{flatten_handle, AnyhowJoinHandle, Web3ProxyApp}; use crate::app::{flatten_handle, AnyhowJoinHandle, Web3ProxyApp};
///! Load balanced communication with a group of web3 providers ///! Load balanced communication with a group of web3 providers
use crate::config::{BlockAndRpc, TxHashAndRpc, Web3RpcConfig}; use crate::config::{BlockAndRpc, TxHashAndRpc, Web3RpcConfig};
@ -877,7 +877,7 @@ impl Web3Rpcs {
.request( .request(
&request.method, &request.method,
&json!(request.params), &json!(request.params),
RequestRevertHandler::Save, RequestErrorHandler::Save,
None, None,
) )
.await; .await;

@ -6,7 +6,7 @@ use crate::app::{flatten_handle, AnyhowJoinHandle};
use crate::config::{BlockAndRpc, Web3RpcConfig}; use crate::config::{BlockAndRpc, Web3RpcConfig};
use crate::frontend::authorization::Authorization; use crate::frontend::authorization::Authorization;
use crate::frontend::errors::{Web3ProxyError, Web3ProxyResult}; use crate::frontend::errors::{Web3ProxyError, Web3ProxyResult};
use crate::rpcs::request::RequestRevertHandler; use crate::rpcs::request::RequestErrorHandler;
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use ethers::prelude::{Bytes, Middleware, ProviderError, TxHash, H256, U64}; use ethers::prelude::{Bytes, Middleware, ProviderError, TxHash, H256, U64};
use ethers::types::{Address, Transaction, U256}; use ethers::types::{Address, Transaction, U256};
@ -716,9 +716,9 @@ impl Web3Rpc {
tx_id_sender: Option<flume::Sender<(TxHash, Arc<Self>)>>, tx_id_sender: Option<flume::Sender<(TxHash, Arc<Self>)>>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let error_handler = if self.backup { let error_handler = if self.backup {
RequestRevertHandler::DebugLevel RequestErrorHandler::DebugLevel
} else { } else {
RequestRevertHandler::ErrorLevel RequestErrorHandler::ErrorLevel
}; };
let mut delay_start = false; let mut delay_start = false;
@ -1331,7 +1331,7 @@ impl Web3Rpc {
self: &Arc<Self>, self: &Arc<Self>,
method: &str, method: &str,
params: &P, params: &P,
revert_handler: RequestRevertHandler, revert_handler: RequestErrorHandler,
authorization: Arc<Authorization>, authorization: Arc<Authorization>,
unlocked_provider: Option<Arc<Web3Provider>>, unlocked_provider: Option<Arc<Web3Provider>>,
) -> anyhow::Result<R> ) -> anyhow::Result<R>

@ -33,9 +33,9 @@ pub struct OpenRequestHandle {
rpc: Arc<Web3Rpc>, rpc: Arc<Web3Rpc>,
} }
/// Depending on the context, RPC errors can require different handling. /// Depending on the context, RPC errors require different handling.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
pub enum RequestRevertHandler { pub enum RequestErrorHandler {
/// Log at the trace level. Use when errors are expected. /// Log at the trace level. Use when errors are expected.
TraceLevel, TraceLevel,
/// Log at the debug level. Use when errors are expected. /// Log at the debug level. Use when errors are expected.
@ -58,13 +58,13 @@ struct EthCallFirstParams {
data: Option<Bytes>, data: Option<Bytes>,
} }
impl From<Level> for RequestRevertHandler { impl From<Level> for RequestErrorHandler {
fn from(level: Level) -> Self { fn from(level: Level) -> Self {
match level { match level {
Level::Trace => RequestRevertHandler::TraceLevel, Level::Trace => RequestErrorHandler::TraceLevel,
Level::Debug => RequestRevertHandler::DebugLevel, Level::Debug => RequestErrorHandler::DebugLevel,
Level::Error => RequestRevertHandler::ErrorLevel, Level::Error => RequestErrorHandler::ErrorLevel,
Level::Warn => RequestRevertHandler::WarnLevel, Level::Warn => RequestErrorHandler::WarnLevel,
_ => unimplemented!("unexpected tracing Level"), _ => unimplemented!("unexpected tracing Level"),
} }
} }
@ -80,7 +80,7 @@ impl Authorization {
let rpc_key_id = match self.checks.rpc_secret_key_id { let rpc_key_id = match self.checks.rpc_secret_key_id {
Some(rpc_key_id) => rpc_key_id.into(), Some(rpc_key_id) => rpc_key_id.into(),
None => { None => {
// // trace!(?self, "cannot save revert without rpc_key_id"); // trace!(?self, "cannot save revert without rpc_key_id");
return Ok(()); return Ok(());
} }
}; };
@ -150,7 +150,7 @@ impl OpenRequestHandle {
self, self,
method: &str, method: &str,
params: &P, params: &P,
revert_handler: RequestRevertHandler, mut error_handler: RequestErrorHandler,
unlocked_provider: Option<Arc<Web3Provider>>, unlocked_provider: Option<Arc<Web3Provider>>,
) -> Result<R, ProviderError> ) -> Result<R, ProviderError>
where where
@ -229,44 +229,45 @@ impl OpenRequestHandle {
if let Err(err) = &response { if let Err(err) = &response {
// only save reverts for some types of calls // only save reverts for some types of calls
// TODO: do something special for eth_sendRawTransaction too // 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? // TODO: should all these be Trace or Debug or a mix?
if !["eth_call", "eth_estimateGas"].contains(&method) { if !["eth_call", "eth_estimateGas"].contains(&method) {
// trace!(%method, "skipping save on revert"); // trace!(%method, "skipping save on revert");
RequestRevertHandler::TraceLevel RequestErrorHandler::TraceLevel
} else if self.authorization.db_conn.is_some() { } else if self.authorization.db_conn.is_some() {
let log_revert_chance = self.authorization.checks.log_revert_chance; let log_revert_chance = self.authorization.checks.log_revert_chance;
if log_revert_chance == 0.0 { if log_revert_chance == 0.0 {
// trace!(%method, "no chance. skipping save on revert"); // trace!(%method, "no chance. skipping save on revert");
RequestRevertHandler::TraceLevel RequestErrorHandler::TraceLevel
} else if log_revert_chance == 1.0 { } else if log_revert_chance == 1.0 {
// trace!(%method, "gaurenteed chance. SAVING on revert"); // 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) } else if thread_fast_rng::thread_fast_rng().gen_range(0.0f64..=1.0)
< log_revert_chance < log_revert_chance
{ {
// trace!(%method, "missed chance. skipping save on revert"); // trace!(%method, "missed chance. skipping save on revert");
RequestRevertHandler::TraceLevel RequestErrorHandler::TraceLevel
} else { } else {
// trace!("Saving on revert"); // trace!("Saving on revert");
// TODO: is always logging at debug level fine? // TODO: is always logging at debug level fine?
revert_handler error_handler
} }
} else { } else {
// trace!(%method, "no database. skipping save on revert"); // trace!(%method, "no database. skipping save on revert");
RequestRevertHandler::TraceLevel RequestErrorHandler::TraceLevel
} }
} else { } else {
revert_handler error_handler
}; };
// TODO: simple enum -> string derive? // TODO: simple enum -> string derive?
// TODO: if ProviderError::UnsupportedRpc, we should retry on another server
#[derive(Debug)] #[derive(Debug)]
enum ResponseTypes { enum ResponseTypes {
Revert, Revert,
RateLimit, RateLimit,
Ok, Error,
} }
// check for "execution reverted" here // check for "execution reverted" here
@ -317,17 +318,18 @@ impl OpenRequestHandle {
trace!("rate limit from {}", self.rpc); trace!("rate limit from {}", self.rpc);
ResponseTypes::RateLimit ResponseTypes::RateLimit
} else { } else {
ResponseTypes::Ok ResponseTypes::Error
} }
} else { } else {
ResponseTypes::Ok ResponseTypes::Error
} }
} else { } else {
ResponseTypes::Ok ResponseTypes::Error
}; };
if matches!(response_type, ResponseTypes::RateLimit) { if matches!(response_type, ResponseTypes::RateLimit) {
if let Some(hard_limit_until) = self.rpc.hard_limit_until.as_ref() { 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); let retry_at = Instant::now() + Duration::from_secs(1);
trace!("retry {} at: {:?}", self.rpc, retry_at); 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 // TODO: think more about the method and param logs. those can be sensitive information
match revert_handler { match error_handler {
RequestRevertHandler::DebugLevel => { RequestErrorHandler::DebugLevel => {
// TODO: think about this revert check more. sometimes we might want reverts logged so this needs a flag // TODO: think about this revert check more. sometimes we might want reverts logged so this needs a flag
if matches!(response_type, ResponseTypes::Revert) { if matches!(response_type, ResponseTypes::Revert) {
debug!( debug!(
@ -347,7 +349,7 @@ impl OpenRequestHandle {
); );
} }
} }
RequestRevertHandler::TraceLevel => { RequestErrorHandler::TraceLevel => {
trace!( trace!(
"bad response from {}! method={} params={:?} err={:?}", "bad response from {}! method={} params={:?} err={:?}",
self.rpc, self.rpc,
@ -356,21 +358,21 @@ impl OpenRequestHandle {
err err
); );
} }
RequestRevertHandler::ErrorLevel => { RequestErrorHandler::ErrorLevel => {
// TODO: include params if not running in release mode // TODO: include params if not running in release mode
error!( error!(
"bad response from {}! method={} err={:?}", "bad response from {}! method={} err={:?}",
self.rpc, method, err self.rpc, method, err
); );
} }
RequestRevertHandler::WarnLevel => { RequestErrorHandler::WarnLevel => {
// TODO: include params if not running in release mode // TODO: include params if not running in release mode
warn!( warn!(
"bad response from {}! method={} err={:?}", "bad response from {}! method={} err={:?}",
self.rpc, method, err self.rpc, method, err
); );
} }
RequestRevertHandler::Save => { RequestErrorHandler::Save => {
trace!( trace!(
"bad response from {}! method={} params={:?} err={:?}", "bad response from {}! method={} params={:?} err={:?}",
self.rpc, self.rpc,