-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #185 from NethermindEth/mu/l2-block-id
Add L2 block_id monitoring
- Loading branch information
Showing
5 changed files
with
95 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
// 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters