Skip to content

Commit 6bb797e

Browse files
committed
refactor(ancestors): make maxSlot return optional and add insert assertion
- Change maxSlot() to return ?Slot instead of Slot, returning null for empty ancestors - Add assertion in addAncestorWithoutCapacityCheck to ensure slots are inserted in order - Update all call sites in epoch_tracker tests to unwrap the optional
1 parent e250f1f commit 6bb797e

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/core/ancestors.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub const Ancestors = struct {
5252
self: *Ancestors,
5353
slot: Slot,
5454
) void {
55+
const keys = self.ancestors.keys();
56+
std.debug.assert(keys.len == 0 or slot > keys[keys.len - 1]);
5557
self.ancestors.putAssumeCapacity(slot, {});
5658
}
5759

@@ -72,12 +74,10 @@ pub const Ancestors = struct {
7274
return .{ .ancestors = try self.ancestors.clone(allocator) };
7375
}
7476

75-
pub fn maxSlot(self: *const Ancestors) Slot {
76-
var max_slot: Slot = 0;
77-
for (self.ancestors.keys()) |slot| {
78-
if (slot > max_slot) max_slot = slot;
79-
}
80-
return max_slot;
77+
pub fn maxSlot(self: *const Ancestors) ?Slot {
78+
const keys = self.ancestors.keys();
79+
if (keys.len == 0) return null;
80+
return keys[keys.len - 1];
8181
}
8282

8383
pub fn subsetInto(

src/core/epoch_tracker.zig

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ test EpochTracker {
784784

785785
_ = try epoch_tracker.insertUnrootedEpochInfo(
786786
allocator,
787-
branch.maxSlot(),
787+
branch.maxSlot().?,
788788
&branch,
789789
try sig.core.stakes.randomEpochStakes(
790790
allocator,
@@ -794,7 +794,7 @@ test EpochTracker {
794794
&.ALL_DISABLED,
795795
);
796796

797-
try epoch_tracker.onSlotRooted(allocator, branch.maxSlot(), &branch);
797+
try epoch_tracker.onSlotRooted(allocator, branch.maxSlot().?, &branch);
798798
}
799799

800800
// Check that the root slot is 9 * 32 and epochs 6, 7, 8, 9 are available
@@ -815,10 +815,10 @@ test EpochTracker {
815815
// Check that trying to insert epoch info for epoch 9 fails because it is already rooted
816816
const branch = try Ancestors.initWithSlots(allocator, &.{319});
817817
defer branch.deinit(allocator);
818-
fail_stakes.stakes.epoch = epoch_schedule.getEpoch(branch.maxSlot());
818+
fail_stakes.stakes.epoch = epoch_schedule.getEpoch(branch.maxSlot().?);
819819
try std.testing.expectError(error.InvalidInsert, epoch_tracker.insertUnrootedEpochInfo(
820820
allocator,
821-
branch.maxSlot(),
821+
branch.maxSlot().?,
822822
&branch,
823823
fail_stakes,
824824
&.ALL_DISABLED,
@@ -845,12 +845,12 @@ test EpochTracker {
845845
defer allocator.free(insert_ptrs);
846846
for (0..4) |i| insert_ptrs[i] = try epoch_tracker.insertUnrootedEpochInfo(
847847
allocator,
848-
branches[i].maxSlot(),
848+
branches[i].maxSlot().?,
849849
&branches[i],
850850
try sig.core.stakes.randomEpochStakes(
851851
allocator,
852852
random,
853-
.{ .epoch = epoch_schedule.getEpoch(branches[i].maxSlot()) },
853+
.{ .epoch = epoch_schedule.getEpoch(branches[i].maxSlot().?) },
854854
),
855855
&.ALL_DISABLED,
856856
);
@@ -862,31 +862,31 @@ test EpochTracker {
862862
);
863863

864864
// Check that another insert hits max forks
865-
fail_stakes.stakes.epoch = epoch_schedule.getEpoch(branches[4].maxSlot());
865+
fail_stakes.stakes.epoch = epoch_schedule.getEpoch(branches[4].maxSlot().?);
866866
try std.testing.expectError(error.MaxForksExceeded, epoch_tracker.insertUnrootedEpochInfo(
867867
allocator,
868-
branches[4].maxSlot(),
868+
branches[4].maxSlot().?,
869869
&branches[4],
870870
fail_stakes,
871871
&.ALL_DISABLED,
872872
));
873873

874874
// Root the first slot from branch 2
875-
try epoch_tracker.onSlotRooted(allocator, branches[2].maxSlot(), &branches[2]);
875+
try epoch_tracker.onSlotRooted(allocator, branches[2].maxSlot().?, &branches[2]);
876876

877877
// Check that the pointers returned from insert match the pointers returned from get
878878
for (0..4) |i| try std.testing.expectEqual(
879879
insert_ptrs[i],
880880
if (i != 2)
881881
try epoch_tracker.unrooted_epochs.get(&branches[i])
882882
else
883-
try epoch_tracker.rooted_epochs.get(epoch_schedule.getEpoch(branches[i].maxSlot())),
883+
try epoch_tracker.rooted_epochs.get(epoch_schedule.getEpoch(branches[i].maxSlot().?)),
884884
);
885885

886886
// Check we can't insert unrooted if the epoch is already rooted
887887
try std.testing.expectError(error.InvalidInsert, epoch_tracker.insertUnrootedEpochInfo(
888888
allocator,
889-
branches[4].maxSlot(),
889+
branches[4].maxSlot().?,
890890
&branches[4],
891891
fail_stakes,
892892
&.ALL_DISABLED,

0 commit comments

Comments
 (0)