Skip to content

Commit

Permalink
Block period to Duration
Browse files Browse the repository at this point in the history
  • Loading branch information
sergerad committed Aug 31, 2024
1 parent 80f887e commit 8fe61a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
4 changes: 3 additions & 1 deletion rollup/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod transaction;
use std::time::Duration;

use transaction::WithdrawalTxData;
pub use transaction::{SignedTransaction, Transaction};

Expand All @@ -18,5 +20,5 @@ use blockchain::Blockchain;
mod address;
pub use address::Address;

pub const BLOCK_PERIOD_MILLIS: u64 = 2000;
pub const BLOCK_PERIOD: Duration = Duration::from_secs(2);
pub const CHAIN_ID: u64 = 83479;
5 changes: 2 additions & 3 deletions rollup/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use std::{
pin::Pin,
sync::{Arc, Mutex},
task::{Context, Poll},
time::Duration,
};

use crate::{
Block, BlockHeader, Blockchain, SignedBlockHeader, SignedTransaction, Signer, Transaction,
BLOCK_PERIOD_MILLIS,
BLOCK_PERIOD,
};

/// Permissioned entity responsible for maintaining the canonical [Blockchain].
Expand All @@ -34,7 +33,7 @@ impl Sequencer {
blockchain: Blockchain::default(),
transactions_pool: vec![],
withdrawals_pool: vec![],
block_timer: tokio::time::interval(Duration::from_millis(BLOCK_PERIOD_MILLIS)),
block_timer: tokio::time::interval(BLOCK_PERIOD),
}
}

Expand Down
12 changes: 6 additions & 6 deletions script/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rollup::{Block, SignedTransaction, Signer, Transaction, BLOCK_PERIOD_MILLIS};
use rollup::{Block, SignedTransaction, Signer, Transaction, BLOCK_PERIOD};
use secp256k1::SecretKey;
use tokio::process::Command;

Expand Down Expand Up @@ -38,13 +38,13 @@ async fn handle_request_err(e: reqwest::Error) {
} else {
println!("Error sending transaction: {:?}", e);
}
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
tokio::time::sleep(BLOCK_PERIOD).await;
}

/// Infinitely sends transactions to the sequencer.
async fn tx_loop() {
// Wait for the sequencer to start.
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
tokio::time::sleep(BLOCK_PERIOD).await;
for i in 0.. {
// Send a deposit transaction.
let signer = Signer::random();
Expand All @@ -65,13 +65,13 @@ async fn tx_loop() {
}

// Wait before sending the next transactions.
tokio::time::sleep(tokio::time::Duration::from_millis(BLOCK_PERIOD_MILLIS / 4)).await;
tokio::time::sleep(BLOCK_PERIOD / 4).await;
}
}

async fn head_loop() {
// Wait for some blocks.
tokio::time::sleep(tokio::time::Duration::from_millis(BLOCK_PERIOD_MILLIS * 2)).await;
tokio::time::sleep(BLOCK_PERIOD * 2).await;
loop {
// Get the head block from the sequencer.
match reqwest::get(&format!("http://{}/", SEQUENCER_URL)).await {
Expand All @@ -92,7 +92,7 @@ async fn head_loop() {
println!("Error getting head block: {:?}", e);
}
}
tokio::time::sleep(tokio::time::Duration::from_millis(BLOCK_PERIOD_MILLIS)).await;
tokio::time::sleep(BLOCK_PERIOD).await;
}
}

Expand Down

0 comments on commit 8fe61a7

Please sign in to comment.