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

Add L2 block_id monitoring #185

Merged
merged 5 commits into from
Oct 10, 2024
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
2 changes: 1 addition & 1 deletion Node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "taiko_preconf_avs_node"
version = "0.1.3"
version = "0.1.4"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
83 changes: 83 additions & 0 deletions Node/src/node/l2_block_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::sync::atomic::{AtomicU64, Ordering};

pub struct L2BlockId {
block_id: AtomicU64,
}

impl L2BlockId {
pub fn new() -> Self {
Self {
block_id: AtomicU64::new(0),
}
}

// Update self.block_id with the maximum value between self.block_id and block_id
pub fn update(&self, block_id: u64) {
self.block_id.fetch_max(block_id, Ordering::AcqRel);
}

// Returns the next block ID
// The next block ID is computed as the maximum value between current_block_id + 1 and new_block_id + 1
pub fn next(&self, block_id: u64) -> u64 {
mikhailUshakoff marked this conversation as resolved.
Show resolved Hide resolved
// Get the current value of self.block_id
let mut current = self.block_id.load(Ordering::Acquire);
loop {
// Get next block ID
// It is the maximum value between current_block_id + 1 and new_block_id + 1
let next = current.max(block_id) + 1;
// Attempt to update the block ID using a compare-exchange operation
match self.block_id.compare_exchange(
current,
next,
Ordering::Release,
Ordering::Acquire,
) {
Ok(_) => return next, // Return immediately on success
Err(previous) => current = previous, // next gets recalculated at the start of the loop
}
}
}
}

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

#[test]
fn test_next() {
let l2_block_id = L2BlockId::new();

assert_eq!(l2_block_id.next(1), 2);
assert_eq!(l2_block_id.next(0), 3);
assert_eq!(l2_block_id.next(1), 4);
assert_eq!(l2_block_id.next(7), 8);
assert_eq!(l2_block_id.next(8), 9);
assert_eq!(l2_block_id.next(4), 10);
}

#[test]
fn test_update() {
let l2_block_id = L2BlockId::new();

l2_block_id.update(1);
assert_eq!(l2_block_id.block_id.load(Ordering::SeqCst), 1);
l2_block_id.update(10);
assert_eq!(l2_block_id.block_id.load(Ordering::SeqCst), 10);
l2_block_id.update(5);
assert_eq!(l2_block_id.block_id.load(Ordering::SeqCst), 10);
}

#[test]
fn test_update_next() {
let l2_block_id = L2BlockId::new();

l2_block_id.update(1);
assert_eq!(l2_block_id.block_id.load(Ordering::SeqCst), 1);
l2_block_id.update(10);
assert_eq!(l2_block_id.block_id.load(Ordering::SeqCst), 10);
assert_eq!(l2_block_id.next(0), 11);
assert_eq!(l2_block_id.next(12), 13);
l2_block_id.update(5);
assert_eq!(l2_block_id.block_id.load(Ordering::SeqCst), 13);
}
}
12 changes: 9 additions & 3 deletions Node/src/node/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod block_proposed_receiver;
mod commit;
mod l2_block_id;
pub mod lookahead_monitor;
pub mod lookahead_updated_receiver;
mod operator;
Expand All @@ -16,6 +17,7 @@ use crate::{
};
use anyhow::Error;
use commit::L2TxListsCommit;
use l2_block_id::L2BlockId;
use operator::{Operator, Status as OperatorStatus};
use preconfirmation_helper::PreconfirmationHelper;
use preconfirmation_message::PreconfirmationMessage;
Expand Down Expand Up @@ -54,6 +56,7 @@ pub struct Node {
preconfirmation_helper: PreconfirmationHelper,
bls_service: Arc<BLSService>,
always_push_lookahead: bool,
l2_block_id: Arc<L2BlockId>,
}

impl Node {
Expand Down Expand Up @@ -86,6 +89,7 @@ impl Node {
preconfirmation_helper: PreconfirmationHelper::new(),
bls_service,
always_push_lookahead,
l2_block_id: Arc::new(L2BlockId::new()),
})
}

Expand All @@ -104,6 +108,7 @@ impl Node {
let taiko = self.taiko.clone();
let is_preconfer_now = self.is_preconfer_now.clone();
let preconfirmation_txs = self.preconfirmation_txs.clone();
let l2_block_id = self.l2_block_id.clone();
if let (Some(node_rx), Some(p2p_to_node_rx)) = (
self.node_block_proposed_rx.take(),
self.p2p_to_node_rx.take(),
Expand All @@ -117,6 +122,7 @@ impl Node {
taiko,
is_preconfer_now,
preconfirmation_txs,
l2_block_id,
)
.await;
});
Expand All @@ -133,6 +139,7 @@ impl Node {
taiko: Arc<Taiko>,
is_preconfer_now: Arc<AtomicBool>,
preconfirmation_txs: Arc<Mutex<HashMap<u64, Vec<u8>>>>,
l2_block_id: Arc<L2BlockId>,
) {
loop {
tokio::select! {
Expand All @@ -154,6 +161,7 @@ impl Node {
if !is_preconfer_now.load(Ordering::Acquire) {
debug!("Received Message from p2p!");
let msg: PreconfirmationMessage = p2p_message.into();
l2_block_id.update(msg.block_height);
Self::advance_l2_head(msg, &preconfirmed_blocks, ethereum_l1.clone(), taiko.clone()).await;
} else {
debug!("Node is Preconfer and received message from p2p: {:?}", p2p_message);
Expand Down Expand Up @@ -480,9 +488,7 @@ impl Node {
pending_tx_lists.tx_list_bytes[0].clone() // TODO: handle multiple tx lists
};

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

let (commit_hash, signature) =
Expand Down
33 changes: 1 addition & 32 deletions Node/src/node/preconfirmation_helper.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
pub struct PreconfirmationHelper {
nonce: u64,
last_block_id: u64,
}

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

pub fn init(&mut self, nonce: u64) {
Expand All @@ -24,31 +20,4 @@ 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);
}
}
Loading