Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node-api) : Implemented genesis time #2014

Draft
wants to merge 23 commits into
base: beacon-endpoints
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1cde1bc
Introduce genesis time
nidhi-singh02 Sep 19, 2024
dc4a4c5
trying to get from initiliaze state
nidhi-singh02 Sep 20, 2024
8329623
executionPayloadHeader
nidhi-singh02 Sep 20, 2024
d0d1df9
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Sep 20, 2024
d8daecd
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Sep 25, 2024
9902e1f
remove uncessary changes from examples
nidhi-singh02 Sep 25, 2024
206d0f8
getting genesis time - works
nidhi-singh02 Sep 25, 2024
988a1e5
cleanup of code
nidhi-singh02 Sep 25, 2024
5a6d4f5
fmt cleanup
nidhi-singh02 Sep 25, 2024
9e44788
time is sec, get from executionPayloadHeader
nidhi-singh02 Sep 25, 2024
8098e3f
generate check
nidhi-singh02 Sep 25, 2024
c16d30a
gosec for genesis time
nidhi-singh02 Sep 25, 2024
66d5082
cleanup
nidhi-singh02 Sep 25, 2024
d28d42e
cleanup
nidhi-singh02 Sep 25, 2024
6b32380
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Sep 27, 2024
f2a4802
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Sep 30, 2024
dcbbbf8
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Sep 30, 2024
0c3b063
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Sep 30, 2024
46022e1
as mockery updated, generate-check
nidhi-singh02 Sep 30, 2024
074dfb4
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Oct 4, 2024
a21e77c
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Oct 4, 2024
6d856c9
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Nov 4, 2024
64e6eed
Merge branch 'beacon-endpoints' into genesis-time
nidhi-singh02 Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mod/cli/pkg/commands/genesis/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command {
return errors.Wrap(err, "failed to read genesis doc from file")
}

//#nosec:G701 // genesis time won't ever be negative i.e.
// before Unix epoch (1st Jan 1970)
// Set the genesis time from app Genesis in the execution payload
// which gets converted to ExecutionPayloadHeader.
payload.Timestamp = uint64(appGenesis.GenesisTime.Unix())

// create the app state
appGenesisState, err := genutiltypes.GenesisStateFromAppGenesis(
appGenesis,
Expand Down
11 changes: 11 additions & 0 deletions mod/node-api/backend/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ func (b Backend[
}
return fork.GetPreviousVersion(), nil
}

// GetGenesisTime returns the genesis time of the beacon chain.
func (b Backend[
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
]) GetGenesisTime(slot math.Slot) (uint64, error) {
st, _, err := b.stateFromSlot(slot)
if err != nil {
return 0, err
}
return st.GetGenesisTime()
}
55 changes: 55 additions & 0 deletions mod/node-api/backend/mocks/beacon_state.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mod/node-api/handlers/beacon/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type Backend[
type GenesisBackend interface {
GenesisValidatorsRoot(slot math.Slot) (common.Root, error)
GetGenesisForkVersion(genesisSlot math.Slot) (common.Version, error)
GetGenesisTime(slot math.Slot) (uint64, error)
}

type HistoricalBackend[ForkT any] interface {
Expand Down
9 changes: 8 additions & 1 deletion mod/node-api/handlers/beacon/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package beacon

import (
"encoding/hex"
"strconv"

beacontypes "github.com/berachain/beacon-kit/mod/node-api/handlers/beacon/types"
"github.com/berachain/beacon-kit/mod/node-api/handlers/types"
Expand All @@ -42,8 +43,14 @@ func (h *Handler[_, ContextT, _, _, _]) GetGenesis(_ ContextT) (any, error) {
return nil, err
}
genesisForkVersionHex := "0x" + hex.EncodeToString(genesisVersion[:])

genesisTime, err := h.backend.GetGenesisTime(utils.Genesis)
if err != nil {
return nil, err
}

return types.Wrap(beacontypes.GenesisData{
GenesisTime: "1590832934", // stub
GenesisTime: strconv.FormatUint(genesisTime, 10),
GenesisValidatorsRoot: genesisRoot,
GenesisForkVersion: genesisForkVersionHex,
}), nil
Expand Down
7 changes: 7 additions & 0 deletions mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,10 @@ type (
// GetValidatorsByEffectiveBalance retrieves validators by effective
// balance.
GetValidatorsByEffectiveBalance() ([]ValidatorT, error)
// GetGenesisTime retrieves the genesis time.
GetGenesisTime() (uint64, error)
// SetGenesisTime updates the genesis time.
SetGenesisTime(time uint64) error
}

// ReadOnlyBeaconState is the interface for a read-only beacon state.
Expand Down Expand Up @@ -969,6 +973,7 @@ type (
ValidatorIndexByCometBFTAddress(
cometBFTAddress []byte,
) (math.ValidatorIndex, error)
GetGenesisTime() (uint64, error)
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
Expand All @@ -992,6 +997,7 @@ type (
SetNextWithdrawalIndex(uint64) error
SetNextWithdrawalValidatorIndex(math.ValidatorIndex) error
SetTotalSlashing(math.Gwei) error
SetGenesisTime(time uint64) error
}

// WriteOnlyStateRoots defines a struct which only has write access to state
Expand Down Expand Up @@ -1139,6 +1145,7 @@ type (
GenesisBackend interface {
GenesisValidatorsRoot(slot math.Slot) (common.Root, error)
GetGenesisForkVersion(genesisSlot math.Slot) (common.Version, error)
GetGenesisTime(slot math.Slot) (uint64, error)
}

HistoricalBackend[ForkT any] interface {
Expand Down
2 changes: 2 additions & 0 deletions mod/state-transition/pkg/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type ReadOnlyBeaconState[
GetSlot() (math.Slot, error)
GetFork() (ForkT, error)
GetGenesisValidatorsRoot() (common.Root, error)
GetGenesisTime() (uint64, error)
GetBlockRootAtIndex(uint64) (common.Root, error)
GetLatestBlockHeader() (BeaconBlockHeaderT, error)
GetTotalActiveBalances(uint64) (math.Gwei, error)
Expand Down Expand Up @@ -109,6 +110,7 @@ type WriteOnlyBeaconState[
SetNextWithdrawalIndex(uint64) error
SetNextWithdrawalValidatorIndex(math.ValidatorIndex) error
SetTotalSlashing(math.Gwei) error
SetGenesisTime(uint64) error
}

// WriteOnlyStateRoots defines a struct which only has write access to state
Expand Down
4 changes: 4 additions & 0 deletions mod/state-transition/pkg/core/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type KVStore[
GetGenesisValidatorsRoot() (common.Root, error)
// SetGenesisValidatorsRoot sets the genesis validators root.
SetGenesisValidatorsRoot(root common.Root) error
// GetGenesisTime retrieves the genesis time.
GetGenesisTime() (uint64, error)
// SetGenesisTime sets the genesis time.
SetGenesisTime(time uint64) error
// GetLatestBlockHeader retrieves the latest block header.
GetLatestBlockHeader() (BeaconBlockHeaderT, error)
// SetLatestBlockHeader sets the latest block header.
Expand Down
5 changes: 5 additions & 0 deletions mod/state-transition/pkg/core/state_processor_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ func (sp *StateProcessor[
return nil, err
}

genesisTime := uint64(executionPayloadHeader.GetTimestamp())
if err = st.SetGenesisTime(genesisTime); err != nil {
return nil, err
}

var updates transition.ValidatorUpdates
updates, err = sp.processSyncCommitteeUpdates(st)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions mod/state-transition/pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ type ExecutionPayload[

type ExecutionPayloadHeader interface {
GetBlockHash() common.ExecutionHash
GetTimestamp() math.U64
}

// Withdrawals defines the interface for managing withdrawal operations.
Expand Down
2 changes: 2 additions & 0 deletions mod/storage/pkg/beacondb/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
NextWithdrawalIndexPrefix
NextWithdrawalValidatorIndexPrefix
ForkPrefix
GenesisTimePrefix
)

//nolint:lll
Expand Down Expand Up @@ -71,4 +72,5 @@ const (
NextWithdrawalIndexPrefixHumanReadable = "NextWithdrawalIndexPrefix"
NextWithdrawalValidatorIndexPrefixHumanReadable = "NextWithdrawalValidatorIndexPrefix"
ForkPrefixHumanReadable = "ForkPrefix"
GenesisTimePrefixHumanReadable = "GenesisTimePrefix"
)
8 changes: 8 additions & 0 deletions mod/storage/pkg/beacondb/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type KVStore[
// Versioning
// genesisValidatorsRoot is the root of the genesis validators.
genesisValidatorsRoot sdkcollections.Item[[]byte]
// genesisTime is the genesis time.
genesisTime sdkcollections.Item[uint64]
// slot is the current slot.
slot sdkcollections.Item[uint64]
// fork is the current fork
Expand Down Expand Up @@ -150,6 +152,12 @@ func New[
keys.GenesisValidatorsRootPrefixHumanReadable,
sdkcollections.BytesValue,
),
genesisTime: sdkcollections.NewItem(
schemaBuilder,
sdkcollections.NewPrefix([]byte{keys.GenesisTimePrefix}),
keys.GenesisTimePrefixHumanReadable,
sdkcollections.Uint64Value,
),
slot: sdkcollections.NewItem(
schemaBuilder,
sdkcollections.NewPrefix([]byte{keys.SlotPrefix}),
Expand Down
23 changes: 23 additions & 0 deletions mod/storage/pkg/beacondb/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package beacondb

import (
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)
Expand Down Expand Up @@ -49,6 +50,28 @@ func (kv *KVStore[
return common.Root(bz), nil
}

// GetGenesisTime retrieves the genesis time from the beacon state.
func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) GetGenesisTime() (uint64, error) {
genesisTime, err := kv.genesisTime.Get(kv.ctx)
if err != nil {
return 0, errors.Wrapf(err, "err in GetGenesisTime")
}
return genesisTime, nil
}

// SetGenesisTime sets the genesis time in the beacon state.
func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT,
]) SetGenesisTime(
genesisTime uint64,
) error {
return kv.genesisTime.Set(kv.ctx, genesisTime)
}

// GetSlot returns the current slot.
func (kv *KVStore[
BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT,
Expand Down
Loading