Skip to content

feat/fix: add functionality & fix off-by-ones in SlotCalculator#20

Merged
anna-carroll merged 4 commits intomainfrom
anna/slot-calculator
Mar 28, 2025
Merged

feat/fix: add functionality & fix off-by-ones in SlotCalculator#20
anna-carroll merged 4 commits intomainfrom
anna/slot-calculator

Conversation

@anna-carroll
Copy link
Copy Markdown
Contributor

@anna-carroll anna-carroll commented Mar 21, 2025

  • fix: off-by-one in slot calculations
  • fix: there's some weirdness on Holesky between block 0 and block 1, interval of 324 seconds but only 2 slots; adjusted start values to fix this
  • feat: calculate start & end timestamps for a block slot
  • feat: calculate the timepoint within a block window for a timestamp

@anna-carroll anna-carroll self-assigned this Mar 21, 2025
@anna-carroll anna-carroll requested a review from a team as a code owner March 21, 2025 20:45
@prestwich prestwich requested a review from Evalir March 22, 2025 10:46
Self { start_timestamp: 1695902400, slot_offset: 0, slot_duration: 12 }
// begin slot calculation for Holesky from block number 1, slot number 2
// because of a strange 324 second gap between block 0 and 1 which
// should have been 27 slots, but which is recorded as 2 slots in chain data
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should look into this more and maybe write something. genesis of new testnets seems very poorly documented

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, i tried googling a bit to see if there was a writeup anywhere and I couldn't find one. granted, the SEO for Holesky incidents is drowning in more recent reports 😅

Copy link
Copy Markdown
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

The math seems OK to me, but I think we should make triple sure our fixes are correct and account for the weird slot gaps. We could do this:

  • query a CL for random slot numbers
  • grab the output
  • calculate the slot with the timestamp it gives us
  • ensure the calculated slot matches the slot the output actually gave back

The point is to ensure that these gaps do not exist elsewhere "in the middle" of the history of the chain.

To accomplish this we can use this endpoint in the CL API.

@prestwich
Copy link
Copy Markdown
Member

query a CL for random slot numbers

why random? seems like latest is sufficient to tell that they've all been at the correct interval

@Evalir
Copy link
Copy Markdown
Member

Evalir commented Mar 25, 2025

Yeah, latest is sufficient—what I meant with this is that we should get more than one test case ideally quite a few blocks apart.

@anna-carroll
Copy link
Copy Markdown
Contributor Author

anna-carroll commented Mar 25, 2025

why random? seems like latest is sufficient to tell that they've all been at the correct interval

correct / agree, and I already checked it against the latest before submitting the PR :~) the test cases are in the code

// calculate slot
assert_eq!(calculator.calculate_slot(1695902424), 3);
assert_eq!(calculator.calculate_slot(1695902425), 3);
assert_eq!(calculator.calculate_slot(1742586984), 3890383);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this was latest on holesky when i submitted the PR

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

you can also see we cover the oldest possible slots on both mainnet & holesky within the test cases too. so, oldest && latest

assert_eq!(calculator.calculate_slot(1738863035), 11003252);
assert_eq!(calculator.calculate_slot(1738866239), 11003519);
assert_eq!(calculator.calculate_slot(1738866227), 11003518);
assert_eq!(calculator.calculate_slot(1742587235), 11313602);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this was latest on mainnet when i submitted the PR

Copy link
Copy Markdown
Member

@Evalir Evalir left a comment

Choose a reason for hiding this comment

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

test cases look great. this code is already running in the node to avoid suspected off by 1s when getting slots (I've temporarily vendored the file).

@prestwich
Copy link
Copy Markdown
Member

CI should be fixed if you rebase :)

@anna-carroll anna-carroll force-pushed the anna/slot-calculator branch from 0d5f158 to 5704f74 Compare March 28, 2025 03:18
@anna-carroll anna-carroll enabled auto-merge (squash) March 28, 2025 03:18
@anna-carroll anna-carroll merged commit cb457b6 into main Mar 28, 2025
5 checks passed
@anna-carroll anna-carroll deleted the anna/slot-calculator branch March 28, 2025 19:46
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.

3 participants