use None if on head block

This commit is contained in:
Bryan Stitt 2023-01-04 12:12:44 -08:00
parent 045065986a
commit 664ecf5924
2 changed files with 47 additions and 46 deletions

View File

@ -10,7 +10,6 @@ use axum::{response::IntoResponse, Extension, Json};
use axum_client_ip::ClientIp; use axum_client_ip::ClientIp;
use axum_macros::debug_handler; use axum_macros::debug_handler;
use itertools::Itertools; use itertools::Itertools;
use log::debug;
use std::sync::Arc; use std::sync::Arc;
/// POST /rpc -- Public entrypoint for HTTP JSON-RPC requests. Web3 wallets use this. /// POST /rpc -- Public entrypoint for HTTP JSON-RPC requests. Web3 wallets use this.

View File

@ -415,60 +415,62 @@ impl Web3Connections {
skip: &[Arc<Web3Connection>], skip: &[Arc<Web3Connection>],
min_block_needed: Option<&U64>, min_block_needed: Option<&U64>,
) -> anyhow::Result<OpenRequestResult> { ) -> anyhow::Result<OpenRequestResult> {
let usable_rpcs_by_head_num_and_weight: BTreeMap<(U64, u64), Vec<Arc<Web3Connection>>> = let usable_rpcs_by_head_num_and_weight: BTreeMap<
if let Some(min_block_needed) = min_block_needed { (Option<U64>, u64),
// need a potentially old block. check all the rpcs Vec<Arc<Web3Connection>>,
let mut m = BTreeMap::new(); > = if let Some(min_block_needed) = min_block_needed {
// need a potentially old block. check all the rpcs
let mut m = BTreeMap::new();
for x in self for x in self
.conns .conns
.values() .values()
.filter(|x| !skip.contains(x)) .filter(|x| !skip.contains(x))
.filter(|x| x.has_block_data(min_block_needed)) .filter(|x| x.has_block_data(min_block_needed))
.cloned() .cloned()
{ {
let x_head_block = x.head_block.read().clone(); let x_head_block = x.head_block.read().clone();
match x_head_block { match x_head_block {
None => continue, None => continue,
Some(x_head) => { Some(x_head) => {
let key = (Some(x_head.number()), u64::MAX - x.tier); let key = (Some(x_head.number()), u64::MAX - x.tier);
m.entry(key).or_insert_with(Vec::new).push(x); m.entry(key).or_insert_with(Vec::new).push(x);
}
} }
} }
}
m m
} else { } else {
// need latest. filter the synced rpcs // need latest. filter the synced rpcs
let synced_connections = self.synced_connections.load(); let synced_connections = self.synced_connections.load();
let head_block = match synced_connections.head_block.as_ref() { let head_block = match synced_connections.head_block.as_ref() {
None => return Ok(OpenRequestResult::NotReady), None => return Ok(OpenRequestResult::NotReady),
Some(x) => x, Some(x) => x,
};
// TODO: self.allowed_lag instead of taking as an arg
if head_block.syncing(allowed_lag) {
return Ok(OpenRequestResult::NotReady);
}
let mut m = BTreeMap::new();
for x in synced_connections
.conns
.iter()
.filter(|x| !skip.contains(x))
{
let key = (None, u64::MAX - x.tier);
m.entry(key).or_insert_with(Vec::new).push(x.clone());
}
m
}; };
// TODO: self.allowed_lag instead of taking as an arg
if head_block.syncing(allowed_lag) {
return Ok(OpenRequestResult::NotReady);
}
let mut m = BTreeMap::new();
for x in synced_connections
.conns
.iter()
.filter(|x| !skip.contains(x))
{
let key = (None, u64::MAX - x.tier);
m.entry(key).or_insert_with(Vec::new).push(x.clone());
}
m
};
let mut earliest_retry_at = None; let mut earliest_retry_at = None;
for usable_rpcs in usable_rpcs_by_head_num_and_weight.into_values().rev() { for usable_rpcs in usable_rpcs_by_head_num_and_weight.into_values().rev() {