From 992860a46d768b6d615cbe39c48fe53f1e815fce Mon Sep 17 00:00:00 2001 From: yenicelik Date: Sun, 25 Jun 2023 16:18:09 -0400 Subject: [PATCH 1/3] forgot to allow aggregate global stats --- web3_proxy/src/stats/influxdb_queries.rs | 88 ++++++++++++------------ 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/web3_proxy/src/stats/influxdb_queries.rs b/web3_proxy/src/stats/influxdb_queries.rs index e1f432ee..3a592fce 100644 --- a/web3_proxy/src/stats/influxdb_queries.rs +++ b/web3_proxy/src/stats/influxdb_queries.rs @@ -57,55 +57,57 @@ pub async fn query_user_stats<'a>( // In any case, we don't allow stats if the target user does not have a balance // No subuser, we can check the balance directly - match balance::Entity::find() - .filter(balance::Column::UserId.eq(user_id)) - .one(db_replica.as_ref()) - .await? - { - // TODO: We should add the threshold that determines if a user is premium into app.config or so - Some(user_balance) => { - if user_balance.total_spent_outside_free_tier - user_balance.total_deposits - < Decimal::from(0) - { - trace!("User has 0 balance"); - return Err(Web3ProxyError::PaymentRequired); - } - // Otherwise make the user pass - } - None => { - trace!("User does not have a balance record, implying that he has no balance. Users must have a balance to access their stats dashboards"); - return Err(Web3ProxyError::PaymentRequired); - } - } - - // (Possible) subuser relation - // Check if the caller is a proper subuser (there is a subuser record) - // Check if the subuser has more than collaborator status - if user_id != caller_user_id { - // Find all rpc-keys related to the caller user - let user_rpc_keys: Vec = rpc_key::Entity::find() - .filter(rpc_key::Column::UserId.eq(user_id)) - .all(db_replica.as_ref()) - .await? - .into_iter() - .map(|x| x.id) - .collect::>(); - - match secondary_user::Entity::find() - .filter(secondary_user::Column::UserId.eq(caller_user_id)) - .filter(secondary_user::Column::RpcSecretKeyId.is_in(user_rpc_keys)) + if user_id != 0 { + match balance::Entity::find() + .filter(balance::Column::UserId.eq(user_id)) .one(db_replica.as_ref()) .await? { - Some(secondary_user_record) => { - if secondary_user_record.role == Role::Collaborator { - trace!("Subuser is only a collaborator, collaborators cannot see stats"); - return Err(Web3ProxyError::AccessDenied); + // TODO: We should add the threshold that determines if a user is premium into app.config or so + Some(user_balance) => { + if user_balance.total_spent_outside_free_tier - user_balance.total_deposits + < Decimal::from(0) + { + trace!("User has 0 balance"); + return Err(Web3ProxyError::PaymentRequired); } + // Otherwise make the user pass } None => { - // Then we must do an access denied - return Err(Web3ProxyError::AccessDeniedNoSubuser); + trace!("User does not have a balance record, implying that he has no balance. Users must have a balance to access their stats dashboards"); + return Err(Web3ProxyError::PaymentRequired); + } + } + + // (Possible) subuser relation + // Check if the caller is a proper subuser (there is a subuser record) + // Check if the subuser has more than collaborator status + if user_id != caller_user_id { + // Find all rpc-keys related to the caller user + let user_rpc_keys: Vec = rpc_key::Entity::find() + .filter(rpc_key::Column::UserId.eq(user_id)) + .all(db_replica.as_ref()) + .await? + .into_iter() + .map(|x| x.id) + .collect::>(); + + match secondary_user::Entity::find() + .filter(secondary_user::Column::UserId.eq(caller_user_id)) + .filter(secondary_user::Column::RpcSecretKeyId.is_in(user_rpc_keys)) + .one(db_replica.as_ref()) + .await? + { + Some(secondary_user_record) => { + if secondary_user_record.role == Role::Collaborator { + trace!("Subuser is only a collaborator, collaborators cannot see stats"); + return Err(Web3ProxyError::AccessDenied); + } + } + None => { + // Then we must do an access denied + return Err(Web3ProxyError::AccessDeniedNoSubuser); + } } } } From 32156e981df59c49df878515032bfc7c7811bbea Mon Sep 17 00:00:00 2001 From: yenicelik Date: Sun, 25 Jun 2023 17:19:31 -0400 Subject: [PATCH 2/3] adding balances --- web3_proxy/src/stats/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/web3_proxy/src/stats/mod.rs b/web3_proxy/src/stats/mod.rs index db7ea861..fe2a1c52 100644 --- a/web3_proxy/src/stats/mod.rs +++ b/web3_proxy/src/stats/mod.rs @@ -725,6 +725,8 @@ impl BufferedRpcQueryStats { // Read the latest balance ... let remaining = self.latest_balance.read().remaining(); + trace!("Remaining balance for influx is {:?}", remaining); + builder = builder .tag("archive_needed", key.archive_needed.to_string()) .tag("error_response", key.error_response.to_string()) @@ -744,13 +746,17 @@ impl BufferedRpcQueryStats { ) .field( "balance", - remaining.to_f64().context("balance is really (too) large")?, + remaining + .to_f64() + .context("balance is really (too) large")?, ); builder = builder.timestamp(key.response_timestamp); let point = builder.build()?; + trace!("Datapoint saving to Influx is {:?}", point); + Ok(point) } } From 88617dc72be325bfaaa77982a5b616e20dfea70b Mon Sep 17 00:00:00 2001 From: yenicelik Date: Sun, 25 Jun 2023 17:25:35 -0400 Subject: [PATCH 3/3] added more tracing for balance --- web3_proxy/src/frontend/authorization.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/web3_proxy/src/frontend/authorization.rs b/web3_proxy/src/frontend/authorization.rs index f28ac8de..90c69608 100644 --- a/web3_proxy/src/frontend/authorization.rs +++ b/web3_proxy/src/frontend/authorization.rs @@ -1192,6 +1192,7 @@ impl Web3ProxyApp { total_deposit: x.total_deposits, total_spend: x.total_spent_outside_free_tier, }; + trace!("Balance for cache retrieved from database is {:?}", x); return Ok(Arc::new(RwLock::new(x))); }