-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* poc for payload electra cloning * partial fixes * fixing build * addressing kasey's comment * forgot to unexport interface * making test more generic * making fuzzing slightly more robust * renaming based on kasey's comment * using fuzz test in same package to avoid exporting interface * fixing build
- Loading branch information
1 parent
49055ac
commit 8364226
Showing
6 changed files
with
146 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package enginev1 | ||
|
||
import "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" | ||
|
||
type copier[T any] interface { | ||
Copy() T | ||
} | ||
|
||
func copySlice[T any, C copier[T]](original []C) []T { | ||
// Create a new slice with the same length as the original | ||
newSlice := make([]T, len(original)) | ||
for i := 0; i < len(newSlice); i++ { | ||
newSlice[i] = original[i].Copy() | ||
} | ||
return newSlice | ||
} | ||
|
||
func (w *Withdrawal) Copy() *Withdrawal { | ||
if w == nil { | ||
return nil | ||
} | ||
|
||
return &Withdrawal{ | ||
Index: w.Index, | ||
ValidatorIndex: w.ValidatorIndex, | ||
Address: bytesutil.SafeCopyBytes(w.Address), | ||
Amount: w.Amount, | ||
} | ||
} | ||
|
||
func (d *DepositRequest) Copy() *DepositRequest { | ||
if d == nil { | ||
return nil | ||
} | ||
return &DepositRequest{ | ||
Pubkey: bytesutil.SafeCopyBytes(d.Pubkey), | ||
WithdrawalCredentials: bytesutil.SafeCopyBytes(d.WithdrawalCredentials), | ||
Amount: d.Amount, | ||
Signature: bytesutil.SafeCopyBytes(d.Signature), | ||
Index: d.Index, | ||
} | ||
} | ||
|
||
func (wr *WithdrawalRequest) Copy() *WithdrawalRequest { | ||
if wr == nil { | ||
return nil | ||
} | ||
return &WithdrawalRequest{ | ||
SourceAddress: bytesutil.SafeCopyBytes(wr.SourceAddress), | ||
ValidatorPubkey: bytesutil.SafeCopyBytes(wr.ValidatorPubkey), | ||
Amount: wr.Amount, | ||
} | ||
} | ||
|
||
func (cr *ConsolidationRequest) Copy() *ConsolidationRequest { | ||
if cr == nil { | ||
return nil | ||
} | ||
return &ConsolidationRequest{ | ||
SourceAddress: bytesutil.SafeCopyBytes(cr.SourceAddress), | ||
SourcePubkey: bytesutil.SafeCopyBytes(cr.SourcePubkey), | ||
TargetPubkey: bytesutil.SafeCopyBytes(cr.TargetPubkey), | ||
} | ||
} | ||
|
||
func (payload *ExecutionPayloadElectra) Copy() *ExecutionPayloadElectra { | ||
if payload == nil { | ||
return nil | ||
} | ||
return &ExecutionPayloadElectra{ | ||
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, | ||
DepositRequests: copySlice(payload.DepositRequests), | ||
WithdrawalRequests: copySlice(payload.WithdrawalRequests), | ||
ConsolidationRequests: copySlice(payload.ConsolidationRequests), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package enginev1_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
fuzz "github.com/google/gofuzz" | ||
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1" | ||
"github.com/prysmaticlabs/prysm/v5/testing/require" | ||
) | ||
|
||
func TestCopyExecutionPayload_Fuzz(t *testing.T) { | ||
fuzzCopies(t, &enginev1.ExecutionPayloadElectra{}) | ||
} | ||
|
||
func fuzzCopies[T any, C enginev1.Copier[T]](t *testing.T, obj C) { | ||
fuzzer := fuzz.NewWithSeed(0) | ||
amount := 1000 | ||
t.Run(fmt.Sprintf("%T", obj), func(t *testing.T) { | ||
for i := 0; i < amount; i++ { | ||
fuzzer.Fuzz(obj) // Populate thing with random values | ||
got := obj.Copy() | ||
require.DeepEqual(t, obj, got) | ||
// check shallow copy working | ||
fuzzer.Fuzz(got) | ||
require.DeepNotEqual(t, obj, got) | ||
// TODO: think of deeper not equal fuzzing | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package enginev1 | ||
|
||
type Copier[T any] copier[T] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters