Use time.Time instead of uint64 for genesis time (#15419)

* Convert genesis times from seconds to time.Time

* Fixing failed forkchoice tests in a new commit so it doesn't get worse

Fixing failed spectest tests in a new commit so it doesn't get worse

Fixing forkchoice tests, then spectests

* Fixing forkchoice tests, then spectests. Now asking for help...

* Fix TestForkChoice_GetProposerHead

* Fix broken build

* Resolve TODO(preston) items

* Changelog fragment

* Resolve TODO(preston) items again

* Resolve lint issues

* Use consistant field names for sinceSlotStart (no spaces)

* Manu's feedback

* Renamed StartTime -> UnsafeStartTime, marked as deprecated because it doesn't handle overflow scenarios.
Renamed SlotTime -> StartTime
Renamed SlotAt -> At
Handled the error in cases where StartTime was used.

@james-prysm feedback

* Revert beacon-chain/blockchain/receive_block_test.go from 1b7844de

* Fixing issues after rebase

* Accepted suggestions from @potuz

* Remove CanonicalHeadSlot from merge conflicts

---------

Co-authored-by: potuz <potuz@prysmaticlabs.com>
This commit is contained in:
Preston Van Loon
2025-07-14 16:04:50 -05:00
committed by GitHub
parent 56e8881bc1
commit 499d27b6ae
143 changed files with 659 additions and 595 deletions

View File

@@ -6,6 +6,7 @@ package state
import (
"context"
"encoding/json"
"time"
customtypes "github.com/OffchainLabs/prysm/v6/beacon-chain/state/state-native/custom-types"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -64,7 +65,7 @@ type ReadOnlyBeaconState interface {
ReadOnlyProposerLookahead
ToProtoUnsafe() interface{}
ToProto() interface{}
GenesisTime() uint64
GenesisTime() time.Time
GenesisValidatorsRoot() []byte
Slot() primitives.Slot
Fork() *ethpb.Fork
@@ -97,7 +98,7 @@ type WriteOnlyBeaconState interface {
WriteOnlyWithdrawals
WriteOnlyDeposits
WriteOnlyProposerLookahead
SetGenesisTime(val uint64) error
SetGenesisTime(val time.Time) error
SetGenesisValidatorsRoot(val []byte) error
SetSlot(val primitives.Slot) error
SetFork(val *ethpb.Fork) error

View File

@@ -16,6 +16,11 @@ import (
// BeaconState defines a struct containing utilities for the Ethereum Beacon Chain state, defining
// getters and setters for its respective values and helpful functions such as HashTreeRoot().
//
// Note: genesisTime is time.Time.Unix(). i.e. the number of seconds elapsed since January 1, 1970 UTC.
// This is preferred over time.Time in the state to avoid unnecessary conversions and precision issues
// that may break spec compliance. Other areas of Prysm should use time.Time, except when complying
// with spec.
type BeaconState struct {
version int
genesisTime uint64

View File

@@ -1,6 +1,8 @@
package state_native
import (
"time"
"github.com/OffchainLabs/prysm/v6/consensus-types/primitives"
ethpb "github.com/OffchainLabs/prysm/v6/proto/prysm/v1alpha1"
"github.com/OffchainLabs/prysm/v6/runtime/version"
@@ -11,12 +13,12 @@ func (b *BeaconState) Id() uint64 {
return b.id
}
// GenesisTime of the beacon state as a uint64.
func (b *BeaconState) GenesisTime() uint64 {
// GenesisTime of the beacon state as a time.Time.
func (b *BeaconState) GenesisTime() time.Time {
b.lock.RLock()
defer b.lock.RUnlock()
return b.genesisTime
return time.Unix(int64(b.genesisTime), 0)
}
// GenesisValidatorsRoot of the beacon state.

View File

@@ -2,6 +2,7 @@ package state_native_test
import (
"testing"
"time"
statenative "github.com/OffchainLabs/prysm/v6/beacon-chain/state/state-native"
"github.com/OffchainLabs/prysm/v6/config/params"
@@ -18,7 +19,7 @@ import (
func TestComputeFieldRootsWithHasher_Phase0(t *testing.T) {
beaconState, err := util.NewBeaconState(util.FillRootsNaturalOpt)
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(123))
require.NoError(t, beaconState.SetGenesisTime(time.Unix(123, 0)))
require.NoError(t, beaconState.SetGenesisValidatorsRoot(genesisValidatorsRoot()))
require.NoError(t, beaconState.SetSlot(123))
require.NoError(t, beaconState.SetFork(fork()))
@@ -82,7 +83,7 @@ func TestComputeFieldRootsWithHasher_Phase0(t *testing.T) {
func TestComputeFieldRootsWithHasher_Altair(t *testing.T) {
beaconState, err := util.NewBeaconStateAltair(util.FillRootsNaturalOptAltair)
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(123))
require.NoError(t, beaconState.SetGenesisTime(time.Unix(123, 0)))
require.NoError(t, beaconState.SetGenesisValidatorsRoot(genesisValidatorsRoot()))
require.NoError(t, beaconState.SetSlot(123))
require.NoError(t, beaconState.SetFork(fork()))
@@ -152,7 +153,7 @@ func TestComputeFieldRootsWithHasher_Altair(t *testing.T) {
func TestComputeFieldRootsWithHasher_Bellatrix(t *testing.T) {
beaconState, err := util.NewBeaconStateBellatrix(util.FillRootsNaturalOptBellatrix)
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(123))
require.NoError(t, beaconState.SetGenesisTime(time.Unix(123, 0)))
require.NoError(t, beaconState.SetGenesisValidatorsRoot(genesisValidatorsRoot()))
require.NoError(t, beaconState.SetSlot(123))
require.NoError(t, beaconState.SetFork(fork()))
@@ -226,7 +227,7 @@ func TestComputeFieldRootsWithHasher_Bellatrix(t *testing.T) {
func TestComputeFieldRootsWithHasher_Capella(t *testing.T) {
beaconState, err := util.NewBeaconStateCapella(util.FillRootsNaturalOptCapella)
require.NoError(t, err)
require.NoError(t, beaconState.SetGenesisTime(123))
require.NoError(t, beaconState.SetGenesisTime(time.Unix(123, 0)))
require.NoError(t, beaconState.SetGenesisValidatorsRoot(genesisValidatorsRoot()))
require.NoError(t, beaconState.SetSlot(123))
require.NoError(t, beaconState.SetFork(fork()))

View File

@@ -1,6 +1,8 @@
package state_native
import (
"time"
"github.com/OffchainLabs/prysm/v6/beacon-chain/state/state-native/types"
"github.com/OffchainLabs/prysm/v6/beacon-chain/state/stateutil"
fieldparams "github.com/OffchainLabs/prysm/v6/config/fieldparams"
@@ -43,11 +45,11 @@ const (
)
// SetGenesisTime for the beacon state.
func (b *BeaconState) SetGenesisTime(val uint64) error {
func (b *BeaconState) SetGenesisTime(val time.Time) error {
b.lock.Lock()
defer b.lock.Unlock()
b.genesisTime = val
b.genesisTime = uint64(val.Unix())
b.markFieldAsDirty(types.GenesisTime)
return nil
}