Skip to content

Commit

Permalink
fix: use tokio::ticker instead of sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
distractedm1nd committed Jul 8, 2024
1 parent 437efbe commit 06cc291
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/node_types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::consts::{CHANNEL_BUFFER_SIZE, DA_RETRY_COUNT, DA_RETRY_INTERVAL};
use crate::error::DataAvailabilityError;
use crate::{
consts::{CHANNEL_BUFFER_SIZE, DA_RETRY_COUNT, DA_RETRY_INTERVAL},
error::DataAvailabilityError,
};
use async_trait::async_trait;
use crypto_hash::{hex_digest, Algorithm};
use ed25519_dalek::{Signer, SigningKey};
Expand All @@ -11,7 +13,7 @@ use tokio::{
Mutex,
},
task::spawn,
time::sleep,
time::{interval, sleep},
};

use crate::{
Expand Down Expand Up @@ -87,6 +89,7 @@ impl NodeType for LightClient {

let handle = spawn(async move {
let mut current_position = 0;
let mut ticker = interval(Duration::from_secs(1));
loop {
// target is updated when a new header is received
let target = self.da.get_message().await.unwrap();
Expand Down Expand Up @@ -134,7 +137,7 @@ impl NodeType for LightClient {
Err(e) => debug!("light client: getting epoch: {}", e),
};
}
sleep(Duration::from_secs(1)).await; // only for testing purposes
ticker.tick().await; // only for testing purposes
current_position = target; // Update the current position to the latest target
}
});
Expand Down Expand Up @@ -180,8 +183,10 @@ impl Sequencer {
async fn main_loop(self: Arc<Self>) {
info!("starting main sequencer loop");
let epoch_buffer = self.epoch_buffer_tx.clone();
let mut ticker = interval(Duration::from_secs(self.epoch_duration));
spawn(async move {
loop {
ticker.tick().await;
match self.finalize_epoch().await {
Ok(epoch) => {
info!(
Expand All @@ -192,15 +197,14 @@ impl Sequencer {
}
Err(e) => error!("sequencer_loop: finalizing epoch: {}", e),
}
// elapsed time/ticker instead of sleep
sleep(Duration::from_secs(self.epoch_duration)).await;
}
});
}

// da_loop is responsible for submitting finalized epochs to the DA layer.
async fn da_loop(self: Arc<Self>) {
info!("starting da submission loop");
let mut ticker = interval(DA_RETRY_INTERVAL);
spawn(async move {
loop {
let epoch = self.get_message().await.unwrap();
Expand All @@ -218,7 +222,7 @@ impl Sequencer {
Err(e) => {
error!("da_loop: submitting epoch: {}", e);
retry_counter += 1;
sleep(DA_RETRY_INTERVAL).await;
ticker.tick().await;
}
};
}
Expand Down

0 comments on commit 06cc291

Please sign in to comment.