Skip to content

Commit 8694e99

Browse files
authored
refactor: add test and checked APIs for slot calculator (#51)
* refactor: add test and checked APIs for slot calculator * fix: basic calc * fix: add more docs and fix the tests * docs: more of them * fix: existing tests, add more API surface, add type distinction * lint: clippy * chore: patch version * fix: doc definitions
1 parent 6aad4fa commit 8694e99

File tree

4 files changed

+276
-89
lines changed

4 files changed

+276
-89
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ name = "init4-bin-base"
44
description = "Internal utilities for binaries produced by the init4 team"
55
keywords = ["init4", "bin", "base"]
66

7-
version = "0.5.1"
7+
version = "0.5.2"
88
edition = "2021"
99
rust-version = "1.81"
1010
authors = ["init4", "James Prestwich"]

src/perms/builders.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,29 +135,36 @@ impl Builders {
135135

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

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

147151
/// Get the index of the builder that is allowed to sign a block at the
148152
/// current timestamp.
149-
pub fn index_now(&self) -> u64 {
153+
pub fn index_now(&self) -> usize {
150154
self.index(now())
151155
}
152156

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

158162
/// Check the query bounds for the current timestamp.
159163
fn check_query_bounds(&self) -> Result<(), BuilderPermissionError> {
160-
let current_slot_time = self.calc().current_timepoint_within_slot();
164+
let current_slot_time = self
165+
.calc()
166+
.current_point_within_slot()
167+
.expect("host chain has started");
161168
if current_slot_time < self.config.block_query_start() {
162169
return Err(BuilderPermissionError::ActionAttemptTooEarly);
163170
}

src/perms/middleware.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ where
162162
permissioned_builder = this.builders.current_builder().sub(),
163163
requesting_builder = tracing::field::Empty,
164164
current_slot = this.builders.calc().current_slot(),
165-
current_timepoint_within_slot =
166-
this.builders.calc().current_timepoint_within_slot(),
165+
current_timepoint_within_slot = this
166+
.builders
167+
.calc()
168+
.current_point_within_slot()
169+
.expect("host chain has started"),
167170
permissioning_error = tracing::field::Empty,
168171
);
169172

0 commit comments

Comments
 (0)