Skip to content

Commit

Permalink
Add ePBS stuff to consensus-types: block
Browse files Browse the repository at this point in the history
  • Loading branch information
potuz committed Jul 31, 2024
1 parent 0b0989c commit f41fc18
Show file tree
Hide file tree
Showing 27 changed files with 1,268 additions and 396 deletions.
3 changes: 3 additions & 0 deletions consensus-types/blocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,14 @@ go_test(
srcs = [
"execution_test.go",
"factory_test.go",
"getters_epbs_test.go",
"getters_test.go",
"kzg_test.go",
"proto_epbs_test.go",
"proto_test.go",
"roblob_test.go",
"roblock_test.go",
"setters_test.go",
],
embed = [":go_default_library"],
deps = [
Expand Down
19 changes: 19 additions & 0 deletions consensus-types/blocks/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func NewSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) {
return initBlindedSignedBlockFromProtoElectra(b)
case *eth.GenericSignedBeaconBlock_BlindedElectra:
return initBlindedSignedBlockFromProtoElectra(b.BlindedElectra)
case *eth.GenericSignedBeaconBlock_Epbs:
return initSignedBlockFromProtoEPBS(b.Epbs)
case *eth.SignedBeaconBlockEpbs:
return initSignedBlockFromProtoEPBS(b)
default:
return nil, errors.Wrapf(ErrUnsupportedSignedBeaconBlock, "unable to create block from type %T", i)
}
Expand Down Expand Up @@ -124,6 +128,10 @@ func NewBeaconBlock(i interface{}) (interfaces.ReadOnlyBeaconBlock, error) {
return initBlindedBlockFromProtoElectra(b)
case *eth.GenericBeaconBlock_BlindedElectra:
return initBlindedBlockFromProtoElectra(b.BlindedElectra)
case *eth.GenericBeaconBlock_Epbs:
return initBlockFromProtoEpbs(b.Epbs)
case *eth.BeaconBlockEpbs:
return initBlockFromProtoEpbs(b)
default:
return nil, errors.Wrapf(errUnsupportedBeaconBlock, "unable to create block from type %T", i)
}
Expand Down Expand Up @@ -154,6 +162,8 @@ func NewBeaconBlockBody(i interface{}) (interfaces.ReadOnlyBeaconBlockBody, erro
return initBlockBodyFromProtoElectra(b)
case *eth.BlindedBeaconBlockBodyElectra:
return initBlindedBlockBodyFromProtoElectra(b)
case *eth.BeaconBlockBodyEpbs:
return initBlockBodyFromProtoEpbs(b)
default:
return nil, errors.Wrapf(errUnsupportedBeaconBlockBody, "unable to create block body from type %T", i)
}
Expand Down Expand Up @@ -233,6 +243,12 @@ func BuildSignedBeaconBlock(blk interfaces.ReadOnlyBeaconBlock, signature []byte
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(&eth.SignedBeaconBlockElectra{Block: pb, Signature: signature})
case version.EPBS:
pb, ok := pb.(*eth.BeaconBlockEpbs)
if !ok {
return nil, errIncorrectBlockVersion
}
return NewSignedBeaconBlock(&eth.SignedBeaconBlockEpbs{Block: pb, Signature: signature})
default:
return nil, errUnsupportedBeaconBlock
}
Expand All @@ -247,6 +263,9 @@ func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBea
if !blk.IsBlinded() {
return nil, errNonBlindedSignedBeaconBlock
}
if blk.Version() >= version.EPBS {
return nil, errors.Wrap(errUnsupportedBeaconBlock, "post epbs blocks no longer need to be unblind")
}
b := blk.Block()
payloadHeader, err := b.Body().Execution()
if err != nil {
Expand Down
47 changes: 47 additions & 0 deletions consensus-types/blocks/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ func Test_NewSignedBeaconBlock(t *testing.T) {
assert.Equal(t, version.Deneb, b.Version())
assert.Equal(t, true, b.IsBlinded())
})
t.Run("GenericSignedBeaconBlock_Epbs", func(t *testing.T) {
pb := &eth.GenericSignedBeaconBlock_Epbs{
Epbs: &eth.SignedBeaconBlockEpbs{
Block: &eth.BeaconBlockEpbs{
Body: &eth.BeaconBlockBodyEpbs{},
},
},
}
b, err := NewSignedBeaconBlock(pb)
require.NoError(t, err)
assert.Equal(t, version.EPBS, b.Version())
})
t.Run("SignedBeaconBlockEpbs", func(t *testing.T) {
pb := &eth.SignedBeaconBlockEpbs{
Block: &eth.BeaconBlockEpbs{
Body: &eth.BeaconBlockBodyEpbs{}}}
b, err := NewSignedBeaconBlock(pb)
require.NoError(t, err)
assert.Equal(t, version.EPBS, b.Version())
})
t.Run("nil", func(t *testing.T) {
_, err := NewSignedBeaconBlock(nil)
assert.ErrorContains(t, "received nil object", err)
Expand Down Expand Up @@ -276,6 +296,18 @@ func Test_NewBeaconBlock(t *testing.T) {
assert.Equal(t, version.Deneb, b.Version())
assert.Equal(t, true, b.IsBlinded())
})
t.Run("GenericBeaconBlock_Epbs", func(t *testing.T) {
pb := &eth.GenericBeaconBlock_Epbs{Epbs: &eth.BeaconBlockEpbs{Body: &eth.BeaconBlockBodyEpbs{}}}
b, err := NewBeaconBlock(pb)
require.NoError(t, err)
assert.Equal(t, version.EPBS, b.Version())
})
t.Run("BeaconBlockEpbs", func(t *testing.T) {
pb := &eth.BeaconBlockEpbs{Body: &eth.BeaconBlockBodyEpbs{}}
b, err := NewBeaconBlock(pb)
require.NoError(t, err)
assert.Equal(t, version.EPBS, b.Version())
})
t.Run("nil", func(t *testing.T) {
_, err := NewBeaconBlock(nil)
assert.ErrorContains(t, "received nil object", err)
Expand Down Expand Up @@ -354,6 +386,14 @@ func Test_NewBeaconBlockBody(t *testing.T) {
assert.Equal(t, version.Deneb, b.version)
assert.Equal(t, true, b.IsBlinded())
})
t.Run("BeaconBlockBodyEpbs", func(t *testing.T) {
pb := &eth.BeaconBlockBodyEpbs{}
i, err := NewBeaconBlockBody(pb)
require.NoError(t, err)
b, ok := i.(*BeaconBlockBody)
require.Equal(t, true, ok)
assert.Equal(t, version.EPBS, b.version)
})
t.Run("nil", func(t *testing.T) {
_, err := NewBeaconBlockBody(nil)
assert.ErrorContains(t, "received nil object", err)
Expand Down Expand Up @@ -425,6 +465,13 @@ func Test_BuildSignedBeaconBlock(t *testing.T) {
assert.Equal(t, version.Deneb, sb.Version())
assert.Equal(t, true, sb.IsBlinded())
})
t.Run("Epbs", func(t *testing.T) {
b := &BeaconBlock{version: version.EPBS, body: &BeaconBlockBody{version: version.EPBS}}
sb, err := BuildSignedBeaconBlock(b, sig[:])
require.NoError(t, err)
assert.DeepEqual(t, sig, sb.Signature())
assert.Equal(t, version.EPBS, sb.Version())
})
}

func TestBuildSignedBeaconBlockFromExecutionPayload(t *testing.T) {
Expand Down
82 changes: 75 additions & 7 deletions consensus-types/blocks/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import (
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)

var (
// ErrAlreadyUnblinded is returned when trying to unblind a full block.
ErrAlreadyUnblinded = errors.New("cannot unblind if a full block")
)

// BeaconBlockIsNil checks if any composite field of input signed beacon block is nil.
// Access to these nil fields will result in run time panic,
// it is recommended to run these checks as first line of defense.
Expand Down Expand Up @@ -75,6 +80,9 @@ func (b *SignedBeaconBlock) Copy() (interfaces.SignedBeaconBlock, error) {
return initBlindedSignedBlockFromProtoElectra(pb.(*eth.SignedBlindedBeaconBlockElectra).Copy())
}
return initSignedBlockFromProtoElectra(pb.(*eth.SignedBeaconBlockElectra).Copy())
case version.EPBS:
cp := eth.CopySignedBeaconBlockEPBS(pb.(*eth.SignedBeaconBlockEpbs))
return initSignedBlockFromProtoEPBS(cp)
default:
return nil, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -131,14 +139,18 @@ func (b *SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, err
return &eth.GenericSignedBeaconBlock{
Block: &eth.GenericSignedBeaconBlock_Electra{Electra: pb.(*eth.SignedBeaconBlockContentsElectra)},
}, nil
case version.EPBS:
return &eth.GenericSignedBeaconBlock{
Block: &eth.GenericSignedBeaconBlock_Epbs{Epbs: pb.(*eth.SignedBeaconBlockEpbs)},
}, nil
default:
return nil, errIncorrectBlockVersion
}
}

// ToBlinded converts a non-blinded block to its blinded equivalent.
func (b *SignedBeaconBlock) ToBlinded() (interfaces.ReadOnlySignedBeaconBlock, error) {
if b.version < version.Bellatrix {
if b.version < version.Bellatrix || b.version >= version.EPBS {
return nil, ErrUnsupportedVersion
}
if b.IsBlinded() {
Expand Down Expand Up @@ -276,11 +288,11 @@ func (b *SignedBeaconBlock) ToBlinded() (interfaces.ReadOnlySignedBeaconBlock, e
}

func (b *SignedBeaconBlock) Unblind(e interfaces.ExecutionData) error {
if e.IsNil() {
if e == nil || e.IsNil() {
return errors.New("cannot unblind with nil execution data")
}
if !b.IsBlinded() {
return errors.New("cannot unblind if the block is already unblinded")
return ErrAlreadyUnblinded
}
payloadRoot, err := e.HashTreeRoot()
if err != nil {
Expand Down Expand Up @@ -310,7 +322,8 @@ func (b *SignedBeaconBlock) Version() int {

// IsBlinded metadata on whether a block is blinded
func (b *SignedBeaconBlock) IsBlinded() bool {
return b.version >= version.Bellatrix && b.block.body.executionPayload == nil
preEPBS := b.version < version.EPBS
return preEPBS && b.version >= version.Bellatrix && b.block.body.executionPayload == nil
}

// Header converts the underlying protobuf object from blinded block to header format.
Expand Down Expand Up @@ -366,6 +379,8 @@ func (b *SignedBeaconBlock) MarshalSSZ() ([]byte, error) {
return pb.(*eth.SignedBlindedBeaconBlockElectra).MarshalSSZ()
}
return pb.(*eth.SignedBeaconBlockElectra).MarshalSSZ()
case version.EPBS:
return pb.(*eth.SignedBeaconBlockEpbs).MarshalSSZ()
default:
return []byte{}, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -403,6 +418,8 @@ func (b *SignedBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) {
return pb.(*eth.SignedBlindedBeaconBlockElectra).MarshalSSZTo(dst)
}
return pb.(*eth.SignedBeaconBlockElectra).MarshalSSZTo(dst)
case version.EPBS:
return pb.(*eth.SignedBeaconBlockEpbs).MarshalSSZTo(dst)
default:
return []byte{}, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -444,6 +461,8 @@ func (b *SignedBeaconBlock) SizeSSZ() int {
return pb.(*eth.SignedBlindedBeaconBlockElectra).SizeSSZ()
}
return pb.(*eth.SignedBeaconBlockElectra).SizeSSZ()
case version.EPBS:
return pb.(*eth.SignedBeaconBlockEpbs).SizeSSZ()
default:
panic(incorrectBlockVersion)
}
Expand Down Expand Up @@ -561,6 +580,16 @@ func (b *SignedBeaconBlock) UnmarshalSSZ(buf []byte) error {
return err
}
}
case version.EPBS:
pb := &eth.SignedBeaconBlockEpbs{}
if err := pb.UnmarshalSSZ(buf); err != nil {
return err
}
var err error
newBlock, err = initSignedBlockFromProtoEPBS(pb)
if err != nil {
return err
}
default:
return errIncorrectBlockVersion
}
Expand Down Expand Up @@ -600,7 +629,8 @@ func (b *BeaconBlock) IsNil() bool {

// IsBlinded checks if the beacon block is a blinded block.
func (b *BeaconBlock) IsBlinded() bool {
return b.version >= version.Bellatrix && b.body.executionPayload == nil
preEPBS := b.version < version.EPBS
return preEPBS && b.version >= version.Bellatrix && b.body.executionPayload == nil
}

// Version of the underlying protobuf object.
Expand Down Expand Up @@ -639,6 +669,8 @@ func (b *BeaconBlock) HashTreeRoot() ([field_params.RootLength]byte, error) {
return pb.(*eth.BlindedBeaconBlockElectra).HashTreeRoot()
}
return pb.(*eth.BeaconBlockElectra).HashTreeRoot()
case version.EPBS:
return pb.(*eth.BeaconBlockEpbs).HashTreeRoot()
default:
return [field_params.RootLength]byte{}, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -675,6 +707,8 @@ func (b *BeaconBlock) HashTreeRootWith(h *ssz.Hasher) error {
return pb.(*eth.BlindedBeaconBlockElectra).HashTreeRootWith(h)
}
return pb.(*eth.BeaconBlockElectra).HashTreeRootWith(h)
case version.EPBS:
return pb.(*eth.BeaconBlockEpbs).HashTreeRootWith(h)
default:
return errIncorrectBlockVersion
}
Expand Down Expand Up @@ -712,6 +746,8 @@ func (b *BeaconBlock) MarshalSSZ() ([]byte, error) {
return pb.(*eth.BlindedBeaconBlockElectra).MarshalSSZ()
}
return pb.(*eth.BeaconBlockElectra).MarshalSSZ()
case version.EPBS:
return pb.(*eth.BeaconBlockEpbs).MarshalSSZ()
default:
return []byte{}, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -749,6 +785,8 @@ func (b *BeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) {
return pb.(*eth.BlindedBeaconBlockElectra).MarshalSSZTo(dst)
}
return pb.(*eth.BeaconBlockElectra).MarshalSSZTo(dst)
case version.EPBS:
return pb.(*eth.BeaconBlockEpbs).MarshalSSZTo(dst)
default:
return []byte{}, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -790,6 +828,8 @@ func (b *BeaconBlock) SizeSSZ() int {
return pb.(*eth.BlindedBeaconBlockElectra).SizeSSZ()
}
return pb.(*eth.BeaconBlockElectra).SizeSSZ()
case version.EPBS:
return pb.(*eth.BeaconBlockEpbs).SizeSSZ()
default:
panic(incorrectBodyVersion)
}
Expand Down Expand Up @@ -907,6 +947,16 @@ func (b *BeaconBlock) UnmarshalSSZ(buf []byte) error {
return err
}
}
case version.EPBS:
pb := &eth.BeaconBlockEpbs{}
if err := pb.UnmarshalSSZ(buf); err != nil {
return err
}
var err error
newBlock, err = initBlockFromProtoEpbs(pb)
if err != nil {
return err
}
default:
return errIncorrectBlockVersion
}
Expand Down Expand Up @@ -945,6 +995,8 @@ func (b *BeaconBlock) AsSignRequestObject() (validatorpb.SignRequestObject, erro
return &validatorpb.SignRequest_BlindedBlockElectra{BlindedBlockElectra: pb.(*eth.BlindedBeaconBlockElectra)}, nil
}
return &validatorpb.SignRequest_BlockElectra{BlockElectra: pb.(*eth.BeaconBlockElectra)}, nil
case version.EPBS:
return &validatorpb.SignRequest_BlockEpbs{BlockEpbs: pb.(*eth.BeaconBlockEpbs)}, nil
default:
return nil, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -984,6 +1036,9 @@ func (b *BeaconBlock) Copy() (interfaces.ReadOnlyBeaconBlock, error) {
return initBlindedBlockFromProtoElectra(pb.(*eth.BlindedBeaconBlockElectra).Copy())
}
return initBlockFromProtoElectra(pb.(*eth.BeaconBlockElectra).Copy())
case version.EPBS:
cp := eth.CopyBeaconBlockEPBS(pb.(*eth.BeaconBlockEpbs))
return initBlockFromProtoEpbs(cp)
default:
return nil, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -1081,7 +1136,7 @@ func (b *BeaconBlockBody) SyncAggregate() (*eth.SyncAggregate, error) {
// Execution returns the execution payload of the block body.
func (b *BeaconBlockBody) Execution() (interfaces.ExecutionData, error) {
switch b.version {
case version.Phase0, version.Altair:
case version.Phase0, version.Altair, version.EPBS:
return nil, consensus_types.ErrNotSupported("Execution", b.version)
default:
if b.IsBlinded() {
Expand Down Expand Up @@ -1110,6 +1165,16 @@ func (b *BeaconBlockBody) BlobKzgCommitments() ([][]byte, error) {
}
}

// PayloadAttestations returns the payload attestations in the block.
func (b *BeaconBlockBody) PayloadAttestations() []*eth.PayloadAttestation {
return b.payloadAttestations
}

// SignedExecutionPayloadHeader returns the signed execution payload header in the block.
func (b *BeaconBlockBody) SignedExecutionPayloadHeader() *enginev1.SignedExecutionPayloadHeader {
return b.signedExecutionPayloadHeader
}

// Version returns the version of the beacon block body
func (b *BeaconBlockBody) Version() int {
return b.version
Expand Down Expand Up @@ -1146,12 +1211,15 @@ func (b *BeaconBlockBody) HashTreeRoot() ([field_params.RootLength]byte, error)
return pb.(*eth.BlindedBeaconBlockBodyElectra).HashTreeRoot()
}
return pb.(*eth.BeaconBlockBodyElectra).HashTreeRoot()
case version.EPBS:
return pb.(*eth.BeaconBlockBodyEpbs).HashTreeRoot()
default:
return [field_params.RootLength]byte{}, errIncorrectBodyVersion
}
}

// IsBlinded checks if the beacon block body is a blinded block body.
func (b *BeaconBlockBody) IsBlinded() bool {
return b.version >= version.Bellatrix && b.executionPayload == nil
preEPBS := b.version < version.EPBS
return preEPBS && b.version >= version.Bellatrix && b.executionPayload == nil
}
Loading

0 comments on commit f41fc18

Please sign in to comment.