2019-10-21 23:15:32 +00:00
|
|
|
package slotutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-02-22 23:20:57 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2019-10-21 23:15:32 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-09-22 11:49:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/timeutils"
|
2019-10-21 23:15:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SlotStartTime returns the start time in terms of its unix epoch
|
|
|
|
// value.
|
2021-02-16 07:45:34 +00:00
|
|
|
func SlotStartTime(genesis uint64, slot types.Slot) time.Time {
|
|
|
|
duration := time.Second * time.Duration(slot.Mul(params.BeaconConfig().SecondsPerSlot))
|
2019-10-21 23:15:32 +00:00
|
|
|
startTime := time.Unix(int64(genesis), 0).Add(duration)
|
|
|
|
return startTime
|
|
|
|
}
|
2020-01-02 08:09:28 +00:00
|
|
|
|
|
|
|
// SlotsSinceGenesis returns the number of slots since
|
|
|
|
// the provided genesis time.
|
2021-02-16 07:45:34 +00:00
|
|
|
func SlotsSinceGenesis(genesis time.Time) types.Slot {
|
2020-09-22 11:49:58 +00:00
|
|
|
if genesis.After(timeutils.Now()) { // Genesis has not occurred yet.
|
2020-06-15 21:56:11 +00:00
|
|
|
return 0
|
|
|
|
}
|
2021-02-16 07:45:34 +00:00
|
|
|
return types.Slot(uint64(timeutils.Since(genesis).Seconds()) / params.BeaconConfig().SecondsPerSlot)
|
2020-01-02 08:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EpochsSinceGenesis returns the number of slots since
|
|
|
|
// the provided genesis time.
|
2021-02-09 10:05:22 +00:00
|
|
|
func EpochsSinceGenesis(genesis time.Time) types.Epoch {
|
|
|
|
return types.Epoch(SlotsSinceGenesis(genesis) / params.BeaconConfig().SlotsPerEpoch)
|
2020-01-02 08:09:28 +00:00
|
|
|
}
|
2020-05-13 03:28:17 +00:00
|
|
|
|
|
|
|
// DivideSlotBy divides the SECONDS_PER_SLOT configuration
|
|
|
|
// parameter by a specified number. It returns a value of time.Duration
|
|
|
|
// in milliseconds, useful for dividing values such as 1 second into
|
|
|
|
// millisecond-based durations.
|
|
|
|
func DivideSlotBy(timesPerSlot int64) time.Duration {
|
|
|
|
return time.Duration(int64(params.BeaconConfig().SecondsPerSlot*1000)/timesPerSlot) * time.Millisecond
|
|
|
|
}
|
2021-06-10 21:13:15 +00:00
|
|
|
|
2021-06-17 00:17:52 +00:00
|
|
|
// MultiplySlotBy multiplies the SECONDS_PER_SLOT configuration
|
|
|
|
// parameter by a specified number. It returns a value of time.Duration
|
|
|
|
// in millisecond-based durations.
|
|
|
|
func MultiplySlotBy(times int64) time.Duration {
|
|
|
|
return time.Duration(int64(params.BeaconConfig().SecondsPerSlot)*times) * time.Second
|
|
|
|
}
|
|
|
|
|
2021-06-10 21:13:15 +00:00
|
|
|
// AbsoluteValueSlotDifference between two slots.
|
|
|
|
func AbsoluteValueSlotDifference(x, y types.Slot) uint64 {
|
|
|
|
if x > y {
|
|
|
|
return uint64(x.SubSlot(y))
|
|
|
|
}
|
|
|
|
return uint64(y.SubSlot(x))
|
|
|
|
}
|