Skip to content

refactor: represent slot intervals with enum#496

Open
dicethedev wants to merge 3 commits into
lambdaclass:mainfrom
dicethedev:refactor/slot-interval-enum
Open

refactor: represent slot intervals with enum#496
dicethedev wants to merge 3 commits into
lambdaclass:mainfrom
dicethedev:refactor/slot-interval-enum

Conversation

@dicethedev

Copy link
Copy Markdown
Contributor

🗒️ Description / Motivation

  • Replaces raw 0..4 interval dispatch in blockchain tick handling with a typed SlotInterval enum.
  • This makes interval duties self-documenting and lets Rust enforce exhaustive handling when intervals change.
  • Closes the gap where interval-specific behavior was matched using integer literals.

What Changed

  • crates/blockchain/src/lib.rs

    • Added SlotInterval with from_slot_index as the integer slot-to-enum constructor.
    • Updated BlockChainServer::on_tick to compare and match on SlotInterval variants.
    • Kept interval logging as numeric output through Display.
  • crates/blockchain/src/store.rs

    • Reused SlotInterval for the store-level tick interval match.

Correctness / Behavior Guarantees

  • No behavior change intended.
  • The same interval indices map to the same duties:
    • 0 → block publication
    • 1 → attestation production
    • 2 → aggregation
    • 3 → safe target update
    • 4 → end of slot
  • Interval matches are now exhaustive, so adding/removing interval variants requires updating all dispatch sites at compile time.

Tests Added / Run

  • No tests added; this is a refactor with no intended behavior change.
  • Ran:
    • cargo fmt
    • cargo check -p ethlambda-blockchain

Related Issues / PRs

✅ Verification Checklist

  • Ran make fmt — clean
  • Ran make lint (clippy with -D warnings) — clean
  • Ran cargo test --workspace --release — all passing

@greptile-apps

greptile-apps Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

No reviewable files after applying ignore patterns.

}
}

impl fmt::Display for SlotInterval {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is unused, we should just remove it

}

impl SlotInterval {
pub(crate) fn from_slot_index(index: u64) -> Self {

@MegaRedHand MegaRedHand Jul 3, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, we should give only current time in ms -> SlotInterval and current time in intervals -> SlotInterval constructors, so that callers don't have to compute the interval themselves. That way we centralize the logic in this enum

Comment on lines +234 to +236
let interval = SlotInterval::from_slot_index(
(time_since_genesis_ms % MILLISECONDS_PER_SLOT) / MILLISECONDS_PER_INTERVAL,
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, for example, we could do:

Suggested change
let interval = SlotInterval::from_slot_index(
(time_since_genesis_ms % MILLISECONDS_PER_SLOT) / MILLISECONDS_PER_INTERVAL,
);
let interval = SlotInterval::from_ms_since_genesis(time_since_genesis_ms);


let slot = store.time() / INTERVALS_PER_SLOT;
let interval = store.time() % INTERVALS_PER_SLOT;
let interval = SlotInterval::from_slot_index(store.time() % INTERVALS_PER_SLOT);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here:

Suggested change
let interval = SlotInterval::from_slot_index(store.time() % INTERVALS_PER_SLOT);
let interval = SlotInterval::from_intervals_since_genesis(store.time());

@MegaRedHand MegaRedHand left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments. The direction is good, but there are some improvements I think are necessary before we merge this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use an enum to represent the different intervals in BlockChainServer::on_tick

2 participants