From dc4a4c5d055cb9ff443cbe4de8edb46154959c5d Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Fri, 20 Sep 2024 11:19:38 +0530 Subject: [PATCH] trying to get from initiliaze state Signed-off-by: nidhi-singh02 --- .../berad/pkg/state-transition/interfaces.go | 4 ++-- .../state_processor_genesis.go | 4 ++++ examples/berad/pkg/state-transition/types.go | 1 + examples/berad/pkg/storage/keys/keys.go | 2 ++ examples/berad/pkg/storage/kvstore.go | 8 +++++++ examples/berad/pkg/storage/versioning.go | 22 +++++++++++++++++++ mod/beacon/blockchain/types.go | 2 ++ mod/consensus-types/pkg/types/genesis.go | 7 ++++++ mod/consensus-types/pkg/types/state.go | 10 +++++++++ mod/node-core/pkg/components/interfaces.go | 4 ++++ mod/state-transition/pkg/core/interfaces.go | 1 + .../pkg/core/state/statedb.go | 6 +++++ mod/state-transition/pkg/core/state/types.go | 1 + .../pkg/core/state_processor_genesis.go | 4 ++++ mod/state-transition/pkg/core/types.go | 1 + mod/storage/pkg/beacondb/versioning.go | 3 ++- 16 files changed, 77 insertions(+), 3 deletions(-) diff --git a/examples/berad/pkg/state-transition/interfaces.go b/examples/berad/pkg/state-transition/interfaces.go index 438eb609b5..bdb8b792dd 100644 --- a/examples/berad/pkg/state-transition/interfaces.go +++ b/examples/berad/pkg/state-transition/interfaces.go @@ -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. @@ -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 diff --git a/examples/berad/pkg/state-transition/state_processor_genesis.go b/examples/berad/pkg/state-transition/state_processor_genesis.go index e99eac25ca..3f55027c03 100644 --- a/examples/berad/pkg/state-transition/state_processor_genesis.go +++ b/examples/berad/pkg/state-transition/state_processor_genesis.go @@ -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 { diff --git a/examples/berad/pkg/state-transition/types.go b/examples/berad/pkg/state-transition/types.go index a9601b5c19..1e5e7ae079 100644 --- a/examples/berad/pkg/state-transition/types.go +++ b/examples/berad/pkg/state-transition/types.go @@ -171,6 +171,7 @@ type ExecutionPayload[ type ExecutionPayloadHeader interface { GetBlockHash() common.ExecutionHash + GetTimestamp() math.U64 } // ExecutionEngine is the interface for the execution engine. diff --git a/examples/berad/pkg/storage/keys/keys.go b/examples/berad/pkg/storage/keys/keys.go index 3bd1deec78..08bf0ac4ff 100644 --- a/examples/berad/pkg/storage/keys/keys.go +++ b/examples/berad/pkg/storage/keys/keys.go @@ -36,6 +36,7 @@ const ( // NextWithdrawalIndexPrefix // NextWithdrawalValidatorIndexPrefix. ForkPrefix + GenesisTimePrefix ) //nolint:lll @@ -53,4 +54,5 @@ const ( GenesisValidatorsRootPrefixHumanReadable = "GenesisValidatorsRootPrefix" WithdrawalsPrefixHumanReadable = "WithdrawalsPrefix" ForkPrefixHumanReadable = "ForkPrefix" + GenesisTimePrefixHumanReadable = "GenesisTimePrefix" ) diff --git a/examples/berad/pkg/storage/kvstore.go b/examples/berad/pkg/storage/kvstore.go index 8aafa03caf..e5fdfd4cd2 100644 --- a/examples/berad/pkg/storage/kvstore.go +++ b/examples/berad/pkg/storage/kvstore.go @@ -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 @@ -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}), diff --git a/examples/berad/pkg/storage/versioning.go b/examples/berad/pkg/storage/versioning.go index 5d13b06522..b5f8e2b289 100644 --- a/examples/berad/pkg/storage/versioning.go +++ b/examples/berad/pkg/storage/versioning.go @@ -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) +} diff --git a/mod/beacon/blockchain/types.go b/mod/beacon/blockchain/types.go index 3a50033017..65159d5566 100644 --- a/mod/beacon/blockchain/types.go +++ b/mod/beacon/blockchain/types.go @@ -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. @@ -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( diff --git a/mod/consensus-types/pkg/types/genesis.go b/mod/consensus-types/pkg/types/genesis.go index 6dc80054ea..9e0e46e125 100644 --- a/mod/consensus-types/pkg/types/genesis.go +++ b/mod/consensus-types/pkg/types/genesis.go @@ -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. @@ -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, diff --git a/mod/consensus-types/pkg/types/state.go b/mod/consensus-types/pkg/types/state.go index d48f36a8cc..ad4560e6aa 100644 --- a/mod/consensus-types/pkg/types/state.go +++ b/mod/consensus-types/pkg/types/state.go @@ -71,6 +71,8 @@ type BeaconState[ // Slashing Slashings []uint64 TotalSlashing math.Gwei + // Genesis + //GenesisTime uint64 } // New creates a new BeaconState. @@ -99,6 +101,7 @@ func (st *BeaconState[ nextWithdrawalValidatorIndex math.ValidatorIndex, slashings []uint64, totalSlashing math.Gwei, + // genesisTime uint64, ) (*BeaconState[ BeaconBlockHeaderT, Eth1DataT, @@ -131,6 +134,7 @@ func (st *BeaconState[ NextWithdrawalValidatorIndex: nextWithdrawalValidatorIndex, Slashings: slashings, TotalSlashing: totalSlashing, + //GenesisTime: genesisTime, }, nil } @@ -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. @@ -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 } diff --git a/mod/node-core/pkg/components/interfaces.go b/mod/node-core/pkg/components/interfaces.go index d8c98f2dad..9f62873088 100644 --- a/mod/node-core/pkg/components/interfaces.go +++ b/mod/node-core/pkg/components/interfaces.go @@ -208,6 +208,7 @@ type ( nextWithdrawalIndex uint64, nextWithdrawalValidatorIndex math.U64, slashings []uint64, totalSlashing math.U64, + //genesisTime uint64, ) (T, error) } @@ -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. @@ -638,6 +641,7 @@ type ( []DepositT, ExecutionPayloadHeaderT, common.Version, + //uint64, ) (transition.ValidatorUpdates, error) // ProcessSlot processes the slot. ProcessSlots( diff --git a/mod/state-transition/pkg/core/interfaces.go b/mod/state-transition/pkg/core/interfaces.go index 8c59c5789f..b945bf9efe 100644 --- a/mod/state-transition/pkg/core/interfaces.go +++ b/mod/state-transition/pkg/core/interfaces.go @@ -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 diff --git a/mod/state-transition/pkg/core/state/statedb.go b/mod/state-transition/pkg/core/state/statedb.go index 115816152c..2840a4c0e3 100644 --- a/mod/state-transition/pkg/core/state/statedb.go +++ b/mod/state-transition/pkg/core/state/statedb.go @@ -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( @@ -397,6 +402,7 @@ func (s *StateDB[ nextWithdrawalValidatorIndex, slashings, totalSlashings, + //genesisTime, ) } diff --git a/mod/state-transition/pkg/core/state/types.go b/mod/state-transition/pkg/core/state/types.go index 6bd3021f56..dcf42c9725 100644 --- a/mod/state-transition/pkg/core/state/types.go +++ b/mod/state-transition/pkg/core/state/types.go @@ -55,6 +55,7 @@ type BeaconStateMarshallable[ nextWithdrawalIndex uint64, nextWithdrawalValidatorIndex math.U64, slashings []uint64, totalSlashing math.U64, + //genesisTime uint64, ) (T, error) } diff --git a/mod/state-transition/pkg/core/state_processor_genesis.go b/mod/state-transition/pkg/core/state_processor_genesis.go index a142950598..75fdcdddc7 100644 --- a/mod/state-transition/pkg/core/state_processor_genesis.go +++ b/mod/state-transition/pkg/core/state_processor_genesis.go @@ -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 { diff --git a/mod/state-transition/pkg/core/types.go b/mod/state-transition/pkg/core/types.go index 5a359344d3..2231533bf2 100644 --- a/mod/state-transition/pkg/core/types.go +++ b/mod/state-transition/pkg/core/types.go @@ -171,6 +171,7 @@ type ExecutionPayload[ type ExecutionPayloadHeader interface { GetBlockHash() common.ExecutionHash + GetTimestamp() math.U64 } // ExecutionEngine is the interface for the execution engine. diff --git a/mod/storage/pkg/beacondb/versioning.go b/mod/storage/pkg/beacondb/versioning.go index d1cb27c698..e5bf370235 100644 --- a/mod/storage/pkg/beacondb/versioning.go +++ b/mod/storage/pkg/beacondb/versioning.go @@ -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" ) @@ -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 }