From 05e94ff81cda33677b20d4495b76f0911e3ed514 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 12 Sep 2022 14:33:55 +0000 Subject: [PATCH] send UNAUTHORIZED to unknown keys --- web3_proxy/src/frontend/errors.rs | 9 +++++++++ web3_proxy/src/frontend/http.rs | 3 ++- web3_proxy/src/frontend/rate_limit.rs | 3 +++ web3_proxy/src/rpcs/blockchain.rs | 10 +++++----- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/web3_proxy/src/frontend/errors.rs b/web3_proxy/src/frontend/errors.rs index 6381e953..5ab08b26 100644 --- a/web3_proxy/src/frontend/errors.rs +++ b/web3_proxy/src/frontend/errors.rs @@ -24,6 +24,7 @@ pub enum FrontendErrorResponse { Database(DbErr), RateLimitedUser(u64, Option), RateLimitedIp(IpAddr, Option), + UnknownKey, NotFound, } @@ -117,6 +118,14 @@ impl IntoResponse for FrontendErrorResponse { ), ) } + Self::UnknownKey => ( + StatusCode::UNAUTHORIZED, + JsonRpcForwardedResponse::from_str( + "unknown api key!", + Some(StatusCode::UNAUTHORIZED.as_u16().into()), + None, + ), + ), Self::NotFound => { // TODO: emit a stat? ( diff --git a/web3_proxy/src/frontend/http.rs b/web3_proxy/src/frontend/http.rs index 4cc7df64..3a1714a9 100644 --- a/web3_proxy/src/frontend/http.rs +++ b/web3_proxy/src/frontend/http.rs @@ -8,6 +8,7 @@ use tracing::instrument; /// Health check page for load balancers to use #[instrument(skip_all)] pub async fn health(Extension(app): Extension>) -> impl IntoResponse { + // TODO: also check that the head block is not too old if app.balanced_rpcs.synced() { (StatusCode::OK, "OK") } else { @@ -26,10 +27,10 @@ pub async fn prometheus(Extension(app): Extension>) -> impl In /// TODO: replace this with proper stats and monitoring #[instrument(skip_all)] pub async fn status(Extension(app): Extension>) -> impl IntoResponse { - // TODO: what else should we include? uptime? app.pending_transactions.sync(); app.user_cache.sync(); + // TODO: what else should we include? uptime, cache hit rates, cpu load let body = json!({ "pending_transactions_count": app.pending_transactions.entry_count(), "pending_transactions_size": app.pending_transactions.weighted_size(), diff --git a/web3_proxy/src/frontend/rate_limit.rs b/web3_proxy/src/frontend/rate_limit.rs index e07cfb70..4aeb00d4 100644 --- a/web3_proxy/src/frontend/rate_limit.rs +++ b/web3_proxy/src/frontend/rate_limit.rs @@ -29,6 +29,7 @@ pub async fn rate_limit_by_ip( RateLimitResult::RateLimitedIp(x, retry_at) => { Err(FrontendErrorResponse::RateLimitedIp(x, retry_at)) } + // TODO: don't panic. give the user an error x => unimplemented!("rate_limit_by_ip shouldn't ever see these: {:?}", x), } } @@ -43,6 +44,8 @@ pub async fn rate_limit_by_key( RateLimitResult::RateLimitedUser(x, retry_at) => { Err(FrontendErrorResponse::RateLimitedUser(x, retry_at)) } + RateLimitResult::UnknownKey => Err(FrontendErrorResponse::UnknownKey), + // TODO: don't panic. give the user an error x => unimplemented!("rate_limit_by_key shouldn't ever see these: {:?}", x), } } diff --git a/web3_proxy/src/rpcs/blockchain.rs b/web3_proxy/src/rpcs/blockchain.rs index 28c1cfb6..c404955d 100644 --- a/web3_proxy/src/rpcs/blockchain.rs +++ b/web3_proxy/src/rpcs/blockchain.rs @@ -281,19 +281,19 @@ impl Web3Connections { // iterate the known heads to find the highest_work_block let mut checked_heads = HashSet::new(); let mut highest_work_block: Option = None; - for (conn_name, conn_head_hash) in connection_heads.iter() { - if checked_heads.contains(conn_head_hash) { + for (conn_name, connection_head_hash) in connection_heads.iter() { + if checked_heads.contains(connection_head_hash) { // we already checked this head from another rpc continue; } // don't check the same hash multiple times - checked_heads.insert(conn_head_hash); + checked_heads.insert(connection_head_hash); - let conn_head_block = if let Some(x) = self.block_hashes.get(conn_head_hash) { + let conn_head_block = if let Some(x) = self.block_hashes.get(connection_head_hash) { x } else { // TODO: why does this happen? - warn!(%conn_head_hash, %conn_name, %rpc, "Missing block in connection_heads"); + warn!(%connection_head_hash, %conn_name, %rpc, "Missing connection_head_block in block_hashes"); continue; };