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 May 8, 2024
1 parent 26fe38b commit 39930a2
Show file tree
Hide file tree
Showing 28 changed files with 1,242 additions and 398 deletions.
3 changes: 3 additions & 0 deletions consensus-types/blocks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,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 @@ -75,6 +75,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 @@ -125,6 +129,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 @@ -155,6 +163,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 @@ -234,6 +244,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 @@ -248,6 +264,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 @@ -163,6 +163,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 @@ -278,6 +298,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 @@ -356,6 +388,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 @@ -427,6 +467,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
83 changes: 75 additions & 8 deletions consensus-types/blocks/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
log "github.com/sirupsen/logrus"
)

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 @@ -88,7 +93,9 @@ func (b *SignedBeaconBlock) Copy() (interfaces.SignedBeaconBlock, error) {
}
cp := eth.CopySignedBeaconBlockElectra(pb.(*eth.SignedBeaconBlockElectra))
return initSignedBlockFromProtoElectra(cp)

case version.EPBS:
cp := eth.CopySignedBeaconBlockEPBS(pb.(*eth.SignedBeaconBlockEpbs))
return initSignedBlockFromProtoEPBS(cp)
default:
return nil, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -145,14 +152,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 @@ -291,11 +302,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 @@ -325,7 +336,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
}

// ValueInWei metadata on the payload value returned by the builder.
Expand Down Expand Up @@ -419,6 +431,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 @@ -456,6 +470,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 @@ -497,6 +513,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 @@ -614,6 +632,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 @@ -653,7 +681,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 @@ -692,6 +721,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 @@ -728,6 +759,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 @@ -765,6 +798,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 @@ -802,6 +837,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 @@ -843,6 +880,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 @@ -960,6 +999,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 @@ -998,6 +1047,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 @@ -1047,6 +1098,9 @@ func (b *BeaconBlock) Copy() (interfaces.ReadOnlyBeaconBlock, error) {
}
cp := eth.CopyBeaconBlockElectra(pb.(*eth.BeaconBlockElectra))
return initBlockFromProtoElectra(cp)
case version.EPBS:
cp := eth.CopyBeaconBlockEPBS(pb.(*eth.BeaconBlockEpbs))
return initBlockFromProtoEpbs(cp)
default:
return nil, errIncorrectBlockVersion
}
Expand Down Expand Up @@ -1144,7 +1198,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 @@ -1177,6 +1231,16 @@ func (b *BeaconBlockBody) Consolidations() []*eth.SignedConsolidation {
return b.signedConsolidations
}

// 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 @@ -1213,12 +1277,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 39930a2

Please sign in to comment.