Skip to content

Commit

Permalink
Add block setters for consensus type (#11751)
Browse files Browse the repository at this point in the history
* Add block getters

* Rm changes

* Rm changes

* Revert "Rm changes"

This reverts commit 1ae5db7.

* Fix tests

* Set graffiti right place

* Potuz feedback

* Update consensus-types/blocks/setters.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Radek feedback

* Fix comments

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
  • Loading branch information
terencechain and rkapka authored Dec 13, 2022
1 parent 0798092 commit 3fcdd58
Show file tree
Hide file tree
Showing 11 changed files with 527 additions and 36 deletions.
1 change: 1 addition & 0 deletions consensus-types/blocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"factory.go",
"getters.go",
"proto.go",
"setters.go",
"types.go",
],
importpath = "github.com/prysmaticlabs/prysm/v3/consensus-types/blocks",
Expand Down
42 changes: 41 additions & 1 deletion consensus-types/blocks/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ func (e executionPayload) WithdrawalsRoot() ([]byte, error) {
return nil, ErrUnsupportedGetter
}

// PbBellatrix --
func (e executionPayload) PbBellatrix() (*enginev1.ExecutionPayload, error) {
return e.p, nil
}

// PbCapella --
func (executionPayload) PbCapella() (*enginev1.ExecutionPayloadCapella, error) {
return nil, ErrUnsupportedGetter
}

// executionPayloadHeader is a convenience wrapper around a blinded beacon block body's execution header data structure
// This wrapper allows us to conform to a common interface so that beacon
// blocks for future forks can also be applied across Prysm without issues.
Expand Down Expand Up @@ -296,6 +306,16 @@ func (e executionPayloadHeader) WithdrawalsRoot() ([]byte, error) {
return nil, ErrUnsupportedGetter
}

// PbCapella --
func (executionPayloadHeader) PbCapella() (*enginev1.ExecutionPayloadCapella, error) {
return nil, ErrUnsupportedGetter
}

// PbBellatrix --
func (executionPayloadHeader) PbBellatrix() (*enginev1.ExecutionPayload, error) {
return nil, ErrUnsupportedGetter
}

// PayloadToHeader converts `payload` into execution payload header format.
func PayloadToHeader(payload interfaces.ExecutionData) (*enginev1.ExecutionPayloadHeader, error) {
txs, err := payload.Transactions()
Expand Down Expand Up @@ -465,6 +485,16 @@ func (e executionPayloadCapella) WithdrawalsRoot() ([]byte, error) {
return nil, ErrUnsupportedGetter
}

// PbCapella --
func (e executionPayloadCapella) PbCapella() (*enginev1.ExecutionPayloadCapella, error) {
return e.p, nil
}

// PbBellatrix --
func (executionPayloadCapella) PbBellatrix() (*enginev1.ExecutionPayload, error) {
return nil, ErrUnsupportedGetter
}

// executionPayloadHeaderCapella is a convenience wrapper around a blinded beacon block body's execution header data structure
// This wrapper allows us to conform to a common interface so that beacon
// blocks for future forks can also be applied across Prysm without issues.
Expand Down Expand Up @@ -601,11 +631,21 @@ func (e executionPayloadHeaderCapella) Withdrawals() ([]*enginev1.Withdrawal, er
return nil, ErrUnsupportedGetter
}

// WitdrawalsRoot --
// WithdrawalsRoot --
func (e executionPayloadHeaderCapella) WithdrawalsRoot() ([]byte, error) {
return e.p.WithdrawalsRoot, nil
}

// PbCapella --
func (executionPayloadHeaderCapella) PbCapella() (*enginev1.ExecutionPayloadCapella, error) {
return nil, ErrUnsupportedGetter
}

// PbBellatrix --
func (executionPayloadHeaderCapella) PbBellatrix() (*enginev1.ExecutionPayload, error) {
return nil, ErrUnsupportedGetter
}

// PayloadToHeaderCapella converts `payload` into execution payload header format.
func PayloadToHeaderCapella(payload interfaces.ExecutionData) (*enginev1.ExecutionPayloadHeaderCapella, error) {
txs, err := payload.Transactions()
Expand Down
38 changes: 38 additions & 0 deletions consensus-types/blocks/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,44 @@ func TestWrapExecutionPayloadHeaderCapella_SSZ(t *testing.T) {
assert.NoError(t, payload.UnmarshalSSZ(encoded))
}

func Test_executionPayload_Pb(t *testing.T) {
payload := createWrappedPayload(t)
pb, err := payload.PbBellatrix()
require.NoError(t, err)
assert.DeepEqual(t, payload.Proto(), pb)

_, err = payload.PbCapella()
require.ErrorIs(t, err, blocks.ErrUnsupportedGetter)
}

func Test_executionPayloadHeader_Pb(t *testing.T) {
payload := createWrappedPayloadHeader(t)
_, err := payload.PbBellatrix()
require.ErrorIs(t, err, blocks.ErrUnsupportedGetter)

_, err = payload.PbCapella()
require.ErrorIs(t, err, blocks.ErrUnsupportedGetter)
}

func Test_executionPayloadCapella_Pb(t *testing.T) {
payload := createWrappedPayloadCapella(t)
pb, err := payload.PbCapella()
require.NoError(t, err)
assert.DeepEqual(t, payload.Proto(), pb)

_, err = payload.PbBellatrix()
require.ErrorIs(t, err, blocks.ErrUnsupportedGetter)
}

func Test_executionPayloadHeaderCapella_Pb(t *testing.T) {
payload := createWrappedPayloadHeaderCapella(t)
_, err := payload.PbBellatrix()
require.ErrorIs(t, err, blocks.ErrUnsupportedGetter)

_, err = payload.PbCapella()
require.ErrorIs(t, err, blocks.ErrUnsupportedGetter)
}

func createWrappedPayload(t testing.TB) interfaces.ExecutionData {
wsb, err := blocks.WrappedExecutionPayload(&enginev1.ExecutionPayload{
ParentHash: make([]byte, fieldparams.RootLength),
Expand Down
12 changes: 6 additions & 6 deletions consensus-types/blocks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var (
// ErrNilObject is returned in a constructor when the underlying object is nil.
ErrNilObject = errors.New("received nil object")
// ErrNilSignedBeaconBlock is returned when a nil signed beacon block is received.
ErrNilSignedBeaconBlock = errors.New("signed beacon block can't be nil")
ErrNilSignedBeaconBlock = errors.New("signed beacon block can't be nil")
errNonBlindedSignedBeaconBlock = errors.New("can only build signed beacon block from blinded format")
)

// NewSignedBeaconBlock creates a signed beacon block from a protobuf signed beacon block.
Expand Down Expand Up @@ -177,14 +178,13 @@ func BuildSignedBeaconBlockFromExecutionPayload(
if err := BeaconBlockIsNil(blk); err != nil {
return nil, err
}
if !blk.IsBlinded() {
return nil, errNonBlindedSignedBeaconBlock
}
b := blk.Block()
payloadHeader, err := b.Body().Execution()
switch {
case errors.Is(err, ErrUnsupportedGetter):
return nil, errors.Wrap(err, "can only build signed beacon block from blinded format")
case err != nil:
if err != nil {
return nil, errors.Wrap(err, "could not get execution payload header")
default:
}

var wrappedPayload interfaces.ExecutionData
Expand Down
4 changes: 2 additions & 2 deletions consensus-types/blocks/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,14 @@ func TestBuildSignedBeaconBlockFromExecutionPayload(t *testing.T) {
_, err := BuildSignedBeaconBlockFromExecutionPayload(nil, nil)
require.ErrorIs(t, ErrNilSignedBeaconBlock, err)
})
t.Run("unsupported field payload header", func(t *testing.T) {
t.Run("not blinded payload", func(t *testing.T) {
altairBlock := &eth.SignedBeaconBlockAltair{
Block: &eth.BeaconBlockAltair{
Body: &eth.BeaconBlockBodyAltair{}}}
blk, err := NewSignedBeaconBlock(altairBlock)
require.NoError(t, err)
_, err = BuildSignedBeaconBlockFromExecutionPayload(blk, nil)
require.Equal(t, true, errors.Is(err, ErrUnsupportedGetter))
require.Equal(t, true, errors.Is(err, errNonBlindedSignedBeaconBlock))
})
t.Run("payload header root and payload root mismatch", func(t *testing.T) {
blockHash := bytesutil.Bytes32(1)
Expand Down
35 changes: 35 additions & 0 deletions consensus-types/blocks/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,41 @@ func (b *BeaconBlock) AsSignRequestObject() (validatorpb.SignRequestObject, erro
}
}

func (b *BeaconBlock) Copy() (interfaces.BeaconBlock, error) {
if b == nil {
return nil, nil
}

pb, err := b.Proto()
if err != nil {
return nil, err
}
switch b.version {
case version.Phase0:
cp := eth.CopyBeaconBlock(pb.(*eth.BeaconBlock))
return initBlockFromProtoPhase0(cp)
case version.Altair:
cp := eth.CopyBeaconBlockAltair(pb.(*eth.BeaconBlockAltair))
return initBlockFromProtoAltair(cp)
case version.Bellatrix:
if b.IsBlinded() {
cp := eth.CopyBlindedBeaconBlockBellatrix(pb.(*eth.BlindedBeaconBlockBellatrix))
return initBlindedBlockFromProtoBellatrix(cp)
}
cp := eth.CopyBeaconBlockBellatrix(pb.(*eth.BeaconBlockBellatrix))
return initBlockFromProtoBellatrix(cp)
case version.Capella:
if b.IsBlinded() {
cp := eth.CopyBlindedBeaconBlockCapella(pb.(*eth.BlindedBeaconBlockCapella))
return initBlindedBlockFromProtoCapella(cp)
}
cp := eth.CopyBeaconBlockCapella(pb.(*eth.BeaconBlockCapella))
return initBlockFromProtoCapella(cp)
default:
return nil, errIncorrectBlockVersion
}
}

// IsNil checks if the block body is nil.
func (b *BeaconBlockBody) IsNil() bool {
return b == nil
Expand Down
Loading

0 comments on commit 3fcdd58

Please sign in to comment.