Compare commits

..

5 Commits

Author SHA1 Message Date
james-prysm
9e448ab3ba Merge branch 'develop' into simplify-parse-beacon-block 2026-02-04 14:10:00 -06:00
james-prysm
17f8e67646 addressing satyajit comment and adding more tests 2026-02-04 13:23:50 -06:00
james-prysm
b64c7d57a1 Merge branch 'develop' into simplify-parse-beacon-block 2026-02-03 13:49:05 -08:00
james-prysm
e5e0bf7426 changelog 2026-02-03 15:47:37 -06:00
james-prysm
7bcbc1f8f4 refactor 2026-01-30 15:35:27 -06:00
12 changed files with 640 additions and 577 deletions

View File

@@ -1,3 +0,0 @@
### Fixed
- Fixed a bug where `cmd/beacon-chain/execution` was being ignored by `hack/gen-logs.sh` due to a `.gitignore` rule.

View File

@@ -0,0 +1,3 @@
### Ignored
- improving maintainability and deduplication on get and post block parsing.

View File

@@ -1,9 +1,5 @@
// Code generated by hack/gen-logs.sh; DO NOT EDIT.
// This file is created and regenerated automatically. Anything added here might get removed.
package execution
import "github.com/sirupsen/logrus"
// The prefix for logs from this package will be the text after the last slash in the package path.
// If you wish to change this, you should add your desired name in the runtime/logging/logrus-prefixed-formatter/prefix-replacement.go file.
var log = logrus.WithField("package", "cmd/beacon-chain/execution")
var log = logrus.WithField("prefix", "execution")

View File

@@ -31,11 +31,6 @@ EXCLUDED_PATH_PREFIXES=(
".vscode"
)
# Gitignore overrides: paths that should still be scanned even if ignored by VCS.
GITIGNORE_OVERRIDES=(
"cmd/beacon-chain/execution"
)
# The logrus import path
LOGRUS_IMPORT="github.com/sirupsen/logrus"
# ----------------------------
@@ -75,14 +70,6 @@ rg_args=(
-0 # NUL-delimited output
)
if [[ ${#GITIGNORE_OVERRIDES[@]} -gt 0 ]]; then
# Disable VCS ignores so overrides are honored.
rg_args+=( --no-ignore-vcs )
for ov in "${GITIGNORE_OVERRIDES[@]}"; do
rg_args+=( --glob "$ov/**" )
done
fi
for ex in "${EXCLUDED_PATH_PREFIXES[@]}"; do
rg_args+=( --glob "!$ex/**" )
done

View File

@@ -52,6 +52,7 @@ go_library(
"//consensus-types/primitives:go_default_library",
"//consensus-types/validator:go_default_library",
"//encoding/bytesutil:go_default_library",
"//encoding/ssz:go_default_library",
"//monitoring/tracing/trace:go_default_library",
"//network/httputil:go_default_library",
"//proto/engine/v1:go_default_library",

View File

@@ -66,12 +66,13 @@ func (c *beaconApiValidatorClient) duties(ctx context.Context, in *ethpb.DutiesR
}()
nextEpochDuties := &ethpb.ValidatorDutiesContainer{}
if err := c.dutiesForEpoch(ctx, nextEpochDuties, in.Epoch+1, vals, fetchSyncDuties); err != nil {
return nil, errors.Wrapf(err, "failed to get duties for next epoch `%d`", in.Epoch+1)
}
nextEpochErr := c.dutiesForEpoch(ctx, nextEpochDuties, in.Epoch+1, vals, fetchSyncDuties)
if err = <-errCh; err != nil {
return nil, err
if currEpochErr := <-errCh; currEpochErr != nil {
return nil, currEpochErr
}
if nextEpochErr != nil {
return nil, errors.Wrapf(nextEpochErr, "failed to get duties for next epoch `%d`", in.Epoch+1)
}
return &ethpb.ValidatorDutiesContainer{

View File

@@ -55,114 +55,153 @@ func (c *beaconApiValidatorClient) beaconBlock(ctx context.Context, slot primiti
}
}
// sszBlockCodec defines SSZ unmarshalers for a fork's block and blinded block types.
type sszBlockCodec struct {
unmarshalBlock func([]byte) (*ethpb.GenericBeaconBlock, error)
unmarshalBlinded func([]byte) (*ethpb.GenericBeaconBlock, error) // nil for Phase0/Altair
}
type sszCodecEntry struct {
minVersion int
codec sszBlockCodec
}
// sszCodecs is ordered descending by version so that unknown future versions
// fall through to the latest known fork (matching the original if-cascade).
var sszCodecs = []sszCodecEntry{
{
minVersion: version.Fulu,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlockContentsFulu{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Fulu{Fulu: block}}, nil
},
unmarshalBlinded: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
blindedBlock := &ethpb.BlindedBeaconBlockFulu{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedFulu{BlindedFulu: blindedBlock}, IsBlinded: true}, nil
},
},
},
{
minVersion: version.Electra,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlockContentsElectra{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Electra{Electra: block}}, nil
},
unmarshalBlinded: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
blindedBlock := &ethpb.BlindedBeaconBlockElectra{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedElectra{BlindedElectra: blindedBlock}, IsBlinded: true}, nil
},
},
},
{
minVersion: version.Deneb,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlockContentsDeneb{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Deneb{Deneb: block}}, nil
},
unmarshalBlinded: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
blindedBlock := &ethpb.BlindedBeaconBlockDeneb{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedDeneb{BlindedDeneb: blindedBlock}, IsBlinded: true}, nil
},
},
},
{
minVersion: version.Capella,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlockCapella{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Capella{Capella: block}}, nil
},
unmarshalBlinded: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
blindedBlock := &ethpb.BlindedBeaconBlockCapella{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedCapella{BlindedCapella: blindedBlock}, IsBlinded: true}, nil
},
},
},
{
minVersion: version.Bellatrix,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlockBellatrix{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Bellatrix{Bellatrix: block}}, nil
},
unmarshalBlinded: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
blindedBlock := &ethpb.BlindedBeaconBlockBellatrix{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedBellatrix{BlindedBellatrix: blindedBlock}, IsBlinded: true}, nil
},
},
},
{
minVersion: version.Altair,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlockAltair{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Altair{Altair: block}}, nil
},
},
},
{
minVersion: version.Phase0,
codec: sszBlockCodec{
unmarshalBlock: func(data []byte) (*ethpb.GenericBeaconBlock, error) {
block := &ethpb.BeaconBlock{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Phase0{Phase0: block}}, nil
},
},
},
}
func processBlockSSZResponse(ver int, data []byte, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if ver >= version.Fulu {
return processBlockSSZResponseFulu(data, isBlinded)
}
if ver >= version.Electra {
return processBlockSSZResponseElectra(data, isBlinded)
}
if ver >= version.Deneb {
return processBlockSSZResponseDeneb(data, isBlinded)
}
if ver >= version.Capella {
return processBlockSSZResponseCapella(data, isBlinded)
}
if ver >= version.Bellatrix {
return processBlockSSZResponseBellatrix(data, isBlinded)
}
if ver >= version.Altair {
block := &ethpb.BeaconBlockAltair{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
for _, entry := range sszCodecs {
if ver >= entry.minVersion {
if isBlinded && entry.codec.unmarshalBlinded != nil {
return entry.codec.unmarshalBlinded(data)
}
return entry.codec.unmarshalBlock(data)
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Altair{Altair: block}}, nil
}
if ver >= version.Phase0 {
block := &ethpb.BeaconBlock{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Phase0{Phase0: block}}, nil
}
return nil, fmt.Errorf("unsupported block version %s", version.String(ver))
}
func processBlockSSZResponseFulu(data []byte, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
blindedBlock := &ethpb.BlindedBeaconBlockFulu{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedFulu{BlindedFulu: blindedBlock}, IsBlinded: true}, nil
}
block := &ethpb.BeaconBlockContentsFulu{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Fulu{Fulu: block}}, nil
}
func processBlockSSZResponseElectra(data []byte, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
blindedBlock := &ethpb.BlindedBeaconBlockElectra{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedElectra{BlindedElectra: blindedBlock}, IsBlinded: true}, nil
}
block := &ethpb.BeaconBlockContentsElectra{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Electra{Electra: block}}, nil
}
func processBlockSSZResponseDeneb(data []byte, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
blindedBlock := &ethpb.BlindedBeaconBlockDeneb{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedDeneb{BlindedDeneb: blindedBlock}, IsBlinded: true}, nil
}
block := &ethpb.BeaconBlockContentsDeneb{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Deneb{Deneb: block}}, nil
}
func processBlockSSZResponseCapella(data []byte, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
blindedBlock := &ethpb.BlindedBeaconBlockCapella{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedCapella{BlindedCapella: blindedBlock}, IsBlinded: true}, nil
}
block := &ethpb.BeaconBlockCapella{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Capella{Capella: block}}, nil
}
func processBlockSSZResponseBellatrix(data []byte, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
blindedBlock := &ethpb.BlindedBeaconBlockBellatrix{}
if err := blindedBlock.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_BlindedBellatrix{BlindedBellatrix: blindedBlock}, IsBlinded: true}, nil
}
block := &ethpb.BeaconBlockBellatrix{}
if err := block.UnmarshalSSZ(data); err != nil {
return nil, err
}
return &ethpb.GenericBeaconBlock{Block: &ethpb.GenericBeaconBlock_Bellatrix{Bellatrix: block}}, nil
}
func convertBlockToGeneric(decoder *json.Decoder, dest ethpb.GenericConverter, version string, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
typeName := version
if isBlinded {
@@ -180,69 +219,52 @@ func convertBlockToGeneric(decoder *json.Decoder, dest ethpb.GenericConverter, v
return genericBlock, nil
}
// jsonBlockTypes defines factory functions for creating block and blinded block structs for JSON decoding.
type jsonBlockTypes struct {
newBlock func() ethpb.GenericConverter
newBlinded func() ethpb.GenericConverter // nil for Phase0/Altair
}
var jsonBlockFactories = map[string]jsonBlockTypes{
version.String(version.Phase0): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlock{} },
},
version.String(version.Altair): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlockAltair{} },
},
version.String(version.Bellatrix): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlockBellatrix{} },
newBlinded: func() ethpb.GenericConverter { return &structs.BlindedBeaconBlockBellatrix{} },
},
version.String(version.Capella): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlockCapella{} },
newBlinded: func() ethpb.GenericConverter { return &structs.BlindedBeaconBlockCapella{} },
},
version.String(version.Deneb): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlockContentsDeneb{} },
newBlinded: func() ethpb.GenericConverter { return &structs.BlindedBeaconBlockDeneb{} },
},
version.String(version.Electra): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlockContentsElectra{} },
newBlinded: func() ethpb.GenericConverter { return &structs.BlindedBeaconBlockElectra{} },
},
version.String(version.Fulu): {
newBlock: func() ethpb.GenericConverter { return &structs.BeaconBlockContentsFulu{} },
newBlinded: func() ethpb.GenericConverter { return &structs.BlindedBeaconBlockFulu{} },
},
}
func processBlockJSONResponse(ver string, isBlinded bool, decoder *json.Decoder) (*ethpb.GenericBeaconBlock, error) {
if decoder == nil {
return nil, errors.New("no produce block json decoder found")
}
switch ver {
case version.String(version.Phase0):
return convertBlockToGeneric(decoder, &structs.BeaconBlock{}, version.String(version.Phase0), false)
case version.String(version.Altair):
return convertBlockToGeneric(decoder, &structs.BeaconBlockAltair{}, "altair", false)
case version.String(version.Bellatrix):
return processBellatrixBlock(decoder, isBlinded)
case version.String(version.Capella):
return processCapellaBlock(decoder, isBlinded)
case version.String(version.Deneb):
return processDenebBlock(decoder, isBlinded)
case version.String(version.Electra):
return processElectraBlock(decoder, isBlinded)
case version.String(version.Fulu):
return processFuluBlock(decoder, isBlinded)
default:
factory, ok := jsonBlockFactories[ver]
if !ok {
return nil, errors.Errorf("unsupported consensus version `%s`", ver)
}
}
func processBellatrixBlock(decoder *json.Decoder, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
return convertBlockToGeneric(decoder, &structs.BlindedBeaconBlockBellatrix{}, "bellatrix", true)
if isBlinded && factory.newBlinded != nil {
return convertBlockToGeneric(decoder, factory.newBlinded(), ver, true)
}
return convertBlockToGeneric(decoder, &structs.BeaconBlockBellatrix{}, "bellatrix", false)
}
func processCapellaBlock(decoder *json.Decoder, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
return convertBlockToGeneric(decoder, &structs.BlindedBeaconBlockCapella{}, "capella", true)
}
return convertBlockToGeneric(decoder, &structs.BeaconBlockCapella{}, "capella", false)
}
func processDenebBlock(decoder *json.Decoder, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
return convertBlockToGeneric(decoder, &structs.BlindedBeaconBlockDeneb{}, "deneb", true)
}
return convertBlockToGeneric(decoder, &structs.BeaconBlockContentsDeneb{}, "deneb", false)
}
func processElectraBlock(decoder *json.Decoder, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
return convertBlockToGeneric(decoder, &structs.BlindedBeaconBlockElectra{}, "electra", true)
}
return convertBlockToGeneric(decoder, &structs.BeaconBlockContentsElectra{}, "electra", false)
}
func processFuluBlock(decoder *json.Decoder, isBlinded bool) (*ethpb.GenericBeaconBlock, error) {
if isBlinded {
return convertBlockToGeneric(decoder, &structs.BlindedBeaconBlockFulu{}, "fulu", true)
}
return convertBlockToGeneric(decoder, &structs.BeaconBlockContentsFulu{}, "fulu", false)
return convertBlockToGeneric(decoder, factory.newBlock(), ver, false)
}

View File

@@ -11,6 +11,7 @@ import (
"github.com/OffchainLabs/prysm/v7/api/server/structs"
"github.com/OffchainLabs/prysm/v7/consensus-types/primitives"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/OffchainLabs/prysm/v7/runtime/version"
"github.com/OffchainLabs/prysm/v7/testing/assert"
"github.com/OffchainLabs/prysm/v7/testing/require"
"github.com/OffchainLabs/prysm/v7/validator/client/beacon-api/mock"
@@ -25,7 +26,7 @@ func TestGetBeaconBlock_RequestFailed(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
gomock.Any(),
@@ -149,7 +150,7 @@ func TestGetBeaconBlock_Error(t *testing.T) {
b, err := json.Marshal(resp)
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
gomock.Any(),
@@ -185,7 +186,7 @@ func TestGetBeaconBlock_Phase0Valid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -208,6 +209,25 @@ func TestGetBeaconBlock_Phase0Valid(t *testing.T) {
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestSSZCodecs_OrderAndCoverage(t *testing.T) {
versions := version.All()
require.NotEmpty(t, versions)
expected := make([]int, 0, len(versions))
for i := len(versions) - 1; i >= 0; i-- {
expected = append(expected, versions[i])
}
require.Equal(t, len(expected), len(sszCodecs))
for i, entry := range sszCodecs {
assert.Equal(t, expected[i], entry.minVersion, "sszCodecs[%d] has wrong fork order", i)
if i > 0 {
require.Equal(t, true, entry.minVersion < sszCodecs[i-1].minVersion, "sszCodecs not strictly descending at index %d", i)
}
}
}
// Add SSZ test cases below this line
func TestGetBeaconBlock_SSZ_BellatrixValid(t *testing.T) {
@@ -224,7 +244,7 @@ func TestGetBeaconBlock_SSZ_BellatrixValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -266,7 +286,7 @@ func TestGetBeaconBlock_SSZ_BlindedBellatrixValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -308,7 +328,7 @@ func TestGetBeaconBlock_SSZ_CapellaValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -350,7 +370,7 @@ func TestGetBeaconBlock_SSZ_BlindedCapellaValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -392,7 +412,7 @@ func TestGetBeaconBlock_SSZ_DenebValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -434,7 +454,7 @@ func TestGetBeaconBlock_SSZ_BlindedDenebValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -476,7 +496,7 @@ func TestGetBeaconBlock_SSZ_ElectraValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -518,7 +538,7 @@ func TestGetBeaconBlock_SSZ_BlindedElectraValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -546,6 +566,90 @@ func TestGetBeaconBlock_SSZ_BlindedElectraValid(t *testing.T) {
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestGetBeaconBlock_SSZ_FuluValid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
proto := testhelpers.GenerateProtoFuluBeaconBlockContents()
bytes, err := proto.MarshalSSZ()
require.NoError(t, err)
const slot = primitives.Slot(1)
randaoReveal := []byte{2}
graffiti := []byte{3}
ctx := t.Context()
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
).Return(
bytes,
http.Header{
"Content-Type": []string{api.OctetStreamMediaType},
api.VersionHeader: []string{"fulu"},
api.ExecutionPayloadBlindedHeader: []string{"false"},
},
nil,
).Times(1)
validatorClient := &beaconApiValidatorClient{handler: handler}
beaconBlock, err := validatorClient.beaconBlock(ctx, slot, randaoReveal, graffiti)
require.NoError(t, err)
expectedBeaconBlock := &ethpb.GenericBeaconBlock{
Block: &ethpb.GenericBeaconBlock_Fulu{
Fulu: proto,
},
IsBlinded: false,
}
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestGetBeaconBlock_SSZ_BlindedFuluValid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
proto := testhelpers.GenerateProtoBlindedFuluBeaconBlock()
bytes, err := proto.MarshalSSZ()
require.NoError(t, err)
const slot = primitives.Slot(1)
randaoReveal := []byte{2}
graffiti := []byte{3}
ctx := t.Context()
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
).Return(
bytes,
http.Header{
"Content-Type": []string{api.OctetStreamMediaType},
api.VersionHeader: []string{"fulu"},
api.ExecutionPayloadBlindedHeader: []string{"true"},
},
nil,
).Times(1)
validatorClient := &beaconApiValidatorClient{handler: handler}
beaconBlock, err := validatorClient.beaconBlock(ctx, slot, randaoReveal, graffiti)
require.NoError(t, err)
expectedBeaconBlock := &ethpb.GenericBeaconBlock{
Block: &ethpb.GenericBeaconBlock_BlindedFulu{
BlindedFulu: proto,
},
IsBlinded: true,
}
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestGetBeaconBlock_SSZ_UnsupportedVersion(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
@@ -556,7 +660,7 @@ func TestGetBeaconBlock_SSZ_UnsupportedVersion(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -589,7 +693,7 @@ func TestGetBeaconBlock_SSZ_InvalidBlindedHeader(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -622,7 +726,7 @@ func TestGetBeaconBlock_SSZ_InvalidVersionHeader(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -651,7 +755,7 @@ func TestGetBeaconBlock_SSZ_GetSSZError(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -680,7 +784,7 @@ func TestGetBeaconBlock_SSZ_Phase0Valid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -722,7 +826,7 @@ func TestGetBeaconBlock_SSZ_AltairValid(t *testing.T) {
ctx := t.Context()
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -770,7 +874,7 @@ func TestGetBeaconBlock_AltairValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -814,7 +918,7 @@ func TestGetBeaconBlock_BellatrixValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -859,7 +963,7 @@ func TestGetBeaconBlock_BlindedBellatrixValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -904,7 +1008,7 @@ func TestGetBeaconBlock_CapellaValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -949,7 +1053,7 @@ func TestGetBeaconBlock_BlindedCapellaValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -973,6 +1077,96 @@ func TestGetBeaconBlock_BlindedCapellaValid(t *testing.T) {
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestGetBeaconBlock_FuluValid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
proto := testhelpers.GenerateProtoFuluBeaconBlockContents()
block := testhelpers.GenerateJsonFuluBeaconBlockContents()
bytes, err := json.Marshal(block)
require.NoError(t, err)
const slot = primitives.Slot(1)
randaoReveal := []byte{2}
graffiti := []byte{3}
ctx := t.Context()
b, err := json.Marshal(structs.ProduceBlockV3Response{
Version: "fulu",
ExecutionPayloadBlinded: false,
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
).Return(
b,
http.Header{"Content-Type": []string{"application/json"}},
nil,
).Times(1)
validatorClient := &beaconApiValidatorClient{handler: handler}
beaconBlock, err := validatorClient.beaconBlock(ctx, slot, randaoReveal, graffiti)
require.NoError(t, err)
expectedBeaconBlock := &ethpb.GenericBeaconBlock{
Block: &ethpb.GenericBeaconBlock_Fulu{
Fulu: proto,
},
IsBlinded: false,
}
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestGetBeaconBlock_BlindedFuluValid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
proto := testhelpers.GenerateProtoBlindedFuluBeaconBlock()
block := testhelpers.GenerateJsonBlindedFuluBeaconBlock()
bytes, err := json.Marshal(block)
require.NoError(t, err)
const slot = primitives.Slot(1)
randaoReveal := []byte{2}
graffiti := []byte{3}
ctx := t.Context()
b, err := json.Marshal(structs.ProduceBlockV3Response{
Version: "fulu",
ExecutionPayloadBlinded: true,
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
).Return(
b,
http.Header{"Content-Type": []string{"application/json"}},
nil,
).Times(1)
validatorClient := &beaconApiValidatorClient{handler: handler}
beaconBlock, err := validatorClient.beaconBlock(ctx, slot, randaoReveal, graffiti)
require.NoError(t, err)
expectedBeaconBlock := &ethpb.GenericBeaconBlock{
Block: &ethpb.GenericBeaconBlock_BlindedFulu{
BlindedFulu: proto,
},
IsBlinded: true,
}
assert.DeepEqual(t, expectedBeaconBlock, beaconBlock)
}
func TestGetBeaconBlock_DenebValid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
@@ -994,7 +1188,7 @@ func TestGetBeaconBlock_DenebValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -1039,7 +1233,7 @@ func TestGetBeaconBlock_BlindedDenebValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -1084,7 +1278,7 @@ func TestGetBeaconBlock_ElectraValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),
@@ -1129,7 +1323,7 @@ func TestGetBeaconBlock_BlindedElectraValid(t *testing.T) {
Data: bytes,
})
require.NoError(t, err)
handler := mock.NewMockJsonRestHandler(ctrl)
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().GetSSZ(
gomock.Any(),
fmt.Sprintf("/eth/v3/validator/blocks/%d?graffiti=%s&randao_reveal=%s", slot, hexutil.Encode(graffiti), hexutil.Encode(randaoReveal)),

View File

@@ -7,6 +7,7 @@ import (
"net/http"
"github.com/OffchainLabs/prysm/v7/api/server/structs"
"github.com/OffchainLabs/prysm/v7/encoding/ssz"
"github.com/OffchainLabs/prysm/v7/network/httputil"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/pkg/errors"
@@ -21,34 +22,128 @@ type blockProcessingResult struct {
marshalJSON func() ([]byte, error)
}
type sszMarshaler interface {
MarshalSSZ() ([]byte, error)
}
func buildBlockResult(
versionName string,
blinded bool,
sszObj sszMarshaler,
rootObj ssz.Hashable,
jsonFn func() ([]byte, error),
) (*blockProcessingResult, error) {
beaconBlockRoot, err := rootObj.HashTreeRoot()
if err != nil {
return nil, errors.Wrapf(err, "failed to compute block root for %s beacon block", versionName)
}
marshaledSSZ, err := sszObj.MarshalSSZ()
if err != nil {
return nil, errors.Wrapf(err, "failed to serialize %s beacon block", versionName)
}
return &blockProcessingResult{
consensusVersion: versionName,
blinded: blinded,
beaconBlockRoot: beaconBlockRoot,
marshalledSSZ: marshaledSSZ,
marshalJSON: jsonFn,
}, nil
}
func (c *beaconApiValidatorClient) proposeBeaconBlock(ctx context.Context, in *ethpb.GenericSignedBeaconBlock) (*ethpb.ProposeResponse, error) {
var res *blockProcessingResult
var err error
switch blockType := in.Block.(type) {
case *ethpb.GenericSignedBeaconBlock_Phase0:
res, err = handlePhase0Block(blockType)
res, err = buildBlockResult("phase0", false, blockType.Phase0, blockType.Phase0.Block, func() ([]byte, error) {
return json.Marshal(structs.SignedBeaconBlockPhase0FromConsensus(blockType.Phase0))
})
case *ethpb.GenericSignedBeaconBlock_Altair:
res, err = handleAltairBlock(blockType)
res, err = buildBlockResult("altair", false, blockType.Altair, blockType.Altair.Block, func() ([]byte, error) {
return json.Marshal(structs.SignedBeaconBlockAltairFromConsensus(blockType.Altair))
})
case *ethpb.GenericSignedBeaconBlock_Bellatrix:
res, err = handleBellatrixBlock(blockType)
res, err = buildBlockResult("bellatrix", false, blockType.Bellatrix, blockType.Bellatrix.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockBellatrixFromConsensus(blockType.Bellatrix)
if err != nil {
return nil, errors.Wrap(err, "failed to convert bellatrix beacon block")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_BlindedBellatrix:
res, err = handleBlindedBellatrixBlock(blockType)
res, err = buildBlockResult("bellatrix", true, blockType.BlindedBellatrix, blockType.BlindedBellatrix.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockBellatrixFromConsensus(blockType.BlindedBellatrix)
if err != nil {
return nil, errors.Wrap(err, "failed to convert blinded bellatrix beacon block")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_Capella:
res, err = handleCapellaBlock(blockType)
res, err = buildBlockResult("capella", false, blockType.Capella, blockType.Capella.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockCapellaFromConsensus(blockType.Capella)
if err != nil {
return nil, errors.Wrap(err, "failed to convert capella beacon block")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_BlindedCapella:
res, err = handleBlindedCapellaBlock(blockType)
res, err = buildBlockResult("capella", true, blockType.BlindedCapella, blockType.BlindedCapella.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockCapellaFromConsensus(blockType.BlindedCapella)
if err != nil {
return nil, errors.Wrap(err, "failed to convert blinded capella beacon block")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_Deneb:
res, err = handleDenebBlockContents(blockType)
res, err = buildBlockResult("deneb", false, blockType.Deneb, blockType.Deneb.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockContentsDenebFromConsensus(blockType.Deneb)
if err != nil {
return nil, errors.Wrap(err, "failed to convert deneb beacon block contents")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_BlindedDeneb:
res, err = handleBlindedDenebBlock(blockType)
res, err = buildBlockResult("deneb", true, blockType.BlindedDeneb, blockType.BlindedDeneb, func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockDenebFromConsensus(blockType.BlindedDeneb)
if err != nil {
return nil, errors.Wrap(err, "failed to convert deneb blinded beacon block")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_Electra:
res, err = handleElectraBlockContents(blockType)
res, err = buildBlockResult("electra", false, blockType.Electra, blockType.Electra.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockContentsElectraFromConsensus(blockType.Electra)
if err != nil {
return nil, errors.Wrap(err, "failed to convert electra beacon block contents")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_BlindedElectra:
res, err = handleBlindedElectraBlock(blockType)
res, err = buildBlockResult("electra", true, blockType.BlindedElectra, blockType.BlindedElectra, func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockElectraFromConsensus(blockType.BlindedElectra)
if err != nil {
return nil, errors.Wrap(err, "failed to convert electra blinded beacon block")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_Fulu:
res, err = handleFuluBlockContents(blockType)
res, err = buildBlockResult("fulu", false, blockType.Fulu, blockType.Fulu.Block, func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockContentsFuluFromConsensus(blockType.Fulu)
if err != nil {
return nil, errors.Wrap(err, "failed to convert fulu beacon block contents")
}
return json.Marshal(signedBlock)
})
case *ethpb.GenericSignedBeaconBlock_BlindedFulu:
res, err = handleBlindedFuluBlock(blockType)
res, err = buildBlockResult("fulu", true, blockType.BlindedFulu, blockType.BlindedFulu, func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockFuluFromConsensus(blockType.BlindedFulu)
if err != nil {
return nil, errors.Wrap(err, "failed to convert fulu blinded beacon block")
}
return json.Marshal(signedBlock)
})
default:
return nil, errors.Errorf("unsupported block type %T", in.Block)
}
@@ -116,357 +211,3 @@ func (c *beaconApiValidatorClient) proposeBeaconBlock(ctx context.Context, in *e
return &ethpb.ProposeResponse{BlockRoot: res.beaconBlockRoot[:]}, nil
}
func handlePhase0Block(block *ethpb.GenericSignedBeaconBlock_Phase0) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "phase0"
res.blinded = false
beaconBlockRoot, err := block.Phase0.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for phase0 beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Phase0.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize block for phase0 beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock := structs.SignedBeaconBlockPhase0FromConsensus(block.Phase0)
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleAltairBlock(block *ethpb.GenericSignedBeaconBlock_Altair) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "altair"
res.blinded = false
beaconBlockRoot, err := block.Altair.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for altair beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Altair.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize block for altair beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock := structs.SignedBeaconBlockAltairFromConsensus(block.Altair)
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleBellatrixBlock(block *ethpb.GenericSignedBeaconBlock_Bellatrix) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "bellatrix"
res.blinded = false
beaconBlockRoot, err := block.Bellatrix.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for bellatrix beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Bellatrix.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize block for bellatrix beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockBellatrixFromConsensus(block.Bellatrix)
if err != nil {
return nil, errors.Wrap(err, "failed to convert bellatrix beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleBlindedBellatrixBlock(block *ethpb.GenericSignedBeaconBlock_BlindedBellatrix) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "bellatrix"
res.blinded = true
beaconBlockRoot, err := block.BlindedBellatrix.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for bellatrix beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.BlindedBellatrix.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize block for bellatrix beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockBellatrixFromConsensus(block.BlindedBellatrix)
if err != nil {
return nil, errors.Wrap(err, "failed to convert blinded bellatrix beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleCapellaBlock(block *ethpb.GenericSignedBeaconBlock_Capella) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "capella"
res.blinded = false
beaconBlockRoot, err := block.Capella.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for capella beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Capella.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize capella beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockCapellaFromConsensus(block.Capella)
if err != nil {
return nil, errors.Wrap(err, "failed to convert capella beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleBlindedCapellaBlock(block *ethpb.GenericSignedBeaconBlock_BlindedCapella) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "capella"
res.blinded = true
beaconBlockRoot, err := block.BlindedCapella.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for blinded capella beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.BlindedCapella.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize blinded capella beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockCapellaFromConsensus(block.BlindedCapella)
if err != nil {
return nil, errors.Wrap(err, "failed to convert blinded capella beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleDenebBlockContents(block *ethpb.GenericSignedBeaconBlock_Deneb) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "deneb"
res.blinded = false
beaconBlockRoot, err := block.Deneb.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for deneb beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Deneb.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize deneb beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockContentsDenebFromConsensus(block.Deneb)
if err != nil {
return nil, errors.Wrap(err, "failed to convert deneb beacon block contents")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleBlindedDenebBlock(block *ethpb.GenericSignedBeaconBlock_BlindedDeneb) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "deneb"
res.blinded = true
beaconBlockRoot, err := block.BlindedDeneb.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for deneb blinded beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.BlindedDeneb.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize blinded deneb beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockDenebFromConsensus(block.BlindedDeneb)
if err != nil {
return nil, errors.Wrap(err, "failed to convert deneb blinded beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleElectraBlockContents(block *ethpb.GenericSignedBeaconBlock_Electra) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "electra"
res.blinded = false
beaconBlockRoot, err := block.Electra.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for electra beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Electra.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize electra beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockContentsElectraFromConsensus(block.Electra)
if err != nil {
return nil, errors.Wrap(err, "failed to convert electra beacon block contents")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleBlindedElectraBlock(block *ethpb.GenericSignedBeaconBlock_BlindedElectra) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "electra"
res.blinded = true
beaconBlockRoot, err := block.BlindedElectra.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for electra blinded beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.BlindedElectra.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize blinded electra beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockElectraFromConsensus(block.BlindedElectra)
if err != nil {
return nil, errors.Wrap(err, "failed to convert electra blinded beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleFuluBlockContents(block *ethpb.GenericSignedBeaconBlock_Fulu) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "fulu"
res.blinded = false
beaconBlockRoot, err := block.Fulu.Block.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for fulu beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.Fulu.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize fulu beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBeaconBlockContentsFuluFromConsensus(block.Fulu)
if err != nil {
return nil, errors.Wrap(err, "failed to convert fulu beacon block contents")
}
return json.Marshal(signedBlock)
}
return &res, nil
}
func handleBlindedFuluBlock(block *ethpb.GenericSignedBeaconBlock_BlindedFulu) (*blockProcessingResult, error) {
var res blockProcessingResult
res.consensusVersion = "fulu"
res.blinded = true
beaconBlockRoot, err := block.BlindedFulu.HashTreeRoot()
if err != nil {
return nil, errors.Wrap(err, "failed to compute block root for fulu blinded beacon block")
}
res.beaconBlockRoot = beaconBlockRoot
// Marshal SSZ
ssz, err := block.BlindedFulu.MarshalSSZ()
if err != nil {
return nil, errors.Wrap(err, "failed to serialize blinded fulu beacon block")
}
res.marshalledSSZ = ssz
// Set up JSON marshalling function for fallback
res.marshalJSON = func() ([]byte, error) {
signedBlock, err := structs.SignedBlindedBeaconBlockFuluFromConsensus(block.BlindedFulu)
if err != nil {
return nil, errors.Wrap(err, "failed to convert fulu blinded beacon block")
}
return json.Marshal(signedBlock)
}
return &res, nil
}

View File

@@ -620,6 +620,42 @@ func TestProposeBeaconBlock_SSZFails_406_FallbackToJSON(t *testing.T) {
}
}
func TestProposeBeaconBlock_SSZFails_406_JSONFallbackFails(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := t.Context()
handler := mock.NewMockHandler(ctrl)
handler.EXPECT().PostSSZ(
gomock.Any(),
"/eth/v2/beacon/blocks",
gomock.Any(),
gomock.Any(),
).Return(
nil, nil, &httputil.DefaultJsonError{
Code: http.StatusNotAcceptable,
Message: "SSZ not supported",
},
).Times(1)
handler.EXPECT().Post(
gomock.Any(),
"/eth/v2/beacon/blocks",
gomock.Any(),
gomock.Any(),
nil,
).Return(
errors.New("json fallback failed"),
).Times(1)
validatorClient := &beaconApiValidatorClient{handler: handler}
_, err := validatorClient.proposeBeaconBlock(ctx, &ethpb.GenericSignedBeaconBlock{
Block: generateSignedPhase0Block(),
})
assert.ErrorContains(t, "failed to submit block via JSON fallback", err)
}
func TestProposeBeaconBlock_SSZFails_Non406_NoFallback(t *testing.T) {
testCases := []struct {
name string
@@ -676,3 +712,41 @@ func TestProposeBeaconBlock_SSZFails_Non406_NoFallback(t *testing.T) {
})
}
}
type badHashable struct{}
func (badHashable) HashTreeRoot() ([32]byte, error) {
return [32]byte{}, errors.New("hash root error")
}
type badMarshaler struct{}
func (badMarshaler) MarshalSSZ() ([]byte, error) {
return nil, errors.New("marshal ssz error")
}
type okMarshaler struct{}
func (okMarshaler) MarshalSSZ() ([]byte, error) {
return []byte{1, 2, 3}, nil
}
type okHashable struct{}
func (okHashable) HashTreeRoot() ([32]byte, error) {
return [32]byte{1}, nil
}
func TestBuildBlockResult_HashTreeRootError(t *testing.T) {
_, err := buildBlockResult("phase0", false, okMarshaler{}, badHashable{}, func() ([]byte, error) {
return []byte(`{}`), nil
})
assert.ErrorContains(t, "failed to compute block root for phase0 beacon block", err)
}
func TestBuildBlockResult_MarshalSSZError(t *testing.T) {
_, err := buildBlockResult("phase0", false, badMarshaler{}, okHashable{}, func() ([]byte, error) {
return []byte(`{}`), nil
})
assert.ErrorContains(t, "failed to serialize phase0 beacon block", err)
}

View File

@@ -9,6 +9,7 @@ go_library(
"capella_beacon_block_test_helpers.go",
"deneb_beacon_block_test_helpers.go",
"electra_beacon_block_test_helpers.go",
"fulu_beacon_block_test_helpers.go",
"phase0_beacon_block_test_helpers.go",
"test_helpers.go",
],

View File

@@ -0,0 +1,46 @@
package test_helpers
import (
"github.com/OffchainLabs/prysm/v7/api/server/structs"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
)
func GenerateProtoFuluBeaconBlockContents() *ethpb.BeaconBlockContentsFulu {
electra := GenerateProtoElectraBeaconBlockContents()
return &ethpb.BeaconBlockContentsFulu{
Block: electra.Block,
KzgProofs: electra.KzgProofs,
Blobs: electra.Blobs,
}
}
func GenerateProtoBlindedFuluBeaconBlock() *ethpb.BlindedBeaconBlockFulu {
electra := GenerateProtoBlindedElectraBeaconBlock()
return &ethpb.BlindedBeaconBlockFulu{
Slot: electra.Slot,
ProposerIndex: electra.ProposerIndex,
ParentRoot: electra.ParentRoot,
StateRoot: electra.StateRoot,
Body: electra.Body,
}
}
func GenerateJsonFuluBeaconBlockContents() *structs.BeaconBlockContentsFulu {
electra := GenerateJsonElectraBeaconBlockContents()
return &structs.BeaconBlockContentsFulu{
Block: electra.Block,
KzgProofs: electra.KzgProofs,
Blobs: electra.Blobs,
}
}
func GenerateJsonBlindedFuluBeaconBlock() *structs.BlindedBeaconBlockFulu {
electra := GenerateJsonBlindedElectraBeaconBlock()
return &structs.BlindedBeaconBlockFulu{
Slot: electra.Slot,
ProposerIndex: electra.ProposerIndex,
ParentRoot: electra.ParentRoot,
StateRoot: electra.StateRoot,
Body: electra.Body,
}
}