fix from_rpcs

This commit is contained in:
Bryan Stitt 2023-10-07 10:06:01 -07:00
parent bb9c8f297e
commit b2ab5bd6d6
2 changed files with 17 additions and 8 deletions

View File

@ -129,16 +129,24 @@ pub struct RpcsForRequest {
}
impl RankedRpcs {
pub fn from_rpcs(rpcs: Vec<Arc<Web3Rpc>>, head_block: Web3ProxyBlock) -> Option<Self> {
pub fn from_rpcs(rpcs: Vec<Arc<Web3Rpc>>, head_block: Option<Web3ProxyBlock>) -> Option<Self> {
// we don't need to sort the rpcs now. we will sort them when a request neds them
// TODO: the shame about this is that we lose just being able to compare 2 random servers
let head_block = head_block?;
let backups_needed = rpcs.iter().any(|x| x.backup);
let num_synced = rpcs.len();
// TODO: do we need real data in here? if we are calling from_rpcs, we probably don't even track their block
let rpc_data = Default::default();
let mut rpc_data = HashMap::<Arc<Web3Rpc>, ConsensusRpcData>::with_capacity(num_synced);
for rpc in rpcs.iter().cloned() {
let data = ConsensusRpcData::new(&rpc, &head_block);
rpc_data.insert(rpc, data);
}
let sort_mode = SortMethod::Shuffle;

View File

@ -445,20 +445,21 @@ impl Web3Rpcs {
let ranked_rpcs: Arc<RankedRpcs> =
if let Some(ranked_rpcs) = self.watch_ranked_rpcs.borrow().clone() {
ranked_rpcs
} else if let Some(head_block) = web3_request.head_block.clone() {
// if we are here, self isn't watching head blocks but some other Web3Rpcs is. Return all the rpcs
} else if self.watch_head_block.is_some() {
// if we are here, this set of rpcs is subscribed to newHeads. But we didn't get a RankedRpcs. that means something is wrong
return Err(Web3ProxyError::NoServersSynced);
} else {
// no RankedRpcs, but also no newHeads subscription. This is probably a set of "protected" rpcs or similar
// TODO: return a future that resolves once we do have something?
let rpcs = self.by_name.read().values().cloned().collect();
if let Some(x) = RankedRpcs::from_rpcs(rpcs, head_block) {
if let Some(x) = RankedRpcs::from_rpcs(rpcs, web3_request.head_block.clone()) {
Arc::new(x)
} else {
// i doubt we will ever get here
// TODO: return a future that resolves once we do have something?
return Err(Web3ProxyError::NoServersSynced);
}
} else {
// TODO: return a future that resolves once we do have something?
return Err(Web3ProxyError::NoServersSynced);
};
match ranked_rpcs.for_request(web3_request) {