2019-10-21 23:15:32 +00:00
|
|
|
package slotutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-01-02 08:09:28 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/roughtime"
|
2019-10-21 23:15:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SlotStartTime returns the start time in terms of its unix epoch
|
|
|
|
// value.
|
|
|
|
func SlotStartTime(genesis uint64, slot uint64) time.Time {
|
|
|
|
duration := time.Second * time.Duration(slot*params.BeaconConfig().SecondsPerSlot)
|
|
|
|
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.
|
|
|
|
func SlotsSinceGenesis(genesis time.Time) uint64 {
|
2020-06-15 21:56:11 +00:00
|
|
|
if genesis.After(roughtime.Now()) { // Genesis has not occurred yet.
|
|
|
|
return 0
|
|
|
|
}
|
2020-01-02 08:09:28 +00:00
|
|
|
return uint64(roughtime.Since(genesis).Seconds()) / params.BeaconConfig().SecondsPerSlot
|
|
|
|
}
|
|
|
|
|
|
|
|
// EpochsSinceGenesis returns the number of slots since
|
|
|
|
// the provided genesis time.
|
|
|
|
func EpochsSinceGenesis(genesis time.Time) uint64 {
|
|
|
|
return SlotsSinceGenesis(genesis) / params.BeaconConfig().SlotsPerEpoch
|
|
|
|
}
|
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
|
|
|
|
}
|