Clean up state types (#11916)

* Clean up state types

* rename package
This commit is contained in:
Radosław Kapka
2023-01-26 15:40:12 +01:00
committed by GitHub
parent a296a0c783
commit 77d3ccb9ad
555 changed files with 4696 additions and 4958 deletions

View File

@@ -4,14 +4,14 @@ package slots
import (
"time"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
prysmTime "github.com/prysmaticlabs/prysm/v3/time"
)
// The Ticker interface defines a type which can expose a
// receive-only channel firing slot events.
type Ticker interface {
C() <-chan types.Slot
C() <-chan primitives.Slot
Done()
}
@@ -22,13 +22,13 @@ type Ticker interface {
// multiple of the slot duration.
// In addition, the channel returns the new slot number.
type SlotTicker struct {
c chan types.Slot
c chan primitives.Slot
done chan struct{}
}
// C returns the ticker channel. Call Cancel afterwards to ensure
// that the goroutine exits cleanly.
func (s *SlotTicker) C() <-chan types.Slot {
func (s *SlotTicker) C() <-chan primitives.Slot {
return s.c
}
@@ -45,7 +45,7 @@ func NewSlotTicker(genesisTime time.Time, secondsPerSlot uint64) *SlotTicker {
panic("zero genesis time")
}
ticker := &SlotTicker{
c: make(chan types.Slot),
c: make(chan primitives.Slot),
done: make(chan struct{}),
}
ticker.start(genesisTime, secondsPerSlot, prysmTime.Since, prysmTime.Until, time.After)
@@ -62,7 +62,7 @@ func NewSlotTickerWithOffset(genesisTime time.Time, offset time.Duration, second
panic("invalid ticker offset")
}
ticker := &SlotTicker{
c: make(chan types.Slot),
c: make(chan primitives.Slot),
done: make(chan struct{}),
}
ticker.start(genesisTime.Add(offset), secondsPerSlot, prysmTime.Since, prysmTime.Until, time.After)
@@ -81,7 +81,7 @@ func (s *SlotTicker) start(
sinceGenesis := since(genesisTime)
var nextTickTime time.Time
var slot types.Slot
var slot primitives.Slot
if sinceGenesis < d {
// Handle when the current time is before the genesis time.
nextTickTime = genesisTime
@@ -89,7 +89,7 @@ func (s *SlotTicker) start(
} else {
nextTick := sinceGenesis.Truncate(d) + d
nextTickTime = genesisTime.Add(nextTick)
slot = types.Slot(nextTick / d)
slot = primitives.Slot(nextTick / d)
}
for {

View File

@@ -4,14 +4,14 @@ import (
"testing"
"time"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
)
var _ Ticker = (*SlotTicker)(nil)
func TestSlotTicker(t *testing.T) {
ticker := &SlotTicker{
c: make(chan types.Slot),
c: make(chan primitives.Slot),
done: make(chan struct{}),
}
defer ticker.Done()
@@ -66,7 +66,7 @@ func TestSlotTicker(t *testing.T) {
func TestSlotTickerGenesis(t *testing.T) {
ticker := &SlotTicker{
c: make(chan types.Slot),
c: make(chan primitives.Slot),
done: make(chan struct{}),
}
defer ticker.Done()

View File

@@ -7,7 +7,7 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v3/config/params"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
mathutil "github.com/prysmaticlabs/prysm/v3/math"
prysmTime "github.com/prysmaticlabs/prysm/v3/time"
)
@@ -18,7 +18,7 @@ const MaxSlotBuffer = uint64(1 << 7)
// StartTime returns the start time in terms of its unix epoch
// value.
func StartTime(genesis uint64, slot types.Slot) time.Time {
func StartTime(genesis uint64, slot primitives.Slot) time.Time {
duration := time.Second * time.Duration(slot.Mul(params.BeaconConfig().SecondsPerSlot))
startTime := time.Unix(int64(genesis), 0).Add(duration) // lint:ignore uintcast -- Genesis timestamp will not exceed int64 in your lifetime.
return startTime
@@ -26,17 +26,17 @@ func StartTime(genesis uint64, slot types.Slot) time.Time {
// SinceGenesis returns the number of slots since
// the provided genesis time.
func SinceGenesis(genesis time.Time) types.Slot {
func SinceGenesis(genesis time.Time) primitives.Slot {
if genesis.After(prysmTime.Now()) { // Genesis has not occurred yet.
return 0
}
return types.Slot(uint64(prysmTime.Since(genesis).Seconds()) / params.BeaconConfig().SecondsPerSlot)
return primitives.Slot(uint64(prysmTime.Since(genesis).Seconds()) / params.BeaconConfig().SecondsPerSlot)
}
// EpochsSinceGenesis returns the number of epochs since
// the provided genesis time.
func EpochsSinceGenesis(genesis time.Time) types.Epoch {
return types.Epoch(SinceGenesis(genesis) / params.BeaconConfig().SlotsPerEpoch)
func EpochsSinceGenesis(genesis time.Time) primitives.Epoch {
return primitives.Epoch(SinceGenesis(genesis) / params.BeaconConfig().SlotsPerEpoch)
}
// DivideSlotBy divides the SECONDS_PER_SLOT configuration
@@ -55,7 +55,7 @@ func MultiplySlotBy(times int64) time.Duration {
}
// AbsoluteValueSlotDifference between two slots.
func AbsoluteValueSlotDifference(x, y types.Slot) uint64 {
func AbsoluteValueSlotDifference(x, y primitives.Slot) uint64 {
if x > y {
return uint64(x.SubSlot(y))
}
@@ -71,8 +71,8 @@ func AbsoluteValueSlotDifference(x, y types.Slot) uint64 {
// Return the epoch number at ``slot``.
// """
// return Epoch(slot // SLOTS_PER_EPOCH)
func ToEpoch(slot types.Slot) types.Epoch {
return types.Epoch(slot.DivSlot(params.BeaconConfig().SlotsPerEpoch))
func ToEpoch(slot primitives.Slot) primitives.Epoch {
return primitives.Epoch(slot.DivSlot(params.BeaconConfig().SlotsPerEpoch))
}
// EpochStart returns the first slot number of the
@@ -85,7 +85,7 @@ func ToEpoch(slot types.Slot) types.Epoch {
// Return the start slot of ``epoch``.
// """
// return Slot(epoch * SLOTS_PER_EPOCH)
func EpochStart(epoch types.Epoch) (types.Slot, error) {
func EpochStart(epoch primitives.Epoch) (primitives.Slot, error) {
slot, err := params.BeaconConfig().SlotsPerEpoch.SafeMul(uint64(epoch))
if err != nil {
return slot, errors.Errorf("start slot calculation overflows: %v", err)
@@ -95,7 +95,7 @@ func EpochStart(epoch types.Epoch) (types.Slot, error) {
// EpochEnd returns the last slot number of the
// current epoch.
func EpochEnd(epoch types.Epoch) (types.Slot, error) {
func EpochEnd(epoch primitives.Epoch) (primitives.Slot, error) {
if epoch == math.MaxUint64 {
return 0, errors.New("start slot calculation overflows")
}
@@ -108,23 +108,23 @@ func EpochEnd(epoch types.Epoch) (types.Slot, error) {
// IsEpochStart returns true if the given slot number is an epoch starting slot
// number.
func IsEpochStart(slot types.Slot) bool {
func IsEpochStart(slot primitives.Slot) bool {
return slot%params.BeaconConfig().SlotsPerEpoch == 0
}
// IsEpochEnd returns true if the given slot number is an epoch ending slot
// number.
func IsEpochEnd(slot types.Slot) bool {
func IsEpochEnd(slot primitives.Slot) bool {
return IsEpochStart(slot + 1)
}
// SinceEpochStarts returns number of slots since the start of the epoch.
func SinceEpochStarts(slot types.Slot) types.Slot {
func SinceEpochStarts(slot primitives.Slot) primitives.Slot {
return slot % params.BeaconConfig().SlotsPerEpoch
}
// VerifyTime validates the input slot is not from the future.
func VerifyTime(genesisTime uint64, slot types.Slot, timeTolerance time.Duration) error {
func VerifyTime(genesisTime uint64, slot primitives.Slot, timeTolerance time.Duration) error {
slotTime, err := ToTime(genesisTime, slot)
if err != nil {
return err
@@ -146,7 +146,7 @@ func VerifyTime(genesisTime uint64, slot types.Slot, timeTolerance time.Duration
}
// ToTime takes the given slot and genesis time to determine the start time of the slot.
func ToTime(genesisTimeSec uint64, slot types.Slot) (time.Time, error) {
func ToTime(genesisTimeSec uint64, slot primitives.Slot) (time.Time, error) {
timeSinceGenesis, err := slot.SafeMul(params.BeaconConfig().SecondsPerSlot)
if err != nil {
return time.Unix(0, 0), fmt.Errorf("slot (%d) is in the far distant future: %w", slot, err)
@@ -159,24 +159,24 @@ func ToTime(genesisTimeSec uint64, slot types.Slot) (time.Time, error) {
}
// Since computes the number of time slots that have occurred since the given timestamp.
func Since(time time.Time) types.Slot {
func Since(time time.Time) primitives.Slot {
return CurrentSlot(uint64(time.Unix()))
}
// CurrentSlot returns the current slot as determined by the local clock and
// provided genesis time.
func CurrentSlot(genesisTimeSec uint64) types.Slot {
func CurrentSlot(genesisTimeSec uint64) primitives.Slot {
now := uint64(prysmTime.Now().Unix())
if now < genesisTimeSec {
return 0
}
return types.Slot((now - genesisTimeSec) / params.BeaconConfig().SecondsPerSlot)
return primitives.Slot((now - genesisTimeSec) / params.BeaconConfig().SecondsPerSlot)
}
// ValidateClock validates a provided slot against the local
// clock to ensure slots that are unreasonable are returned with
// an error.
func ValidateClock(slot types.Slot, genesisTimeSec uint64) error {
func ValidateClock(slot primitives.Slot, genesisTimeSec uint64) error {
maxPossibleSlot := CurrentSlot(genesisTimeSec).Add(MaxSlotBuffer)
// Defensive check to ensure that we only process slots up to a hard limit
// from our local clock.
@@ -187,7 +187,7 @@ func ValidateClock(slot types.Slot, genesisTimeSec uint64) error {
}
// RoundUpToNearestEpoch rounds up the provided slot value to the nearest epoch.
func RoundUpToNearestEpoch(slot types.Slot) types.Slot {
func RoundUpToNearestEpoch(slot primitives.Slot) primitives.Slot {
if slot%params.BeaconConfig().SlotsPerEpoch != 0 {
slot -= slot % params.BeaconConfig().SlotsPerEpoch
slot += params.BeaconConfig().SlotsPerEpoch
@@ -197,14 +197,14 @@ func RoundUpToNearestEpoch(slot types.Slot) types.Slot {
// VotingPeriodStartTime returns the current voting period's start time
// depending on the provided genesis and current slot.
func VotingPeriodStartTime(genesis uint64, slot types.Slot) uint64 {
func VotingPeriodStartTime(genesis uint64, slot primitives.Slot) uint64 {
slots := params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().EpochsPerEth1VotingPeriod))
startTime := uint64((slot - slot.ModSlot(slots)).Mul(params.BeaconConfig().SecondsPerSlot))
return genesis + startTime
}
// PrevSlot returns previous slot, with an exception in slot 0 to prevent underflow.
func PrevSlot(slot types.Slot) types.Slot {
func PrevSlot(slot primitives.Slot) primitives.Slot {
if slot > 0 {
return slot.Sub(1)
}
@@ -217,16 +217,16 @@ func PrevSlot(slot types.Slot) types.Slot {
// def compute_sync_committee_period(epoch: Epoch) -> uint64:
//
// return epoch // EPOCHS_PER_SYNC_COMMITTEE_PERIOD
func SyncCommitteePeriod(e types.Epoch) uint64 {
func SyncCommitteePeriod(e primitives.Epoch) uint64 {
return uint64(e / params.BeaconConfig().EpochsPerSyncCommitteePeriod)
}
// SyncCommitteePeriodStartEpoch returns the start epoch of a sync committee period.
func SyncCommitteePeriodStartEpoch(e types.Epoch) (types.Epoch, error) {
func SyncCommitteePeriodStartEpoch(e primitives.Epoch) (primitives.Epoch, error) {
// Overflow is impossible here because of division of `EPOCHS_PER_SYNC_COMMITTEE_PERIOD`.
startEpoch, err := mathutil.Mul64(SyncCommitteePeriod(e), uint64(params.BeaconConfig().EpochsPerSyncCommitteePeriod))
if err != nil {
return 0, err
}
return types.Epoch(startEpoch), nil
return primitives.Epoch(startEpoch), nil
}

View File

@@ -6,7 +6,7 @@ import (
"time"
"github.com/prysmaticlabs/prysm/v3/config/params"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
prysmTime "github.com/prysmaticlabs/prysm/v3/time"
@@ -19,7 +19,7 @@ func TestSlotsSinceGenesis(t *testing.T) {
tests := []struct {
name string
args args
want types.Slot
want primitives.Slot
}{
{
name: "pre-genesis",
@@ -47,8 +47,8 @@ func TestSlotsSinceGenesis(t *testing.T) {
func TestAbsoluteValueSlotDifference(t *testing.T) {
type args struct {
x types.Slot
y types.Slot
x primitives.Slot
y primitives.Slot
}
tests := []struct {
name string
@@ -58,24 +58,24 @@ func TestAbsoluteValueSlotDifference(t *testing.T) {
{
name: "x_<_y",
args: args{
x: types.Slot(3),
y: types.Slot(4),
x: primitives.Slot(3),
y: primitives.Slot(4),
},
want: 1,
},
{
name: "x_>_y",
args: args{
x: types.Slot(100),
y: types.Slot(4),
x: primitives.Slot(100),
y: primitives.Slot(4),
},
want: 96,
},
{
name: "x_==_y",
args: args{
x: types.Slot(100),
y: types.Slot(100),
x: primitives.Slot(100),
y: primitives.Slot(100),
},
want: 0,
},
@@ -131,8 +131,8 @@ func TestMultiplySlotBy(t *testing.T) {
func TestEpochStartSlot_OK(t *testing.T) {
tests := []struct {
epoch types.Epoch
startSlot types.Slot
epoch primitives.Epoch
startSlot primitives.Slot
error bool
}{
{epoch: 0, startSlot: 0 * params.BeaconConfig().SlotsPerEpoch, error: false},
@@ -155,8 +155,8 @@ func TestEpochStartSlot_OK(t *testing.T) {
func TestEpochEndSlot_OK(t *testing.T) {
tests := []struct {
epoch types.Epoch
startSlot types.Slot
epoch primitives.Epoch
startSlot primitives.Slot
error bool
}{
{epoch: 0, startSlot: 1*params.BeaconConfig().SlotsPerEpoch - 1, error: false},
@@ -181,7 +181,7 @@ func TestIsEpochStart(t *testing.T) {
epochLength := params.BeaconConfig().SlotsPerEpoch
tests := []struct {
slot types.Slot
slot primitives.Slot
result bool
}{
{
@@ -211,7 +211,7 @@ func TestIsEpochEnd(t *testing.T) {
epochLength := params.BeaconConfig().SlotsPerEpoch
tests := []struct {
slot types.Slot
slot primitives.Slot
result bool
}{
{
@@ -235,8 +235,8 @@ func TestIsEpochEnd(t *testing.T) {
func TestSlotsSinceEpochStarts(t *testing.T) {
tests := []struct {
slots types.Slot
wantedSlots types.Slot
slots primitives.Slot
wantedSlots primitives.Slot
}{
{slots: 0, wantedSlots: 0},
{slots: 1, wantedSlots: 1},
@@ -251,8 +251,8 @@ func TestSlotsSinceEpochStarts(t *testing.T) {
func TestRoundUpToNearestEpoch_OK(t *testing.T) {
tests := []struct {
startSlot types.Slot
roundedUpSlot types.Slot
startSlot primitives.Slot
roundedUpSlot primitives.Slot
}{
{startSlot: 0 * params.BeaconConfig().SlotsPerEpoch, roundedUpSlot: 0},
{startSlot: 1*params.BeaconConfig().SlotsPerEpoch - 10, roundedUpSlot: 1 * params.BeaconConfig().SlotsPerEpoch},
@@ -266,7 +266,7 @@ func TestRoundUpToNearestEpoch_OK(t *testing.T) {
func TestSlotToTime(t *testing.T) {
type args struct {
genesisTimeSec uint64
slot types.Slot
slot primitives.Slot
}
tests := []struct {
name string
@@ -323,7 +323,7 @@ func TestSlotToTime(t *testing.T) {
func TestVerifySlotTime(t *testing.T) {
type args struct {
genesisTime int64
slot types.Slot
slot primitives.Slot
timeTolerance time.Duration
}
tests := []struct {
@@ -357,7 +357,7 @@ func TestVerifySlotTime(t *testing.T) {
name: "max future slot",
args: args{
genesisTime: prysmTime.Now().Add(-1 * 5 * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second).Unix(),
slot: types.Slot(MaxSlotBuffer + 6),
slot: primitives.Slot(MaxSlotBuffer + 6),
},
wantedErr: "exceeds max allowed value relative to the local clock",
},
@@ -367,7 +367,7 @@ func TestVerifySlotTime(t *testing.T) {
genesisTime: prysmTime.Now().Add(-1 * 24 * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second).Unix(), // 24 slots in the past
// Gets multiplied with slot duration, and results in an overflow. Wraps around to a valid time.
// Lower than max signed int. And chosen specifically to wrap to a valid slot 24
slot: types.Slot((^uint64(0))/params.BeaconConfig().SecondsPerSlot) + 24,
slot: primitives.Slot((^uint64(0))/params.BeaconConfig().SecondsPerSlot) + 24,
},
wantedErr: "is in the far distant future",
},
@@ -387,17 +387,17 @@ func TestVerifySlotTime(t *testing.T) {
func TestValidateSlotClock_HandlesBadSlot(t *testing.T) {
genTime := prysmTime.Now().Add(-1 * time.Duration(MaxSlotBuffer) * time.Duration(params.BeaconConfig().SecondsPerSlot) * time.Second).Unix()
assert.NoError(t, ValidateClock(types.Slot(MaxSlotBuffer), uint64(genTime)), "unexpected error validating slot")
assert.NoError(t, ValidateClock(types.Slot(2*MaxSlotBuffer), uint64(genTime)), "unexpected error validating slot")
assert.ErrorContains(t, "which exceeds max allowed value relative to the local clock", ValidateClock(types.Slot(2*MaxSlotBuffer+1), uint64(genTime)), "no error from bad slot")
assert.NoError(t, ValidateClock(primitives.Slot(MaxSlotBuffer), uint64(genTime)), "unexpected error validating slot")
assert.NoError(t, ValidateClock(primitives.Slot(2*MaxSlotBuffer), uint64(genTime)), "unexpected error validating slot")
assert.ErrorContains(t, "which exceeds max allowed value relative to the local clock", ValidateClock(primitives.Slot(2*MaxSlotBuffer+1), uint64(genTime)), "no error from bad slot")
assert.ErrorContains(t, "which exceeds max allowed value relative to the local clock", ValidateClock(1<<63, uint64(genTime)), "no error from bad slot")
}
func TestPrevSlot(t *testing.T) {
tests := []struct {
name string
slot types.Slot
want types.Slot
slot primitives.Slot
want primitives.Slot
}{
{
name: "no underflow",
@@ -431,7 +431,7 @@ func TestPrevSlot(t *testing.T) {
func TestSyncCommitteePeriod(t *testing.T) {
tests := []struct {
epoch types.Epoch
epoch primitives.Epoch
wanted uint64
}{
{epoch: 0, wanted: 0},
@@ -446,8 +446,8 @@ func TestSyncCommitteePeriod(t *testing.T) {
func TestSyncCommitteePeriodStartEpoch(t *testing.T) {
tests := []struct {
epoch types.Epoch
wanted types.Epoch
epoch primitives.Epoch
wanted primitives.Epoch
}{
{epoch: 0, wanted: 0},
{epoch: params.BeaconConfig().EpochsPerSyncCommitteePeriod + 1, wanted: params.BeaconConfig().EpochsPerSyncCommitteePeriod},

View File

@@ -1,16 +1,16 @@
// Package testing includes useful mocks for slot tickers in unit tests.
package testing
import types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
import "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
// MockTicker defines a useful struct for mocking the Ticker interface
// from the slotutil package.
type MockTicker struct {
Channel chan types.Slot
Channel chan primitives.Slot
}
// C --
func (m *MockTicker) C() <-chan types.Slot {
func (m *MockTicker) C() <-chan primitives.Slot {
return m.Channel
}