Skip to content
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
19 changes: 14 additions & 5 deletions ledger/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,23 @@ func (ls *LedgerState) SlotToTime(slot uint64) (time.Time, error) {
if epoch.StartSlot > math.MaxInt64 ||
epoch.LengthInSlots > math.MaxInt64 ||
epoch.SlotLength > math.MaxInt64 {
return time.Time{}, errors.New("epoch slot values are larger than time.Duration")
return time.Time{}, errors.New(
"epoch slot values are larger than time.Duration",
)
}
if slot < epoch.StartSlot+uint64(epoch.LengthInSlots) {
slotTime = slotTime.Add(
time.Duration(int64(slot)-int64(epoch.StartSlot)) * (time.Duration(epoch.SlotLength) * time.Millisecond),
time.Duration(
int64(slot)-int64(epoch.StartSlot),
) * (time.Duration(epoch.SlotLength) * time.Millisecond),
)
foundSlot = true
break
}
slotTime = slotTime.Add(
time.Duration(epoch.LengthInSlots) * (time.Duration(epoch.SlotLength) * time.Millisecond),
time.Duration(
epoch.LengthInSlots,
) * (time.Duration(epoch.SlotLength) * time.Millisecond),
)
}
if !foundSlot {
Expand All @@ -76,7 +82,9 @@ func (ls *LedgerState) TimeToSlot(t time.Time) (uint64, error) {
for _, epoch := range ls.epochCache {
if epoch.LengthInSlots > math.MaxInt64 ||
epoch.SlotLength > math.MaxInt64 {
return 0, errors.New("epoch slot values are larger than time.Duration")
return 0, errors.New(
"epoch slot values are larger than time.Duration",
)
}
slotDuration := time.Duration(epoch.SlotLength) * time.Millisecond
if slotDuration < 0 {
Expand All @@ -85,7 +93,8 @@ func (ls *LedgerState) TimeToSlot(t time.Time) (uint64, error) {
epochEndTime := epochStartTime.Add(
time.Duration(epoch.LengthInSlots) * slotDuration,
)
if (t.Equal(epochStartTime) || t.After(epochStartTime)) && t.Before(epochEndTime) {
if (t.Equal(epochStartTime) || t.After(epochStartTime)) &&
t.Before(epochEndTime) {
// Figure out how far into the epoch the specified time is
timeDiff := t.Sub(epochStartTime)
// nolint:gosec
Expand Down
33 changes: 27 additions & 6 deletions ledger/slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,18 @@ func TestSlotCalc(t *testing.T) {
epoch: 0,
},
{
slot: 86399,
slotTime: time.Date(2022, time.October, 25, 23, 59, 59, 0, time.UTC),
epoch: 0,
slot: 86399,
slotTime: time.Date(
2022,
time.October,
25,
23,
59,
59,
0,
time.UTC,
),
epoch: 0,
},
{
slot: 86400,
Expand All @@ -104,23 +113,35 @@ func TestSlotCalc(t *testing.T) {
t.Errorf("unexpected error converting slot to time: %s", err)
}
if !tmpSlotToTime.Equal(testDef.slotTime) {
t.Errorf("did not get expected time from slot: got %s, wanted %s", tmpSlotToTime, testDef.slotTime)
t.Errorf(
"did not get expected time from slot: got %s, wanted %s",
tmpSlotToTime,
testDef.slotTime,
)
}
// Time to slot
tmpTimeToSlot, err := testLedgerState.TimeToSlot(testDef.slotTime)
if err != nil {
t.Errorf("unexpected error converting time to slot: %s", err)
}
if tmpTimeToSlot != testDef.slot {
t.Errorf("did not get expected slot from time: got %d, wanted %d", tmpTimeToSlot, testDef.slot)
t.Errorf(
"did not get expected slot from time: got %d, wanted %d",
tmpTimeToSlot,
testDef.slot,
)
}
// Slot to epoch
tmpSlotToEpoch, err := testLedgerState.SlotToEpoch(testDef.slot)
if err != nil {
t.Errorf("unexpected error getting epoch from slot: %s", err)
}
if tmpSlotToEpoch.EpochId != testDef.epoch {
t.Errorf("did not get expected epoch from slot: got %d, wanted %d", tmpSlotToEpoch.EpochId, testDef.epoch)
t.Errorf(
"did not get expected epoch from slot: got %d, wanted %d",
tmpSlotToEpoch.EpochId,
testDef.epoch,
)
}
}
}
Loading