add max wait to fix tests
This commit is contained in:
parent
dafb69fae1
commit
b1a0bcac57
@ -153,7 +153,7 @@ pub async fn user_balance_post(
|
|||||||
// Just make an rpc request, idk if i need to call this super extensive code
|
// Just make an rpc request, idk if i need to call this super extensive code
|
||||||
let transaction_receipt: TransactionReceipt = match app
|
let transaction_receipt: TransactionReceipt = match app
|
||||||
.balanced_rpcs
|
.balanced_rpcs
|
||||||
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None)
|
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None, None)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(OpenRequestResult::Handle(handle)) => {
|
Ok(OpenRequestResult::Handle(handle)) => {
|
||||||
@ -187,7 +187,7 @@ pub async fn user_balance_post(
|
|||||||
debug!("Transaction receipt is: {:?}", transaction_receipt);
|
debug!("Transaction receipt is: {:?}", transaction_receipt);
|
||||||
let accepted_token: Address = match app
|
let accepted_token: Address = match app
|
||||||
.balanced_rpcs
|
.balanced_rpcs
|
||||||
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None)
|
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None, None)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(OpenRequestResult::Handle(handle)) => {
|
Ok(OpenRequestResult::Handle(handle)) => {
|
||||||
@ -242,7 +242,7 @@ pub async fn user_balance_post(
|
|||||||
debug!("Accepted token is: {:?}", accepted_token);
|
debug!("Accepted token is: {:?}", accepted_token);
|
||||||
let decimals: u32 = match app
|
let decimals: u32 = match app
|
||||||
.balanced_rpcs
|
.balanced_rpcs
|
||||||
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None)
|
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None, None)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(OpenRequestResult::Handle(handle)) => {
|
Ok(OpenRequestResult::Handle(handle)) => {
|
||||||
|
@ -34,6 +34,7 @@ use std::fmt::{self, Display};
|
|||||||
use std::sync::atomic::Ordering;
|
use std::sync::atomic::Ordering;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use thread_fast_rng::rand::seq::SliceRandom;
|
use thread_fast_rng::rand::seq::SliceRandom;
|
||||||
|
use tokio::select;
|
||||||
use tokio::sync::{broadcast, watch};
|
use tokio::sync::{broadcast, watch};
|
||||||
use tokio::time::{sleep, sleep_until, Duration, Instant};
|
use tokio::time::{sleep, sleep_until, Duration, Instant};
|
||||||
|
|
||||||
@ -485,6 +486,7 @@ impl Web3Rpcs {
|
|||||||
skip_rpcs: &mut Vec<Arc<Web3Rpc>>,
|
skip_rpcs: &mut Vec<Arc<Web3Rpc>>,
|
||||||
min_block_needed: Option<&U64>,
|
min_block_needed: Option<&U64>,
|
||||||
max_block_needed: Option<&U64>,
|
max_block_needed: Option<&U64>,
|
||||||
|
max_wait: Option<Duration>,
|
||||||
) -> Web3ProxyResult<OpenRequestResult> {
|
) -> Web3ProxyResult<OpenRequestResult> {
|
||||||
let mut earliest_retry_at: Option<Instant> = None;
|
let mut earliest_retry_at: Option<Instant> = None;
|
||||||
|
|
||||||
@ -526,16 +528,14 @@ impl Web3Rpcs {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let start = Instant::now();
|
let stop_trying_at =
|
||||||
|
Instant::now() + max_wait.unwrap_or_else(|| Duration::from_secs(10));
|
||||||
// TODO: get from config or argument
|
|
||||||
let max_wait = Duration::from_secs(10);
|
|
||||||
|
|
||||||
let mut watch_consensus_rpcs = self.watch_consensus_rpcs_sender.subscribe();
|
let mut watch_consensus_rpcs = self.watch_consensus_rpcs_sender.subscribe();
|
||||||
|
|
||||||
let mut potential_rpcs = Vec::with_capacity(self.by_name.load().len());
|
let mut potential_rpcs = Vec::with_capacity(self.by_name.load().len());
|
||||||
|
|
||||||
while start.elapsed() < max_wait {
|
loop {
|
||||||
let consensus_rpcs = watch_consensus_rpcs.borrow_and_update().clone();
|
let consensus_rpcs = watch_consensus_rpcs.borrow_and_update().clone();
|
||||||
|
|
||||||
potential_rpcs.clear();
|
potential_rpcs.clear();
|
||||||
@ -653,12 +653,16 @@ impl Web3Rpcs {
|
|||||||
|
|
||||||
match consensus_rpcs.should_wait_for_block(waiting_for, skip_rpcs) {
|
match consensus_rpcs.should_wait_for_block(waiting_for, skip_rpcs) {
|
||||||
ShouldWaitForBlock::NeverReady => break,
|
ShouldWaitForBlock::NeverReady => break,
|
||||||
ShouldWaitForBlock::Ready => continue,
|
ShouldWaitForBlock::Ready => {}
|
||||||
ShouldWaitForBlock::Wait { .. } => {}
|
ShouldWaitForBlock::Wait { .. } => select! {
|
||||||
|
_ = watch_consensus_rpcs.changed() => {},
|
||||||
|
_ = sleep_until(stop_trying_at) => {},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: select on consensus_rpcs changing and on earliest_retry_at
|
if Instant::now() > stop_trying_at {
|
||||||
watch_consensus_rpcs.changed().await?;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -823,6 +827,7 @@ impl Web3Rpcs {
|
|||||||
&mut skip_rpcs,
|
&mut skip_rpcs,
|
||||||
min_block_needed,
|
min_block_needed,
|
||||||
max_block_needed,
|
max_block_needed,
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
{
|
{
|
||||||
@ -1493,6 +1498,7 @@ mod tests {
|
|||||||
&mut vec![],
|
&mut vec![],
|
||||||
Some(head_block.number.as_ref().unwrap()),
|
Some(head_block.number.as_ref().unwrap()),
|
||||||
None,
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@ -1584,28 +1590,56 @@ mod tests {
|
|||||||
|
|
||||||
// TODO: make sure the handle is for the expected rpc
|
// TODO: make sure the handle is for the expected rpc
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
rpcs.wait_for_best_rpc(&authorization, None, &mut vec![], None, None)
|
rpcs.wait_for_best_rpc(
|
||||||
.await,
|
&authorization,
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(Duration::from_secs(0))
|
||||||
|
)
|
||||||
|
.await,
|
||||||
Ok(OpenRequestResult::Handle(_))
|
Ok(OpenRequestResult::Handle(_))
|
||||||
));
|
));
|
||||||
|
|
||||||
// TODO: make sure the handle is for the expected rpc
|
// TODO: make sure the handle is for the expected rpc
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
rpcs.wait_for_best_rpc(&authorization, None, &mut vec![], Some(&0.into()), None)
|
rpcs.wait_for_best_rpc(
|
||||||
.await,
|
&authorization,
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
Some(&0.into()),
|
||||||
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
|
)
|
||||||
|
.await,
|
||||||
Ok(OpenRequestResult::Handle(_))
|
Ok(OpenRequestResult::Handle(_))
|
||||||
));
|
));
|
||||||
|
|
||||||
// TODO: make sure the handle is for the expected rpc
|
// TODO: make sure the handle is for the expected rpc
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
rpcs.wait_for_best_rpc(&authorization, None, &mut vec![], Some(&1.into()), None)
|
rpcs.wait_for_best_rpc(
|
||||||
.await,
|
&authorization,
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
Some(&1.into()),
|
||||||
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
|
)
|
||||||
|
.await,
|
||||||
Ok(OpenRequestResult::Handle(_))
|
Ok(OpenRequestResult::Handle(_))
|
||||||
));
|
));
|
||||||
|
|
||||||
// future block should not get a handle
|
// future block should not get a handle
|
||||||
let future_rpc = rpcs
|
let future_rpc = rpcs
|
||||||
.wait_for_best_rpc(&authorization, None, &mut vec![], Some(&2.into()), None)
|
.wait_for_best_rpc(
|
||||||
|
&authorization,
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
Some(&2.into()),
|
||||||
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
assert!(matches!(future_rpc, Ok(OpenRequestResult::NotReady)));
|
assert!(matches!(future_rpc, Ok(OpenRequestResult::NotReady)));
|
||||||
}
|
}
|
||||||
@ -1675,7 +1709,6 @@ mod tests {
|
|||||||
let (watch_consensus_rpcs_sender, _) = watch::channel(None);
|
let (watch_consensus_rpcs_sender, _) = watch::channel(None);
|
||||||
let (watch_consensus_head_sender, _watch_consensus_head_receiver) = watch::channel(None);
|
let (watch_consensus_head_sender, _watch_consensus_head_receiver) = watch::channel(None);
|
||||||
|
|
||||||
// TODO: make a Web3Rpcs::new
|
|
||||||
let rpcs = Web3Rpcs {
|
let rpcs = Web3Rpcs {
|
||||||
block_sender,
|
block_sender,
|
||||||
by_name: ArcSwap::from_pointee(rpcs_by_name),
|
by_name: ArcSwap::from_pointee(rpcs_by_name),
|
||||||
@ -1733,6 +1766,7 @@ mod tests {
|
|||||||
&mut vec![],
|
&mut vec![],
|
||||||
Some(head_block.number()),
|
Some(head_block.number()),
|
||||||
None,
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@ -1744,13 +1778,27 @@ mod tests {
|
|||||||
));
|
));
|
||||||
|
|
||||||
let _best_available_server_from_none = rpcs
|
let _best_available_server_from_none = rpcs
|
||||||
.wait_for_best_rpc(&authorization, None, &mut vec![], None, None)
|
.wait_for_best_rpc(
|
||||||
|
&authorization,
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// assert_eq!(best_available_server, best_available_server_from_none);
|
// assert_eq!(best_available_server, best_available_server_from_none);
|
||||||
|
|
||||||
let best_archive_server = rpcs
|
let best_archive_server = rpcs
|
||||||
.wait_for_best_rpc(&authorization, None, &mut vec![], Some(&1.into()), None)
|
.wait_for_best_rpc(
|
||||||
|
&authorization,
|
||||||
|
None,
|
||||||
|
&mut vec![],
|
||||||
|
Some(&1.into()),
|
||||||
|
None,
|
||||||
|
Some(Duration::from_secs(0)),
|
||||||
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match best_archive_server {
|
match best_archive_server {
|
||||||
|
Loading…
Reference in New Issue
Block a user