Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve async by removing Instant::now approach #19

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions orchestrator/cosmos_gravity/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ use gravity_utils::types::LogicCall;
use gravity_utils::types::TransactionBatch;
use gravity_utils::types::Valset;
use prost_types::Any;
use std::time::{Duration, Instant};
use std::time::Duration;
use tokio::time::sleep;
use tonic::transport::Channel;

pub async fn wait_for_cosmos_online(contact: &Contact, timeout: Duration) {
let start = Instant::now();
while let Err(CosmosGrpcError::NodeNotSynced) | Err(CosmosGrpcError::ChainNotRunning) =
contact.wait_for_next_block(timeout).await
{
sleep(Duration::from_secs(1)).await;
if Instant::now() - start > timeout {
match tokio::time::timeout(timeout, contact.wait_for_next_block(timeout)).await {
Ok(Err(CosmosGrpcError::NodeNotSynced) | Err(CosmosGrpcError::ChainNotRunning)) => {
panic!("Cosmos node has not come online during timeout!")
}
Err(_) => debug!("timedout"),
_ => {}
}

for _ in 0..3 {
let _ = contact.wait_for_next_block(timeout).await;
}
contact.wait_for_next_block(timeout).await.unwrap();
contact.wait_for_next_block(timeout).await.unwrap();
contact.wait_for_next_block(timeout).await.unwrap();
}

/// gets the Cosmos last event nonce, no matter how long it takes.
Expand All @@ -37,17 +36,18 @@ pub async fn get_last_event_nonce_with_retry(
our_cosmos_address: CosmosAddress,
prefix: String,
) -> u64 {
let mut res =
get_last_event_nonce_for_validator(client, our_cosmos_address, prefix.clone()).await;
while res.is_err() {
error!(
"Failed to get last event nonce, is the Cosmos GRPC working? {:?}",
res
);
sleep(RETRY_TIME).await;
res = get_last_event_nonce_for_validator(client, our_cosmos_address, prefix.clone()).await;
loop {
match get_last_event_nonce_for_validator(client, our_cosmos_address, prefix.clone()).await {
Err(res) => {
error!(
"Failed to get last event nonce, is the Cosmos GRPC working? {:?}",
res
);
sleep(RETRY_TIME).await;
}
Ok(last_nonce) => return last_nonce,
}
}
res.unwrap()
}

pub enum BadSignatureEvidence {
Expand Down
47 changes: 24 additions & 23 deletions orchestrator/gbt/src/client/deploy_erc20_representation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use gravity_utils::{
connection_prep::{check_for_eth, create_rpc_connections},
error::GravityError,
};
use std::time::{Duration, Instant};
use tokio::time::sleep as delay_for;
use std::time::Duration;
use tokio::time::sleep;
use web30::types::SendTxOption;

pub async fn deploy_erc20_representation(
Expand Down Expand Up @@ -71,29 +71,30 @@ pub async fn deploy_erc20_representation(

info!("We have deployed ERC20 contract {:#066x}, waiting to see if the Cosmos chain choses to adopt it", res);

let start = Instant::now();
loop {
let res = grpc
.denom_to_erc20(QueryDenomToErc20Request {
denom: denom.clone(),
})
.await;
let keep_querying_for_erc20 = async {
loop {
let res = grpc
.denom_to_erc20(QueryDenomToErc20Request {
denom: denom.clone(),
})
.await;

if let Ok(val) = res {
info!(
"Asset {} has accepted new ERC20 representation {}",
denom,
val.into_inner().erc20
);
return Ok(());
if let Ok(val) = res {
info!(
"Asset {} has accepted new ERC20 representation {}",
denom,
val.into_inner().erc20
);
break;
}
sleep(Duration::from_secs(1)).await;
}
};

if Instant::now() - start > Duration::from_secs(100) {
return Err(GravityError::UnrecoverableError(
"Your ERC20 contract was not adopted, double check the metadata and try again"
.into(),
));
}
delay_for(Duration::from_secs(1)).await;
match tokio::time::timeout(Duration::from_secs(100), keep_querying_for_erc20).await {
Ok(_) => Ok(()),
Err(_) => Err(GravityError::UnrecoverableError(
"Your ERC20 contract was not adopted, double check the metadata and try again".into(),
)),
}
}
Loading