Merge remote-tracking branch 'origin/main' into devel

This commit is contained in:
Bryan Stitt 2023-05-29 09:42:21 -07:00
commit b05d866f8a
3 changed files with 39 additions and 32 deletions

View File

@ -7,6 +7,7 @@ use crate::jsonrpc::{JsonRpcForwardedResponse, JsonRpcRequest};
use crate::rpcs::one::Web3Rpc; use crate::rpcs::one::Web3Rpc;
use crate::stats::{AppStat, BackendRequests, RpcQueryStats}; use crate::stats::{AppStat, BackendRequests, RpcQueryStats};
use crate::user_token::UserBearerToken; use crate::user_token::UserBearerToken;
use anyhow::Context;
use axum::headers::authorization::Bearer; use axum::headers::authorization::Bearer;
use axum::headers::{Header, Origin, Referer, UserAgent}; use axum::headers::{Header, Origin, Referer, UserAgent};
use chrono::Utc; use chrono::Utc;
@ -1112,20 +1113,20 @@ impl Web3ProxyApp {
let user_model = user::Entity::find_by_id(rpc_key_model.user_id) let user_model = user::Entity::find_by_id(rpc_key_model.user_id)
.one(db_replica.conn()) .one(db_replica.conn())
.await? .await?
.expect("related user"); .context("no related user")?;
let balance = balance::Entity::find() let balance = balance::Entity::find()
.filter(balance::Column::UserId.eq(user_model.id)) .filter(balance::Column::UserId.eq(user_model.id))
.one(db_replica.conn()) .one(db_replica.conn())
.await? .await?
.expect("related balance") .map(|x| x.available_balance)
.available_balance; .unwrap_or_default();
let user_tier_model = let user_tier_model =
user_tier::Entity::find_by_id(user_model.user_tier_id) user_tier::Entity::find_by_id(user_model.user_tier_id)
.one(db_replica.conn()) .one(db_replica.conn())
.await? .await?
.expect("related user tier"); .context("no related user tier")?;
let allowed_ips: Option<Vec<IpNet>> = let allowed_ips: Option<Vec<IpNet>> =
if let Some(allowed_ips) = rpc_key_model.allowed_ips { if let Some(allowed_ips) = rpc_key_model.allowed_ips {

View File

@ -31,12 +31,15 @@ pub async fn health(
Extension(app): Extension<Arc<Web3ProxyApp>>, Extension(app): Extension<Arc<Web3ProxyApp>>,
Extension(cache): Extension<Arc<ResponseCache>>, Extension(cache): Extension<Arc<ResponseCache>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let (code, content_type, body) = cache // let (code, content_type, body) = cache
.get_or_insert_async::<Infallible, _>(&ResponseCacheKey::Health, async move { // .get_or_insert_async::<Infallible, _>(&ResponseCacheKey::Health, async move {
Ok(_health(app).await) // Ok(_health(app).await)
}) // })
.await // .await
.expect("this cache get is infallible"); // .expect("this cache get is infallible");
// TODO: cache this once new TTLs work
let (code, content_type, body) = _health(app).await;
Response::builder() Response::builder()
.status(code) .status(code)
@ -65,12 +68,15 @@ pub async fn backups_needed(
Extension(app): Extension<Arc<Web3ProxyApp>>, Extension(app): Extension<Arc<Web3ProxyApp>>,
Extension(cache): Extension<Arc<ResponseCache>>, Extension(cache): Extension<Arc<ResponseCache>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let (code, content_type, body) = cache // let (code, content_type, body) = cache
.get_or_insert_async::<Infallible, _>(&ResponseCacheKey::BackupsNeeded, async move { // .get_or_insert_async::<Infallible, _>(&ResponseCacheKey::BackupsNeeded, async move {
Ok(_backups_needed(app).await) // Ok(_backups_needed(app).await)
}) // })
.await // .await
.expect("this cache get is infallible"); // .expect("this cache get is infallible");
// TODO: cache this once new TTLs work
let (code, content_type, body) = _backups_needed(app).await;
Response::builder() Response::builder()
.status(code) .status(code)
@ -115,12 +121,15 @@ pub async fn status(
Extension(app): Extension<Arc<Web3ProxyApp>>, Extension(app): Extension<Arc<Web3ProxyApp>>,
Extension(cache): Extension<Arc<ResponseCache>>, Extension(cache): Extension<Arc<ResponseCache>>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let (code, content_type, body) = cache // let (code, content_type, body) = cache
.get_or_insert_async::<Infallible, _>(&ResponseCacheKey::Status, async move { // .get_or_insert_async::<Infallible, _>(&ResponseCacheKey::Status, async move {
Ok(_status(app).await) // Ok(_status(app).await)
}) // })
.await // .await
.expect("this cache get is infallible"); // .expect("this cache get is infallible");
// TODO: cache this once new TTLs work
let (code, content_type, body) = _status(app).await;
Response::builder() Response::builder()
.status(code) .status(code)

View File

@ -138,7 +138,7 @@ impl ConsensusWeb3Rpcs {
let head_num = self.head_block.number(); let head_num = self.head_block.number();
if Some(head_num) >= needed_block_num { if Some(head_num) >= needed_block_num {
debug!("best (head) block: {}", head_num); trace!("best (head) block: {}", head_num);
return ShouldWaitForBlock::Ready; return ShouldWaitForBlock::Ready;
} }
} }
@ -153,15 +153,14 @@ impl ConsensusWeb3Rpcs {
.iter() .iter()
.any(|rpc| self.rpc_will_work_eventually(rpc, needed_block_num, skip_rpcs)) .any(|rpc| self.rpc_will_work_eventually(rpc, needed_block_num, skip_rpcs))
{ {
// TODO: too verbose trace!("everything in this ranking ({:?}) is skipped", next_ranking);
debug!("everything in this ranking ({:?}) is skipped", next_ranking);
continue; continue;
} }
let next_head_num = next_ranking.head_num.as_ref(); let next_head_num = next_ranking.head_num.as_ref();
if next_head_num >= needed_block_num { if next_head_num >= needed_block_num {
debug!("best (head) block: {:?}", next_head_num); trace!("best (head) block: {:?}", next_head_num);
return ShouldWaitForBlock::Ready; return ShouldWaitForBlock::Ready;
} }
@ -170,14 +169,12 @@ impl ConsensusWeb3Rpcs {
// TODO: this seems wrong // TODO: this seems wrong
if best_num.is_some() { if best_num.is_some() {
// TODO: too verbose trace!("best (old) block: {:?}", best_num);
debug!("best (old) block: {:?}", best_num);
ShouldWaitForBlock::Wait { ShouldWaitForBlock::Wait {
current: best_num.copied(), current: best_num.copied(),
} }
} else { } else {
// TODO: too verbose trace!("never ready");
debug!("never ready");
ShouldWaitForBlock::NeverReady ShouldWaitForBlock::NeverReady
} }
} }
@ -212,10 +209,10 @@ impl ConsensusWeb3Rpcs {
Ordering::Greater | Ordering::Equal => { Ordering::Greater | Ordering::Equal => {
// rpc is synced past the needed block. make sure the block isn't too old for it // rpc is synced past the needed block. make sure the block isn't too old for it
if self.has_block_data(rpc, needed_block_num) { if self.has_block_data(rpc, needed_block_num) {
debug!("{} has {}", rpc, needed_block_num); trace!("{} has {}", rpc, needed_block_num);
return true; return true;
} else { } else {
debug!("{} does not have {}", rpc, needed_block_num); trace!("{} does not have {}", rpc, needed_block_num);
return false; return false;
} }
} }