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 Apr 29, 2024
1 parent 9895c89 commit 3772f13
Show file tree
Hide file tree
Showing 30 changed files with 1,058 additions and 64 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 @@ -56,6 +56,7 @@ type ReadOnlyBeaconState interface {
ReadOnlyParticipation
ReadOnlyInactivity
ReadOnlySyncCommittee
ReadOnlyEpbs
ToProtoUnsafe() interface{}
ToProto() interface{}
GenesisTime() uint64
Expand Down Expand Up @@ -87,6 +88,7 @@ type WriteOnlyBeaconState interface {
WriteOnlyParticipation
WriteOnlyInactivity
WriteOnlySyncCommittee
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
}
8 changes: 8 additions & 0 deletions beacon-chain/state/state-native/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ go_library(
"getters_attestation.go",
"getters_block.go",
"getters_checkpoint.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 @@ -24,6 +26,7 @@ go_library(
"setters_attestation.go",
"setters_block.go",
"setters_checkpoint.go",
"setters_epbs.go",
"setters_eth1.go",
"setters_misc.go",
"setters_participation.go",
Expand All @@ -36,6 +39,7 @@ go_library(
"spec_parameters.go",
"ssz.go",
"state_trie.go",
"state_trie_epbs.go",
"types.go",
] + select({
"//config:mainnet": ["beacon_state_mainnet.go"],
Expand Down Expand Up @@ -84,6 +88,7 @@ go_test(
"getters_block_test.go",
"getters_checkpoint_test.go",
"getters_participation_test.go",
"getters_setters_epbs_test.go",
"getters_test.go",
"getters_validator_test.go",
"getters_withdrawal_test.go",
Expand All @@ -100,6 +105,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",
],
Expand All @@ -114,6 +120,7 @@ go_test(
"//config/features:go_default_library",
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types:go_default_library",
"//consensus-types/blocks:go_default_library",
"//consensus-types/primitives:go_default_library",
"//container/trie:go_default_library",
Expand All @@ -126,6 +133,7 @@ go_test(
"//testing/assert:go_default_library",
"//testing/require:go_default_library",
"//testing/util:go_default_library",
"//testing/util/random:go_default_library",
"@com_github_ethereum_go_ethereum//common/hexutil:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
"@com_github_sirupsen_logrus//: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 @@ -59,6 +59,15 @@ type BeaconState struct {
latestExecutionPayloadHeaderDeneb *enginev1.ExecutionPayloadHeaderDeneb
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

id uint64
lock sync.RWMutex
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 @@ -59,6 +59,15 @@ type BeaconState struct {
latestExecutionPayloadHeaderDeneb *enginev1.ExecutionPayloadHeaderDeneb
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

id uint64
lock sync.RWMutex
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
}
2 changes: 1 addition & 1 deletion beacon-chain/state/state-native/getters_payload_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// LatestExecutionPayloadHeader of the beacon state.
func (b *BeaconState) LatestExecutionPayloadHeader() (interfaces.ExecutionData, error) {
if b.version < version.Bellatrix {
if b.version < version.Bellatrix || b.version >= version.EPBS {
return nil, errNotSupported("LatestExecutionPayloadHeader", b.version)
}

Expand Down
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 3772f13

Please sign in to comment.