Skip to content

Commit

Permalink
block id for few l2 blocks (#170)
Browse files Browse the repository at this point in the history
* l2 slot number from slot clock

* added `from` to the force push lookahead

* increasing the block id for block preconfirmation

* fmt

* check empty lookahead when not being preconfer

* removed `.from` from contract calls

* move the noce increment
  • Loading branch information
mskrzypkows authored Oct 4, 2024
1 parent 7a66326 commit 1d571ba
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 10 deletions.
7 changes: 5 additions & 2 deletions Node/src/ethereum_l1/execution_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl ExecutionLayer {
)
.chain_id(self.l1_chain_id)
.nonce(nonce)
.gas(500_000)
.gas(1_000_000)
.max_fee_per_gas(20_000_000_000)
.max_priority_fee_per_gas(1_000_000_000);

Expand Down Expand Up @@ -597,7 +597,10 @@ impl ExecutionLayer {
let tx = contract
.forcePushLookahead(lookahead_set_params)
.nonce(self.get_preconfer_nonce().await?)
.gas(1_000_000);
.gas(10_000_000)
.max_fee_per_gas(20_000_000_000)
.max_priority_fee_per_gas(1_000_000_000);

match tx.send().await {
Ok(receipt) => {
tracing::debug!("Force push lookahead sent: {}", receipt.tx_hash());
Expand Down
20 changes: 14 additions & 6 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,10 +315,6 @@ impl Node {
self.preconfirm_last_slot().await?;
}
OperatorStatus::Preconfer => {
if !self.is_preconfer_now.load(Ordering::Acquire) {
self.is_preconfer_now.store(true, Ordering::Release);
self.start_propose().await?;
}
self.preconfirm_block(true).await?;
}
OperatorStatus::None => {
Expand All @@ -331,6 +327,9 @@ impl Node {
.slot_clock
.get_l2_slot_number_within_l1_slot()?
);

// TODO: add flag for turning it on
self.operator.check_empty_lookahead().await?;
}
}

Expand Down Expand Up @@ -440,12 +439,16 @@ impl Node {
.get_l2_slot_number_within_l1_slot()?
);

if !self.is_preconfer_now.load(Ordering::Acquire) {
self.is_preconfer_now.store(true, Ordering::Release);
self.start_propose().await?;
}

let lookahead_params = self.get_lookahead_params().await?;
let pending_tx_lists = self.taiko.get_pending_l2_tx_lists().await?;
let pending_tx_lists_bytes = if pending_tx_lists.tx_list_bytes.is_empty() {
if let Some(lookahead_params) = lookahead_params {
debug!("No pending transactions to preconfirm, force pushing lookahead");
self.preconfirmation_helper.increment_nonce();
if let Err(err) = self
.ethereum_l1
.execution_layer
Expand All @@ -457,6 +460,8 @@ impl Node {
} else {
error!("Failed to force push lookahead: {}", err);
}
} else {
self.preconfirmation_helper.increment_nonce();
}
}
// No transactions skip preconfirmation step
Expand All @@ -469,7 +474,10 @@ impl Node {
pending_tx_lists.tx_list_bytes[0].clone() // TODO: handle multiple tx lists
};

let new_block_height = pending_tx_lists.parent_block_id + 1;
let new_block_height = self
.preconfirmation_helper
.get_new_block_id(pending_tx_lists.parent_block_id);
debug!("Preconfirming block with the height: {}", new_block_height);

let (commit_hash, signature) =
self.generate_commit_hash_and_signature(&pending_tx_lists, new_block_height)?;
Expand Down
36 changes: 35 additions & 1 deletion Node/src/node/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use anyhow::Error;
use std::sync::Arc;
use tracing::debug;
use tracing::{debug, error};

pub struct Operator {
ethereum_l1: Arc<EthereumL1>,
Expand Down Expand Up @@ -162,6 +162,40 @@ impl Operator {

Ok(lookahead_pointer)
}

pub async fn check_empty_lookahead(&mut self) -> Result<(), Error> {
debug!("Checking empty lookahead");

let is_required = self
.ethereum_l1
.execution_layer
.is_lookahead_required()
.await?;

if is_required {
self.update_preconfer_lookahead_for_epoch().await?;
if self
.lookahead_preconfer_addresses
.iter()
.all(|addr| *addr == PRECONFER_ADDRESS_ZERO)
{
debug!("Lookahead is empty, force pushing");
match self.ethereum_l1.force_push_lookahead().await {
Ok(_) => {
debug!("Force pushed lookahead");
}
Err(err) => {
if err.to_string().contains("AlreadyKnown") {
debug!("Force push lookahead already known");
} else {
error!("Failed to force push lookahead: {}", err);
}
}
}
}
}
Ok(())
}
}

#[cfg(test)]
Expand Down
33 changes: 32 additions & 1 deletion Node/src/node/preconfirmation_helper.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
pub struct PreconfirmationHelper {
nonce: u64,
last_block_id: u64,
}

impl PreconfirmationHelper {
pub fn new() -> Self {
Self { nonce: 0 }
Self {
nonce: 0,
last_block_id: 0,
}
}

pub fn init(&mut self, nonce: u64) {
Expand All @@ -20,4 +24,31 @@ impl PreconfirmationHelper {
pub fn increment_nonce(&mut self) {
self.nonce += 1;
}

pub fn get_new_block_id(&mut self, parent_block_id: u64) -> u64 {
let mut new_block_id = parent_block_id + 1;
if self.last_block_id >= new_block_id {
new_block_id = self.last_block_id + 1;
}
self.last_block_id = new_block_id;
new_block_id
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_new_block_id() {
let mut helper = PreconfirmationHelper::new();
assert_eq!(helper.get_new_block_id(0), 1);
assert_eq!(helper.get_new_block_id(0), 2);
assert_eq!(helper.get_new_block_id(0), 3);
assert_eq!(helper.get_new_block_id(0), 4);
assert_eq!(helper.get_new_block_id(4), 5);
assert_eq!(helper.get_new_block_id(4), 6);
assert_eq!(helper.get_new_block_id(4), 7);
assert_eq!(helper.get_new_block_id(4), 8);
}
}

0 comments on commit 1d571ba

Please sign in to comment.