Skip to content

Commit

Permalink
Introduce genesis time
Browse files Browse the repository at this point in the history
Signed-off-by: nidhi-singh02 <[email protected]>
  • Loading branch information
nidhi-singh02 committed Sep 19, 2024
1 parent 7369817 commit 1cde1bc
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 1 deletion.
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
//SetGenesi
}

// WriteOnlyStateRoots defines a struct which only has write access to state
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()
}
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 @@ -49,6 +49,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
8 changes: 7 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,13 @@ 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 @@ -933,6 +933,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 @@ -965,6 +969,7 @@ type (
ValidatorIndexByCometBFTAddress(
cometBFTAddress []byte,
) (math.ValidatorIndex, error)
GetGenesisTime() (uint64, error)
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
Expand All @@ -988,6 +993,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 @@ -1132,6 +1138,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
1 change: 1 addition & 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
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
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
22 changes: 22 additions & 0 deletions mod/storage/pkg/beacondb/versioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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, err
}
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

0 comments on commit 1cde1bc

Please sign in to comment.