Skip to content

Commit

Permalink
creating execution payload copy and fuzz tests (#14246)
Browse files Browse the repository at this point in the history
* creating execution payload copy and fuzz tests

* fixing build

* removing unused code
  • Loading branch information
james-prysm authored Jul 22, 2024
1 parent fd9321f commit b1be6cd
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 270 deletions.
30 changes: 4 additions & 26 deletions beacon-chain/state/state-native/getters_payload_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (

"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)

Expand All @@ -21,34 +19,14 @@ func (b *BeaconState) LatestExecutionPayloadHeader() (interfaces.ExecutionData,

switch b.version {
case version.Bellatrix:
return blocks.WrappedExecutionPayloadHeader(b.latestExecutionPayloadHeaderVal())
return blocks.WrappedExecutionPayloadHeader(b.latestExecutionPayloadHeader.Copy())
case version.Capella:
return blocks.WrappedExecutionPayloadHeaderCapella(b.latestExecutionPayloadHeaderCapellaVal())
return blocks.WrappedExecutionPayloadHeaderCapella(b.latestExecutionPayloadHeaderCapella.Copy())
case version.Deneb:
return blocks.WrappedExecutionPayloadHeaderDeneb(b.latestExecutionPayloadHeaderDenebVal())
return blocks.WrappedExecutionPayloadHeaderDeneb(b.latestExecutionPayloadHeaderDeneb.Copy())
case version.Electra:
return blocks.WrappedExecutionPayloadHeaderElectra(b.latestExecutionPayloadHeaderElectraVal())
return blocks.WrappedExecutionPayloadHeaderElectra(b.latestExecutionPayloadHeaderElectra.Copy())
default:
return nil, fmt.Errorf("unsupported version (%s) for latest execution payload header", version.String(b.version))
}
}

// latestExecutionPayloadHeaderVal of the beacon state.
// This assumes that a lock is already held on BeaconState.
func (b *BeaconState) latestExecutionPayloadHeaderVal() *enginev1.ExecutionPayloadHeader {
return ethpb.CopyExecutionPayloadHeader(b.latestExecutionPayloadHeader)
}

// latestExecutionPayloadHeaderCapellaVal of the beacon state.
// This assumes that a lock is already held on BeaconState.
func (b *BeaconState) latestExecutionPayloadHeaderCapellaVal() *enginev1.ExecutionPayloadHeaderCapella {
return ethpb.CopyExecutionPayloadHeaderCapella(b.latestExecutionPayloadHeaderCapella)
}

func (b *BeaconState) latestExecutionPayloadHeaderDenebVal() *enginev1.ExecutionPayloadHeaderDeneb {
return ethpb.CopyExecutionPayloadHeaderDeneb(b.latestExecutionPayloadHeaderDeneb)
}

func (b *BeaconState) latestExecutionPayloadHeaderElectraVal() *enginev1.ExecutionPayloadHeaderElectra {
return ethpb.CopyExecutionPayloadHeaderElectra(b.latestExecutionPayloadHeaderElectra)
}
8 changes: 4 additions & 4 deletions beacon-chain/state/state-native/getters_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (b *BeaconState) ToProto() interface{} {
InactivityScores: inactivityScores,
CurrentSyncCommittee: b.currentSyncCommitteeVal(),
NextSyncCommittee: b.nextSyncCommitteeVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeader.Copy(),
}
case version.Capella:
return &ethpb.BeaconStateCapella{
Expand Down Expand Up @@ -342,7 +342,7 @@ func (b *BeaconState) ToProto() interface{} {
InactivityScores: inactivityScores,
CurrentSyncCommittee: b.currentSyncCommitteeVal(),
NextSyncCommittee: b.nextSyncCommitteeVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderCapellaVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderCapella.Copy(),
NextWithdrawalIndex: b.nextWithdrawalIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
HistoricalSummaries: b.historicalSummariesVal(),
Expand Down Expand Up @@ -373,7 +373,7 @@ func (b *BeaconState) ToProto() interface{} {
InactivityScores: b.inactivityScoresVal(),
CurrentSyncCommittee: b.currentSyncCommitteeVal(),
NextSyncCommittee: b.nextSyncCommitteeVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderDenebVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderDeneb.Copy(),
NextWithdrawalIndex: b.nextWithdrawalIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
HistoricalSummaries: b.historicalSummariesVal(),
Expand Down Expand Up @@ -404,7 +404,7 @@ func (b *BeaconState) ToProto() interface{} {
InactivityScores: b.inactivityScoresVal(),
CurrentSyncCommittee: b.currentSyncCommitteeVal(),
NextSyncCommittee: b.nextSyncCommitteeVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderElectraVal(),
LatestExecutionPayloadHeader: b.latestExecutionPayloadHeaderElectra.Copy(),
NextWithdrawalIndex: b.nextWithdrawalIndex,
NextWithdrawalValidatorIndex: b.nextWithdrawalValidatorIndex,
HistoricalSummaries: b.historicalSummariesVal(),
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/state/state-native/state_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,10 +907,10 @@ func (b *BeaconState) Copy() state.BeaconState {
finalizedCheckpoint: b.finalizedCheckpointVal(),
currentSyncCommittee: b.currentSyncCommitteeVal(),
nextSyncCommittee: b.nextSyncCommitteeVal(),
latestExecutionPayloadHeader: b.latestExecutionPayloadHeaderVal(),
latestExecutionPayloadHeaderCapella: b.latestExecutionPayloadHeaderCapellaVal(),
latestExecutionPayloadHeaderDeneb: b.latestExecutionPayloadHeaderDenebVal(),
latestExecutionPayloadHeaderElectra: b.latestExecutionPayloadHeaderElectraVal(),
latestExecutionPayloadHeader: b.latestExecutionPayloadHeader.Copy(),
latestExecutionPayloadHeaderCapella: b.latestExecutionPayloadHeaderCapella.Copy(),
latestExecutionPayloadHeaderDeneb: b.latestExecutionPayloadHeaderDeneb.Copy(),
latestExecutionPayloadHeaderElectra: b.latestExecutionPayloadHeaderElectra.Copy(),

id: types.Enumerator.Inc(),

Expand Down
183 changes: 183 additions & 0 deletions proto/engine/v1/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func copySlice[T any, C copier[T]](original []C) []T {
return newSlice
}

// Copy --
func (w *Withdrawal) Copy() *Withdrawal {
if w == nil {
return nil
Expand All @@ -28,6 +29,7 @@ func (w *Withdrawal) Copy() *Withdrawal {
}
}

// Copy --
func (d *DepositRequest) Copy() *DepositRequest {
if d == nil {
return nil
Expand All @@ -41,6 +43,7 @@ func (d *DepositRequest) Copy() *DepositRequest {
}
}

// Copy --
func (wr *WithdrawalRequest) Copy() *WithdrawalRequest {
if wr == nil {
return nil
Expand All @@ -52,6 +55,7 @@ func (wr *WithdrawalRequest) Copy() *WithdrawalRequest {
}
}

// Copy --
func (cr *ConsolidationRequest) Copy() *ConsolidationRequest {
if cr == nil {
return nil
Expand All @@ -63,6 +67,7 @@ func (cr *ConsolidationRequest) Copy() *ConsolidationRequest {
}
}

// Copy -- Electra
func (payload *ExecutionPayloadElectra) Copy() *ExecutionPayloadElectra {
if payload == nil {
return nil
Expand Down Expand Up @@ -90,3 +95,181 @@ func (payload *ExecutionPayloadElectra) Copy() *ExecutionPayloadElectra {
ConsolidationRequests: copySlice(payload.ConsolidationRequests),
}
}

// Copy -- Deneb
func (payload *ExecutionPayloadDeneb) Copy() *ExecutionPayloadDeneb {
if payload == nil {
return nil
}
return &ExecutionPayloadDeneb{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
Transactions: bytesutil.SafeCopy2dBytes(payload.Transactions),
Withdrawals: copySlice(payload.Withdrawals),
BlobGasUsed: payload.BlobGasUsed,
ExcessBlobGas: payload.ExcessBlobGas,
}
}

// Copy -- Capella
func (payload *ExecutionPayloadCapella) Copy() *ExecutionPayloadCapella {
if payload == nil {
return nil
}

return &ExecutionPayloadCapella{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
Transactions: bytesutil.SafeCopy2dBytes(payload.Transactions),
Withdrawals: copySlice(payload.Withdrawals),
}
}

// Copy -- Bellatrix
func (payload *ExecutionPayload) Copy() *ExecutionPayload {
if payload == nil {
return nil
}

return &ExecutionPayload{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
Transactions: bytesutil.SafeCopy2dBytes(payload.Transactions),
}
}

// Copy -- Electra
func (payload *ExecutionPayloadHeaderElectra) Copy() *ExecutionPayloadHeaderElectra {
if payload == nil {
return nil
}

return &ExecutionPayloadHeaderElectra{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
TransactionsRoot: bytesutil.SafeCopyBytes(payload.TransactionsRoot),
WithdrawalsRoot: bytesutil.SafeCopyBytes(payload.WithdrawalsRoot),
BlobGasUsed: payload.BlobGasUsed,
ExcessBlobGas: payload.ExcessBlobGas,
DepositRequestsRoot: bytesutil.SafeCopyBytes(payload.DepositRequestsRoot),
WithdrawalRequestsRoot: bytesutil.SafeCopyBytes(payload.WithdrawalRequestsRoot),
ConsolidationRequestsRoot: bytesutil.SafeCopyBytes(payload.ConsolidationRequestsRoot),
}
}

// Copy -- Deneb
func (payload *ExecutionPayloadHeaderDeneb) Copy() *ExecutionPayloadHeaderDeneb {
if payload == nil {
return nil
}
return &ExecutionPayloadHeaderDeneb{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
TransactionsRoot: bytesutil.SafeCopyBytes(payload.TransactionsRoot),
WithdrawalsRoot: bytesutil.SafeCopyBytes(payload.WithdrawalsRoot),
BlobGasUsed: payload.BlobGasUsed,
ExcessBlobGas: payload.ExcessBlobGas,
}
}

// Copy -- Capella
func (payload *ExecutionPayloadHeaderCapella) Copy() *ExecutionPayloadHeaderCapella {
if payload == nil {
return nil
}
return &ExecutionPayloadHeaderCapella{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
TransactionsRoot: bytesutil.SafeCopyBytes(payload.TransactionsRoot),
WithdrawalsRoot: bytesutil.SafeCopyBytes(payload.WithdrawalsRoot),
}
}

// Copy -- Bellatrix
func (payload *ExecutionPayloadHeader) Copy() *ExecutionPayloadHeader {
if payload == nil {
return nil
}
return &ExecutionPayloadHeader{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao),
BlockNumber: payload.BlockNumber,
GasLimit: payload.GasLimit,
GasUsed: payload.GasUsed,
Timestamp: payload.Timestamp,
ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData),
BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas),
BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash),
TransactionsRoot: bytesutil.SafeCopyBytes(payload.TransactionsRoot),
}
}
10 changes: 10 additions & 0 deletions proto/engine/v1/execution_engine_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (

func TestCopyExecutionPayload_Fuzz(t *testing.T) {
fuzzCopies(t, &enginev1.ExecutionPayloadElectra{})
fuzzCopies(t, &enginev1.ExecutionPayloadDeneb{})
fuzzCopies(t, &enginev1.ExecutionPayloadCapella{})
fuzzCopies(t, &enginev1.ExecutionPayload{})
}

func TestCopyExecutionPayloadHeader_Fuzz(t *testing.T) {
fuzzCopies(t, &enginev1.ExecutionPayloadHeaderElectra{})
fuzzCopies(t, &enginev1.ExecutionPayloadHeaderDeneb{})
fuzzCopies(t, &enginev1.ExecutionPayloadHeaderCapella{})
fuzzCopies(t, &enginev1.ExecutionPayloadHeader{})
}

func fuzzCopies[T any, C enginev1.Copier[T]](t *testing.T, obj C) {
Expand Down
Loading

0 comments on commit b1be6cd

Please sign in to comment.