Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add merge beacon block wrappers #9906

Merged
merged 6 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions proto/prysm/v1alpha1/block/block_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type SignedBeaconBlock interface {
Proto() proto.Message
PbPhase0Block() (*ethpb.SignedBeaconBlock, error)
PbAltairBlock() (*ethpb.SignedBeaconBlockAltair, error)
PbMergeBlock() (*ethpb.SignedBeaconBlockMerge, error)
ssz.Marshaler
ssz.Unmarshaler
Version() int
Expand Down Expand Up @@ -54,4 +55,5 @@ type BeaconBlockBody interface {
IsNil() bool
HashTreeRoot() ([32]byte, error)
Proto() proto.Message
ExecutionPayload() (*ethpb.ExecutionPayload, error)
}
68 changes: 68 additions & 0 deletions proto/prysm/v1alpha1/cloners.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,74 @@ func CopySyncAggregate(a *SyncAggregate) *SyncAggregate {
}
}

// CopySignedBeaconBlockMerge copies the provided SignedBeaconBlockMerge.
func CopySignedBeaconBlockMerge(sigBlock *SignedBeaconBlockMerge) *SignedBeaconBlockMerge {
if sigBlock == nil {
return nil
}
return &SignedBeaconBlockMerge{
Block: CopyBeaconBlockMerge(sigBlock.Block),
Signature: bytesutil.SafeCopyBytes(sigBlock.Signature),
}
}

// CopyBeaconBlockMerge copies the provided BeaconBlockMerge.
func CopyBeaconBlockMerge(block *BeaconBlockMerge) *BeaconBlockMerge {
if block == nil {
return nil
}
return &BeaconBlockMerge{
Slot: block.Slot,
ProposerIndex: block.ProposerIndex,
ParentRoot: bytesutil.SafeCopyBytes(block.ParentRoot),
StateRoot: bytesutil.SafeCopyBytes(block.StateRoot),
Body: CopyBeaconBlockBodyMerge(block.Body),
}
}

// CopyBeaconBlockBodyMerge copies the provided BeaconBlockBodyMerge.
func CopyBeaconBlockBodyMerge(body *BeaconBlockBodyMerge) *BeaconBlockBodyMerge {
if body == nil {
return nil
}
return &BeaconBlockBodyMerge{
RandaoReveal: bytesutil.SafeCopyBytes(body.RandaoReveal),
Eth1Data: CopyETH1Data(body.Eth1Data),
Graffiti: bytesutil.SafeCopyBytes(body.Graffiti),
ProposerSlashings: CopyProposerSlashings(body.ProposerSlashings),
AttesterSlashings: CopyAttesterSlashings(body.AttesterSlashings),
Attestations: CopyAttestations(body.Attestations),
Deposits: CopyDeposits(body.Deposits),
VoluntaryExits: CopySignedVoluntaryExits(body.VoluntaryExits),
SyncAggregate: CopySyncAggregate(body.SyncAggregate),
ExecutionPayload: CopyExecutionPayload(body.ExecutionPayload),
}
}

// CopyExecutionPayload copies the provided ApplicationPayload.
func CopyExecutionPayload(payload *ExecutionPayload) *ExecutionPayload {
if payload == nil {
return nil
}

return &ExecutionPayload{
ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash),
FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient),
StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot),
ReceiptRoot: bytesutil.SafeCopyBytes(payload.ReceiptRoot),
LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom),
Random: bytesutil.SafeCopyBytes(payload.Random),
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),
}
}

// CopyExecutionPayloadHeader copies the provided execution payload object.
func CopyExecutionPayloadHeader(payload *ExecutionPayloadHeader) *ExecutionPayloadHeader {
if payload == nil {
Expand Down
81 changes: 81 additions & 0 deletions proto/prysm/v1alpha1/cloners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,36 @@ func TestCopyPayloadHeader(t *testing.T) {
assert.NotEmpty(t, got, "Copied execution payload header has empty fields")
}

func TestCopySignedBeaconBlockMerge(t *testing.T) {
sbb := genSignedBeaconBlockMerge()

got := v1alpha1.CopySignedBeaconBlockMerge(sbb)
if !reflect.DeepEqual(got, sbb) {
t.Errorf("CopySignedBeaconBlockMerge() = %v, want %v", got, sbb)
}
assert.NotEmpty(t, sbb, "Copied signed beacon block Merge has empty fields")
}

func TestCopyBeaconBlockMerge(t *testing.T) {
b := genBeaconBlockMerge()

got := v1alpha1.CopyBeaconBlockMerge(b)
if !reflect.DeepEqual(got, b) {
t.Errorf("CopyBeaconBlockMerge() = %v, want %v", got, b)
}
assert.NotEmpty(t, b, "Copied beacon block Merge has empty fields")
}

func TestCopyBeaconBlockBodyMerge(t *testing.T) {
bb := genBeaconBlockBodyMerge()

got := v1alpha1.CopyBeaconBlockBodyMerge(bb)
if !reflect.DeepEqual(got, bb) {
t.Errorf("CopyBeaconBlockBodyMerge() = %v, want %v", got, bb)
}
assert.NotEmpty(t, bb, "Copied beacon block body Merge has empty fields")
}

func bytes() []byte {
b := make([]byte, 32)
_, err := rand.Read(b)
Expand Down Expand Up @@ -574,6 +604,38 @@ func genSignedBeaconBlockAltair() *v1alpha1.SignedBeaconBlockAltair {
}
}

func genBeaconBlockBodyMerge() *v1alpha1.BeaconBlockBodyMerge {
return &v1alpha1.BeaconBlockBodyMerge{
RandaoReveal: bytes(),
Eth1Data: genEth1Data(),
Graffiti: bytes(),
ProposerSlashings: genProposerSlashings(5),
AttesterSlashings: genAttesterSlashings(5),
Attestations: genAttestations(10),
Deposits: genDeposits(5),
VoluntaryExits: genSignedVoluntaryExits(12),
SyncAggregate: genSyncAggregate(),
ExecutionPayload: genPayload(),
}
}

func genBeaconBlockMerge() *v1alpha1.BeaconBlockMerge {
return &v1alpha1.BeaconBlockMerge{
Slot: 123455,
ProposerIndex: 55433,
ParentRoot: bytes(),
StateRoot: bytes(),
Body: genBeaconBlockBodyMerge(),
}
}

func genSignedBeaconBlockMerge() *v1alpha1.SignedBeaconBlockMerge {
return &v1alpha1.SignedBeaconBlockMerge{
Block: genBeaconBlockMerge(),
Signature: bytes(),
}
}

func genSyncCommitteeMessage() *v1alpha1.SyncCommitteeMessage {
return &v1alpha1.SyncCommitteeMessage{
Slot: 424555,
Expand All @@ -583,6 +645,25 @@ func genSyncCommitteeMessage() *v1alpha1.SyncCommitteeMessage {
}
}

func genPayload() *v1alpha1.ExecutionPayload {
return &v1alpha1.ExecutionPayload{
ParentHash: bytes(),
FeeRecipient: bytes(),
StateRoot: bytes(),
ReceiptRoot: bytes(),
LogsBloom: bytes(),
Random: bytes(),
BlockNumber: 1,
GasLimit: 2,
GasUsed: 3,
Timestamp: 4,
ExtraData: bytes(),
BaseFeePerGas: bytes(),
BlockHash: bytes(),
Transactions: [][]byte{{'a'}, {'b'}, {'c'}},
}
}

func genPayloadHeader() *v1alpha1.ExecutionPayloadHeader {
return &v1alpha1.ExecutionPayloadHeader{
ParentHash: bytes(),
Expand Down
Loading