Skip to content

Commit

Permalink
Add ePBS to state
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed May 7, 2024
1 parent 21b2300 commit 54199c6
Show file tree
Hide file tree
Showing 24 changed files with 1,169 additions and 66 deletions.
1 change: 1 addition & 0 deletions beacon-chain/state/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
srcs = [
"error.go",
"interfaces.go",
"interfaces_epbs.go",
"prometheus.go",
],
importpath = "github.com/prysmaticlabs/prysm/v5/beacon-chain/state",
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ type ReadOnlyBeaconState interface {
ReadOnlySyncCommittee
ReadOnlyDeposits
ReadOnlyConsolidations
ReadOnlyEpbs
ToProtoUnsafe() interface{}
ToProto() interface{}
GenesisTime() uint64
Expand Down Expand Up @@ -93,6 +94,7 @@ type WriteOnlyBeaconState interface {
WriteOnlyConsolidations
WriteOnlyWithdrawals
WriteOnlyDeposits
WriteOnlyEpbs
SetGenesisTime(val uint64) error
SetGenesisValidatorsRoot(val []byte) error
SetSlot(val primitives.Slot) error
Expand Down
28 changes: 28 additions & 0 deletions beacon-chain/state/interfaces_epbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package state

import (
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
)

type ReadOnlyEpbs interface {
PreviousInclusionListSlot() (primitives.Slot, error)
PreviousInclusionListProposer() (primitives.ValidatorIndex, error)
LatestInclusionListSlot() (primitives.Slot, error)
LatestInclusionListProposer() (primitives.ValidatorIndex, error)
IsParentBlockFull() (bool, error)
ExecutionPayloadHeader() (*enginev1.ExecutionPayloadHeaderEPBS, error)
LatestBlockHash() ([]byte, error)
LatestFullSlot() (primitives.Slot, error)
LastWithdrawalsRoot() ([]byte, error)
}

type WriteOnlyEpbs interface {
SetExecutionPayloadHeader(val *enginev1.ExecutionPayloadHeaderEPBS) error
UpdatePreviousInclusionListData() error
SetLatestInclusionListSlot(val primitives.Slot) error
SetLatestInclusionListProposer(val primitives.ValidatorIndex) error
SetLatestBlockHash(val []byte) error
SetLatestFullSlot(val primitives.Slot) error
SetLastWithdrawalsRoot(val []byte) error
}
7 changes: 7 additions & 0 deletions beacon-chain/state/state-native/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ go_library(
"getters_checkpoint.go",
"getters_consolidation.go",
"getters_deposit_receipts.go",
"getters_epbs.go",
"getters_eth1.go",
"getters_misc.go",
"getters_participation.go",
"getters_payload_header.go",
"getters_payload_header_epbs.go",
"getters_randao.go",
"getters_state.go",
"getters_sync_committee.go",
Expand All @@ -33,6 +35,7 @@ go_library(
"setters_churn.go",
"setters_consolidation.go",
"setters_deposit_receipts.go",
"setters_epbs.go",
"setters_eth1.go",
"setters_misc.go",
"setters_participation.go",
Expand All @@ -45,6 +48,7 @@ go_library(
"spec_parameters.go",
"ssz.go",
"state_trie.go",
"state_trie_epbs.go",
"types.go",
"validator_index_cache.go",
],
Expand Down Expand Up @@ -95,6 +99,7 @@ go_test(
"getters_consolidation_test.go",
"getters_deposit_receipts_test.go",
"getters_participation_test.go",
"getters_setters_epbs_test.go",
"getters_test.go",
"getters_validator_test.go",
"getters_withdrawal_test.go",
Expand All @@ -116,6 +121,7 @@ go_test(
"setters_withdrawal_test.go",
"state_fuzz_test.go",
"state_test.go",
"state_trie_epbs_test.go",
"state_trie_test.go",
"types_test.go",
"validator_index_cache_test.go",
Expand Down Expand Up @@ -149,6 +155,7 @@ go_test(
"//testing/assert:go_default_library",
"//testing/require:go_default_library",
"//testing/util:go_default_library",
"//testing/util/random:go_default_library",
"//time/slots:go_default_library",
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_golang_snappy//:go_default_library",
Expand Down
9 changes: 9 additions & 0 deletions beacon-chain/state/state-native/beacon_state_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ type BeaconState struct {
latestExecutionPayloadHeaderElectra *enginev1.ExecutionPayloadHeaderElectra
nextWithdrawalIndex uint64
nextWithdrawalValidatorIndex primitives.ValidatorIndex
// ePBS fields
previousInclusionListProposer primitives.ValidatorIndex
previousInclusionListSlot primitives.Slot
latestInclusionListProposer primitives.ValidatorIndex
latestInclusionListSlot primitives.Slot
latestBlockHash [32]byte
latestFullSlot primitives.Slot
executionPayloadHeader *enginev1.ExecutionPayloadHeaderEPBS
lastWithdrawalsRoot [32]byte

// Electra fields
depositReceiptsStartIndex uint64
Expand Down
9 changes: 9 additions & 0 deletions beacon-chain/state/state-native/beacon_state_minimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ type BeaconState struct {
latestExecutionPayloadHeaderElectra *enginev1.ExecutionPayloadHeaderElectra
nextWithdrawalIndex uint64
nextWithdrawalValidatorIndex primitives.ValidatorIndex
// ePBS fields
previousInclusionListProposer primitives.ValidatorIndex
previousInclusionListSlot primitives.Slot
latestInclusionListProposer primitives.ValidatorIndex
latestInclusionListSlot primitives.Slot
latestBlockHash [32]byte
latestFullSlot primitives.Slot
executionPayloadHeader *enginev1.ExecutionPayloadHeaderEPBS
lastWithdrawalsRoot [32]byte

// Electra fields
depositReceiptsStartIndex uint64
Expand Down
122 changes: 122 additions & 0 deletions beacon-chain/state/state-native/getters_epbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package state_native

import (
"github.com/pkg/errors"
consensus_types "github.com/prysmaticlabs/prysm/v5/consensus-types"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)

// ExecutionPayloadHeader retrieves a copy of the execution payload header.
// It returns an error if the operation is not supported for the beacon state's version.
func (b *BeaconState) ExecutionPayloadHeader() (*enginev1.ExecutionPayloadHeaderEPBS, error) {
if b.version < version.EPBS {
return nil, errors.Wrapf(consensus_types.ErrUnsupportedField, "ExecutionPayloadHeader not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.executionPayloadHeaderVal(), nil
}

// IsParentBlockFull checks if the last committed payload header was fulfilled.
// Returns true if both the beacon block and payload were present.
// Call this function on a beacon state before processing the execution payload header.
func (b *BeaconState) IsParentBlockFull() (bool, error) {
if b.version < version.EPBS {
return false, errors.Wrapf(consensus_types.ErrUnsupportedField, "IsParentBlockFull not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

headerBlockHash := bytesutil.ToBytes32(b.executionPayloadHeader.BlockHash)
return headerBlockHash == b.latestBlockHash, nil
}

// LatestInclusionListProposer returns the proposer index from the latest inclusion list.
func (b *BeaconState) LatestInclusionListProposer() (primitives.ValidatorIndex, error) {
if b.version < version.EPBS {
return 0, errors.Wrapf(consensus_types.ErrUnsupportedField, "LatestInclusionListProposer not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.latestInclusionListProposer, nil
}

// LatestInclusionListSlot returns the slot from the latest inclusion list.
func (b *BeaconState) LatestInclusionListSlot() (primitives.Slot, error) {
if b.version < version.EPBS {
return 0, errors.Wrapf(consensus_types.ErrUnsupportedField, "LatestInclusionListSlot not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.latestInclusionListSlot, nil
}

// PreviousInclusionListProposer returns the proposer index from the previous inclusion list.
func (b *BeaconState) PreviousInclusionListProposer() (primitives.ValidatorIndex, error) {
if b.version < version.EPBS {
return 0, errors.Wrapf(consensus_types.ErrUnsupportedField, "PreviousInclusionListProposer not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.previousInclusionListProposer, nil
}

// PreviousInclusionListSlot returns the slot from the previous inclusion list.
func (b *BeaconState) PreviousInclusionListSlot() (primitives.Slot, error) {
if b.version < version.EPBS {
return 0, errors.Wrapf(consensus_types.ErrUnsupportedField, "PreviousInclusionListSlot not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.previousInclusionListSlot, nil
}

// LatestBlockHash returns the latest block hash.
func (b *BeaconState) LatestBlockHash() ([]byte, error) {
if b.version < version.EPBS {
return nil, errors.Wrapf(consensus_types.ErrUnsupportedField, "LatestBlockHash not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.latestBlockHash[:], nil
}

// LatestFullSlot returns the slot of the latest full block.
func (b *BeaconState) LatestFullSlot() (primitives.Slot, error) {
if b.version < version.EPBS {
return 0, errors.Wrapf(consensus_types.ErrUnsupportedField, "LatestFullSlot not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.latestFullSlot, nil
}

// LastWithdrawalsRoot returns the latest withdrawal root.
func (b *BeaconState) LastWithdrawalsRoot() ([]byte, error) {
if b.version < version.EPBS {
return nil, errors.Wrapf(consensus_types.ErrUnsupportedField, "LastWithdrawalsRoot not supported for version: %d", b.version)
}

b.lock.RLock()
defer b.lock.RUnlock()

return b.lastWithdrawalsRoot[:], nil
}
10 changes: 10 additions & 0 deletions beacon-chain/state/state-native/getters_payload_header_epbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package state_native

import (
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
)

func (b *BeaconState) executionPayloadHeaderVal() *enginev1.ExecutionPayloadHeaderEPBS {
return eth.CopyExecutionPayloadHeaderEPBS(b.executionPayloadHeader)
}
Loading

0 comments on commit 54199c6

Please sign in to comment.