2019-02-01 16:52:35 +00:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SlotToEpoch returns the epoch number of the input slot.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// def slot_to_epoch(slot: SlotNumber) -> EpochNumber:
|
2019-02-18 16:52:16 +00:00
|
|
|
// return slot // SLOTS_PER_EPOCH
|
2019-02-01 16:52:35 +00:00
|
|
|
func SlotToEpoch(slot uint64) uint64 {
|
2019-02-18 16:52:16 +00:00
|
|
|
return slot / params.BeaconConfig().SlotsPerEpoch
|
2019-02-01 16:52:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CurrentEpoch returns the current epoch number calculated from
|
|
|
|
// the slot number stored in beacon state.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// def get_current_epoch(state: BeaconState) -> EpochNumber:
|
|
|
|
// return slot_to_epoch(state.slot)
|
|
|
|
func CurrentEpoch(state *pb.BeaconState) uint64 {
|
|
|
|
return SlotToEpoch(state.Slot)
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrevEpoch returns the previous epoch number calculated from
|
|
|
|
// the slot number stored in beacon state. It also checks for
|
|
|
|
// underflow condition.
|
|
|
|
func PrevEpoch(state *pb.BeaconState) uint64 {
|
2019-02-25 21:16:12 +00:00
|
|
|
if SlotToEpoch(state.Slot) == params.BeaconConfig().GenesisEpoch {
|
|
|
|
return params.BeaconConfig().GenesisEpoch
|
2019-02-01 16:52:35 +00:00
|
|
|
}
|
|
|
|
return SlotToEpoch(state.Slot) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextEpoch returns the next epoch number calculated form
|
|
|
|
// the slot number stored in beacon state.
|
|
|
|
func NextEpoch(state *pb.BeaconState) uint64 {
|
|
|
|
return SlotToEpoch(state.Slot) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartSlot returns the first slot number of the
|
|
|
|
// current epoch.
|
|
|
|
//
|
|
|
|
// Spec pseudocode definition:
|
|
|
|
// def get_epoch_start_slot(epoch: EpochNumber) -> SlotNumber:
|
2019-02-18 16:52:16 +00:00
|
|
|
// return epoch * SLOTS_PER_EPOCH
|
2019-02-01 16:52:35 +00:00
|
|
|
func StartSlot(epoch uint64) uint64 {
|
2019-02-18 16:52:16 +00:00
|
|
|
return epoch * params.BeaconConfig().SlotsPerEpoch
|
2019-02-01 16:52:35 +00:00
|
|
|
}
|
2019-02-06 16:44:27 +00:00
|
|
|
|
|
|
|
// AttestationCurrentEpoch returns the current epoch referenced by the attestation.
|
|
|
|
func AttestationCurrentEpoch(att *pb.AttestationData) uint64 {
|
|
|
|
return SlotToEpoch(att.Slot)
|
|
|
|
}
|