Skip to content

Commit

Permalink
trying to get from initiliaze state
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 20, 2024
1 parent 1cde1bc commit dc4a4c5
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 3 deletions.
4 changes: 2 additions & 2 deletions examples/berad/pkg/state-transition/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ type ReadOnlyBeaconState[
ValidatorIndexByCometBFTAddress(
cometBFTAddress []byte,
) (math.ValidatorIndex, error)
//GetGenesisTime() (uint64, error)
GetGenesisTime() (uint64, error)
}

// WriteOnlyBeaconState is the interface for a write-only beacon state.
Expand All @@ -112,7 +112,7 @@ type WriteOnlyBeaconState[
SetSlot(math.Slot) error
SetWithdrawals(WithdrawalsT) error
UpdateBlockRootAtIndex(uint64, common.Root) error
//SetGenesi
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
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
4 changes: 4 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 @@ -538,6 +539,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 @@ -638,6 +641,7 @@ type (
[]DepositT,
ExecutionPayloadHeaderT,
common.Version,
//uint64,
) (transition.ValidatorUpdates, error)
// ProcessSlot processes the slot.
ProcessSlots(
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 @@ -110,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
6 changes: 6 additions & 0 deletions mod/state-transition/pkg/core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ func (s *StateDB[
if err != nil {
return empty, err
}
//
//genesisTime, err := s.GetGenesisTime()
//if err != nil {
// return empty, nil
//}

// TODO: Properly move BeaconState into full generics.
return (*new(BeaconStateMarshallableT)).New(
Expand All @@ -397,6 +402,7 @@ func (s *StateDB[
nextWithdrawalValidatorIndex,
slashings,
totalSlashings,
//genesisTime,
)
}

Expand Down
1 change: 1 addition & 0 deletions mod/state-transition/pkg/core/state/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type BeaconStateMarshallable[
nextWithdrawalIndex uint64,
nextWithdrawalValidatorIndex math.U64,
slashings []uint64, totalSlashing math.U64,
//genesisTime uint64,
) (T, error)
}

Expand Down
4 changes: 4 additions & 0 deletions mod/state-transition/pkg/core/state_processor_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ func (sp *StateProcessor[
return nil, err
}

if err = st.SetGenesisTime(uint64(executionPayloadHeader.GetTimestamp())); 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
}

// ExecutionEngine is the interface for the execution engine.
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -56,7 +57,7 @@ func (kv *KVStore[
]) GetGenesisTime() (uint64, error) {
genesisTime, err := kv.genesisTime.Get(kv.ctx)
if err != nil {
return 0, err
return 0, errors.Wrapf(err, "err in GetGenesisTime")
}
return genesisTime, nil
}
Expand Down

0 comments on commit dc4a4c5

Please sign in to comment.