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

trying some stuff for genesis - WIP #2014

Draft
wants to merge 4 commits into
base: beacon-endpoints
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions examples/berad/pkg/state-transition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type ReadOnlyBeaconState[
ValidatorIndexByCometBFTAddress(
cometBFTAddress []byte,
) (math.ValidatorIndex, error)
GetGenesisTime() (uint64, error)
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
Expand All @@ -111,6 +112,7 @@ type WriteOnlyBeaconState[
SetSlot(math.Slot) error
SetWithdrawals(WithdrawalsT) error
UpdateBlockRootAtIndex(uint64, common.Root) error
SetGenesisTime(uint64) error
}

// WriteOnlyStateRoots defines a struct which only has write access to state
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func (sp *StateProcessor[
return nil, err
}

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

if err = st.SetLatestExecutionPayloadHeader(
executionPayloadHeader,
); err != nil {
Expand Down
1 change: 1 addition & 0 deletions examples/berad/pkg/state-transition/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
}

// ExecutionEngine is the interface for the execution engine.
Expand Down
2 changes: 2 additions & 0 deletions examples/berad/pkg/storage/keys/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
// NextWithdrawalIndexPrefix
// NextWithdrawalValidatorIndexPrefix.
ForkPrefix
GenesisTimePrefix
)

//nolint:lll
Expand All @@ -53,4 +54,5 @@ const (
GenesisValidatorsRootPrefixHumanReadable = "GenesisValidatorsRootPrefix"
WithdrawalsPrefixHumanReadable = "WithdrawalsPrefix"
ForkPrefixHumanReadable = "ForkPrefix"
GenesisTimePrefixHumanReadable = "GenesisTimePrefix"
)
8 changes: 8 additions & 0 deletions examples/berad/pkg/storage/kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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 @@ -145,6 +147,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
22 changes: 22 additions & 0 deletions examples/berad/pkg/storage/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,25 @@ func (kv *KVStore[
]) GetFork() (ForkT, error) {
return kv.fork.Get(kv.ctx)
}

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

// SetGenesisTime sets the genesis time in the beacon state.
func (kv *KVStore[
BeaconBlockHeaderT, ExecutionPayloadHeaderT,
ForkT, ValidatorT, ValidatorsT, WithdrawalT, WithdrawalsT,
]) SetGenesisTime(
genesisTime uint64,
) error {
return kv.genesisTime.Set(kv.ctx, genesisTime)
}
2 changes: 2 additions & 0 deletions mod/beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type Genesis[DepositT any, ExecutionPayloadHeaderT any] interface {
GetDeposits() []DepositT
// GetExecutionPayloadHeader returns the execution payload header.
GetExecutionPayloadHeader() ExecutionPayloadHeaderT
GetGenesisTime() uint64
}

// LocalBuilder is the interface for the builder service.
Expand Down Expand Up @@ -186,6 +187,7 @@ type StateProcessor[
[]DepositT,
ExecutionPayloadHeaderT,
common.Version,
//uint64,
) (transition.ValidatorUpdates, error)
// ProcessSlots processes the state transition for a range of slots.
ProcessSlots(
Expand Down
10 changes: 10 additions & 0 deletions mod/cli/pkg/commands/genesis/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package genesis

import (
"fmt"
"unsafe"

"github.com/berachain/beacon-kit/mod/cli/pkg/context"
Expand Down Expand Up @@ -58,6 +59,8 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command {
}
genesisBlock := ethGenesis.ToBlock()

fmt.Printf("genesis Block nidz %v", genesisBlock.Time())

// Create the execution payload.
payload := gethprimitives.BlockToExecutableData(
genesisBlock,
Expand All @@ -74,6 +77,8 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command {
return errors.Wrap(err, "failed to read genesis doc from file")
}

fmt.Printf("\n appGenesis %v \n ", appGenesis.GenesisTime)

// create the app state
appGenesisState, err := genutiltypes.GenesisStateFromAppGenesis(
appGenesis,
Expand All @@ -82,6 +87,8 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command {
return err
}

fmt.Printf("appGenesisState %v \n ", appGenesisState)

genesisInfo := &types.Genesis[
*types.Deposit, *types.ExecutionPayloadHeader,
]{}
Expand All @@ -92,6 +99,9 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command {
return errors.Wrap(err, "failed to unmarshal beacon state")
}

fmt.Printf("payload nidz %v \n ", payload.Timestamp)

fmt.Printf("genesisInfo nids %v \n ", genesisInfo)
// Inject the execution payload.
genesisInfo.ExecutionPayloadHeader = executableDataToExecutionPayloadHeader(
version.ToUint32(genesisInfo.ForkVersion),
Expand Down
7 changes: 7 additions & 0 deletions mod/consensus-types/pkg/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Genesis[
DepositT any,
ExecutionPayloadHeaderT interface {
NewFromJSON([]byte, uint32) (ExecutionPayloadHeaderT, error)
//GetTimestamp() math.U64
},
] struct {
// ForkVersion is the fork version of the genesis slot.
Expand Down Expand Up @@ -74,6 +75,12 @@ func (g *Genesis[
return g.ExecutionPayloadHeader
}

// GetGenesisTime returns the genesis time.
func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) GetGenesisTime() uint64 {
return g.GetGenesisTime()
//uint64(g.ExecutionPayloadHeader.GetTimestamp())
}

// UnmarshalJSON for Genesis.
func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) UnmarshalJSON(
data []byte,
Expand Down
10 changes: 10 additions & 0 deletions mod/consensus-types/pkg/types/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type BeaconState[
// Slashing
Slashings []uint64
TotalSlashing math.Gwei
// Genesis
//GenesisTime uint64
}

// New creates a new BeaconState.
Expand Down Expand Up @@ -99,6 +101,7 @@ func (st *BeaconState[
nextWithdrawalValidatorIndex math.ValidatorIndex,
slashings []uint64,
totalSlashing math.Gwei,
// genesisTime uint64,
) (*BeaconState[
BeaconBlockHeaderT,
Eth1DataT,
Expand Down Expand Up @@ -131,6 +134,7 @@ func (st *BeaconState[
NextWithdrawalValidatorIndex: nextWithdrawalValidatorIndex,
Slashings: slashings,
TotalSlashing: totalSlashing,
//GenesisTime: genesisTime,
}, nil
}

Expand Down Expand Up @@ -204,6 +208,9 @@ func (st *BeaconState[
ssz.DefineSliceOfUint64sContent(codec, &st.Balances, 1099511627776)
ssz.DefineSliceOfStaticBytesContent(codec, &st.RandaoMixes, 65536)
ssz.DefineSliceOfUint64sContent(codec, &st.Slashings, 1099511627776)

// Genesis Time
//ssz.DefineUint64(codec, &st.GenesisTime)
}

// MarshalSSZ marshals the BeaconState into SSZ format.
Expand Down Expand Up @@ -391,6 +398,9 @@ func (st *BeaconState[
// Field (15) 'TotalSlashing'
hh.PutUint64(uint64(st.TotalSlashing))

// Field (16) 'GenesisTime'
//hh.PutUint64(st.GenesisTime)

hh.Merkleize(indx)
return nil
}
Expand Down
30 changes: 30 additions & 0 deletions mod/node-api/backend/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package backend

import (
"fmt"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
)
Expand Down Expand Up @@ -51,3 +52,32 @@ 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) {

//state := b.sb.StateFromContext(ctx)
//if state == nil {
// fmt.Errorf("Failed to retrieve beacon state")
// return 0, errors.New("failed to retrieve beacon state")
//}
//
//genesisTime, err := state.GetGenesisTime()
//if err != nil {
// fmt.Errorf("Failed to get genesis time", err)
// return 0, err
//}
//
//fmt.Printf("Retrieved genesis time %v", genesisTime)
//return genesisTime, nil

st, _, err := b.stateFromSlot(slot)
if err != nil {
return 0, err
}

fmt.Printf("state nidzi %v", st)
return st.GetGenesisTime()
}
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 @@ -51,6 +51,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
21 changes: 20 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,8 @@ package beacon

import (
"encoding/hex"
"github.com/berachain/beacon-kit/mod/errors"
"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 +44,25 @@ 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
}

if genesisTime == 0 {
h.Logger().Warn("Genesis time is 0, this may indicate an initialization issue")
// Optionally, you might want to return an error here instead of continuing
return nil, errors.New("genesis time not properly initialized")
}

h.Logger().Info("Retrieved genesis data",
"genesisTime", genesisTime,
"genesisValidatorsRoot", genesisRoot,
"genesisForkVersion", genesisForkVersionHex)

return types.Wrap(beacontypes.GenesisData{
GenesisTime: "1590832934", // stub
GenesisTime: strconv.FormatUint(genesisTime, 10),
GenesisValidatorsRoot: genesisRoot,
GenesisForkVersion: genesisForkVersionHex,
}), nil
Expand Down
11 changes: 11 additions & 0 deletions mod/node-core/pkg/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type (
nextWithdrawalIndex uint64,
nextWithdrawalValidatorIndex math.U64,
slashings []uint64, totalSlashing math.U64,
//genesisTime uint64,
) (T, error)
}

Expand Down Expand Up @@ -541,6 +542,8 @@ type (
GetDeposits() []DepositT
// GetExecutionPayloadHeader returns the execution payload header.
GetExecutionPayloadHeader() ExecutionPayloadHeaderT
// GetGenesisTime returns the genesis time.
GetGenesisTime() uint64
}

// IndexDB is the interface for the range DB.
Expand Down Expand Up @@ -641,6 +644,7 @@ type (
[]DepositT,
ExecutionPayloadHeaderT,
common.Version,
//uint64,
) (transition.ValidatorUpdates, error)
// ProcessSlot processes the slot.
ProcessSlots(
Expand Down Expand Up @@ -936,6 +940,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 @@ -968,6 +976,7 @@ type (
ValidatorIndexByCometBFTAddress(
cometBFTAddress []byte,
) (math.ValidatorIndex, error)
GetGenesisTime() (uint64, error)
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
Expand All @@ -991,6 +1000,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 @@ -1136,6 +1146,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
Loading
Loading