Skip to content

refactor: add test and checked APIs for slot calculator #51

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

Merged
merged 8 commits into from
Jul 3, 2025
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name = "init4-bin-base"
description = "Internal utilities for binaries produced by the init4 team"
keywords = ["init4", "bin", "base"]

version = "0.5.1"
version = "0.5.2"
edition = "2021"
rust-version = "1.81"
authors = ["init4", "James Prestwich"]
Expand Down
19 changes: 13 additions & 6 deletions src/perms/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,29 +135,36 @@ impl Builders {

/// Get the builder permissioned at a specific timestamp.
pub fn builder_at_timestamp(&self, timestamp: u64) -> &Builder {
self.builder_at(self.index(timestamp) as usize)
self.builder_at(self.index(timestamp))
}

/// Get the index of the builder that is allowed to sign a block for a
/// particular timestamp.
pub fn index(&self, timestamp: u64) -> u64 {
self.config.calc().calculate_slot(timestamp) % self.builders.len() as u64
pub fn index(&self, timestamp: u64) -> usize {
self.config
.calc()
.slot_containing(timestamp)
.expect("host chain has started")
% self.builders.len()
}

/// Get the index of the builder that is allowed to sign a block at the
/// current timestamp.
pub fn index_now(&self) -> u64 {
pub fn index_now(&self) -> usize {
self.index(now())
}

/// Get the builder that is allowed to sign a block at the current timestamp.
pub fn current_builder(&self) -> &Builder {
self.builder_at(self.index_now() as usize)
self.builder_at(self.index_now())
}

/// Check the query bounds for the current timestamp.
fn check_query_bounds(&self) -> Result<(), BuilderPermissionError> {
let current_slot_time = self.calc().current_timepoint_within_slot();
let current_slot_time = self
.calc()
.current_point_within_slot()
.expect("host chain has started");
if current_slot_time < self.config.block_query_start() {
return Err(BuilderPermissionError::ActionAttemptTooEarly);
}
Expand Down
7 changes: 5 additions & 2 deletions src/perms/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,11 @@ where
permissioned_builder = this.builders.current_builder().sub(),
requesting_builder = tracing::field::Empty,
current_slot = this.builders.calc().current_slot(),
current_timepoint_within_slot =
this.builders.calc().current_timepoint_within_slot(),
current_timepoint_within_slot = this
.builders
.calc()
.current_point_within_slot()
.expect("host chain has started"),
permissioning_error = tracing::field::Empty,
);

Expand Down
Loading