Skip to content
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
a65d19f
feat: add clock module for slot/epoch timing (Zig 0.16)
GrapeBaBa May 8, 2026
8152b31
feat(clock): migrate Config from seconds_per_slot to slot_duration_ms
GrapeBaBa May 8, 2026
da65ee2
fix(clock): address codex review on gr/clock-replant
GrapeBaBa May 8, 2026
b2806e4
feat(clock): fork-aware slot duration schedule
GrapeBaBa May 8, 2026
3c9959a
refactor(clock): swap schedule slice for TS-style 2-field fork config
GrapeBaBa May 8, 2026
eb98c28
refactor(clock): slot duration with optional `duration_transitions` s…
GrapeBaBa May 8, 2026
7a1be9f
refactor(clock): inline duration_transitions as a fixed-size array
GrapeBaBa May 8, 2026
cba0b3a
fix(clock): use std.Io.concurrent for waitForSlot, address codex P1
GrapeBaBa May 8, 2026
90b9bea
fix(clock): close two remaining codex P2 findings
GrapeBaBa May 8, 2026
08ea084
test(clock): wire time_source tests into test:clock
GrapeBaBa May 8, 2026
07565e0
fix(clock): invoke listener callbacks outside mutex
GrapeBaBa May 9, 2026
6a49f5f
fix(clock): reserve snapshot capacity before iter.next, fail-fast on OOM
GrapeBaBa May 9, 2026
7adc8ab
refactor(clock): panic on snapshot OOM, narrow error set back to Canc…
GrapeBaBa May 9, 2026
c874d73
refactor(clock): surface snapshot OOM as error.OutOfMemory
GrapeBaBa May 9, 2026
d39a73d
fix(clock): address codex P1-P4 (waiter ordering, catch-up timing, ta…
GrapeBaBa May 9, 2026
ccad20d
fix(clock): close codex round-2 P2 findings (cancel race, suspend-awa…
GrapeBaBa May 9, 2026
b3da05b
fix(clock): close codex round-3 P2 findings (in-flight fast-path + sl…
GrapeBaBa May 9, 2026
cf96065
refactor(clock): drop mutex / dispatching / snapshot dance for single…
GrapeBaBa May 9, 2026
02ca002
fix(clock): adopt Evented-only model; close codex round-4 P2 findings
GrapeBaBa May 9, 2026
7f7ed18
refactor(clock): restore PR #301 EventClock structure
GrapeBaBa May 9, 2026
d713863
docs(clock): clarify callback re-entry rules (off* safe, on* not)
GrapeBaBa May 9, 2026
400364d
docs(clock): drop TS-mirror references from comments
GrapeBaBa May 9, 2026
2383d45
ci(clock): switch test IO to Threaded; wire test:clock into CI
GrapeBaBa May 9, 2026
ad04e5f
test(clock): use std.testing.io instead of a custom TestIo
GrapeBaBa May 9, 2026
de9cbf4
ci(clock): skip clock tests on macOS
GrapeBaBa May 9, 2026
7ee67d2
ci(clock): per-test diagnostic on macOS to find hang
GrapeBaBa May 9, 2026
12ee8af
ci(clock): fix filter option name for diagnostic step
GrapeBaBa May 9, 2026
100fe46
fix(clock): use std.Io.concurrent for start + waitForSlot spawns
GrapeBaBa May 9, 2026
72fe0bd
docs(clock): trim redundant comments
GrapeBaBa May 9, 2026
c1a6d65
fix(clock): drop redundant destroy on concurrent-spawn failure
GrapeBaBa May 9, 2026
a3207f4
feat(clock): switch tests to zio runtime; add property + gap tests
GrapeBaBa May 12, 2026
620f4c0
docs(clock): drop section dividers and outdated GCD-futex references
GrapeBaBa May 12, 2026
c3b9a25
refactor(clock): align Slot/Epoch with consensus_types; drop Unix ali…
GrapeBaBa May 13, 2026
2330514
refactor(clock): replace empty-body while with for-else in transitions()
GrapeBaBa May 13, 2026
1233cb2
refactor(clock): forward-scan slotDurationMsAt with early break
GrapeBaBa May 13, 2026
ffa7df7
refactor(clock): replace dead defensive checks with asserts; tighten …
GrapeBaBa May 13, 2026
f3be750
refactor(clock): single nowMs snapshot in gossip-disparity helpers
GrapeBaBa May 13, 2026
b132167
style(clock): separate defer/errdefer resource blocks with blank lines
GrapeBaBa May 13, 2026
1ead736
refactor(clock): introduce BoundedArray helper; harden clock buffer caps
GrapeBaBa May 13, 2026
d40ec6f
style(clock): reorder struct members to fields-first
GrapeBaBa May 13, 2026
90dc69b
docs(clock): document the no-mutex safety contract
GrapeBaBa May 21, 2026
664660a
refactor(clock): address PR review — extract ClockConfig, naming nits
GrapeBaBa May 27, 2026
8b9d271
Merge remote-tracking branch 'origin/main' into gr/clock-replant
GrapeBaBa May 30, 2026
c863f0a
chore(clock): bump zio dependency v0.11.0 -> v0.13.0
GrapeBaBa May 31, 2026
7f8180f
refactor(clock): address verify-review findings on the clock module
GrapeBaBa Jun 20, 2026
8503986
Merge remote-tracking branch 'origin/main' into gr/clock-replant
GrapeBaBa Jun 20, 2026
9f90f5d
style(clock): drop stray comment, restore over-trimmed one
GrapeBaBa Jun 20, 2026
6582099
refactor(clock): comptime time source instead of runtime union
GrapeBaBa Jun 20, 2026
e25af88
fix(clock): resolve waiters whose slot was reached when stop() lands
GrapeBaBa Jun 20, 2026
309d991
fix(clock): correct pre-genesis gossip-disparity in both clock helpers
GrapeBaBa Jun 20, 2026
cc4f254
chore(clock): bump zio dependency v0.13.0 -> v0.14.0
GrapeBaBa Jun 23, 2026
f5a83bb
refactor(clock): inject time via std.Io, drop the TimeSource layer
GrapeBaBa Jun 29, 2026
289584e
refactor(clock): plain arithmetic across the clock module; null only …
GrapeBaBa Jun 29, 2026
497dd16
refactor(clock): drop redundant non-negative asserts on wall-clock casts
GrapeBaBa Jun 30, 2026
698be4b
refactor(clock): consolidate wall-clock readers into the time module
GrapeBaBa Jun 30, 2026
162d847
refactor(clock): drop the SlotClock.nowMs forwarder, call time.nowMs …
GrapeBaBa Jun 30, 2026
c2d7571
refactor(time): rename timestampNow -> start, unify on Clock.<variant…
GrapeBaBa Jun 30, 2026
ad319b6
refactor(clock): remove unreachable maxInt guards, inline secToMs
GrapeBaBa Jun 30, 2026
5549c93
refactor(clock): remove dead trailing dispatchWaiters in advanceAndDi…
GrapeBaBa Jun 30, 2026
22f213a
test(clock): drop redundant spawn/join in runInRuntime helper
GrapeBaBa Jun 30, 2026
046cf73
test(clock): inline runtime setup, delete runInRuntime helper
GrapeBaBa Jun 30, 2026
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
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ jobs:
- name: Run fork-choice tests
run: |
zig build test:fork_choice
- name: Run clock tests
run: |
zig build test:clock

- name: Build benchmarks
run: |
Expand Down
13 changes: 13 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
// .url = "git+https://github.com/karlseguin/http.zig.git#2924ead41db9e1986db3b4909a82185c576f6361",
// .hash = "httpz-0.0.0-PNVzrC7CBgDSMZNP7chgAczR3FyAz5Eum6Kb1_K9kmYR",
// },
.zio = .{
.url = "git+https://github.com/lalinsky/zio?ref=v0.14.0#aa860381beb42bc077e24ef304827cbdc23fcef9",
.hash = "zio-0.14.0-xHbVVPxFHgBCop4CvfF4u360kzWlHD4FS8Ud-RIkPcZ7",
},
},
.options_modules = .{
.build_options = .{
Expand Down Expand Up @@ -136,6 +140,13 @@
.time = .{
.root_source_file = "src/time.zig",
},
.bounded_array = .{
.root_source_file = "src/bounded_array.zig",
},
.clock = .{
.root_source_file = "src/clock/root.zig",
.imports = .{ .zio, .consensus_types, .bounded_array },
},
.cpu_count = .{
.root_source_file = "src/cpu_count.zig",
},
Expand Down Expand Up @@ -359,6 +370,7 @@
// Per-module unit tests (mirror the old auto-generated `test:<module>` steps).
.constants = .{ .root_module = .constants },
.hex = .{ .root_module = .hex },
.bounded_array = .{ .root_module = .bounded_array },
.cpu_count = .{ .root_module = .cpu_count },
.hashing = .{ .root_module = .hashing },
.preset = .{ .root_module = .preset },
Expand All @@ -371,6 +383,7 @@
.state_transition = .{ .root_module = .state_transition },
.era = .{ .root_module = .era },
.fork_choice = .{ .root_module = .fork_choice },
.clock = .{ .root_module = .clock },

.int = .{
.root_module = .{
Expand Down
148 changes: 148 additions & 0 deletions src/bounded_array.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
//! Stack-resident array with a compile-time hard capacity. No allocator;
//! runtime ops are infallible — over-capacity is a programming error and
//! asserts rather than returning an error.

const std = @import("std");

pub fn BoundedArray(comptime T: type, comptime capacity_: u32) type {
return struct {
buffer: [capacity_]T = undefined,
count: u32 = 0,

const Self = @This();
pub const capacity: u32 = capacity_;

pub fn slice(self: *Self) []T {
return self.buffer[0..self.count];
}

pub fn constSlice(self: *const Self) []const T {
return self.buffer[0..self.count];
}

pub fn full(self: *const Self) bool {
return self.count == capacity;
}

pub fn empty(self: *const Self) bool {
return self.count == 0;
}

pub fn push(self: *Self, item: T) void {
std.debug.assert(!self.full());
self.buffer[self.count] = item;
self.count += 1;
}

pub fn pop(self: *Self) ?T {
if (self.empty()) return null;
self.count -= 1;
return self.buffer[self.count];
}

pub fn orderedRemove(self: *Self, i: u32) void {
std.debug.assert(i < self.count);
var j: u32 = i;
while (j + 1 < self.count) : (j += 1) {
self.buffer[j] = self.buffer[j + 1];
}
self.count -= 1;
}

pub fn clear(self: *Self) void {
self.count = 0;
}
};
}

const testing = std.testing;

test "default-constructed is empty" {
const A = BoundedArray(u32, 4);
var a: A = .{};
try testing.expect(a.empty());
try testing.expect(!a.full());
try testing.expectEqual(@as(u32, 0), a.count);
try testing.expectEqual(@as(u32, 4), A.capacity);
try testing.expectEqual(@as(usize, 0), a.slice().len);
}

test "push fills up to capacity" {
var a: BoundedArray(u32, 3) = .{};
a.push(10);
a.push(20);
a.push(30);
try testing.expect(a.full());
try testing.expectEqualSlices(u32, &.{ 10, 20, 30 }, a.slice());
}

test "pop returns elements in LIFO order" {
var a: BoundedArray(u32, 3) = .{};
a.push(10);
a.push(20);
try testing.expectEqual(@as(?u32, 20), a.pop());
try testing.expectEqual(@as(?u32, 10), a.pop());
try testing.expectEqual(@as(?u32, null), a.pop());
}

test "orderedRemove shifts trailing elements down" {
var a: BoundedArray(u32, 4) = .{};
a.push(1);
a.push(2);
a.push(3);
a.push(4);
a.orderedRemove(1);
try testing.expectEqualSlices(u32, &.{ 1, 3, 4 }, a.slice());
a.orderedRemove(0);
try testing.expectEqualSlices(u32, &.{ 3, 4 }, a.slice());
a.orderedRemove(1);
try testing.expectEqualSlices(u32, &.{3}, a.slice());
}

test "clear resets count without touching buffer" {
var a: BoundedArray(u32, 3) = .{};
a.push(1);
a.push(2);
a.clear();
try testing.expect(a.empty());
try testing.expectEqualSlices(u32, &.{}, a.slice());
}

test "slice allows in-place mutation" {
var a: BoundedArray(u32, 3) = .{};
a.push(1);
a.push(2);
a.push(3);
for (a.slice()) |*x| x.* *= 10;
try testing.expectEqualSlices(u32, &.{ 10, 20, 30 }, a.constSlice());
}

test "constSlice from const receiver returns read-only view" {
var a: BoundedArray(u32, 3) = .{};
a.push(1);
a.push(2);
const ptr: *const BoundedArray(u32, 3) = &a;
const view = ptr.constSlice();
try testing.expectEqual(@as(usize, 2), view.len);
try testing.expectEqualSlices(u32, &.{ 1, 2 }, view);
}

test "struct payload" {
const Entry = struct { id: u64, value: i32 };
var a: BoundedArray(Entry, 2) = .{};
a.push(.{ .id = 1, .value = -5 });
a.push(.{ .id = 2, .value = 7 });
try testing.expectEqual(@as(u64, 1), a.slice()[0].id);
try testing.expectEqual(@as(i32, 7), a.slice()[1].value);
}

test "comptime construction" {
const a = comptime blk: {
var arr: BoundedArray(u32, 3) = .{};
arr.push(1);
arr.push(2);
break :blk arr;
};
try testing.expectEqual(@as(u32, 2), a.count);
try testing.expectEqualSlices(u32, &.{ 1, 2 }, a.constSlice());
}
Loading
Loading