From 1cde1bc4c834e72cf5538e3611fe3a464317010f Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Thu, 19 Sep 2024 21:16:48 +0530 Subject: [PATCH 01/13] Introduce genesis time Signed-off-by: nidhi-singh02 --- .../berad/pkg/state-transition/interfaces.go | 2 ++ mod/node-api/backend/genesis.go | 11 ++++++++++ mod/node-api/handlers/beacon/backend.go | 1 + mod/node-api/handlers/beacon/genesis.go | 8 ++++++- mod/node-core/pkg/components/interfaces.go | 7 ++++++ mod/state-transition/pkg/core/interfaces.go | 1 + .../pkg/core/state/interfaces.go | 4 ++++ mod/storage/pkg/beacondb/keys/keys.go | 2 ++ mod/storage/pkg/beacondb/kvstore.go | 8 +++++++ mod/storage/pkg/beacondb/versioning.go | 22 +++++++++++++++++++ 10 files changed, 65 insertions(+), 1 deletion(-) diff --git a/examples/berad/pkg/state-transition/interfaces.go b/examples/berad/pkg/state-transition/interfaces.go index a05be4fae7..438eb609b5 100644 --- a/examples/berad/pkg/state-transition/interfaces.go +++ b/examples/berad/pkg/state-transition/interfaces.go @@ -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. @@ -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 diff --git a/mod/node-api/backend/genesis.go b/mod/node-api/backend/genesis.go index 5f42455dfa..484a888637 100644 --- a/mod/node-api/backend/genesis.go +++ b/mod/node-api/backend/genesis.go @@ -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() +} diff --git a/mod/node-api/handlers/beacon/backend.go b/mod/node-api/handlers/beacon/backend.go index c1394b7de8..8c3abe4945 100644 --- a/mod/node-api/handlers/beacon/backend.go +++ b/mod/node-api/handlers/beacon/backend.go @@ -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 { diff --git a/mod/node-api/handlers/beacon/genesis.go b/mod/node-api/handlers/beacon/genesis.go index 52d74895e9..e49a74c8cc 100644 --- a/mod/node-api/handlers/beacon/genesis.go +++ b/mod/node-api/handlers/beacon/genesis.go @@ -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" @@ -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 diff --git a/mod/node-core/pkg/components/interfaces.go b/mod/node-core/pkg/components/interfaces.go index 59c2322df2..d8c98f2dad 100644 --- a/mod/node-core/pkg/components/interfaces.go +++ b/mod/node-core/pkg/components/interfaces.go @@ -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. @@ -965,6 +969,7 @@ type ( ValidatorIndexByCometBFTAddress( cometBFTAddress []byte, ) (math.ValidatorIndex, error) + GetGenesisTime() (uint64, error) } // WriteOnlyBeaconState is the interface for a write-only beacon state. @@ -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 @@ -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 { diff --git a/mod/state-transition/pkg/core/interfaces.go b/mod/state-transition/pkg/core/interfaces.go index 6631e65374..8c59c5789f 100644 --- a/mod/state-transition/pkg/core/interfaces.go +++ b/mod/state-transition/pkg/core/interfaces.go @@ -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) diff --git a/mod/state-transition/pkg/core/state/interfaces.go b/mod/state-transition/pkg/core/state/interfaces.go index c40d5eff9e..07710418a5 100644 --- a/mod/state-transition/pkg/core/state/interfaces.go +++ b/mod/state-transition/pkg/core/state/interfaces.go @@ -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. diff --git a/mod/storage/pkg/beacondb/keys/keys.go b/mod/storage/pkg/beacondb/keys/keys.go index 1efec85e9d..65c394fe8f 100644 --- a/mod/storage/pkg/beacondb/keys/keys.go +++ b/mod/storage/pkg/beacondb/keys/keys.go @@ -44,6 +44,7 @@ const ( NextWithdrawalIndexPrefix NextWithdrawalValidatorIndexPrefix ForkPrefix + GenesisTimePrefix ) //nolint:lll @@ -71,4 +72,5 @@ const ( NextWithdrawalIndexPrefixHumanReadable = "NextWithdrawalIndexPrefix" NextWithdrawalValidatorIndexPrefixHumanReadable = "NextWithdrawalValidatorIndexPrefix" ForkPrefixHumanReadable = "ForkPrefix" + GenesisTimePrefixHumanReadable = "GenesisTimePrefix" ) diff --git a/mod/storage/pkg/beacondb/kvstore.go b/mod/storage/pkg/beacondb/kvstore.go index 7680df3256..bba6417217 100644 --- a/mod/storage/pkg/beacondb/kvstore.go +++ b/mod/storage/pkg/beacondb/kvstore.go @@ -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 @@ -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}), diff --git a/mod/storage/pkg/beacondb/versioning.go b/mod/storage/pkg/beacondb/versioning.go index 82d48e4456..d1cb27c698 100644 --- a/mod/storage/pkg/beacondb/versioning.go +++ b/mod/storage/pkg/beacondb/versioning.go @@ -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, From dc4a4c5d055cb9ff443cbe4de8edb46154959c5d Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Fri, 20 Sep 2024 11:19:38 +0530 Subject: [PATCH 02/13] 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 } From 8329623d7c824d9b7bdf10a1cd3047dc42d27470 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Fri, 20 Sep 2024 19:48:04 +0530 Subject: [PATCH 03/13] executionPayloadHeader Signed-off-by: nidhi-singh02 --- mod/cli/pkg/commands/genesis/payload.go | 10 ++++++++++ mod/node-api/backend/genesis.go | 19 +++++++++++++++++++ mod/node-api/handlers/beacon/genesis.go | 13 +++++++++++++ .../pkg/core/state_processor_genesis.go | 10 +++++++++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index deccc019ad..6ce105b555 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -21,6 +21,7 @@ package genesis import ( + "fmt" "unsafe" "github.com/berachain/beacon-kit/mod/cli/pkg/context" @@ -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, @@ -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, @@ -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, ]{} @@ -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), diff --git a/mod/node-api/backend/genesis.go b/mod/node-api/backend/genesis.go index 484a888637..9642cfb68a 100644 --- a/mod/node-api/backend/genesis.go +++ b/mod/node-api/backend/genesis.go @@ -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" ) @@ -56,9 +57,27 @@ func (b Backend[ 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() } diff --git a/mod/node-api/handlers/beacon/genesis.go b/mod/node-api/handlers/beacon/genesis.go index e49a74c8cc..742b747334 100644 --- a/mod/node-api/handlers/beacon/genesis.go +++ b/mod/node-api/handlers/beacon/genesis.go @@ -22,6 +22,7 @@ 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" @@ -48,6 +49,18 @@ func (h *Handler[_, ContextT, _, _]) GetGenesis(_ ContextT) (any, error) { 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: strconv.FormatUint(genesisTime, 10), GenesisValidatorsRoot: genesisRoot, diff --git a/mod/state-transition/pkg/core/state_processor_genesis.go b/mod/state-transition/pkg/core/state_processor_genesis.go index 75fdcdddc7..d52fc13bdb 100644 --- a/mod/state-transition/pkg/core/state_processor_genesis.go +++ b/mod/state-transition/pkg/core/state_processor_genesis.go @@ -21,6 +21,7 @@ package core import ( + "fmt" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constants" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex" @@ -47,6 +48,10 @@ func (sp *StateProcessor[ executionPayloadHeader ExecutionPayloadHeaderT, genesisVersion common.Version, ) (transition.ValidatorUpdates, error) { + + fmt.Printf("Execution Payload Header %v %v %v %v\n", + executionPayloadHeader.GetTimestamp(), executionPayloadHeader.GetBlockHash()) + var ( blkHeader BeaconBlockHeaderT blkBody BeaconBlockBodyT @@ -152,7 +157,10 @@ func (sp *StateProcessor[ return nil, err } - if err = st.SetGenesisTime(uint64(executionPayloadHeader.GetTimestamp())); err != nil { + genesisTime := uint64(executionPayloadHeader.GetTimestamp()) + fmt.Printf("Setting genesis time %v", genesisTime) + if err = st.SetGenesisTime(genesisTime); err != nil { + fmt.Errorf("failed to set genesis time %v", err) return nil, err } From 9902e1f561363f3aae0927943845a050d462b431 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 13:13:13 +0530 Subject: [PATCH 04/13] remove uncessary changes from examples Signed-off-by: nidhi-singh02 --- .../berad/pkg/state-transition/interfaces.go | 2 -- .../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/consensus-types/pkg/types/genesis.go | 2 +- 7 files changed, 1 insertion(+), 40 deletions(-) diff --git a/examples/berad/pkg/state-transition/interfaces.go b/examples/berad/pkg/state-transition/interfaces.go index bdb8b792dd..a05be4fae7 100644 --- a/examples/berad/pkg/state-transition/interfaces.go +++ b/examples/berad/pkg/state-transition/interfaces.go @@ -90,7 +90,6 @@ type ReadOnlyBeaconState[ ValidatorIndexByCometBFTAddress( cometBFTAddress []byte, ) (math.ValidatorIndex, error) - GetGenesisTime() (uint64, error) } // WriteOnlyBeaconState is the interface for a write-only beacon state. @@ -112,7 +111,6 @@ 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 diff --git a/examples/berad/pkg/state-transition/state_processor_genesis.go b/examples/berad/pkg/state-transition/state_processor_genesis.go index 3f55027c03..e99eac25ca 100644 --- a/examples/berad/pkg/state-transition/state_processor_genesis.go +++ b/examples/berad/pkg/state-transition/state_processor_genesis.go @@ -98,10 +98,6 @@ 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 e88c87b4c2..df93b6875f 100644 --- a/examples/berad/pkg/state-transition/types.go +++ b/examples/berad/pkg/state-transition/types.go @@ -171,7 +171,6 @@ 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 08bf0ac4ff..3bd1deec78 100644 --- a/examples/berad/pkg/storage/keys/keys.go +++ b/examples/berad/pkg/storage/keys/keys.go @@ -36,7 +36,6 @@ const ( // NextWithdrawalIndexPrefix // NextWithdrawalValidatorIndexPrefix. ForkPrefix - GenesisTimePrefix ) //nolint:lll @@ -54,5 +53,4 @@ 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 e5fdfd4cd2..8aafa03caf 100644 --- a/examples/berad/pkg/storage/kvstore.go +++ b/examples/berad/pkg/storage/kvstore.go @@ -57,8 +57,6 @@ 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 @@ -147,12 +145,6 @@ 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 b5f8e2b289..5d13b06522 100644 --- a/examples/berad/pkg/storage/versioning.go +++ b/examples/berad/pkg/storage/versioning.go @@ -85,25 +85,3 @@ 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/consensus-types/pkg/types/genesis.go b/mod/consensus-types/pkg/types/genesis.go index 9e0e46e125..05c2e4b1f6 100644 --- a/mod/consensus-types/pkg/types/genesis.go +++ b/mod/consensus-types/pkg/types/genesis.go @@ -78,7 +78,7 @@ func (g *Genesis[ // GetGenesisTime returns the genesis time. func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) GetGenesisTime() uint64 { return g.GetGenesisTime() - //uint64(g.ExecutionPayloadHeader.GetTimestamp()) + uint64(g.ExecutionPayloadHeader.GetTimestamp()) } // UnmarshalJSON for Genesis. From 206d0f8e34b544c2b3c231b1f1f8ff51dca1d233 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 13:34:32 +0530 Subject: [PATCH 05/13] getting genesis time - works Signed-off-by: nidhi-singh02 --- mod/cli/pkg/commands/genesis/payload.go | 10 +++++----- mod/consensus-types/pkg/types/genesis.go | 2 +- .../pkg/core/state_processor_genesis.go | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index 6ce105b555..6b631089ec 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -77,7 +77,9 @@ 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) + fmt.Printf("\n appGenesis GenesisTime %v \n ", appGenesis.GenesisTime) + // Set the timestamp in the payload + payload.Timestamp = uint64(appGenesis.GenesisTime.UnixNano()) // create the app state appGenesisState, err := genutiltypes.GenesisStateFromAppGenesis( @@ -87,8 +89,6 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { return err } - fmt.Printf("appGenesisState %v \n ", appGenesisState) - genesisInfo := &types.Genesis[ *types.Deposit, *types.ExecutionPayloadHeader, ]{} @@ -99,9 +99,8 @@ 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("payload nidz timestamp ======%v \n ", payload.Timestamp) - fmt.Printf("genesisInfo nids %v \n ", genesisInfo) // Inject the execution payload. genesisInfo.ExecutionPayloadHeader = executableDataToExecutionPayloadHeader( version.ToUint32(genesisInfo.ForkVersion), @@ -165,6 +164,7 @@ func executableDataToExecutionPayloadHeader( excessBlobGas = *data.ExcessBlobGas } + fmt.Printf("data.Timestamp %v /n", math.U64(data.Timestamp)) executionPayloadHeader = &types.ExecutionPayloadHeader{ ParentHash: common.ExecutionHash(data.ParentHash), FeeRecipient: common.ExecutionAddress(data.FeeRecipient), diff --git a/mod/consensus-types/pkg/types/genesis.go b/mod/consensus-types/pkg/types/genesis.go index 05c2e4b1f6..9e0e46e125 100644 --- a/mod/consensus-types/pkg/types/genesis.go +++ b/mod/consensus-types/pkg/types/genesis.go @@ -78,7 +78,7 @@ func (g *Genesis[ // GetGenesisTime returns the genesis time. func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) GetGenesisTime() uint64 { return g.GetGenesisTime() - uint64(g.ExecutionPayloadHeader.GetTimestamp()) + //uint64(g.ExecutionPayloadHeader.GetTimestamp()) } // UnmarshalJSON for Genesis. diff --git a/mod/state-transition/pkg/core/state_processor_genesis.go b/mod/state-transition/pkg/core/state_processor_genesis.go index d52fc13bdb..e3cff869bd 100644 --- a/mod/state-transition/pkg/core/state_processor_genesis.go +++ b/mod/state-transition/pkg/core/state_processor_genesis.go @@ -49,7 +49,7 @@ func (sp *StateProcessor[ genesisVersion common.Version, ) (transition.ValidatorUpdates, error) { - fmt.Printf("Execution Payload Header %v %v %v %v\n", + fmt.Printf("Execution Payload Header InitializePreminedBeaconStateFromEth1 %v %v \n", executionPayloadHeader.GetTimestamp(), executionPayloadHeader.GetBlockHash()) var ( @@ -158,7 +158,7 @@ func (sp *StateProcessor[ } genesisTime := uint64(executionPayloadHeader.GetTimestamp()) - fmt.Printf("Setting genesis time %v", genesisTime) + fmt.Printf("Setting genesis time %v \n", genesisTime) if err = st.SetGenesisTime(genesisTime); err != nil { fmt.Errorf("failed to set genesis time %v", err) return nil, err From 988a1e528525b003ed6c7406fee2327fe15a9e01 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 14:02:59 +0530 Subject: [PATCH 06/13] cleanup of code Signed-off-by: nidhi-singh02 --- mod/beacon/blockchain/types.go | 1 - mod/consensus-types/pkg/types/genesis.go | 2 -- mod/consensus-types/pkg/types/state.go | 10 ---------- mod/node-api/backend/genesis.go | 18 ------------------ mod/node-api/handlers/beacon/genesis.go | 5 ----- mod/node-core/pkg/components/interfaces.go | 2 -- mod/state-transition/pkg/core/state/statedb.go | 6 ------ mod/state-transition/pkg/core/state/types.go | 1 - 8 files changed, 45 deletions(-) diff --git a/mod/beacon/blockchain/types.go b/mod/beacon/blockchain/types.go index 65159d5566..93fc719832 100644 --- a/mod/beacon/blockchain/types.go +++ b/mod/beacon/blockchain/types.go @@ -187,7 +187,6 @@ 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 9e0e46e125..71d5948baf 100644 --- a/mod/consensus-types/pkg/types/genesis.go +++ b/mod/consensus-types/pkg/types/genesis.go @@ -41,7 +41,6 @@ type Genesis[ DepositT any, ExecutionPayloadHeaderT interface { NewFromJSON([]byte, uint32) (ExecutionPayloadHeaderT, error) - //GetTimestamp() math.U64 }, ] struct { // ForkVersion is the fork version of the genesis slot. @@ -78,7 +77,6 @@ func (g *Genesis[ // GetGenesisTime returns the genesis time. func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) GetGenesisTime() uint64 { return g.GetGenesisTime() - //uint64(g.ExecutionPayloadHeader.GetTimestamp()) } // UnmarshalJSON for Genesis. diff --git a/mod/consensus-types/pkg/types/state.go b/mod/consensus-types/pkg/types/state.go index ad4560e6aa..d48f36a8cc 100644 --- a/mod/consensus-types/pkg/types/state.go +++ b/mod/consensus-types/pkg/types/state.go @@ -71,8 +71,6 @@ type BeaconState[ // Slashing Slashings []uint64 TotalSlashing math.Gwei - // Genesis - //GenesisTime uint64 } // New creates a new BeaconState. @@ -101,7 +99,6 @@ func (st *BeaconState[ nextWithdrawalValidatorIndex math.ValidatorIndex, slashings []uint64, totalSlashing math.Gwei, - // genesisTime uint64, ) (*BeaconState[ BeaconBlockHeaderT, Eth1DataT, @@ -134,7 +131,6 @@ func (st *BeaconState[ NextWithdrawalValidatorIndex: nextWithdrawalValidatorIndex, Slashings: slashings, TotalSlashing: totalSlashing, - //GenesisTime: genesisTime, }, nil } @@ -208,9 +204,6 @@ 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. @@ -398,9 +391,6 @@ 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-api/backend/genesis.go b/mod/node-api/backend/genesis.go index 9642cfb68a..775ddc6290 100644 --- a/mod/node-api/backend/genesis.go +++ b/mod/node-api/backend/genesis.go @@ -21,7 +21,6 @@ package backend import ( - "fmt" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/math" ) @@ -57,27 +56,10 @@ func (b Backend[ 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() } diff --git a/mod/node-api/handlers/beacon/genesis.go b/mod/node-api/handlers/beacon/genesis.go index cd8addee8d..aa2e134d3b 100644 --- a/mod/node-api/handlers/beacon/genesis.go +++ b/mod/node-api/handlers/beacon/genesis.go @@ -56,11 +56,6 @@ func (h *Handler[_, ContextT, _, _, _]) GetGenesis(_ ContextT) (any, error) { 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: strconv.FormatUint(genesisTime, 10), GenesisValidatorsRoot: genesisRoot, diff --git a/mod/node-core/pkg/components/interfaces.go b/mod/node-core/pkg/components/interfaces.go index 9e45b919e8..a49701aab7 100644 --- a/mod/node-core/pkg/components/interfaces.go +++ b/mod/node-core/pkg/components/interfaces.go @@ -208,7 +208,6 @@ type ( nextWithdrawalIndex uint64, nextWithdrawalValidatorIndex math.U64, slashings []uint64, totalSlashing math.U64, - //genesisTime uint64, ) (T, error) } @@ -644,7 +643,6 @@ type ( []DepositT, ExecutionPayloadHeaderT, common.Version, - //uint64, ) (transition.ValidatorUpdates, error) // ProcessSlot processes the slot. ProcessSlots( diff --git a/mod/state-transition/pkg/core/state/statedb.go b/mod/state-transition/pkg/core/state/statedb.go index 2840a4c0e3..115816152c 100644 --- a/mod/state-transition/pkg/core/state/statedb.go +++ b/mod/state-transition/pkg/core/state/statedb.go @@ -377,11 +377,6 @@ 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( @@ -402,7 +397,6 @@ 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 dcf42c9725..6bd3021f56 100644 --- a/mod/state-transition/pkg/core/state/types.go +++ b/mod/state-transition/pkg/core/state/types.go @@ -55,7 +55,6 @@ type BeaconStateMarshallable[ nextWithdrawalIndex uint64, nextWithdrawalValidatorIndex math.U64, slashings []uint64, totalSlashing math.U64, - //genesisTime uint64, ) (T, error) } From 5a6d4f5701e4f1b928ad0ccaa726cd7638c973ae Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 14:17:14 +0530 Subject: [PATCH 07/13] fmt cleanup Signed-off-by: nidhi-singh02 --- mod/cli/pkg/commands/genesis/payload.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index 6b631089ec..6e1523412c 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -21,7 +21,6 @@ package genesis import ( - "fmt" "unsafe" "github.com/berachain/beacon-kit/mod/cli/pkg/context" @@ -59,8 +58,6 @@ 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, @@ -77,8 +74,7 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { return errors.Wrap(err, "failed to read genesis doc from file") } - fmt.Printf("\n appGenesis GenesisTime %v \n ", appGenesis.GenesisTime) - // Set the timestamp in the payload + // Set the genesis time from app Genesis in the payload payload.Timestamp = uint64(appGenesis.GenesisTime.UnixNano()) // create the app state @@ -99,8 +95,6 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { return errors.Wrap(err, "failed to unmarshal beacon state") } - fmt.Printf("payload nidz timestamp ======%v \n ", payload.Timestamp) - // Inject the execution payload. genesisInfo.ExecutionPayloadHeader = executableDataToExecutionPayloadHeader( version.ToUint32(genesisInfo.ForkVersion), @@ -164,7 +158,6 @@ func executableDataToExecutionPayloadHeader( excessBlobGas = *data.ExcessBlobGas } - fmt.Printf("data.Timestamp %v /n", math.U64(data.Timestamp)) executionPayloadHeader = &types.ExecutionPayloadHeader{ ParentHash: common.ExecutionHash(data.ParentHash), FeeRecipient: common.ExecutionAddress(data.FeeRecipient), From 9e44788aff39cc99cab35aa375d5f900c1e8697c Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 14:32:21 +0530 Subject: [PATCH 08/13] time is sec, get from executionPayloadHeader Signed-off-by: nidhi-singh02 --- mod/cli/pkg/commands/genesis/payload.go | 2 +- mod/consensus-types/pkg/types/genesis.go | 3 ++- mod/node-api/handlers/beacon/genesis.go | 7 ------- mod/state-transition/pkg/core/state_processor_genesis.go | 7 ------- 4 files changed, 3 insertions(+), 16 deletions(-) diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index 6e1523412c..f7df5410d0 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -75,7 +75,7 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { } // Set the genesis time from app Genesis in the payload - payload.Timestamp = uint64(appGenesis.GenesisTime.UnixNano()) + payload.Timestamp = uint64(appGenesis.GenesisTime.Unix()) // create the app state appGenesisState, err := genutiltypes.GenesisStateFromAppGenesis( diff --git a/mod/consensus-types/pkg/types/genesis.go b/mod/consensus-types/pkg/types/genesis.go index 71d5948baf..2b5ff3ca87 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. @@ -76,7 +77,7 @@ func (g *Genesis[ // GetGenesisTime returns the genesis time. func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) GetGenesisTime() uint64 { - return g.GetGenesisTime() + return uint64(g.ExecutionPayloadHeader.GetTimestamp()) } // UnmarshalJSON for Genesis. diff --git a/mod/node-api/handlers/beacon/genesis.go b/mod/node-api/handlers/beacon/genesis.go index aa2e134d3b..efaddb3d8a 100644 --- a/mod/node-api/handlers/beacon/genesis.go +++ b/mod/node-api/handlers/beacon/genesis.go @@ -22,7 +22,6 @@ 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" @@ -50,12 +49,6 @@ func (h *Handler[_, ContextT, _, _, _]) GetGenesis(_ ContextT) (any, error) { 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") - } - return types.Wrap(beacontypes.GenesisData{ GenesisTime: strconv.FormatUint(genesisTime, 10), GenesisValidatorsRoot: genesisRoot, diff --git a/mod/state-transition/pkg/core/state_processor_genesis.go b/mod/state-transition/pkg/core/state_processor_genesis.go index e3cff869bd..47011804e7 100644 --- a/mod/state-transition/pkg/core/state_processor_genesis.go +++ b/mod/state-transition/pkg/core/state_processor_genesis.go @@ -21,7 +21,6 @@ package core import ( - "fmt" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/constants" "github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/hex" @@ -48,10 +47,6 @@ func (sp *StateProcessor[ executionPayloadHeader ExecutionPayloadHeaderT, genesisVersion common.Version, ) (transition.ValidatorUpdates, error) { - - fmt.Printf("Execution Payload Header InitializePreminedBeaconStateFromEth1 %v %v \n", - executionPayloadHeader.GetTimestamp(), executionPayloadHeader.GetBlockHash()) - var ( blkHeader BeaconBlockHeaderT blkBody BeaconBlockBodyT @@ -158,9 +153,7 @@ func (sp *StateProcessor[ } genesisTime := uint64(executionPayloadHeader.GetTimestamp()) - fmt.Printf("Setting genesis time %v \n", genesisTime) if err = st.SetGenesisTime(genesisTime); err != nil { - fmt.Errorf("failed to set genesis time %v", err) return nil, err } From 8098e3f674d2ca3256a9f4c26b70d3d88224bd63 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 14:34:50 +0530 Subject: [PATCH 09/13] generate check Signed-off-by: nidhi-singh02 --- .../backend/mocks/beacon_state.mock.go | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/mod/node-api/backend/mocks/beacon_state.mock.go b/mod/node-api/backend/mocks/beacon_state.mock.go index 8d54a59f20..c79d665553 100644 --- a/mod/node-api/backend/mocks/beacon_state.mock.go +++ b/mod/node-api/backend/mocks/beacon_state.mock.go @@ -360,6 +360,61 @@ func (_c *BeaconState_GetFork_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPaylo return _c } +// GetGenesisTime provides a mock function with given fields: +func (_m *BeaconState[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]) GetGenesisTime() (uint64, error) { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetGenesisTime") + } + + var r0 uint64 + var r1 error + if rf, ok := ret.Get(0).(func() (uint64, error)); ok { + return rf() + } + if rf, ok := ret.Get(0).(func() uint64); ok { + r0 = rf() + } else { + r0 = ret.Get(0).(uint64) + } + + if rf, ok := ret.Get(1).(func() error); ok { + r1 = rf() + } else { + r1 = ret.Error(1) + } + + return r0, r1 +} + +// BeaconState_GetGenesisTime_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetGenesisTime' +type BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT interface{}, Eth1DataT interface{}, ExecutionPayloadHeaderT interface{}, ForkT interface{}, ValidatorT interface{}, ValidatorsT interface{}, WithdrawalT interface{}] struct { + *mock.Call +} + +// GetGenesisTime is a helper method to define mock.On call +func (_e *BeaconState_Expecter[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]) GetGenesisTime() *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT] { + return &BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]{Call: _e.mock.On("GetGenesisTime")} +} + +func (_c *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]) Run(run func()) *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT] { + _c.Call.Run(func(args mock.Arguments) { + run() + }) + return _c +} + +func (_c *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]) Return(_a0 uint64, _a1 error) *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT] { + _c.Call.Return(_a0, _a1) + return _c +} + +func (_c *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]) RunAndReturn(run func() (uint64, error)) *BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT] { + _c.Call.Return(run) + return _c +} + // GetGenesisValidatorsRoot provides a mock function with given fields: func (_m *BeaconState[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, ForkT, ValidatorT, ValidatorsT, WithdrawalT]) GetGenesisValidatorsRoot() (common.Root, error) { ret := _m.Called() From c16d30aa50a09a6ba416785d84c7253ae9b2be3e Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 16:02:07 +0530 Subject: [PATCH 10/13] gosec for genesis time Signed-off-by: nidhi-singh02 --- mod/cli/pkg/commands/genesis/payload.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index f7df5410d0..92f679f135 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -74,7 +74,10 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { return errors.Wrap(err, "failed to read genesis doc from file") } - // Set the genesis time from app Genesis in the payload + //#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 From 66d508284ff6d20c50f59d304f9b126051f05fba Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 16:31:10 +0530 Subject: [PATCH 11/13] cleanup Signed-off-by: nidhi-singh02 --- mod/beacon/blockchain/types.go | 1 - mod/node-api/backend/genesis.go | 1 - 2 files changed, 2 deletions(-) diff --git a/mod/beacon/blockchain/types.go b/mod/beacon/blockchain/types.go index 93fc719832..3a50033017 100644 --- a/mod/beacon/blockchain/types.go +++ b/mod/beacon/blockchain/types.go @@ -113,7 +113,6 @@ 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. diff --git a/mod/node-api/backend/genesis.go b/mod/node-api/backend/genesis.go index 775ddc6290..484a888637 100644 --- a/mod/node-api/backend/genesis.go +++ b/mod/node-api/backend/genesis.go @@ -60,6 +60,5 @@ func (b Backend[ if err != nil { return 0, err } - return st.GetGenesisTime() } From d28d42e5384e28e1cce6591fb3fd00571a5d2594 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Wed, 25 Sep 2024 22:05:31 +0530 Subject: [PATCH 12/13] cleanup Signed-off-by: nidhi-singh02 --- mod/consensus-types/pkg/types/genesis.go | 6 ------ mod/node-core/pkg/components/interfaces.go | 2 -- 2 files changed, 8 deletions(-) diff --git a/mod/consensus-types/pkg/types/genesis.go b/mod/consensus-types/pkg/types/genesis.go index 2b5ff3ca87..6dc80054ea 100644 --- a/mod/consensus-types/pkg/types/genesis.go +++ b/mod/consensus-types/pkg/types/genesis.go @@ -41,7 +41,6 @@ type Genesis[ DepositT any, ExecutionPayloadHeaderT interface { NewFromJSON([]byte, uint32) (ExecutionPayloadHeaderT, error) - GetTimestamp() math.U64 }, ] struct { // ForkVersion is the fork version of the genesis slot. @@ -75,11 +74,6 @@ func (g *Genesis[ return g.ExecutionPayloadHeader } -// GetGenesisTime returns the genesis time. -func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) GetGenesisTime() uint64 { - return uint64(g.ExecutionPayloadHeader.GetTimestamp()) -} - // UnmarshalJSON for Genesis. func (g *Genesis[DepositT, ExecutionPayloadHeaderT]) UnmarshalJSON( data []byte, diff --git a/mod/node-core/pkg/components/interfaces.go b/mod/node-core/pkg/components/interfaces.go index a49701aab7..6ec5754e7d 100644 --- a/mod/node-core/pkg/components/interfaces.go +++ b/mod/node-core/pkg/components/interfaces.go @@ -541,8 +541,6 @@ 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. From 46022e1784039c972b7febe78967df36f61e7f63 Mon Sep 17 00:00:00 2001 From: nidhi-singh02 Date: Mon, 30 Sep 2024 16:21:01 +0530 Subject: [PATCH 13/13] as mockery updated, generate-check Signed-off-by: nidhi-singh02 --- mod/node-api/backend/mocks/beacon_state.mock.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod/node-api/backend/mocks/beacon_state.mock.go b/mod/node-api/backend/mocks/beacon_state.mock.go index bacea91a70..9e9a5083e9 100644 --- a/mod/node-api/backend/mocks/beacon_state.mock.go +++ b/mod/node-api/backend/mocks/beacon_state.mock.go @@ -391,7 +391,7 @@ func (_m *BeaconState[BeaconBlockHeaderT, Eth1DataT, ExecutionPayloadHeaderT, Fo } // BeaconState_GetGenesisTime_Call is a *mock.Call that shadows Run/Return methods with type explicit version for method 'GetGenesisTime' -type BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT interface{}, Eth1DataT interface{}, ExecutionPayloadHeaderT interface{}, ForkT interface{}, ValidatorT interface{}, ValidatorsT interface{}, WithdrawalT interface{}] struct { +type BeaconState_GetGenesisTime_Call[BeaconBlockHeaderT any, Eth1DataT any, ExecutionPayloadHeaderT any, ForkT any, ValidatorT any, ValidatorsT any, WithdrawalT any] struct { *mock.Call }