Skip to content

Commit

Permalink
Electra: Beacon State
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvanloon committed Apr 26, 2024
1 parent d41c9fa commit f667110
Show file tree
Hide file tree
Showing 33 changed files with 1,174 additions and 133 deletions.
6 changes: 3 additions & 3 deletions beacon-chain/blockchain/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ func (s *Service) getPayloadAttribute(ctx context.Context, st state.BeaconState,

var attr payloadattribute.Attributer
switch st.Version() {
case version.Deneb:
withdrawals, err := st.ExpectedWithdrawals()
case version.Deneb, version.Electra:
withdrawals, _, err := st.ExpectedWithdrawals()
if err != nil {
log.WithError(err).Error("Could not get expected withdrawals to get payload attribute")
return emptyAttri
Expand All @@ -342,7 +342,7 @@ func (s *Service) getPayloadAttribute(ctx context.Context, st state.BeaconState,
return emptyAttri
}
case version.Capella:
withdrawals, err := st.ExpectedWithdrawals()
withdrawals, _, err := st.ExpectedWithdrawals()
if err != nil {
log.WithError(err).Error("Could not get expected withdrawals to get payload attribute")
return emptyAttri
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/withdrawals.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func ValidateBLSToExecutionChange(st state.ReadOnlyBeaconState, signed *ethpb.Si
// next_validator_index = ValidatorIndex(next_index % len(state.validators))
// state.next_withdrawal_validator_index = next_validator_index
func ProcessWithdrawals(st state.BeaconState, executionData interfaces.ExecutionData) (state.BeaconState, error) {
expectedWithdrawals, err := st.ExpectedWithdrawals()
expectedWithdrawals, _, err := st.ExpectedWithdrawals()
if err != nil {
return nil, errors.Wrap(err, "could not get expected withdrawals")
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/rpc/eth/builder/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *Server) ExpectedWithdrawals(w http.ResponseWriter, r *http.Request) {
})
return
}
withdrawals, err := st.ExpectedWithdrawals()
withdrawals, _, err := st.ExpectedWithdrawals()
if err != nil {
httputil.WriteError(w, &httputil.DefaultJsonError{
Message: "could not get expected withdrawals",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/eth/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (s *Server) sendPayloadAttributes(ctx context.Context, w http.ResponseWrite
SuggestedFeeRecipient: hexutil.Encode(headPayload.FeeRecipient()),
}
case version.Capella:
withdrawals, err := headState.ExpectedWithdrawals()
withdrawals, _, err := headState.ExpectedWithdrawals()
if err != nil {
return write(w, flusher, "Could not get head state expected withdrawals: "+err.Error())
}
Expand All @@ -451,7 +451,7 @@ func (s *Server) sendPayloadAttributes(ctx context.Context, w http.ResponseWrite
Withdrawals: structs.WithdrawalsFromConsensus(withdrawals),
}
case version.Deneb:
withdrawals, err := headState.ExpectedWithdrawals()
withdrawals, _, err := headState.ExpectedWithdrawals()
if err != nil {
return write(w, flusher, "Could not get head state expected withdrawals: "+err.Error())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (vs *Server) getLocalPayload(ctx context.Context, blk interfaces.ReadOnlyBe
}
var attr payloadattribute.Attributer
switch st.Version() {
case version.Deneb:
withdrawals, err := st.ExpectedWithdrawals()
case version.Deneb, version.Electra:
withdrawals, _, err := st.ExpectedWithdrawals()
if err != nil {
return nil, false, err
}
Expand All @@ -143,7 +143,7 @@ func (vs *Server) getLocalPayload(ctx context.Context, blk interfaces.ReadOnlyBe
return nil, false, err
}
case version.Capella:
withdrawals, err := st.ExpectedWithdrawals()
withdrawals, _, err := st.ExpectedWithdrawals()
if err != nil {
return nil, false, err
}
Expand Down
32 changes: 31 additions & 1 deletion 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
ReadOnlyElectra
ToProtoUnsafe() interface{}
ToProto() interface{}
GenesisTime() uint64
Expand Down Expand Up @@ -87,6 +88,8 @@ type WriteOnlyBeaconState interface {
WriteOnlyParticipation
WriteOnlyInactivity
WriteOnlySyncCommittee
WriteOnlyPendingBalanceDeposits
WriteOnlyElectra
SetGenesisTime(val uint64) error
SetGenesisValidatorsRoot(val []byte) error
SetSlot(val primitives.Slot) error
Expand Down Expand Up @@ -181,7 +184,7 @@ type ReadOnlyAttestations interface {

// ReadOnlyWithdrawals defines a struct which only has read access to withdrawal methods.
type ReadOnlyWithdrawals interface {
ExpectedWithdrawals() ([]*enginev1.Withdrawal, error)
ExpectedWithdrawals() ([]*enginev1.Withdrawal, uint64, error)
NextWithdrawalValidatorIndex() (primitives.ValidatorIndex, error)
NextWithdrawalIndex() (uint64, error)
}
Expand All @@ -203,6 +206,18 @@ type ReadOnlySyncCommittee interface {
NextSyncCommittee() (*ethpb.SyncCommittee, error)
}

type ReadOnlyElectra interface {
ConsolidationBalanceToConsume() (uint64, error)
DepositBalanceToConsume() (uint64, error)
EarliestConsolidationEpoch() (primitives.Epoch, error)
PendingBalanceDeposits() ([]*ethpb.PendingBalanceDeposit, error)
PendingConsolidations() ([]*ethpb.PendingConsolidation, error)
NumPendingConsolidations() uint64
NumPendingPartialWithdrawals() uint64
ExitEpochAndUpdateChurn(exitBalance uint64) (primitives.Epoch, error)
PendingBalanceToWithdraw(idx primitives.ValidatorIndex) (uint64, error)
}

// WriteOnlyBlockRoots defines a struct which only has write access to block roots methods.
type WriteOnlyBlockRoots interface {
SetBlockRoots(val [][]byte) error
Expand Down Expand Up @@ -282,3 +297,18 @@ type WriteOnlySyncCommittee interface {
SetCurrentSyncCommittee(val *ethpb.SyncCommittee) error
SetNextSyncCommittee(val *ethpb.SyncCommittee) error
}

type WriteOnlyPendingBalanceDeposits interface {
AppendPendingBalanceDeposit(index primitives.ValidatorIndex, amount uint64) error
}

type WriteOnlyElectra interface {
SetConsolidationBalanceToConsume(gwei uint64) error
SetEarliestConsolidationEpoch(epoch primitives.Epoch) error
SetPendingBalanceDeposits(val []*ethpb.PendingBalanceDeposit) error
SetDepositBalanceToConsume(gwei uint64) error
SetPendingConsolidations(val []*ethpb.PendingConsolidation) error
DequeuePartialWithdrawals(idx uint64) error
AppendPendingConsolidation(val *ethpb.PendingConsolidation) error
AppendPendingPartialWithdrawal(ppw *ethpb.PendingPartialWithdrawal) error
}
13 changes: 9 additions & 4 deletions beacon-chain/state/state-native/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"beacon_state_mainnet.go",
"beacon_state_minimal.go", # keep
"doc.go",
"error.go",
"getters_attestation.go",
"getters_balance_deposits.go",
"getters_block.go",
"getters_checkpoint.go",
"getters_consolidation.go",
"getters_eth1.go",
"getters_misc.go",
"getters_participation.go",
Expand All @@ -22,8 +26,11 @@ go_library(
"proofs.go",
"readonly_validator.go",
"setters_attestation.go",
"setters_balance_deposits.go",
"setters_block.go",
"setters_checkpoint.go",
"setters_churn.go",
"setters_consolidation.go",
"setters_eth1.go",
"setters_misc.go",
"setters_participation.go",
Expand All @@ -37,13 +44,11 @@ go_library(
"ssz.go",
"state_trie.go",
"types.go",
] + select({
"//config:mainnet": ["beacon_state_mainnet.go"],
"//config:minimal": ["beacon_state_minimal.go"],
}),
],
importpath = "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/time:go_default_library",
"//beacon-chain/state:go_default_library",
"//beacon-chain/state/fieldtrie:go_default_library",
Expand Down
13 changes: 13 additions & 0 deletions beacon-chain/state/state-native/beacon_state_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,21 @@ type BeaconState struct {
latestExecutionPayloadHeader *enginev1.ExecutionPayloadHeader
latestExecutionPayloadHeaderCapella *enginev1.ExecutionPayloadHeaderCapella
latestExecutionPayloadHeaderDeneb *enginev1.ExecutionPayloadHeaderDeneb
latestExecutionPayloadHeaderElectra *enginev1.ExecutionPayloadHeaderElectra
nextWithdrawalIndex uint64
nextWithdrawalValidatorIndex primitives.ValidatorIndex

// Electra fields
depositReceiptsStartIndex uint64
depositBalanceToConsume uint64 // Gwei
exitBalanceToConsume uint64 // Gwei
earliestExitEpoch primitives.Epoch
consolidationBalanceToConsume uint64 // Gwei
earliestConsolidationEpoch primitives.Epoch
pendingBalanceDeposits []*ethpb.PendingBalanceDeposit // pending_balance_deposits: List[PendingBalanceDeposit, PENDING_BALANCE_DEPOSITS_LIMIT]
pendingPartialWithdrawals []*ethpb.PendingPartialWithdrawal // pending_partial_withdrawals: List[PartialWithdrawal, PENDING_PARTIAL_WITHDRAWALS_LIMIT]
pendingConsolidations []*ethpb.PendingConsolidation // pending_consolidations: List[PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT]

id uint64
lock sync.RWMutex
dirtyFields map[types.FieldIndex]bool
Expand Down Expand Up @@ -163,6 +175,7 @@ func (b *BeaconState) MarshalJSON() ([]byte, error) {
LatestExecutionPayloadHeaderCapella: b.latestExecutionPayloadHeaderCapella,
NextWithdrawalIndex: b.nextWithdrawalIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
// TODO: Electra fields!
}
return json.Marshal(marshalable)
}
13 changes: 13 additions & 0 deletions beacon-chain/state/state-native/beacon_state_minimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,21 @@ type BeaconState struct {
latestExecutionPayloadHeader *enginev1.ExecutionPayloadHeader
latestExecutionPayloadHeaderCapella *enginev1.ExecutionPayloadHeaderCapella
latestExecutionPayloadHeaderDeneb *enginev1.ExecutionPayloadHeaderDeneb
latestExecutionPayloadHeaderElectra *enginev1.ExecutionPayloadHeaderElectra
nextWithdrawalIndex uint64
nextWithdrawalValidatorIndex primitives.ValidatorIndex

// Electra fields
depositReceiptsStartIndex uint64
depositBalanceToConsume uint64 // Gwei
exitBalanceToConsume uint64 // Gwei
earliestExitEpoch primitives.Epoch
consolidationBalanceToConsume uint64 // Gwei
earliestConsolidationEpoch primitives.Epoch
pendingBalanceDeposits []*ethpb.PendingBalanceDeposit // pending_balance_deposits: List[PendingBalanceDeposit, PENDING_BALANCE_DEPOSITS_LIMIT]
pendingPartialWithdrawals []*ethpb.PendingPartialWithdrawal // pending_partial_withdrawals: List[PartialWithdrawal, PENDING_PARTIAL_WITHDRAWALS_LIMIT]
pendingConsolidations []*ethpb.PendingConsolidation // pending_consolidations: List[PendingConsolidation, PENDING_CONSOLIDATIONS_LIMIT]

id uint64
lock sync.RWMutex
dirtyFields map[types.FieldIndex]bool
Expand Down Expand Up @@ -163,6 +175,7 @@ func (b *BeaconState) MarshalJSON() ([]byte, error) {
LatestExecutionPayloadHeaderCapella: b.latestExecutionPayloadHeaderCapella,
NextWithdrawalIndex: b.nextWithdrawalIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
// TODO: Electra fields!
}
return json.Marshal(marshalable)
}
32 changes: 32 additions & 0 deletions beacon-chain/state/state-native/getters_balance_deposits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package state_native

import (
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)

func (b *BeaconState) DepositBalanceToConsume() (uint64, error) {
if b.version < version.Electra {
return 0, errNotSupported("DepositBalanceToConsume", b.version)
}
b.lock.RLock()
defer b.lock.RUnlock()
return b.depositBalanceToConsume, nil
}

func (b *BeaconState) PendingBalanceDeposits() ([]*ethpb.PendingBalanceDeposit, error) {
if b.version < version.Electra {
return nil, errNotSupported("PendingBalanceDeposits", b.version)
}
b.lock.RLock()
defer b.lock.RUnlock()
return b.pendingBalanceDeposits, nil
}

func (b *BeaconState) pendingBalanceDepositsVal() []*ethpb.PendingBalanceDeposit {
if b.pendingBalanceDeposits == nil {
return nil
}

return ethpb.CopyPendingBalanceDeposits(b.pendingBalanceDeposits)
}
48 changes: 48 additions & 0 deletions beacon-chain/state/state-native/getters_consolidation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package state_native

import (
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)

func (b *BeaconState) EarliestConsolidationEpoch() (primitives.Epoch, error) {
if b.version < version.Electra {
return 0, errNotSupported("EarliestConsolidationEpoch", b.version)
}
b.lock.RLock()
defer b.lock.RUnlock()
return b.earliestConsolidationEpoch, nil
}

func (b *BeaconState) ConsolidationBalanceToConsume() (uint64, error) {
if b.version < version.Electra {
return 0, errNotSupported("ConsolidationBalanceToConsume", b.version)
}
b.lock.RLock()
defer b.lock.RUnlock()
return b.consolidationBalanceToConsume, nil
}

func (b *BeaconState) PendingConsolidations() ([]*ethpb.PendingConsolidation, error) {
if b.version < version.Electra {
return nil, errNotSupported("PendingConsolidations", b.version)
}
b.lock.RLock()
defer b.lock.RUnlock()
return b.pendingConsolidations, nil
}

func (b *BeaconState) NumPendingConsolidations() uint64 {
b.lock.RLock()
defer b.lock.RUnlock()
return uint64(len(b.pendingConsolidations))
}

func (b *BeaconState) pendingConsolidationsVal() []*ethpb.PendingConsolidation {
if b.pendingConsolidations == nil {
return nil
}

return ethpb.CopyPendingConsolidations(b.pendingConsolidations)
}
20 changes: 14 additions & 6 deletions beacon-chain/state/state-native/getters_payload_header.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package state_native

import (
"errors"
"math/big"

"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
Expand All @@ -19,15 +20,18 @@ func (b *BeaconState) LatestExecutionPayloadHeader() (interfaces.ExecutionData,
b.lock.RLock()
defer b.lock.RUnlock()

if b.version == version.Bellatrix {
switch b.version {
case version.Bellatrix:
return blocks.WrappedExecutionPayloadHeader(b.latestExecutionPayloadHeaderVal())
}

if b.version == version.Capella {
case version.Capella:
return blocks.WrappedExecutionPayloadHeaderCapella(b.latestExecutionPayloadHeaderCapellaVal(), big.NewInt(0))
case version.Deneb:
return blocks.WrappedExecutionPayloadHeaderDeneb(b.latestExecutionPayloadHeaderDenebVal(), big.NewInt(0))
case version.Electra:
return blocks.WrappedExecutionPayloadHeaderElectra(b.latestExecutionPayloadHeaderElectraVal(), big.NewInt(0))
default:
return nil, errors.New("unsupported version for latest execution payload header")
}

return blocks.WrappedExecutionPayloadHeaderDeneb(b.latestExecutionPayloadHeaderDenebVal(), big.NewInt(0))
}

// latestExecutionPayloadHeaderVal of the beacon state.
Expand All @@ -45,3 +49,7 @@ func (b *BeaconState) latestExecutionPayloadHeaderCapellaVal() *enginev1.Executi
func (b *BeaconState) latestExecutionPayloadHeaderDenebVal() *enginev1.ExecutionPayloadHeaderDeneb {
return ethpb.CopyExecutionPayloadHeaderDeneb(b.latestExecutionPayloadHeaderDeneb)
}

func (b *BeaconState) latestExecutionPayloadHeaderElectraVal() *enginev1.ExecutionPayloadHeaderElectra {
return ethpb.CopyExecutionPayloadHeaderElectra(b.latestExecutionPayloadHeaderElectra)
}
Loading

0 comments on commit f667110

Please sign in to comment.