From 7f4c694bb395a533c8b079d4a088ca9271e70d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Kapka?= Date: Wed, 3 Aug 2022 15:33:05 +0200 Subject: [PATCH] Native Blocks Ep. 3 - Remove old code (#11158) * Native Blocks Ep. 3 - Remove old code * rename back package * fix errors --- consensus-types/wrapper/BUILD.bazel | 49 +-- consensus-types/wrapper/beacon_block.go | 261 ----------- .../wrapper/beacon_block_altair.go | 322 -------------- .../wrapper/beacon_block_altair_test.go | 367 ---------------- .../wrapper/beacon_block_bellatrix.go | 351 --------------- .../wrapper/beacon_block_bellatrix_test.go | 414 ------------------ .../wrapper/beacon_block_phase0.go | 310 ------------- .../wrapper/beacon_block_phase0_test.go | 88 ---- consensus-types/wrapper/beacon_block_test.go | 160 ------- .../wrapper/blinded_beacon_block_bellatrix.go | 317 -------------- .../blinded_beacon_block_bellatrix_test.go | 406 ----------------- consensus-types/wrapper/execution.go | 351 --------------- consensus-types/wrapper/execution_test.go | 124 ------ consensus-types/wrapper/mutator.go | 77 ---- 14 files changed, 2 insertions(+), 3595 deletions(-) delete mode 100644 consensus-types/wrapper/beacon_block.go delete mode 100644 consensus-types/wrapper/beacon_block_altair.go delete mode 100644 consensus-types/wrapper/beacon_block_altair_test.go delete mode 100644 consensus-types/wrapper/beacon_block_bellatrix.go delete mode 100644 consensus-types/wrapper/beacon_block_bellatrix_test.go delete mode 100644 consensus-types/wrapper/beacon_block_phase0.go delete mode 100644 consensus-types/wrapper/beacon_block_phase0_test.go delete mode 100644 consensus-types/wrapper/beacon_block_test.go delete mode 100644 consensus-types/wrapper/blinded_beacon_block_bellatrix.go delete mode 100644 consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go delete mode 100644 consensus-types/wrapper/execution.go delete mode 100644 consensus-types/wrapper/execution_test.go delete mode 100644 consensus-types/wrapper/mutator.go diff --git a/consensus-types/wrapper/BUILD.bazel b/consensus-types/wrapper/BUILD.bazel index 6e08f776b6..33c8060075 100644 --- a/consensus-types/wrapper/BUILD.bazel +++ b/consensus-types/wrapper/BUILD.bazel @@ -1,60 +1,15 @@ -load("@prysm//tools/go:def.bzl", "go_library", "go_test") +load("@prysm//tools/go:def.bzl", "go_library") go_library( name = "go_default_library", - srcs = [ - "beacon_block.go", - "beacon_block_altair.go", - "beacon_block_bellatrix.go", - "beacon_block_phase0.go", - "blinded_beacon_block_bellatrix.go", - "execution.go", - "metadata.go", - "mutator.go", - ], + srcs = ["metadata.go"], importpath = "github.com/prysmaticlabs/prysm/consensus-types/wrapper", visibility = ["//visibility:public"], deps = [ - "//config/fieldparams:go_default_library", - "//consensus-types/interfaces:go_default_library", - "//consensus-types/primitives:go_default_library", - "//encoding/bytesutil:go_default_library", - "//encoding/ssz:go_default_library", - "//proto/engine/v1:go_default_library", "//proto/prysm/v1alpha1:go_default_library", "//proto/prysm/v1alpha1/metadata:go_default_library", - "//proto/prysm/v1alpha1/validator-client:go_default_library", "//runtime/version:go_default_library", - "@com_github_pkg_errors//:go_default_library", - "@com_github_prysmaticlabs_fastssz//:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", "@org_golang_google_protobuf//proto:go_default_library", ], ) - -go_test( - name = "go_default_test", - srcs = [ - "beacon_block_altair_test.go", - "beacon_block_bellatrix_test.go", - "beacon_block_phase0_test.go", - "beacon_block_test.go", - "blinded_beacon_block_bellatrix_test.go", - "execution_test.go", - ], - deps = [ - ":go_default_library", - "//config/fieldparams:go_default_library", - "//consensus-types/interfaces:go_default_library", - "//consensus-types/primitives:go_default_library", - "//encoding/bytesutil:go_default_library", - "//proto/engine/v1:go_default_library", - "//proto/prysm/v1alpha1:go_default_library", - "//proto/prysm/v1alpha1/validator-client:go_default_library", - "//runtime/version:go_default_library", - "//testing/assert:go_default_library", - "//testing/require:go_default_library", - "//testing/util:go_default_library", - "@com_github_pkg_errors//:go_default_library", - ], -) diff --git a/consensus-types/wrapper/beacon_block.go b/consensus-types/wrapper/beacon_block.go deleted file mode 100644 index 4ada85b33f..0000000000 --- a/consensus-types/wrapper/beacon_block.go +++ /dev/null @@ -1,261 +0,0 @@ -package wrapper - -import ( - "fmt" - - "github.com/pkg/errors" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" - eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" -) - -var ( - // ErrUnsupportedField is returned when a field is not supported by a specific beacon block type. - // This allows us to create a generic beacon block interface that is implemented by different - // fork versions of beacon blocks. - ErrUnsupportedField = errors.New("unsupported field for block type") - // ErrUnsupportedVersion for beacon block methods. - ErrUnsupportedVersion = errors.New("unsupported beacon block version") - // ErrUnsupportedSignedBeaconBlock is returned when the struct type is not a supported signed - // beacon block type. - ErrUnsupportedSignedBeaconBlock = errors.New("unsupported signed beacon block") - // ErrUnsupportedBeaconBlock is returned when the struct type is not a supported beacon block - // type. - ErrUnsupportedBeaconBlock = errors.New("unsupported beacon block") - // ErrUnsupportedBeaconBlockBody is returned when the struct type is not a supported beacon block body - // type. - ErrUnsupportedBeaconBlockBody = errors.New("unsupported beacon block body") - // ErrUnsupportedPhase0Block is returned when accessing a phase0 block from a non-phase0 wrapped - // block. - ErrUnsupportedPhase0Block = errors.New("unsupported phase0 block") - // ErrUnsupportedAltairBlock is returned when accessing an altair block from non-altair wrapped - // block. - ErrUnsupportedAltairBlock = errors.New("unsupported altair block") - // ErrUnsupportedBellatrixBlock is returned when accessing a bellatrix block from a non-bellatrix wrapped - // block. - ErrUnsupportedBellatrixBlock = errors.New("unsupported bellatrix block") - // ErrUnsupportedBlindedBellatrixBlock is returned when accessing a blinded bellatrix block from unsupported method. - ErrUnsupportedBlindedBellatrixBlock = errors.New("unsupported blinded bellatrix block") - // ErrNilObjectWrapped is returned in a constructor when the underlying object is nil. - ErrNilObjectWrapped = errors.New("attempted to wrap nil object") - ErrNilSignedBeaconBlock = errors.New("signed beacon block can't be nil") - ErrNilBeaconBlock = errors.New("beacon block can't be nil") - ErrNilBeaconBlockBody = errors.New("beacon block body can't be nil") -) - -// WrappedSignedBeaconBlock will wrap a signed beacon block to conform to the -// signed beacon block interface. -func WrappedSignedBeaconBlock(i interface{}) (interfaces.SignedBeaconBlock, error) { - switch b := i.(type) { - case *eth.GenericSignedBeaconBlock_Phase0: - return wrappedPhase0SignedBeaconBlock(b.Phase0), nil - case *eth.SignedBeaconBlock: - return wrappedPhase0SignedBeaconBlock(b), nil - case *eth.GenericSignedBeaconBlock_Altair: - return wrappedAltairSignedBeaconBlock(b.Altair) - case *eth.SignedBeaconBlockAltair: - return wrappedAltairSignedBeaconBlock(b) - case *eth.GenericSignedBeaconBlock_Bellatrix: - return wrappedBellatrixSignedBeaconBlock(b.Bellatrix) - case *eth.SignedBeaconBlockBellatrix: - return wrappedBellatrixSignedBeaconBlock(b) - case *eth.GenericSignedBeaconBlock_BlindedBellatrix: - return wrappedBellatrixSignedBlindedBeaconBlock(b.BlindedBellatrix) - case *eth.SignedBlindedBeaconBlockBellatrix: - return wrappedBellatrixSignedBlindedBeaconBlock(b) - case nil: - return nil, ErrNilObjectWrapped - default: - return nil, errors.Wrapf(ErrUnsupportedSignedBeaconBlock, "unable to wrap block of type %T", i) - } -} - -// WrappedBeaconBlock will wrap a beacon block to conform to the -// beacon block interface. -func WrappedBeaconBlock(i interface{}) (interfaces.BeaconBlock, error) { - switch b := i.(type) { - case *eth.GenericBeaconBlock_Phase0: - return wrappedPhase0BeaconBlock(b.Phase0), nil - case *eth.BeaconBlock: - return wrappedPhase0BeaconBlock(b), nil - case *eth.GenericBeaconBlock_Altair: - return wrappedAltairBeaconBlock(b.Altair) - case *eth.BeaconBlockAltair: - return wrappedAltairBeaconBlock(b) - case *eth.GenericBeaconBlock_Bellatrix: - return wrappedBellatrixBeaconBlock(b.Bellatrix) - case *eth.BeaconBlockBellatrix: - return wrappedBellatrixBeaconBlock(b) - case *eth.GenericBeaconBlock_BlindedBellatrix: - return wrappedBellatrixBlindedBeaconBlock(b.BlindedBellatrix) - case *eth.BlindedBeaconBlockBellatrix: - return wrappedBellatrixBlindedBeaconBlock(b) - case nil: - return nil, ErrNilObjectWrapped - default: - return nil, errors.Wrapf(ErrUnsupportedBeaconBlock, "unable to wrap block of type %T", i) - } -} - -// WrappedBeaconBlockBody will wrap a beacon block body to conform to the -// beacon block interface. -func WrappedBeaconBlockBody(i interface{}) (interfaces.BeaconBlockBody, error) { - switch b := i.(type) { - case *eth.BeaconBlockBody: - return wrappedPhase0BeaconBlockBody(b), nil - case *eth.BeaconBlockBodyAltair: - return wrappedAltairBeaconBlockBody(b) - case *eth.BeaconBlockBodyBellatrix: - return wrappedBellatrixBeaconBlockBody(b) - case *eth.BlindedBeaconBlockBodyBellatrix: - return wrappedBellatrixBlindedBeaconBlockBody(b) - case nil: - return nil, ErrNilObjectWrapped - default: - return nil, errors.Wrapf(ErrUnsupportedBeaconBlockBody, "unable to wrap block body of type %T", i) - } -} - -// BuildSignedBeaconBlock assembles a block.SignedBeaconBlock interface compatible struct from a -// given beacon block an the appropriate signature. This method may be used to easily create a -// signed beacon block. -func BuildSignedBeaconBlock(blk interfaces.BeaconBlock, signature []byte) (interfaces.SignedBeaconBlock, error) { - switch b := blk.(type) { - case Phase0BeaconBlock: - pb, err := b.Proto() - if err != nil { - return nil, err - } - blkPb, ok := pb.(*eth.BeaconBlock) - if !ok { - return nil, errors.New("unable to access inner phase0 proto") - } - return WrappedSignedBeaconBlock(ð.SignedBeaconBlock{Block: blkPb, Signature: signature}) - case altairBeaconBlock: - pb, err := b.Proto() - if err != nil { - return nil, err - } - blkPb, ok := pb.(*eth.BeaconBlockAltair) - if !ok { - return nil, errors.New("unable to access inner altair proto") - } - return WrappedSignedBeaconBlock(ð.SignedBeaconBlockAltair{Block: blkPb, Signature: signature}) - case bellatrixBeaconBlock: - pb, err := b.Proto() - if err != nil { - return nil, err - } - blkPb, ok := pb.(*eth.BeaconBlockBellatrix) - if !ok { - return nil, errors.New("unable to access inner bellatrix proto") - } - return WrappedSignedBeaconBlock(ð.SignedBeaconBlockBellatrix{Block: blkPb, Signature: signature}) - case blindedBeaconBlockBellatrix: - pb, err := b.Proto() - if err != nil { - return nil, err - } - blkPb, ok := pb.(*eth.BlindedBeaconBlockBellatrix) - if !ok { - return nil, errors.New("unable to access inner bellatrix proto") - } - return WrappedSignedBeaconBlock(ð.SignedBlindedBeaconBlockBellatrix{Block: blkPb, Signature: signature}) - default: - return nil, errors.Wrapf(ErrUnsupportedBeaconBlock, "unable to wrap block of type %T", b) - } -} - -// BuildSignedBeaconBlockFromExecutionPayload takes a signed, blinded beacon block and converts into -// a full, signed beacon block by specifying an execution payload. -func BuildSignedBeaconBlockFromExecutionPayload( - blk interfaces.SignedBeaconBlock, payload *enginev1.ExecutionPayload, -) (interfaces.SignedBeaconBlock, error) { - if err := BeaconBlockIsNil(blk); err != nil { - return nil, err - } - b := blk.Block() - payloadHeader, err := b.Body().Execution() - switch { - case errors.Is(err, ErrUnsupportedField): - return nil, errors.Wrap(err, "can only build signed beacon block from blinded format") - case err != nil: - return nil, errors.Wrap(err, "could not get execution payload header") - default: - } - payloadRoot, err := payload.HashTreeRoot() - if err != nil { - return nil, errors.Wrap(err, "could not hash tree root execution payload") - } - payloadHeaderRoot, err := payloadHeader.HashTreeRoot() - if err != nil { - return nil, errors.Wrap(err, "could not hash tree root payload header") - } - if payloadRoot != payloadHeaderRoot { - return nil, fmt.Errorf( - "payload %#x and header %#x roots do not match", - payloadRoot, - payloadHeaderRoot, - ) - } - syncAgg, err := b.Body().SyncAggregate() - if err != nil { - return nil, errors.Wrap(err, "could not get sync aggregate from block body") - } - bellatrixFullBlock := ð.SignedBeaconBlockBellatrix{ - Block: ð.BeaconBlockBellatrix{ - Slot: b.Slot(), - ProposerIndex: b.ProposerIndex(), - ParentRoot: b.ParentRoot(), - StateRoot: b.StateRoot(), - Body: ð.BeaconBlockBodyBellatrix{ - RandaoReveal: b.Body().RandaoReveal(), - Eth1Data: b.Body().Eth1Data(), - Graffiti: b.Body().Graffiti(), - ProposerSlashings: b.Body().ProposerSlashings(), - AttesterSlashings: b.Body().AttesterSlashings(), - Attestations: b.Body().Attestations(), - Deposits: b.Body().Deposits(), - VoluntaryExits: b.Body().VoluntaryExits(), - SyncAggregate: syncAgg, - ExecutionPayload: payload, - }, - }, - Signature: blk.Signature(), - } - return wrappedBellatrixSignedBeaconBlock(bellatrixFullBlock) -} - -func UnwrapGenericSignedBeaconBlock(gb *eth.GenericSignedBeaconBlock) (interfaces.SignedBeaconBlock, error) { - if gb == nil { - return nil, ErrNilObjectWrapped - } - switch bb := gb.Block.(type) { - case *eth.GenericSignedBeaconBlock_Phase0: - return WrappedSignedBeaconBlock(bb.Phase0) - case *eth.GenericSignedBeaconBlock_Altair: - return WrappedSignedBeaconBlock(bb.Altair) - case *eth.GenericSignedBeaconBlock_Bellatrix: - return WrappedSignedBeaconBlock(bb.Bellatrix) - case *eth.GenericSignedBeaconBlock_BlindedBellatrix: - return WrappedSignedBeaconBlock(bb.BlindedBellatrix) - default: - return nil, errors.Wrapf(ErrUnsupportedSignedBeaconBlock, "unable to wrap block of type %T", gb) - } -} - -// 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. -func BeaconBlockIsNil(b interfaces.SignedBeaconBlock) error { - if b == nil || b.IsNil() { - return ErrNilSignedBeaconBlock - } - if b.Block().IsNil() { - return ErrNilBeaconBlock - } - if b.Block().Body().IsNil() { - return ErrNilBeaconBlockBody - } - return nil -} diff --git a/consensus-types/wrapper/beacon_block_altair.go b/consensus-types/wrapper/beacon_block_altair.go deleted file mode 100644 index 96855f2288..0000000000 --- a/consensus-types/wrapper/beacon_block_altair.go +++ /dev/null @@ -1,322 +0,0 @@ -package wrapper - -import ( - "github.com/pkg/errors" - ssz "github.com/prysmaticlabs/fastssz" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "google.golang.org/protobuf/proto" -) - -var ( - _ = interfaces.SignedBeaconBlock(&altairSignedBeaconBlock{}) - _ = interfaces.BeaconBlock(&altairBeaconBlock{}) - _ = interfaces.BeaconBlockBody(&altairBeaconBlockBody{}) -) - -// altairSignedBeaconBlock is a convenience wrapper around a altair beacon block -// object. 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. -type altairSignedBeaconBlock struct { - b *eth.SignedBeaconBlockAltair -} - -// wrappedAltairSignedBeaconBlock is constructor which wraps a protobuf altair block -// with the block wrapper. -func wrappedAltairSignedBeaconBlock(b *eth.SignedBeaconBlockAltair) (interfaces.SignedBeaconBlock, error) { - w := altairSignedBeaconBlock{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// Signature returns the respective block signature. -func (w altairSignedBeaconBlock) Signature() []byte { - return w.b.Signature -} - -// Block returns the underlying beacon block object. -func (w altairSignedBeaconBlock) Block() interfaces.BeaconBlock { - return altairBeaconBlock{b: w.b.Block} -} - -// IsNil checks if the underlying beacon block is -// nil. -func (w altairSignedBeaconBlock) IsNil() bool { - return w.b == nil || w.b.Block == nil -} - -// Copy performs a deep copy of the signed beacon block -// object. -func (w altairSignedBeaconBlock) Copy() (interfaces.SignedBeaconBlock, error) { - return altairSignedBeaconBlock{b: eth.CopySignedBeaconBlockAltair(w.b)}, nil -} - -// MarshalSSZ marshals the signed beacon block to its relevant ssz -// form. -func (w altairSignedBeaconBlock) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the signed beacon block's ssz -// form to the provided byte buffer. -func (w altairSignedBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized signed block -func (w altairSignedBeaconBlock) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the signed beacon block from its relevant ssz -// form. -func (w altairSignedBeaconBlock) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the block in its underlying protobuf -// interface. -func (w altairSignedBeaconBlock) Proto() (proto.Message, error) { - return w.b, nil -} - -// PbGenericBlock returns a generic signed beacon block. -func (w altairSignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, error) { - return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_Altair{Altair: w.b}, - }, nil -} - -// PbAltairBlock returns the underlying protobuf object. -func (w altairSignedBeaconBlock) PbAltairBlock() (*eth.SignedBeaconBlockAltair, error) { - return w.b, nil -} - -// PbPhase0Block is a stub. -func (altairSignedBeaconBlock) PbPhase0Block() (*eth.SignedBeaconBlock, error) { - return nil, ErrUnsupportedPhase0Block -} - -// PbBellatrixBlock is a stub. -func (altairSignedBeaconBlock) PbBellatrixBlock() (*eth.SignedBeaconBlockBellatrix, error) { - return nil, ErrUnsupportedBellatrixBlock -} - -// PbBlindedBellatrixBlock is a stub. -func (altairSignedBeaconBlock) PbBlindedBellatrixBlock() (*eth.SignedBlindedBeaconBlockBellatrix, error) { - return nil, ErrUnsupportedBlindedBellatrixBlock -} - -func (altairSignedBeaconBlock) ToBlinded() (interfaces.SignedBeaconBlock, error) { - return nil, ErrUnsupportedVersion -} - -// Version of the underlying protobuf object. -func (altairSignedBeaconBlock) Version() int { - return version.Altair -} - -func (w altairSignedBeaconBlock) Header() (*eth.SignedBeaconBlockHeader, error) { - root, err := w.b.Block.Body.HashTreeRoot() - if err != nil { - return nil, errors.Wrapf(err, "could not hash block") - } - - return ð.SignedBeaconBlockHeader{ - Header: ð.BeaconBlockHeader{ - Slot: w.b.Block.Slot, - ProposerIndex: w.b.Block.ProposerIndex, - ParentRoot: w.b.Block.ParentRoot, - StateRoot: w.b.Block.StateRoot, - BodyRoot: root[:], - }, - Signature: w.Signature(), - }, nil -} - -// altairBeaconBlock is the wrapper for the actual block. -type altairBeaconBlock struct { - b *eth.BeaconBlockAltair -} - -// wrappedAltairBeaconBlock is constructor which wraps a protobuf altair object -// with the block wrapper. -func wrappedAltairBeaconBlock(b *eth.BeaconBlockAltair) (interfaces.BeaconBlock, error) { - w := altairBeaconBlock{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// Slot returns the respective slot of the block. -func (w altairBeaconBlock) Slot() types.Slot { - return w.b.Slot -} - -// ProposerIndex returns the proposer index of the beacon block. -func (w altairBeaconBlock) ProposerIndex() types.ValidatorIndex { - return w.b.ProposerIndex -} - -// ParentRoot returns the parent root of beacon block. -func (w altairBeaconBlock) ParentRoot() []byte { - return w.b.ParentRoot -} - -// StateRoot returns the state root of the beacon block. -func (w altairBeaconBlock) StateRoot() []byte { - return w.b.StateRoot -} - -// Body returns the underlying block body. -func (w altairBeaconBlock) Body() interfaces.BeaconBlockBody { - return altairBeaconBlockBody{b: w.b.Body} -} - -// IsNil checks if the beacon block is nil. -func (w altairBeaconBlock) IsNil() bool { - return w.b == nil -} - -// IsBlinded checks if the beacon block is a blinded block. -func (altairBeaconBlock) IsBlinded() bool { - return false -} - -// HashTreeRoot returns the ssz root of the block. -func (w altairBeaconBlock) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// HashTreeRootWith ssz hashes the BeaconBlock object with a hasher. -func (w altairBeaconBlock) HashTreeRootWith(hh *ssz.Hasher) error { - return w.b.HashTreeRootWith(hh) -} - -// MarshalSSZ marshals the block into its respective -// ssz form. -func (w altairBeaconBlock) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the beacon block's ssz -// form to the provided byte buffer. -func (w altairBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized block. -func (w altairBeaconBlock) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the beacon block from its relevant ssz -// form. -func (w altairBeaconBlock) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the underlying block object in its -// proto form. -func (w altairBeaconBlock) Proto() (proto.Message, error) { - return w.b, nil -} - -// Version of the underlying protobuf object. -func (altairBeaconBlock) Version() int { - return version.Altair -} - -// AsSignRequestObject returns the underlying sign request object. -func (w altairBeaconBlock) AsSignRequestObject() (validatorpb.SignRequestObject, error) { - return &validatorpb.SignRequest_BlockV2{ - BlockV2: w.b, - }, nil -} - -// altairBeaconBlockBody is a wrapper of a beacon block body. -type altairBeaconBlockBody struct { - b *eth.BeaconBlockBodyAltair -} - -// wrappedAltairBeaconBlockBody is constructor which wraps a protobuf altair object -// with the block wrapper. -func wrappedAltairBeaconBlockBody(b *eth.BeaconBlockBodyAltair) (interfaces.BeaconBlockBody, error) { - w := altairBeaconBlockBody{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// RandaoReveal returns the randao reveal from the block body. -func (w altairBeaconBlockBody) RandaoReveal() []byte { - return w.b.RandaoReveal -} - -// Eth1Data returns the eth1 data in the block. -func (w altairBeaconBlockBody) Eth1Data() *eth.Eth1Data { - return w.b.Eth1Data -} - -// Graffiti returns the graffiti in the block. -func (w altairBeaconBlockBody) Graffiti() []byte { - return w.b.Graffiti -} - -// ProposerSlashings returns the proposer slashings in the block. -func (w altairBeaconBlockBody) ProposerSlashings() []*eth.ProposerSlashing { - return w.b.ProposerSlashings -} - -// AttesterSlashings returns the attester slashings in the block. -func (w altairBeaconBlockBody) AttesterSlashings() []*eth.AttesterSlashing { - return w.b.AttesterSlashings -} - -// Attestations returns the stored attestations in the block. -func (w altairBeaconBlockBody) Attestations() []*eth.Attestation { - return w.b.Attestations -} - -// Deposits returns the stored deposits in the block. -func (w altairBeaconBlockBody) Deposits() []*eth.Deposit { - return w.b.Deposits -} - -// VoluntaryExits returns the voluntary exits in the block. -func (w altairBeaconBlockBody) VoluntaryExits() []*eth.SignedVoluntaryExit { - return w.b.VoluntaryExits -} - -// SyncAggregate returns the sync aggregate in the block. -func (w altairBeaconBlockBody) SyncAggregate() (*eth.SyncAggregate, error) { - return w.b.SyncAggregate, nil -} - -// IsNil checks if the block body is nil. -func (w altairBeaconBlockBody) IsNil() bool { - return w.b == nil -} - -// HashTreeRoot returns the ssz root of the block body. -func (w altairBeaconBlockBody) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// Proto returns the underlying proto form of the block -// body. -func (w altairBeaconBlockBody) Proto() (proto.Message, error) { - return w.b, nil -} - -// Execution is a stub. -func (w altairBeaconBlockBody) Execution() (interfaces.ExecutionData, error) { - return nil, errors.Wrapf(ErrUnsupportedField, "ExecutionPayload for %T", w) -} diff --git a/consensus-types/wrapper/beacon_block_altair_test.go b/consensus-types/wrapper/beacon_block_altair_test.go deleted file mode 100644 index e4337a4e99..0000000000 --- a/consensus-types/wrapper/beacon_block_altair_test.go +++ /dev/null @@ -1,367 +0,0 @@ -package wrapper_test - -import ( - "bytes" - "testing" - - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - "github.com/prysmaticlabs/prysm/consensus-types/wrapper" - ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" - "github.com/prysmaticlabs/prysm/testing/util" -) - -func TestAltairSignedBeaconBlock_Signature(t *testing.T) { - sig := []byte{0x11, 0x22} - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: ðpb.BeaconBlockAltair{}, Signature: sig}) - require.NoError(t, err) - - if !bytes.Equal(sig, wsb.Signature()) { - t.Error("Wrong signature returned") - } -} - -func TestAltairSignedBeaconBlock_Block(t *testing.T) { - blk := ðpb.BeaconBlockAltair{Slot: 54} - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: blk}) - require.NoError(t, err) - - pb, err := wsb.Block().Proto() - require.NoError(t, err) - assert.DeepEqual(t, blk, pb) -} - -func TestAltairSignedBeaconBlock_IsNil(t *testing.T) { - _, err := wrapper.WrappedSignedBeaconBlock(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: ðpb.BeaconBlockAltair{}}) - require.NoError(t, err) - - assert.Equal(t, false, wsb.IsNil()) -} - -func TestAltairSignedBeaconBlock_Copy(t *testing.T) { - t.Skip("TODO: Missing mutation evaluation helpers") -} - -func TestAltairSignedBeaconBlock_Proto(t *testing.T) { - sb := ðpb.SignedBeaconBlockAltair{ - Block: ðpb.BeaconBlockAltair{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - pb, err := wsb.Proto() - require.NoError(t, err) - assert.Equal(t, sb, pb) -} - -func TestAltairSignedBeaconBlock_PbPhase0Block(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: ðpb.BeaconBlockAltair{}}) - require.NoError(t, err) - - if _, err := wsb.PbPhase0Block(); err != wrapper.ErrUnsupportedPhase0Block { - t.Errorf("Wrong error returned. Want %v got %v", wrapper.ErrUnsupportedPhase0Block, err) - } -} - -func TestAltairSignedBeaconBlock_PbAltairBlock(t *testing.T) { - sb := ðpb.SignedBeaconBlockAltair{ - Block: ðpb.BeaconBlockAltair{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - got, err := wsb.PbAltairBlock() - assert.NoError(t, err) - assert.Equal(t, sb, got) -} - -func TestAltairSignedBeaconBlock_MarshalSSZTo(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(util.HydrateSignedBeaconBlockAltair(ðpb.SignedBeaconBlockAltair{})) - assert.NoError(t, err) - - var b []byte - b, err = wsb.MarshalSSZTo(b) - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) -} - -func TestAltairSignedBeaconBlock_SSZ(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(util.HydrateSignedBeaconBlockAltair(ðpb.SignedBeaconBlockAltair{})) - assert.NoError(t, err) - - b, err := wsb.MarshalSSZ() - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - - assert.NotEqual(t, 0, wsb.SizeSSZ()) - - assert.NoError(t, wsb.UnmarshalSSZ(b)) -} - -func TestAltairSignedBeaconBlock_Version(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockAltair{Block: ðpb.BeaconBlockAltair{}}) - require.NoError(t, err) - - assert.Equal(t, version.Altair, wsb.Version()) -} - -func TestAltairBeaconBlock_Slot(t *testing.T) { - slot := types.Slot(546) - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{Slot: slot}) - require.NoError(t, err) - - assert.Equal(t, slot, wb.Slot()) -} - -func TestAltairBeaconBlock_ProposerIndex(t *testing.T) { - pi := types.ValidatorIndex(555) - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{ProposerIndex: pi}) - require.NoError(t, err) - - assert.Equal(t, pi, wb.ProposerIndex()) -} - -func TestAltairBeaconBlock_ParentRoot(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{ParentRoot: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wb.ParentRoot()) -} - -func TestAltairBeaconBlock_StateRoot(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{StateRoot: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wb.StateRoot()) -} - -func TestAltairBeaconBlock_Body(t *testing.T) { - body := ðpb.BeaconBlockBodyAltair{Graffiti: []byte{0x44}} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{Body: body}) - require.NoError(t, err) - - pb, err := wb.Body().Proto() - require.NoError(t, err) - assert.Equal(t, body, pb) -} - -func TestAltairBeaconBlock_IsNil(t *testing.T) { - _, err := wrapper.WrappedBeaconBlock(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{}) - require.NoError(t, err) - - assert.Equal(t, false, wb.IsNil()) -} - -func TestAltairBeaconBlock_IsBlinded(t *testing.T) { - wsb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{}) - require.NoError(t, err) - require.Equal(t, false, wsb.IsNil()) -} - -func TestAltairBeaconBlock_HashTreeRoot(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(util.HydrateBeaconBlockAltair(ðpb.BeaconBlockAltair{})) - require.NoError(t, err) - - rt, err := wb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) -} - -func TestAltairBeaconBlock_Proto(t *testing.T) { - blk := ðpb.BeaconBlockAltair{ProposerIndex: 234} - wb, err := wrapper.WrappedBeaconBlock(blk) - require.NoError(t, err) - - pb, err := wb.Proto() - require.NoError(t, err) - assert.Equal(t, blk, pb) -} - -func TestAltairBeaconBlock_SSZ(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(util.HydrateBeaconBlockAltair(ðpb.BeaconBlockAltair{})) - assert.NoError(t, err) - - b, err := wb.MarshalSSZ() - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - - assert.NotEqual(t, 0, wb.SizeSSZ()) - - assert.NoError(t, wb.UnmarshalSSZ(b)) -} - -func TestAltairBeaconBlock_Version(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockAltair{}) - require.NoError(t, err) - - assert.Equal(t, version.Altair, wb.Version()) -} - -func TestAltairBeaconBlockBody_RandaoReveal(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wbb, err := wrapper.WrappedBeaconBlockBody(ðpb.BeaconBlockBodyAltair{RandaoReveal: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wbb.RandaoReveal()) -} - -func TestAltairBeaconBlockBody_Eth1Data(t *testing.T) { - data := ðpb.Eth1Data{} - body := ðpb.BeaconBlockBodyAltair{ - Eth1Data: data, - } - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - assert.Equal(t, data, wbb.Eth1Data()) -} - -func TestAltairBeaconBlockBody_Graffiti(t *testing.T) { - graffiti := []byte{0x66, 0xAA} - body := ðpb.BeaconBlockBodyAltair{Graffiti: graffiti} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, graffiti, wbb.Graffiti()) -} - -func TestAltairBeaconBlockBody_ProposerSlashings(t *testing.T) { - ps := []*ethpb.ProposerSlashing{ - {Header_1: ðpb.SignedBeaconBlockHeader{ - Signature: []byte{0x11, 0x20}, - }}, - } - body := ðpb.BeaconBlockBodyAltair{ProposerSlashings: ps} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, ps, wbb.ProposerSlashings()) -} - -func TestAltairBeaconBlockBody_AttesterSlashings(t *testing.T) { - as := []*ethpb.AttesterSlashing{ - {Attestation_1: ðpb.IndexedAttestation{Signature: []byte{0x11}}}, - } - body := ðpb.BeaconBlockBodyAltair{AttesterSlashings: as} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, as, wbb.AttesterSlashings()) -} - -func TestAltairBeaconBlockBody_Attestations(t *testing.T) { - atts := []*ethpb.Attestation{{Signature: []byte{0x88}}} - - body := ðpb.BeaconBlockBodyAltair{Attestations: atts} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, atts, wbb.Attestations()) -} - -func TestAltairBeaconBlockBody_Deposits(t *testing.T) { - deposits := []*ethpb.Deposit{ - {Proof: [][]byte{{0x54, 0x10}}}, - } - body := ðpb.BeaconBlockBodyAltair{Deposits: deposits} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, deposits, wbb.Deposits()) -} - -func TestAltairBeaconBlockBody_VoluntaryExits(t *testing.T) { - exits := []*ethpb.SignedVoluntaryExit{ - {Exit: ðpb.VoluntaryExit{Epoch: 54}}, - } - body := ðpb.BeaconBlockBodyAltair{VoluntaryExits: exits} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, exits, wbb.VoluntaryExits()) -} - -func TestAltairBeaconBlockBody_IsNil(t *testing.T) { - _, err := wrapper.WrappedBeaconBlockBody(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wbb, err := wrapper.WrappedBeaconBlockBody(ðpb.BeaconBlockBodyAltair{}) - require.NoError(t, err) - assert.Equal(t, false, wbb.IsNil()) - -} - -func TestAltairBeaconBlockBody_HashTreeRoot(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlockBody(util.HydrateBeaconBlockBodyAltair(ðpb.BeaconBlockBodyAltair{})) - assert.NoError(t, err) - - rt, err := wb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) -} - -func TestAltairBeaconBlockBody_Proto(t *testing.T) { - body := ðpb.BeaconBlockBodyAltair{Graffiti: []byte{0x66, 0xAA}} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - pb, err := wbb.Proto() - require.NoError(t, err) - assert.Equal(t, body, pb) -} - -func TestAltairBeaconBlock_PbGenericBlock(t *testing.T) { - abb := ðpb.SignedBeaconBlockAltair{ - Block: util.HydrateBeaconBlockAltair(ðpb.BeaconBlockAltair{}), - } - wsb, err := wrapper.WrappedSignedBeaconBlock(abb) - require.NoError(t, err) - - got, err := wsb.PbGenericBlock() - require.NoError(t, err) - assert.Equal(t, abb, got.GetAltair()) -} - -func TestAltairBeaconBlock_AsSignRequestObject(t *testing.T) { - abb := util.HydrateBeaconBlockAltair(ðpb.BeaconBlockAltair{}) - wsb, err := wrapper.WrappedBeaconBlock(abb) - require.NoError(t, err) - - sro, err := wsb.AsSignRequestObject() - require.NoError(t, err) - got, ok := sro.(*validatorpb.SignRequest_BlockV2) - require.Equal(t, true, ok, "Not a SignRequest_BlockV2") - assert.Equal(t, abb, got.BlockV2) -} - -func TestAltairBeaconBlock_PbBlindedBellatrixBlock(t *testing.T) { - sb := ðpb.SignedBeaconBlockAltair{ - Block: ðpb.BeaconBlockAltair{Slot: 66}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - _, err = wsb.PbBlindedBellatrixBlock() - require.ErrorContains(t, "unsupported blinded bellatrix block", err) -} - -func TestAltairBeaconBlock_ExecutionPayloadHeader(t *testing.T) { - sb := ðpb.SignedBeaconBlockAltair{ - Block: ðpb.BeaconBlockAltair{Slot: 66}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - _, err = wsb.Block().Body().Execution() - require.ErrorContains(t, "unsupported field for block type", err) -} diff --git a/consensus-types/wrapper/beacon_block_bellatrix.go b/consensus-types/wrapper/beacon_block_bellatrix.go deleted file mode 100644 index 84c42f1c8a..0000000000 --- a/consensus-types/wrapper/beacon_block_bellatrix.go +++ /dev/null @@ -1,351 +0,0 @@ -package wrapper - -import ( - "github.com/pkg/errors" - ssz "github.com/prysmaticlabs/fastssz" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "google.golang.org/protobuf/proto" -) - -var ( - _ = interfaces.SignedBeaconBlock(&bellatrixSignedBeaconBlock{}) - _ = interfaces.BeaconBlock(&bellatrixBeaconBlock{}) - _ = interfaces.BeaconBlockBody(&bellatrixBeaconBlockBody{}) -) - -// bellatrixSignedBeaconBlock is a convenience wrapper around a Bellatrix blinded beacon block -// object. 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. -type bellatrixSignedBeaconBlock struct { - b *eth.SignedBeaconBlockBellatrix -} - -// wrappedBellatrixSignedBeaconBlock is a constructor which wraps a protobuf Bellatrix block with the block wrapper. -func wrappedBellatrixSignedBeaconBlock(b *eth.SignedBeaconBlockBellatrix) (interfaces.SignedBeaconBlock, error) { - w := bellatrixSignedBeaconBlock{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// Signature returns the respective block signature. -func (w bellatrixSignedBeaconBlock) Signature() []byte { - return w.b.Signature -} - -// Block returns the underlying beacon block object. -func (w bellatrixSignedBeaconBlock) Block() interfaces.BeaconBlock { - return bellatrixBeaconBlock{b: w.b.Block} -} - -// IsNil checks if the underlying beacon block is nil. -func (w bellatrixSignedBeaconBlock) IsNil() bool { - return w.b == nil || w.b.Block == nil -} - -// Copy performs a deep copy of the signed beacon block object. -func (w bellatrixSignedBeaconBlock) Copy() (interfaces.SignedBeaconBlock, error) { - return bellatrixSignedBeaconBlock{b: eth.CopySignedBeaconBlockBellatrix(w.b)}, nil -} - -// MarshalSSZ marshals the signed beacon block to its relevant ssz form. -func (w bellatrixSignedBeaconBlock) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the signed beacon block's ssz -// form to the provided byte buffer. -func (w bellatrixSignedBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized signed block -func (w bellatrixSignedBeaconBlock) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the signed beacon block from its relevant ssz -// form. -func (w bellatrixSignedBeaconBlock) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the block in its underlying protobuf interface. -func (w bellatrixSignedBeaconBlock) Proto() (proto.Message, error) { - return w.b, nil -} - -// PbGenericBlock returns a generic signed beacon block. -func (w bellatrixSignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, error) { - return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_Bellatrix{Bellatrix: w.b}, - }, nil -} - -// PbBellatrixBlock returns the underlying protobuf object. -func (w bellatrixSignedBeaconBlock) PbBellatrixBlock() (*eth.SignedBeaconBlockBellatrix, error) { - return w.b, nil -} - -// PbBlindedBellatrixBlock is a stub. -func (bellatrixSignedBeaconBlock) PbBlindedBellatrixBlock() (*eth.SignedBlindedBeaconBlockBellatrix, error) { - return nil, ErrUnsupportedBlindedBellatrixBlock -} - -// PbPhase0Block is a stub. -func (bellatrixSignedBeaconBlock) PbPhase0Block() (*eth.SignedBeaconBlock, error) { - return nil, ErrUnsupportedPhase0Block -} - -// PbAltairBlock returns the underlying protobuf object. -func (bellatrixSignedBeaconBlock) PbAltairBlock() (*eth.SignedBeaconBlockAltair, error) { - return nil, ErrUnsupportedAltairBlock -} - -func (w bellatrixSignedBeaconBlock) ToBlinded() (interfaces.SignedBeaconBlock, error) { - if w.Block().IsNil() { - return nil, errors.New("cannot convert nil block to blinded format") - } - payload := w.b.Block.Body.ExecutionPayload - wrappedPayload, err := WrappedExecutionPayload(payload) - if err != nil { - return nil, err - } - header, err := PayloadToHeader(wrappedPayload) - if err != nil { - return nil, err - } - return signedBlindedBeaconBlockBellatrix{ - b: ð.SignedBlindedBeaconBlockBellatrix{ - Block: ð.BlindedBeaconBlockBellatrix{ - Slot: w.b.Block.Slot, - ProposerIndex: w.b.Block.ProposerIndex, - ParentRoot: w.b.Block.ParentRoot, - StateRoot: w.b.Block.StateRoot, - Body: ð.BlindedBeaconBlockBodyBellatrix{ - RandaoReveal: w.b.Block.Body.RandaoReveal, - Eth1Data: w.b.Block.Body.Eth1Data, - Graffiti: w.b.Block.Body.Graffiti, - ProposerSlashings: w.b.Block.Body.ProposerSlashings, - AttesterSlashings: w.b.Block.Body.AttesterSlashings, - Attestations: w.b.Block.Body.Attestations, - Deposits: w.b.Block.Body.Deposits, - VoluntaryExits: w.b.Block.Body.VoluntaryExits, - SyncAggregate: w.b.Block.Body.SyncAggregate, - ExecutionPayloadHeader: header, - }, - }, - Signature: w.b.Signature, - }, - }, nil -} - -// Version of the underlying protobuf object. -func (bellatrixSignedBeaconBlock) Version() int { - return version.Bellatrix -} - -func (w bellatrixSignedBeaconBlock) Header() (*eth.SignedBeaconBlockHeader, error) { - root, err := w.b.Block.Body.HashTreeRoot() - if err != nil { - return nil, errors.Wrapf(err, "could not hash block") - } - - return ð.SignedBeaconBlockHeader{ - Header: ð.BeaconBlockHeader{ - Slot: w.b.Block.Slot, - ProposerIndex: w.b.Block.ProposerIndex, - ParentRoot: w.b.Block.ParentRoot, - StateRoot: w.b.Block.StateRoot, - BodyRoot: root[:], - }, - Signature: w.Signature(), - }, nil -} - -// bellatrixBeaconBlock is the wrapper for the actual block. -type bellatrixBeaconBlock struct { - b *eth.BeaconBlockBellatrix -} - -// wrappedBellatrixBeaconBlock is a constructor which wraps a protobuf Bellatrix object -// with the block wrapper. -func wrappedBellatrixBeaconBlock(b *eth.BeaconBlockBellatrix) (interfaces.BeaconBlock, error) { - w := bellatrixBeaconBlock{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// Slot returns the respective slot of the block. -func (w bellatrixBeaconBlock) Slot() types.Slot { - return w.b.Slot -} - -// ProposerIndex returns the proposer index of the beacon block. -func (w bellatrixBeaconBlock) ProposerIndex() types.ValidatorIndex { - return w.b.ProposerIndex -} - -// ParentRoot returns the parent root of beacon block. -func (w bellatrixBeaconBlock) ParentRoot() []byte { - return w.b.ParentRoot -} - -// StateRoot returns the state root of the beacon block. -func (w bellatrixBeaconBlock) StateRoot() []byte { - return w.b.StateRoot -} - -// Body returns the underlying block body. -func (w bellatrixBeaconBlock) Body() interfaces.BeaconBlockBody { - return bellatrixBeaconBlockBody{b: w.b.Body} -} - -// IsNil checks if the beacon block is nil. -func (w bellatrixBeaconBlock) IsNil() bool { - return w.b == nil -} - -// IsBlinded checks if the beacon block is a blinded block. -func (bellatrixBeaconBlock) IsBlinded() bool { - return false -} - -// HashTreeRoot returns the ssz root of the block. -func (w bellatrixBeaconBlock) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// HashTreeRootWith ssz hashes the BeaconBlock object with a hasher. -func (w bellatrixBeaconBlock) HashTreeRootWith(hh *ssz.Hasher) error { - return w.b.HashTreeRootWith(hh) -} - -// MarshalSSZ marshals the block into its respective -// ssz form. -func (w bellatrixBeaconBlock) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the beacon block's ssz -// form to the provided byte buffer. -func (w bellatrixBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized block. -func (w bellatrixBeaconBlock) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the beacon block from its relevant ssz -// form. -func (w bellatrixBeaconBlock) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the underlying block object in its -// proto form. -func (w bellatrixBeaconBlock) Proto() (proto.Message, error) { - return w.b, nil -} - -// Version of the underlying protobuf object. -func (bellatrixBeaconBlock) Version() int { - return version.Bellatrix -} - -// AsSignRequestObject returns the underlying sign request object. -func (w bellatrixBeaconBlock) AsSignRequestObject() (validatorpb.SignRequestObject, error) { - return &validatorpb.SignRequest_BlockV3{ - BlockV3: w.b, - }, nil -} - -// bellatrixBeaconBlockBody is a wrapper of a beacon block body. -type bellatrixBeaconBlockBody struct { - b *eth.BeaconBlockBodyBellatrix -} - -// wrappedBellatrixBeaconBlockBody is a constructor which wraps a protobuf bellatrix object -// with the block wrapper. -func wrappedBellatrixBeaconBlockBody(b *eth.BeaconBlockBodyBellatrix) (interfaces.BeaconBlockBody, error) { - w := bellatrixBeaconBlockBody{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// RandaoReveal returns the randao reveal from the block body. -func (w bellatrixBeaconBlockBody) RandaoReveal() []byte { - return w.b.RandaoReveal -} - -// Eth1Data returns the eth1 data in the block. -func (w bellatrixBeaconBlockBody) Eth1Data() *eth.Eth1Data { - return w.b.Eth1Data -} - -// Graffiti returns the graffiti in the block. -func (w bellatrixBeaconBlockBody) Graffiti() []byte { - return w.b.Graffiti -} - -// ProposerSlashings returns the proposer slashings in the block. -func (w bellatrixBeaconBlockBody) ProposerSlashings() []*eth.ProposerSlashing { - return w.b.ProposerSlashings -} - -// AttesterSlashings returns the attester slashings in the block. -func (w bellatrixBeaconBlockBody) AttesterSlashings() []*eth.AttesterSlashing { - return w.b.AttesterSlashings -} - -// Attestations returns the stored attestations in the block. -func (w bellatrixBeaconBlockBody) Attestations() []*eth.Attestation { - return w.b.Attestations -} - -// Deposits returns the stored deposits in the block. -func (w bellatrixBeaconBlockBody) Deposits() []*eth.Deposit { - return w.b.Deposits -} - -// VoluntaryExits returns the voluntary exits in the block. -func (w bellatrixBeaconBlockBody) VoluntaryExits() []*eth.SignedVoluntaryExit { - return w.b.VoluntaryExits -} - -// SyncAggregate returns the sync aggregate in the block. -func (w bellatrixBeaconBlockBody) SyncAggregate() (*eth.SyncAggregate, error) { - return w.b.SyncAggregate, nil -} - -// IsNil checks if the block body is nil. -func (w bellatrixBeaconBlockBody) IsNil() bool { - return w.b == nil -} - -// HashTreeRoot returns the ssz root of the block body. -func (w bellatrixBeaconBlockBody) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// Proto returns the underlying proto form of the block -// body. -func (w bellatrixBeaconBlockBody) Proto() (proto.Message, error) { - return w.b, nil -} - -// Execution returns the Execution payload of the block body. -func (w bellatrixBeaconBlockBody) Execution() (interfaces.ExecutionData, error) { - return WrappedExecutionPayload(w.b.ExecutionPayload) -} diff --git a/consensus-types/wrapper/beacon_block_bellatrix_test.go b/consensus-types/wrapper/beacon_block_bellatrix_test.go deleted file mode 100644 index 76aa6fe1fc..0000000000 --- a/consensus-types/wrapper/beacon_block_bellatrix_test.go +++ /dev/null @@ -1,414 +0,0 @@ -package wrapper_test - -import ( - "bytes" - "testing" - - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - "github.com/prysmaticlabs/prysm/consensus-types/wrapper" - "github.com/prysmaticlabs/prysm/encoding/bytesutil" - enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" - ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" - "github.com/prysmaticlabs/prysm/testing/util" -) - -func TestBellatrixSignedBeaconBlock_Header(t *testing.T) { - root := bytesutil.PadTo([]byte("root"), 32) - signature := bytesutil.PadTo([]byte("sig"), 96) - body := ðpb.BeaconBlockBodyBellatrix{} - body = util.HydrateBeaconBlockBodyBellatrix(body) - bodyRoot, err := body.HashTreeRoot() - require.NoError(t, err) - block := ðpb.SignedBeaconBlockBellatrix{ - Block: ðpb.BeaconBlockBellatrix{ - Slot: 1, - ProposerIndex: 1, - ParentRoot: root, - StateRoot: root, - Body: body, - }, - Signature: signature, - } - wrapped, err := wrapper.WrappedSignedBeaconBlock(block) - require.NoError(t, err) - - header, err := wrapped.Header() - require.NoError(t, err) - assert.Equal(t, types.ValidatorIndex(1), header.Header.ProposerIndex) - assert.Equal(t, types.Slot(1), header.Header.Slot) - assert.DeepEqual(t, bodyRoot[:], header.Header.BodyRoot) - assert.DeepEqual(t, root, header.Header.StateRoot) - assert.DeepEqual(t, root, header.Header.ParentRoot) - assert.DeepEqual(t, signature, header.Signature) -} - -func TestBellatrixSignedBeaconBlock_Signature(t *testing.T) { - sig := []byte{0x11, 0x22} - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: ðpb.BeaconBlockBellatrix{}, Signature: sig}) - require.NoError(t, err) - - if !bytes.Equal(sig, wsb.Signature()) { - t.Error("Wrong signature returned") - } -} - -func TestBellatrixSignedBeaconBlock_Block(t *testing.T) { - blk := ðpb.BeaconBlockBellatrix{Slot: 54} - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: blk}) - require.NoError(t, err) - - pb, err := wsb.Block().Proto() - require.NoError(t, err) - assert.DeepEqual(t, blk, pb) -} - -func TestBellatrixSignedBeaconBlock_IsNil(t *testing.T) { - _, err := wrapper.WrappedSignedBeaconBlock(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: ðpb.BeaconBlockBellatrix{}}) - require.NoError(t, err) - - assert.Equal(t, false, wsb.IsNil()) -} - -func TestBellatrixSignedBeaconBlock_Copy(t *testing.T) { - t.Skip("TODO: Missing mutation evaluation helpers") -} - -func TestBellatrixSignedBeaconBlock_Proto(t *testing.T) { - sb := ðpb.SignedBeaconBlockBellatrix{ - Block: ðpb.BeaconBlockBellatrix{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - pb, err := wsb.Proto() - require.NoError(t, err) - assert.Equal(t, sb, pb) -} - -func TestBellatrixSignedBeaconBlock_PbPhase0Block(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: ðpb.BeaconBlockBellatrix{}}) - require.NoError(t, err) - - if _, err := wsb.PbPhase0Block(); err != wrapper.ErrUnsupportedPhase0Block { - t.Errorf("Wrong error returned. Want %v got %v", wrapper.ErrUnsupportedPhase0Block, err) - } -} - -func TestBellatrixSignedBeaconBlock_PbBellatrixBlock(t *testing.T) { - sb := ðpb.SignedBeaconBlockBellatrix{ - Block: ðpb.BeaconBlockBellatrix{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - got, err := wsb.PbBellatrixBlock() - assert.NoError(t, err) - assert.Equal(t, sb, got) -} - -func TestBellatrixSignedBeaconBlock_MarshalSSZTo(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(util.HydrateSignedBeaconBlockBellatrix(ðpb.SignedBeaconBlockBellatrix{})) - assert.NoError(t, err) - - var b []byte - b, err = wsb.MarshalSSZTo(b) - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) -} - -func TestBellatrixSignedBeaconBlock_SSZ(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(util.HydrateSignedBeaconBlockBellatrix(ðpb.SignedBeaconBlockBellatrix{})) - assert.NoError(t, err) - - b, err := wsb.MarshalSSZ() - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - - assert.NotEqual(t, 0, wsb.SizeSSZ()) - - assert.NoError(t, wsb.UnmarshalSSZ(b)) -} - -func TestBellatrixSignedBeaconBlock_Version(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBeaconBlockBellatrix{Block: ðpb.BeaconBlockBellatrix{}}) - require.NoError(t, err) - - assert.Equal(t, version.Bellatrix, wsb.Version()) -} - -func TestBellatrixBeaconBlock_Slot(t *testing.T) { - slot := types.Slot(546) - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{Slot: slot}) - require.NoError(t, err) - - assert.Equal(t, slot, wb.Slot()) -} - -func TestBellatrixBeaconBlock_ProposerIndex(t *testing.T) { - pi := types.ValidatorIndex(555) - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{ProposerIndex: pi}) - require.NoError(t, err) - - assert.Equal(t, pi, wb.ProposerIndex()) -} - -func TestBellatrixBeaconBlock_ParentRoot(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{ParentRoot: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wb.ParentRoot()) -} - -func TestBellatrixBeaconBlock_StateRoot(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{StateRoot: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wb.StateRoot()) -} - -func TestBellatrixBeaconBlock_Body(t *testing.T) { - body := ðpb.BeaconBlockBodyBellatrix{Graffiti: []byte{0x44}} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{Body: body}) - require.NoError(t, err) - - pb, err := wb.Body().Proto() - require.NoError(t, err) - assert.Equal(t, body, pb) -} - -func TestBellatrixBeaconBlock_IsNil(t *testing.T) { - _, err := wrapper.WrappedBeaconBlock(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{}) - require.NoError(t, err) - - assert.Equal(t, false, wb.IsNil()) -} - -func TestBellatrixBeaconBlock_IsBlinded(t *testing.T) { - wsb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{}) - require.NoError(t, err) - require.Equal(t, false, wsb.IsNil()) -} - -func TestBellatrixBeaconBlock_HashTreeRoot(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(util.HydrateBeaconBlockBellatrix(ðpb.BeaconBlockBellatrix{})) - require.NoError(t, err) - - rt, err := wb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) -} - -func TestBellatrixBeaconBlock_Proto(t *testing.T) { - blk := ðpb.BeaconBlockBellatrix{ProposerIndex: 234} - wb, err := wrapper.WrappedBeaconBlock(blk) - require.NoError(t, err) - - pb, err := wb.Proto() - require.NoError(t, err) - assert.Equal(t, blk, pb) -} - -func TestBellatrixBeaconBlock_SSZ(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(util.HydrateBeaconBlockBellatrix(ðpb.BeaconBlockBellatrix{})) - assert.NoError(t, err) - - b, err := wb.MarshalSSZ() - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - - assert.NotEqual(t, 0, wb.SizeSSZ()) - - assert.NoError(t, wb.UnmarshalSSZ(b)) -} - -func TestBellatrixBeaconBlock_Version(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(ðpb.BeaconBlockBellatrix{}) - require.NoError(t, err) - - assert.Equal(t, version.Bellatrix, wb.Version()) -} - -func TestBellatrixBeaconBlockBody_RandaoReveal(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wbb, err := wrapper.WrappedBeaconBlockBody(ðpb.BeaconBlockBodyBellatrix{RandaoReveal: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wbb.RandaoReveal()) -} - -func TestBellatrixBeaconBlockBody_Eth1Data(t *testing.T) { - data := ðpb.Eth1Data{} - body := ðpb.BeaconBlockBodyBellatrix{ - Eth1Data: data, - } - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - assert.Equal(t, data, wbb.Eth1Data()) -} - -func TestBellatrixBeaconBlockBody_Graffiti(t *testing.T) { - graffiti := []byte{0x66, 0xAA} - body := ðpb.BeaconBlockBodyBellatrix{Graffiti: graffiti} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, graffiti, wbb.Graffiti()) -} - -func TestBellatrixBeaconBlockBody_ProposerSlashings(t *testing.T) { - ps := []*ethpb.ProposerSlashing{ - {Header_1: ðpb.SignedBeaconBlockHeader{ - Signature: []byte{0x11, 0x20}, - }}, - } - body := ðpb.BeaconBlockBodyBellatrix{ProposerSlashings: ps} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, ps, wbb.ProposerSlashings()) -} - -func TestBellatrixBeaconBlockBody_AttesterSlashings(t *testing.T) { - as := []*ethpb.AttesterSlashing{ - {Attestation_1: ðpb.IndexedAttestation{Signature: []byte{0x11}}}, - } - body := ðpb.BeaconBlockBodyBellatrix{AttesterSlashings: as} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, as, wbb.AttesterSlashings()) -} - -func TestBellatrixBeaconBlockBody_Attestations(t *testing.T) { - atts := []*ethpb.Attestation{{Signature: []byte{0x88}}} - - body := ðpb.BeaconBlockBodyBellatrix{Attestations: atts} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, atts, wbb.Attestations()) -} - -func TestBellatrixBeaconBlockBody_Deposits(t *testing.T) { - deposits := []*ethpb.Deposit{ - {Proof: [][]byte{{0x54, 0x10}}}, - } - body := ðpb.BeaconBlockBodyBellatrix{Deposits: deposits} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, deposits, wbb.Deposits()) -} - -func TestBellatrixBeaconBlockBody_VoluntaryExits(t *testing.T) { - exits := []*ethpb.SignedVoluntaryExit{ - {Exit: ðpb.VoluntaryExit{Epoch: 54}}, - } - body := ðpb.BeaconBlockBodyBellatrix{VoluntaryExits: exits} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, exits, wbb.VoluntaryExits()) -} - -func TestBellatrixBeaconBlockBody_IsNil(t *testing.T) { - _, err := wrapper.WrappedBeaconBlockBody(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wbb, err := wrapper.WrappedBeaconBlockBody(ðpb.BeaconBlockBodyBellatrix{}) - require.NoError(t, err) - assert.Equal(t, false, wbb.IsNil()) - -} - -func TestBellatrixBeaconBlockBody_HashTreeRoot(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlockBody(util.HydrateBeaconBlockBodyBellatrix(ðpb.BeaconBlockBodyBellatrix{})) - assert.NoError(t, err) - - rt, err := wb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) -} - -func TestBellatrixBeaconBlockBody_Proto(t *testing.T) { - body := ðpb.BeaconBlockBodyBellatrix{Graffiti: []byte{0x66, 0xAA}} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - pb, err := wbb.Proto() - require.NoError(t, err) - assert.Equal(t, body, pb) -} - -func TestBellatrixBeaconBlockBody_ExecutionPayload(t *testing.T) { - payloads := &enginev1.ExecutionPayload{ - BlockNumber: 100, - } - body := ðpb.BeaconBlockBodyBellatrix{ExecutionPayload: payloads} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - got, err := wbb.Execution() - require.NoError(t, err) - assert.DeepEqual(t, payloads, got.Proto()) -} - -func TestBellatrixBeaconBlock_PbGenericBlock(t *testing.T) { - abb := ðpb.SignedBeaconBlockBellatrix{ - Block: util.HydrateBeaconBlockBellatrix(ðpb.BeaconBlockBellatrix{}), - } - wsb, err := wrapper.WrappedSignedBeaconBlock(abb) - require.NoError(t, err) - - got, err := wsb.PbGenericBlock() - require.NoError(t, err) - assert.Equal(t, abb, got.GetBellatrix()) -} - -func TestBellatrixBeaconBlock_AsSignRequestObject(t *testing.T) { - abb := util.HydrateBeaconBlockBellatrix(ðpb.BeaconBlockBellatrix{}) - wsb, err := wrapper.WrappedBeaconBlock(abb) - require.NoError(t, err) - - sro, err := wsb.AsSignRequestObject() - require.NoError(t, err) - got, ok := sro.(*validatorpb.SignRequest_BlockV3) - require.Equal(t, true, ok, "Not a SignRequest_BlockV3") - assert.Equal(t, abb, got.BlockV3) -} - -func TestBellatrixBeaconBlock_PbBlindedBellatrixBlock(t *testing.T) { - sb := ðpb.SignedBeaconBlockBellatrix{ - Block: ðpb.BeaconBlockBellatrix{Slot: 66}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - _, err = wsb.PbBlindedBellatrixBlock() - require.ErrorContains(t, "unsupported blinded bellatrix block", err) -} - -func TestBellatrixBeaconBlock_ExecutionPayloadHeader(t *testing.T) { - sb := ðpb.SignedBeaconBlockBellatrix{ - Block: ðpb.BeaconBlockBellatrix{Slot: 66, Body: ðpb.BeaconBlockBodyBellatrix{ - ExecutionPayload: &enginev1.ExecutionPayload{}, - }}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - _, err = wsb.Block().Body().Execution() - require.NoError(t, err) -} diff --git a/consensus-types/wrapper/beacon_block_phase0.go b/consensus-types/wrapper/beacon_block_phase0.go deleted file mode 100644 index 1106a61542..0000000000 --- a/consensus-types/wrapper/beacon_block_phase0.go +++ /dev/null @@ -1,310 +0,0 @@ -package wrapper - -import ( - "github.com/pkg/errors" - ssz "github.com/prysmaticlabs/fastssz" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "google.golang.org/protobuf/proto" -) - -var ( - _ = interfaces.SignedBeaconBlock(&Phase0SignedBeaconBlock{}) - _ = interfaces.BeaconBlock(&Phase0BeaconBlock{}) - _ = interfaces.BeaconBlockBody(&Phase0BeaconBlockBody{}) -) - -// Phase0SignedBeaconBlock is a convenience wrapper around a phase 0 beacon block -// object. 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. -type Phase0SignedBeaconBlock struct { - b *eth.SignedBeaconBlock -} - -// wrappedPhase0SignedBeaconBlock is constructor which wraps a protobuf phase 0 block -// with the block wrapper. -func wrappedPhase0SignedBeaconBlock(b *eth.SignedBeaconBlock) interfaces.SignedBeaconBlock { - return Phase0SignedBeaconBlock{b: b} -} - -// Signature returns the respective block signature. -func (w Phase0SignedBeaconBlock) Signature() []byte { - return w.b.Signature -} - -// Block returns the underlying beacon block object. -func (w Phase0SignedBeaconBlock) Block() interfaces.BeaconBlock { - return wrappedPhase0BeaconBlock(w.b.Block) -} - -// IsNil checks if the underlying beacon block is -// nil. -func (w Phase0SignedBeaconBlock) IsNil() bool { - return w.b == nil || w.Block().IsNil() -} - -// Copy performs a deep copy of the signed beacon block -// object. -func (w Phase0SignedBeaconBlock) Copy() (interfaces.SignedBeaconBlock, error) { - return wrappedPhase0SignedBeaconBlock(eth.CopySignedBeaconBlock(w.b)), nil -} - -// MarshalSSZ marshals the signed beacon block to its relevant ssz -// form. -func (w Phase0SignedBeaconBlock) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the signed beacon block's ssz -// form to the provided byte buffer. -func (w Phase0SignedBeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized signed block -func (w Phase0SignedBeaconBlock) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the signed beacon block from its relevant ssz -// form. -func (w Phase0SignedBeaconBlock) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the block in its underlying protobuf -// interface. -func (w Phase0SignedBeaconBlock) Proto() (proto.Message, error) { - return w.b, nil -} - -// PbGenericBlock returns a generic signed beacon block. -func (w Phase0SignedBeaconBlock) PbGenericBlock() (*eth.GenericSignedBeaconBlock, error) { - return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_Phase0{Phase0: w.b}, - }, nil -} - -// PbPhase0Block returns the underlying protobuf object. -func (w Phase0SignedBeaconBlock) PbPhase0Block() (*eth.SignedBeaconBlock, error) { - return w.b, nil -} - -// PbAltairBlock is a stub. -func (Phase0SignedBeaconBlock) PbAltairBlock() (*eth.SignedBeaconBlockAltair, error) { - return nil, ErrUnsupportedAltairBlock -} - -// PbBellatrixBlock is a stub. -func (Phase0SignedBeaconBlock) PbBellatrixBlock() (*eth.SignedBeaconBlockBellatrix, error) { - return nil, ErrUnsupportedBellatrixBlock -} - -// PbBlindedBellatrixBlock is a stub. -func (Phase0SignedBeaconBlock) PbBlindedBellatrixBlock() (*eth.SignedBlindedBeaconBlockBellatrix, error) { - return nil, ErrUnsupportedBlindedBellatrixBlock -} - -func (Phase0SignedBeaconBlock) ToBlinded() (interfaces.SignedBeaconBlock, error) { - return nil, ErrUnsupportedVersion -} - -// Version of the underlying protobuf object. -func (Phase0SignedBeaconBlock) Version() int { - return version.Phase0 -} - -func (w Phase0SignedBeaconBlock) Header() (*eth.SignedBeaconBlockHeader, error) { - root, err := w.b.Block.Body.HashTreeRoot() - if err != nil { - return nil, errors.Wrapf(err, "could not hash block") - } - - return ð.SignedBeaconBlockHeader{ - Header: ð.BeaconBlockHeader{ - Slot: w.b.Block.Slot, - ProposerIndex: w.b.Block.ProposerIndex, - ParentRoot: w.b.Block.ParentRoot, - StateRoot: w.b.Block.StateRoot, - BodyRoot: root[:], - }, - Signature: w.Signature(), - }, nil -} - -// Phase0BeaconBlock is the wrapper for the actual block. -type Phase0BeaconBlock struct { - b *eth.BeaconBlock -} - -// wrappedPhase0BeaconBlock is constructor which wraps a protobuf phase 0 object -// with the block wrapper. -func wrappedPhase0BeaconBlock(b *eth.BeaconBlock) interfaces.BeaconBlock { - return Phase0BeaconBlock{b: b} -} - -// Slot returns the respective slot of the block. -func (w Phase0BeaconBlock) Slot() types.Slot { - return w.b.Slot -} - -// ProposerIndex returns the proposer index of the beacon block. -func (w Phase0BeaconBlock) ProposerIndex() types.ValidatorIndex { - return w.b.ProposerIndex -} - -// ParentRoot returns the parent root of beacon block. -func (w Phase0BeaconBlock) ParentRoot() []byte { - return w.b.ParentRoot -} - -// StateRoot returns the state root of the beacon block. -func (w Phase0BeaconBlock) StateRoot() []byte { - return w.b.StateRoot -} - -// Body returns the underlying block body. -func (w Phase0BeaconBlock) Body() interfaces.BeaconBlockBody { - return wrappedPhase0BeaconBlockBody(w.b.Body) -} - -// IsNil checks if the beacon block is nil. -func (w Phase0BeaconBlock) IsNil() bool { - return w.b == nil || w.Body().IsNil() -} - -// IsBlinded checks if the beacon block is a blinded block. -func (Phase0BeaconBlock) IsBlinded() bool { - return false -} - -// HashTreeRoot returns the ssz root of the block. -func (w Phase0BeaconBlock) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// HashTreeRootWith ssz hashes the BeaconBlock object with a hasher. -func (w Phase0BeaconBlock) HashTreeRootWith(hh *ssz.Hasher) error { - return w.b.HashTreeRootWith(hh) -} - -// MarshalSSZ marshals the block into its respective -// ssz form. -func (w Phase0BeaconBlock) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the beacon block's ssz -// form to the provided byte buffer. -func (w Phase0BeaconBlock) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized block. -func (w Phase0BeaconBlock) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the beacon block from its relevant ssz -// form. -func (w Phase0BeaconBlock) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the underlying block object in its -// proto form. -func (w Phase0BeaconBlock) Proto() (proto.Message, error) { - return w.b, nil -} - -// Version of the underlying protobuf object. -func (Phase0BeaconBlock) Version() int { - return version.Phase0 -} - -// AsSignRequestObject returns the underlying sign request object. -func (w Phase0BeaconBlock) AsSignRequestObject() (validatorpb.SignRequestObject, error) { - return &validatorpb.SignRequest_Block{ - Block: w.b, - }, nil -} - -// Phase0BeaconBlockBody is a wrapper of a beacon block body. -type Phase0BeaconBlockBody struct { - b *eth.BeaconBlockBody -} - -// wrappedPhase0BeaconBlockBody is constructor which wraps a protobuf phase 0 object -// with the block wrapper. -func wrappedPhase0BeaconBlockBody(b *eth.BeaconBlockBody) interfaces.BeaconBlockBody { - return Phase0BeaconBlockBody{b: b} -} - -// RandaoReveal returns the randao reveal from the block body. -func (w Phase0BeaconBlockBody) RandaoReveal() []byte { - return w.b.RandaoReveal -} - -// Eth1Data returns the eth1 data in the block. -func (w Phase0BeaconBlockBody) Eth1Data() *eth.Eth1Data { - return w.b.Eth1Data -} - -// Graffiti returns the graffiti in the block. -func (w Phase0BeaconBlockBody) Graffiti() []byte { - return w.b.Graffiti -} - -// ProposerSlashings returns the proposer slashings in the block. -func (w Phase0BeaconBlockBody) ProposerSlashings() []*eth.ProposerSlashing { - return w.b.ProposerSlashings -} - -// AttesterSlashings returns the attester slashings in the block. -func (w Phase0BeaconBlockBody) AttesterSlashings() []*eth.AttesterSlashing { - return w.b.AttesterSlashings -} - -// Attestations returns the stored attestations in the block. -func (w Phase0BeaconBlockBody) Attestations() []*eth.Attestation { - return w.b.Attestations -} - -// Deposits returns the stored deposits in the block. -func (w Phase0BeaconBlockBody) Deposits() []*eth.Deposit { - return w.b.Deposits -} - -// VoluntaryExits returns the voluntary exits in the block. -func (w Phase0BeaconBlockBody) VoluntaryExits() []*eth.SignedVoluntaryExit { - return w.b.VoluntaryExits -} - -// SyncAggregate returns the sync aggregate in the block. -func (Phase0BeaconBlockBody) SyncAggregate() (*eth.SyncAggregate, error) { - return nil, errors.New("Sync aggregate is not supported in phase 0 block") -} - -// IsNil checks if the block body is nil. -func (w Phase0BeaconBlockBody) IsNil() bool { - return w.b == nil -} - -// HashTreeRoot returns the ssz root of the block body. -func (w Phase0BeaconBlockBody) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// Proto returns the underlying proto form of the block -// body. -func (w Phase0BeaconBlockBody) Proto() (proto.Message, error) { - return w.b, nil -} - -// Execution is a stub. -func (w Phase0BeaconBlockBody) Execution() (interfaces.ExecutionData, error) { - return nil, errors.Wrapf(ErrUnsupportedField, "ExecutionPayload for %T", w) -} diff --git a/consensus-types/wrapper/beacon_block_phase0_test.go b/consensus-types/wrapper/beacon_block_phase0_test.go deleted file mode 100644 index 7cd69f67c2..0000000000 --- a/consensus-types/wrapper/beacon_block_phase0_test.go +++ /dev/null @@ -1,88 +0,0 @@ -package wrapper_test - -import ( - "testing" - - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - "github.com/prysmaticlabs/prysm/consensus-types/wrapper" - "github.com/prysmaticlabs/prysm/encoding/bytesutil" - ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" - "github.com/prysmaticlabs/prysm/testing/util" -) - -func TestPhase0SignedBeaconBlock_Header(t *testing.T) { - root := bytesutil.PadTo([]byte("root"), 32) - signature := bytesutil.PadTo([]byte("sig"), 96) - body := ðpb.BeaconBlockBody{} - body = util.HydrateBeaconBlockBody(body) - bodyRoot, err := body.HashTreeRoot() - require.NoError(t, err) - block := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{ - Slot: 1, - ProposerIndex: 1, - ParentRoot: root, - StateRoot: root, - Body: body, - }, - Signature: signature, - } - wrapped, err := wrapper.WrappedSignedBeaconBlock(block) - require.NoError(t, err) - - header, err := wrapped.Header() - require.NoError(t, err) - assert.Equal(t, types.ValidatorIndex(1), header.Header.ProposerIndex) - assert.Equal(t, types.Slot(1), header.Header.Slot) - assert.DeepEqual(t, bodyRoot[:], header.Header.BodyRoot) - assert.DeepEqual(t, root, header.Header.StateRoot) - assert.DeepEqual(t, root, header.Header.ParentRoot) - assert.DeepEqual(t, signature, header.Signature) -} - -func TestBeaconBlock_PbGenericBlock(t *testing.T) { - abb := ðpb.SignedBeaconBlock{ - Block: util.HydrateBeaconBlock(ðpb.BeaconBlock{}), - } - wsb, err := wrapper.WrappedSignedBeaconBlock(abb) - require.NoError(t, err) - - got, err := wsb.PbGenericBlock() - require.NoError(t, err) - assert.Equal(t, abb, got.GetPhase0()) -} - -func TestBeaconBlock_AsSignRequestObject(t *testing.T) { - abb := util.HydrateBeaconBlock(ðpb.BeaconBlock{}) - wsb, err := wrapper.WrappedBeaconBlock(abb) - require.NoError(t, err) - - sro, err := wsb.AsSignRequestObject() - require.NoError(t, err) - got, ok := sro.(*validatorpb.SignRequest_Block) - require.Equal(t, true, ok, "Not a SignRequest_Block") - assert.Equal(t, abb, got.Block) -} - -func TestPhase0BeaconBlock_PbBlindedBellatrixBlock(t *testing.T) { - sb := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{Slot: 66}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - _, err = wsb.PbBlindedBellatrixBlock() - require.ErrorContains(t, "unsupported blinded bellatrix block", err) -} - -func TestPhase0BeaconBlock_ExecutionPayloadHeader(t *testing.T) { - sb := ðpb.SignedBeaconBlock{ - Block: ðpb.BeaconBlock{Slot: 66}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - _, err = wsb.Block().Body().Execution() - require.ErrorContains(t, "unsupported field for block type", err) -} diff --git a/consensus-types/wrapper/beacon_block_test.go b/consensus-types/wrapper/beacon_block_test.go deleted file mode 100644 index 60e634c0bc..0000000000 --- a/consensus-types/wrapper/beacon_block_test.go +++ /dev/null @@ -1,160 +0,0 @@ -package wrapper_test - -import ( - "testing" - - "github.com/pkg/errors" - fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" - "github.com/prysmaticlabs/prysm/consensus-types/wrapper" - enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" - "github.com/prysmaticlabs/prysm/testing/require" - "github.com/prysmaticlabs/prysm/testing/util" -) - -func TestBuildSignedBeaconBlockFromExecutionPayload(t *testing.T) { - t.Run("nil block check", func(t *testing.T) { - _, err := wrapper.BuildSignedBeaconBlockFromExecutionPayload(nil, nil) - require.ErrorIs(t, wrapper.ErrNilSignedBeaconBlock, err) - }) - t.Run("unsupported field payload header", func(t *testing.T) { - altairBlock := util.NewBeaconBlockAltair() - blk, err := wrapper.WrappedSignedBeaconBlock(altairBlock) - require.NoError(t, err) - _, err = wrapper.BuildSignedBeaconBlockFromExecutionPayload(blk, nil) - require.Equal(t, true, errors.Is(err, wrapper.ErrUnsupportedField)) - }) - t.Run("payload header root and payload root mismatch", func(t *testing.T) { - payload := &enginev1.ExecutionPayload{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, 20), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, 256), - PrevRandao: make([]byte, fieldparams.RootLength), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - Transactions: make([][]byte, 0), - } - wrapped, err := wrapper.WrappedExecutionPayload(payload) - require.NoError(t, err) - header, err := wrapper.PayloadToHeader(wrapped) - require.NoError(t, err) - blindedBlock := util.NewBlindedBeaconBlockBellatrix() - - // Modify the header. - header.GasUsed += 1 - blindedBlock.Block.Body.ExecutionPayloadHeader = header - - blk, err := wrapper.WrappedSignedBeaconBlock(blindedBlock) - require.NoError(t, err) - _, err = wrapper.BuildSignedBeaconBlockFromExecutionPayload(blk, payload) - require.ErrorContains(t, "roots do not match", err) - }) - t.Run("ok", func(t *testing.T) { - payload := &enginev1.ExecutionPayload{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, 20), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, 256), - PrevRandao: make([]byte, fieldparams.RootLength), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - Transactions: make([][]byte, 0), - } - wrapped, err := wrapper.WrappedExecutionPayload(payload) - require.NoError(t, err) - header, err := wrapper.PayloadToHeader(wrapped) - require.NoError(t, err) - blindedBlock := util.NewBlindedBeaconBlockBellatrix() - blindedBlock.Block.Body.ExecutionPayloadHeader = header - - blk, err := wrapper.WrappedSignedBeaconBlock(blindedBlock) - require.NoError(t, err) - builtBlock, err := wrapper.BuildSignedBeaconBlockFromExecutionPayload(blk, payload) - require.NoError(t, err) - - got, err := builtBlock.Block().Body().Execution() - require.NoError(t, err) - require.DeepEqual(t, payload, got.Proto()) - }) -} - -func TestWrapSignedBlindedBeaconBlock(t *testing.T) { - t.Run("nil block check", func(t *testing.T) { - _, err := wrapper.BuildSignedBeaconBlockFromExecutionPayload(nil, nil) - require.ErrorIs(t, wrapper.ErrNilSignedBeaconBlock, err) - }) - t.Run("unsupported field execution payload", func(t *testing.T) { - altairBlock := util.NewBeaconBlockAltair() - blk, err := wrapper.WrappedSignedBeaconBlock(altairBlock) - require.NoError(t, err) - _, err = wrapper.BuildSignedBeaconBlockFromExecutionPayload(blk, nil) - require.Equal(t, true, errors.Is(err, wrapper.ErrUnsupportedField)) - }) - t.Run("ok", func(t *testing.T) { - payload := &enginev1.ExecutionPayload{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, 20), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, 256), - PrevRandao: make([]byte, fieldparams.RootLength), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - Transactions: make([][]byte, 0), - } - bellatrixBlk := util.NewBeaconBlockBellatrix() - bellatrixBlk.Block.Body.ExecutionPayload = payload - - wrapped, err := wrapper.WrappedExecutionPayload(payload) - require.NoError(t, err) - want, err := wrapper.PayloadToHeader(wrapped) - require.NoError(t, err) - - blk, err := wrapper.WrappedSignedBeaconBlock(bellatrixBlk) - require.NoError(t, err) - builtBlock, err := blk.ToBlinded() - require.NoError(t, err) - - got, err := builtBlock.Block().Body().Execution() - require.NoError(t, err) - require.DeepEqual(t, want, got.Proto()) - }) -} - -func TestWrappedSignedBeaconBlock(t *testing.T) { - tests := []struct { - name string - blk interface{} - wantErr bool - }{ - { - name: "unsupported type", - blk: "not a beacon block", - wantErr: true, - }, - { - name: "phase0", - blk: util.NewBeaconBlock(), - }, - { - name: "altair", - blk: util.NewBeaconBlockAltair(), - }, - { - name: "bellatrix", - blk: util.NewBeaconBlockBellatrix(), - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - _, err := wrapper.WrappedSignedBeaconBlock(tt.blk) - if tt.wantErr { - require.ErrorIs(t, err, wrapper.ErrUnsupportedSignedBeaconBlock) - } else { - require.NoError(t, err) - } - }) - } -} diff --git a/consensus-types/wrapper/blinded_beacon_block_bellatrix.go b/consensus-types/wrapper/blinded_beacon_block_bellatrix.go deleted file mode 100644 index e8a467c3ab..0000000000 --- a/consensus-types/wrapper/blinded_beacon_block_bellatrix.go +++ /dev/null @@ -1,317 +0,0 @@ -package wrapper - -import ( - "github.com/pkg/errors" - ssz "github.com/prysmaticlabs/fastssz" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "google.golang.org/protobuf/proto" -) - -var ( - _ = interfaces.SignedBeaconBlock(&signedBlindedBeaconBlockBellatrix{}) - _ = interfaces.BeaconBlock(&blindedBeaconBlockBellatrix{}) - _ = interfaces.BeaconBlockBody(&blindedBeaconBlockBodyBellatrix{}) -) - -// signedBlindedBeaconBlockBellatrix is a convenience wrapper around a Bellatrix blinded beacon block -// object. 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. -type signedBlindedBeaconBlockBellatrix struct { - b *eth.SignedBlindedBeaconBlockBellatrix -} - -// wrappedBellatrixSignedBlindedBeaconBlock is a constructor which wraps a protobuf Bellatrix blinded block with the block wrapper. -func wrappedBellatrixSignedBlindedBeaconBlock(b *eth.SignedBlindedBeaconBlockBellatrix) (interfaces.SignedBeaconBlock, error) { - w := signedBlindedBeaconBlockBellatrix{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// Signature returns the respective block signature. -func (w signedBlindedBeaconBlockBellatrix) Signature() []byte { - return w.b.Signature -} - -// Block returns the underlying beacon block object. -func (w signedBlindedBeaconBlockBellatrix) Block() interfaces.BeaconBlock { - return blindedBeaconBlockBellatrix{b: w.b.Block} -} - -// IsNil checks if the underlying beacon block is nil. -func (w signedBlindedBeaconBlockBellatrix) IsNil() bool { - return w.b == nil || w.b.Block == nil -} - -// Copy performs a deep copy of the signed beacon block object. -func (w signedBlindedBeaconBlockBellatrix) Copy() (interfaces.SignedBeaconBlock, error) { - return signedBlindedBeaconBlockBellatrix{b: eth.CopySignedBlindedBeaconBlockBellatrix(w.b)}, nil -} - -// MarshalSSZ marshals the signed beacon block to its relevant ssz form. -func (w signedBlindedBeaconBlockBellatrix) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the signed beacon block's ssz -// form to the provided byte buffer. -func (w signedBlindedBeaconBlockBellatrix) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized signed block -func (w signedBlindedBeaconBlockBellatrix) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the signed beacon block from its relevant ssz -// form. -func (w signedBlindedBeaconBlockBellatrix) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the block in its underlying protobuf interface. -func (w signedBlindedBeaconBlockBellatrix) Proto() (proto.Message, error) { - return w.b, nil -} - -// PbGenericBlock returns a generic signed beacon block. -func (w signedBlindedBeaconBlockBellatrix) PbGenericBlock() (*eth.GenericSignedBeaconBlock, error) { - return ð.GenericSignedBeaconBlock{ - Block: ð.GenericSignedBeaconBlock_BlindedBellatrix{BlindedBellatrix: w.b}, - }, nil -} - -// PbBellatrixBlock returns the underlying protobuf object. -func (signedBlindedBeaconBlockBellatrix) PbBellatrixBlock() (*eth.SignedBeaconBlockBellatrix, error) { - return nil, ErrUnsupportedBellatrixBlock -} - -// PbBlindedBellatrixBlock returns the underlying protobuf object. -func (w signedBlindedBeaconBlockBellatrix) PbBlindedBellatrixBlock() (*eth.SignedBlindedBeaconBlockBellatrix, error) { - return w.b, nil -} - -// PbPhase0Block returns the underlying protobuf object. -func (signedBlindedBeaconBlockBellatrix) PbPhase0Block() (*eth.SignedBeaconBlock, error) { - return nil, ErrUnsupportedPhase0Block -} - -// PbAltairBlock returns the underlying protobuf object. -func (signedBlindedBeaconBlockBellatrix) PbAltairBlock() (*eth.SignedBeaconBlockAltair, error) { - return nil, ErrUnsupportedAltairBlock -} - -func (signedBlindedBeaconBlockBellatrix) ToBlinded() (interfaces.SignedBeaconBlock, error) { - return nil, ErrUnsupportedVersion -} - -// Version of the underlying protobuf object. -func (signedBlindedBeaconBlockBellatrix) Version() int { - return version.BellatrixBlind -} - -// Header converts the underlying protobuf object from blinded block to header format. -func (w signedBlindedBeaconBlockBellatrix) Header() (*eth.SignedBeaconBlockHeader, error) { - root, err := w.b.Block.Body.HashTreeRoot() - if err != nil { - return nil, errors.Wrapf(err, "could not hash block") - } - - return ð.SignedBeaconBlockHeader{ - Header: ð.BeaconBlockHeader{ - Slot: w.b.Block.Slot, - ProposerIndex: w.b.Block.ProposerIndex, - ParentRoot: w.b.Block.ParentRoot, - StateRoot: w.b.Block.StateRoot, - BodyRoot: root[:], - }, - Signature: w.Signature(), - }, nil -} - -// blindedBeaconBlockBellatrix is the wrapper for the actual block. -type blindedBeaconBlockBellatrix struct { - b *eth.BlindedBeaconBlockBellatrix -} - -// wrappedBellatrixBlindedBeaconBlock is a constructor which wraps a protobuf Bellatrix object -// with the block wrapper. -func wrappedBellatrixBlindedBeaconBlock(b *eth.BlindedBeaconBlockBellatrix) (interfaces.BeaconBlock, error) { - w := blindedBeaconBlockBellatrix{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// Slot returns the respective slot of the block. -func (w blindedBeaconBlockBellatrix) Slot() types.Slot { - return w.b.Slot -} - -// ProposerIndex returns the proposer index of the beacon block. -func (w blindedBeaconBlockBellatrix) ProposerIndex() types.ValidatorIndex { - return w.b.ProposerIndex -} - -// ParentRoot returns the parent root of beacon block. -func (w blindedBeaconBlockBellatrix) ParentRoot() []byte { - return w.b.ParentRoot -} - -// StateRoot returns the state root of the beacon block. -func (w blindedBeaconBlockBellatrix) StateRoot() []byte { - return w.b.StateRoot -} - -// Body returns the underlying block body. -func (w blindedBeaconBlockBellatrix) Body() interfaces.BeaconBlockBody { - return blindedBeaconBlockBodyBellatrix{b: w.b.Body} -} - -// IsNil checks if the beacon block is nil. -func (w blindedBeaconBlockBellatrix) IsNil() bool { - return w.b == nil -} - -// IsBlinded checks if the beacon block is a blinded block. -func (blindedBeaconBlockBellatrix) IsBlinded() bool { - return true -} - -// HashTreeRoot returns the ssz root of the block. -func (w blindedBeaconBlockBellatrix) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// HashTreeRootWith ssz hashes the BeaconBlock object with a hasher. -func (w blindedBeaconBlockBellatrix) HashTreeRootWith(hh *ssz.Hasher) error { - return w.b.HashTreeRootWith(hh) -} - -// MarshalSSZ marshals the block into its respective -// ssz form. -func (w blindedBeaconBlockBellatrix) MarshalSSZ() ([]byte, error) { - return w.b.MarshalSSZ() -} - -// MarshalSSZTo marshals the beacon block's ssz -// form to the provided byte buffer. -func (w blindedBeaconBlockBellatrix) MarshalSSZTo(dst []byte) ([]byte, error) { - return w.b.MarshalSSZTo(dst) -} - -// SizeSSZ returns the size of the serialized block. -func (w blindedBeaconBlockBellatrix) SizeSSZ() int { - return w.b.SizeSSZ() -} - -// UnmarshalSSZ unmarshals the beacon block from its relevant ssz -// form. -func (w blindedBeaconBlockBellatrix) UnmarshalSSZ(buf []byte) error { - return w.b.UnmarshalSSZ(buf) -} - -// Proto returns the underlying block object in its -// proto form. -func (w blindedBeaconBlockBellatrix) Proto() (proto.Message, error) { - return w.b, nil -} - -// Version of the underlying protobuf object. -func (blindedBeaconBlockBellatrix) Version() int { - return version.BellatrixBlind -} - -// AsSignRequestObject returns the underlying sign request object. -func (w blindedBeaconBlockBellatrix) AsSignRequestObject() (validatorpb.SignRequestObject, error) { - return &validatorpb.SignRequest_BlindedBlockV3{ - BlindedBlockV3: w.b, - }, nil -} - -// blindedBeaconBlockBodyBellatrix is a wrapper of a beacon block body. -type blindedBeaconBlockBodyBellatrix struct { - b *eth.BlindedBeaconBlockBodyBellatrix -} - -// wrappedBellatrixBlindedBeaconBlockBody is a constructor which wraps a protobuf bellatrix object -// with the block wrapper. -func wrappedBellatrixBlindedBeaconBlockBody(b *eth.BlindedBeaconBlockBodyBellatrix) (interfaces.BeaconBlockBody, error) { - w := blindedBeaconBlockBodyBellatrix{b: b} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// RandaoReveal returns the randao reveal from the block body. -func (w blindedBeaconBlockBodyBellatrix) RandaoReveal() []byte { - return w.b.RandaoReveal -} - -// Eth1Data returns the eth1 data in the block. -func (w blindedBeaconBlockBodyBellatrix) Eth1Data() *eth.Eth1Data { - return w.b.Eth1Data -} - -// Graffiti returns the graffiti in the block. -func (w blindedBeaconBlockBodyBellatrix) Graffiti() []byte { - return w.b.Graffiti -} - -// ProposerSlashings returns the proposer slashings in the block. -func (w blindedBeaconBlockBodyBellatrix) ProposerSlashings() []*eth.ProposerSlashing { - return w.b.ProposerSlashings -} - -// AttesterSlashings returns the attester slashings in the block. -func (w blindedBeaconBlockBodyBellatrix) AttesterSlashings() []*eth.AttesterSlashing { - return w.b.AttesterSlashings -} - -// Attestations returns the stored attestations in the block. -func (w blindedBeaconBlockBodyBellatrix) Attestations() []*eth.Attestation { - return w.b.Attestations -} - -// Deposits returns the stored deposits in the block. -func (w blindedBeaconBlockBodyBellatrix) Deposits() []*eth.Deposit { - return w.b.Deposits -} - -// VoluntaryExits returns the voluntary exits in the block. -func (w blindedBeaconBlockBodyBellatrix) VoluntaryExits() []*eth.SignedVoluntaryExit { - return w.b.VoluntaryExits -} - -// SyncAggregate returns the sync aggregate in the block. -func (w blindedBeaconBlockBodyBellatrix) SyncAggregate() (*eth.SyncAggregate, error) { - return w.b.SyncAggregate, nil -} - -// IsNil checks if the block body is nil. -func (w blindedBeaconBlockBodyBellatrix) IsNil() bool { - return w.b == nil -} - -// HashTreeRoot returns the ssz root of the block body. -func (w blindedBeaconBlockBodyBellatrix) HashTreeRoot() ([32]byte, error) { - return w.b.HashTreeRoot() -} - -// Proto returns the underlying proto form of the block -// body. -func (w blindedBeaconBlockBodyBellatrix) Proto() (proto.Message, error) { - return w.b, nil -} - -func (w blindedBeaconBlockBodyBellatrix) Execution() (interfaces.ExecutionData, error) { - return WrappedExecutionPayloadHeader(w.b.ExecutionPayloadHeader) -} diff --git a/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go b/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go deleted file mode 100644 index 32525f5aee..0000000000 --- a/consensus-types/wrapper/blinded_beacon_block_bellatrix_test.go +++ /dev/null @@ -1,406 +0,0 @@ -package wrapper_test - -import ( - "bytes" - "testing" - - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - "github.com/prysmaticlabs/prysm/consensus-types/wrapper" - "github.com/prysmaticlabs/prysm/encoding/bytesutil" - enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" - ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - validatorpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1/validator-client" - "github.com/prysmaticlabs/prysm/runtime/version" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" - "github.com/prysmaticlabs/prysm/testing/util" -) - -func TestBellatrixSignedBlindedBeaconBlock_Header(t *testing.T) { - root := bytesutil.PadTo([]byte("root"), 32) - signature := bytesutil.PadTo([]byte("sig"), 96) - body := ðpb.BlindedBeaconBlockBodyBellatrix{} - body = util.HydrateBlindedBeaconBlockBodyBellatrix(body) - bodyRoot, err := body.HashTreeRoot() - require.NoError(t, err) - block := ðpb.SignedBlindedBeaconBlockBellatrix{ - Block: ðpb.BlindedBeaconBlockBellatrix{ - Slot: 1, - ProposerIndex: 1, - ParentRoot: root, - StateRoot: root, - Body: body, - }, - Signature: signature, - } - wrapped, err := wrapper.WrappedSignedBeaconBlock(block) - require.NoError(t, err) - - header, err := wrapped.Header() - require.NoError(t, err) - assert.Equal(t, types.ValidatorIndex(1), header.Header.ProposerIndex) - assert.Equal(t, types.Slot(1), header.Header.Slot) - assert.DeepEqual(t, bodyRoot[:], header.Header.BodyRoot) - assert.DeepEqual(t, root, header.Header.StateRoot) - assert.DeepEqual(t, root, header.Header.ParentRoot) - assert.DeepEqual(t, signature, header.Signature) -} - -func TestBellatrixSignedBlindedBeaconBlock_Signature(t *testing.T) { - sig := []byte{0x11, 0x22} - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBlindedBeaconBlockBellatrix{Block: ðpb.BlindedBeaconBlockBellatrix{}, Signature: sig}) - require.NoError(t, err) - - if !bytes.Equal(sig, wsb.Signature()) { - t.Error("Wrong signature returned") - } -} - -func TestBellatrixSignedBlindedBeaconBlock_Block(t *testing.T) { - blk := ðpb.BlindedBeaconBlockBellatrix{Slot: 54} - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBlindedBeaconBlockBellatrix{Block: blk}) - require.NoError(t, err) - - pb, err := wsb.Block().Proto() - require.NoError(t, err) - assert.DeepEqual(t, blk, pb) -} - -func TestBellatrixSignedBlindedBeaconBlock_IsNil(t *testing.T) { - _, err := wrapper.WrappedSignedBeaconBlock(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBlindedBeaconBlockBellatrix{Block: ðpb.BlindedBeaconBlockBellatrix{}}) - require.NoError(t, err) - - assert.Equal(t, false, wsb.IsNil()) -} - -func TestBellatrixSignedBlindedBeaconBlock_Copy(t *testing.T) { - t.Skip("TODO: Missing mutation evaluation helpers") -} - -func TestBellatrixSignedBlindedBeaconBlock_Proto(t *testing.T) { - sb := ðpb.SignedBlindedBeaconBlockBellatrix{ - Block: ðpb.BlindedBeaconBlockBellatrix{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - pb, err := wsb.Proto() - require.NoError(t, err) - assert.Equal(t, sb, pb) -} - -func TestBellatrixSignedBlindedBeaconBlock_PbPhase0Block(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBlindedBeaconBlockBellatrix{Block: ðpb.BlindedBeaconBlockBellatrix{}}) - require.NoError(t, err) - - if _, err := wsb.PbPhase0Block(); err != wrapper.ErrUnsupportedPhase0Block { - t.Errorf("Wrong error returned. Want %v got %v", wrapper.ErrUnsupportedPhase0Block, err) - } -} - -func TestBellatrixSignedBlindedBeaconBlock_PbBellatrixBlock(t *testing.T) { - sb := ðpb.SignedBlindedBeaconBlockBellatrix{ - Block: ðpb.BlindedBeaconBlockBellatrix{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - _, err = wsb.PbBellatrixBlock() - require.ErrorContains(t, "unsupported bellatrix block", err) -} - -func TestBellatrixSignedBlindedBeaconBlock_PbBlindedBellatrixBlock(t *testing.T) { - sb := ðpb.SignedBlindedBeaconBlockBellatrix{ - Block: ðpb.BlindedBeaconBlockBellatrix{Slot: 66}, - Signature: []byte{0x11, 0x22}, - } - wsb, err := wrapper.WrappedSignedBeaconBlock(sb) - require.NoError(t, err) - - got, err := wsb.PbBlindedBellatrixBlock() - assert.NoError(t, err) - assert.Equal(t, sb, got) -} - -func TestBellatrixSignedBlindedBeaconBlock_MarshalSSZTo(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(util.HydrateSignedBlindedBeaconBlockBellatrix(ðpb.SignedBlindedBeaconBlockBellatrix{})) - assert.NoError(t, err) - - var b []byte - b, err = wsb.MarshalSSZTo(b) - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) -} - -func TestBellatrixSignedBlindedBeaconBlock_SSZ(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(util.HydrateSignedBlindedBeaconBlockBellatrix(ðpb.SignedBlindedBeaconBlockBellatrix{})) - assert.NoError(t, err) - - b, err := wsb.MarshalSSZ() - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - - assert.NotEqual(t, 0, wsb.SizeSSZ()) - - assert.NoError(t, wsb.UnmarshalSSZ(b)) -} - -func TestBellatrixSignedBlindedBeaconBlock_Version(t *testing.T) { - wsb, err := wrapper.WrappedSignedBeaconBlock(ðpb.SignedBlindedBeaconBlockBellatrix{Block: ðpb.BlindedBeaconBlockBellatrix{}}) - require.NoError(t, err) - - assert.Equal(t, version.BellatrixBlind, wsb.Version()) -} - -func TestBellatrixBlindedBeaconBlock_Slot(t *testing.T) { - slot := types.Slot(546) - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{Slot: slot}) - require.NoError(t, err) - - assert.Equal(t, slot, wb.Slot()) -} - -func TestBellatrixBlindedBeaconBlock_ProposerIndex(t *testing.T) { - pi := types.ValidatorIndex(555) - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{ProposerIndex: pi}) - require.NoError(t, err) - - assert.Equal(t, pi, wb.ProposerIndex()) -} - -func TestBellatrixBlindedBeaconBlock_ParentRoot(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{ParentRoot: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wb.ParentRoot()) -} - -func TestBellatrixBlindedBeaconBlock_StateRoot(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{StateRoot: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wb.StateRoot()) -} - -func TestBellatrixBlindedBeaconBlock_Body(t *testing.T) { - body := ðpb.BlindedBeaconBlockBodyBellatrix{Graffiti: []byte{0x44}} - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{Body: body}) - require.NoError(t, err) - - pb, err := wb.Body().Proto() - require.NoError(t, err) - assert.Equal(t, body, pb) -} - -func TestBellatrixBlindedBeaconBlock_IsNil(t *testing.T) { - _, err := wrapper.WrappedBeaconBlockBody(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{}) - require.NoError(t, err) - - assert.Equal(t, false, wb.IsNil()) -} - -func TestBellatrixBlindedBeaconBlock_IsBlinded(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{}) - require.NoError(t, err) - - assert.Equal(t, true, wb.IsBlinded()) -} - -func TestBellatrixBlindedBeaconBlock_HashTreeRoot(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(util.HydrateBlindedBeaconBlockBellatrix(ðpb.BlindedBeaconBlockBellatrix{})) - require.NoError(t, err) - - rt, err := wb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) -} - -func TestBellatrixBlindedBeaconBlock_Proto(t *testing.T) { - blk := ðpb.BlindedBeaconBlockBellatrix{ProposerIndex: 234} - wb, err := wrapper.WrappedBeaconBlock(blk) - require.NoError(t, err) - - pb, err := wb.Proto() - require.NoError(t, err) - assert.Equal(t, blk, pb) -} - -func TestBellatrixBlindedBeaconBlock_SSZ(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(util.HydrateBlindedBeaconBlockBellatrix(ðpb.BlindedBeaconBlockBellatrix{})) - assert.NoError(t, err) - - b, err := wb.MarshalSSZ() - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - - assert.NotEqual(t, 0, wb.SizeSSZ()) - - assert.NoError(t, wb.UnmarshalSSZ(b)) -} - -func TestBellatrixBlindedBeaconBlock_Version(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlock(ðpb.BlindedBeaconBlockBellatrix{}) - require.NoError(t, err) - - assert.Equal(t, version.BellatrixBlind, wb.Version()) -} - -func TestBellatrixBlindedBeaconBlockBody_RandaoReveal(t *testing.T) { - root := []byte{0xAA, 0xBF, 0x33, 0x01} - wbb, err := wrapper.WrappedBeaconBlockBody(ðpb.BlindedBeaconBlockBodyBellatrix{RandaoReveal: root}) - require.NoError(t, err) - - assert.DeepEqual(t, root, wbb.RandaoReveal()) -} - -func TestBellatrixBlindedBeaconBlockBody_Eth1Data(t *testing.T) { - data := ðpb.Eth1Data{} - body := ðpb.BlindedBeaconBlockBodyBellatrix{ - Eth1Data: data, - } - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - assert.Equal(t, data, wbb.Eth1Data()) -} - -func TestBellatrixBlindedBeaconBlockBody_Graffiti(t *testing.T) { - graffiti := []byte{0x66, 0xAA} - body := ðpb.BlindedBeaconBlockBodyBellatrix{Graffiti: graffiti} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, graffiti, wbb.Graffiti()) -} - -func TestBellatrixBlindedBeaconBlockBody_ProposerSlashings(t *testing.T) { - ps := []*ethpb.ProposerSlashing{ - {Header_1: ðpb.SignedBeaconBlockHeader{ - Signature: []byte{0x11, 0x20}, - }}, - } - body := ðpb.BlindedBeaconBlockBodyBellatrix{ProposerSlashings: ps} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, ps, wbb.ProposerSlashings()) -} - -func TestBellatrixBlindedBeaconBlockBody_AttesterSlashings(t *testing.T) { - as := []*ethpb.AttesterSlashing{ - {Attestation_1: ðpb.IndexedAttestation{Signature: []byte{0x11}}}, - } - body := ðpb.BlindedBeaconBlockBodyBellatrix{AttesterSlashings: as} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, as, wbb.AttesterSlashings()) -} - -func TestBellatrixBlindedBeaconBlockBody_Attestations(t *testing.T) { - atts := []*ethpb.Attestation{{Signature: []byte{0x88}}} - - body := ðpb.BlindedBeaconBlockBodyBellatrix{Attestations: atts} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, atts, wbb.Attestations()) -} - -func TestBellatrixBlindedBeaconBlockBody_Deposits(t *testing.T) { - deposits := []*ethpb.Deposit{ - {Proof: [][]byte{{0x54, 0x10}}}, - } - body := ðpb.BlindedBeaconBlockBodyBellatrix{Deposits: deposits} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, deposits, wbb.Deposits()) -} - -func TestBellatrixBlindedBeaconBlockBody_VoluntaryExits(t *testing.T) { - exits := []*ethpb.SignedVoluntaryExit{ - {Exit: ðpb.VoluntaryExit{Epoch: 54}}, - } - body := ðpb.BlindedBeaconBlockBodyBellatrix{VoluntaryExits: exits} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - assert.DeepEqual(t, exits, wbb.VoluntaryExits()) -} - -func TestBellatrixBlindedBeaconBlockBody_IsNil(t *testing.T) { - _, err := wrapper.WrappedBeaconBlockBody(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - wbb, err := wrapper.WrappedBeaconBlockBody(ðpb.BlindedBeaconBlockBodyBellatrix{}) - require.NoError(t, err) - assert.Equal(t, false, wbb.IsNil()) - -} - -func TestBellatrixBlindedBeaconBlockBody_HashTreeRoot(t *testing.T) { - wb, err := wrapper.WrappedBeaconBlockBody(util.HydrateBlindedBeaconBlockBodyBellatrix(ðpb.BlindedBeaconBlockBodyBellatrix{})) - assert.NoError(t, err) - - rt, err := wb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) -} - -func TestBellatrixBlindedBeaconBlockBody_Proto(t *testing.T) { - body := ðpb.BlindedBeaconBlockBodyBellatrix{Graffiti: []byte{0x66, 0xAA}} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - pb, err := wbb.Proto() - require.NoError(t, err) - assert.Equal(t, body, pb) -} - -func TestBellatrixBlindedBeaconBlockBody_ExecutionPayloadHeader(t *testing.T) { - payloads := &enginev1.ExecutionPayloadHeader{ - BlockNumber: 100, - } - body := ðpb.BlindedBeaconBlockBodyBellatrix{ExecutionPayloadHeader: payloads} - wbb, err := wrapper.WrappedBeaconBlockBody(body) - require.NoError(t, err) - - exec, err := wbb.Execution() - require.NoError(t, err) - _, err = exec.Transactions() - require.ErrorContains(t, wrapper.ErrUnsupportedField.Error(), err) -} - -func TestBellatrixBlindedBeaconBlock_PbGenericBlock(t *testing.T) { - abb := ðpb.SignedBlindedBeaconBlockBellatrix{ - Block: util.HydrateBlindedBeaconBlockBellatrix(ðpb.BlindedBeaconBlockBellatrix{}), - } - wsb, err := wrapper.WrappedSignedBeaconBlock(abb) - require.NoError(t, err) - - got, err := wsb.PbGenericBlock() - require.NoError(t, err) - assert.Equal(t, abb, got.GetBlindedBellatrix()) -} - -func TestBellatrixBlindedBeaconBlock_AsSignRequestObject(t *testing.T) { - abb := util.HydrateBlindedBeaconBlockBellatrix(ðpb.BlindedBeaconBlockBellatrix{}) - wsb, err := wrapper.WrappedBeaconBlock(abb) - require.NoError(t, err) - - sro, err := wsb.AsSignRequestObject() - require.NoError(t, err) - got, ok := sro.(*validatorpb.SignRequest_BlindedBlockV3) - require.Equal(t, true, ok, "Not a SignRequest_BlockV3") - assert.Equal(t, abb, got.BlindedBlockV3) -} diff --git a/consensus-types/wrapper/execution.go b/consensus-types/wrapper/execution.go deleted file mode 100644 index 783873e12e..0000000000 --- a/consensus-types/wrapper/execution.go +++ /dev/null @@ -1,351 +0,0 @@ -package wrapper - -import ( - "bytes" - - "github.com/pkg/errors" - fastssz "github.com/prysmaticlabs/fastssz" - fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - "github.com/prysmaticlabs/prysm/encoding/bytesutil" - "github.com/prysmaticlabs/prysm/encoding/ssz" - enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" - "google.golang.org/protobuf/proto" -) - -// executionPayload is a convenience wrapper around a beacon block body's execution payload 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. -type executionPayload struct { - p *enginev1.ExecutionPayload -} - -// WrappedExecutionPayload is a constructor which wraps a protobuf execution payload into an interface. -func WrappedExecutionPayload(p *enginev1.ExecutionPayload) (interfaces.ExecutionData, error) { - w := executionPayload{p: p} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// IsNil checks if the underlying data is nil. -func (e executionPayload) IsNil() bool { - return e.p == nil -} - -// MarshalSSZ -- -func (e executionPayload) MarshalSSZ() ([]byte, error) { - return e.p.MarshalSSZ() -} - -// MarshalSSZTo -- -func (e executionPayload) MarshalSSZTo(dst []byte) ([]byte, error) { - return e.p.MarshalSSZTo(dst) -} - -// SizeSSZ -- -func (e executionPayload) SizeSSZ() int { - return e.p.SizeSSZ() -} - -// UnmarshalSSZ -- -func (e executionPayload) UnmarshalSSZ(buf []byte) error { - return e.p.UnmarshalSSZ(buf) -} - -// HashTreeRoot -- -func (e executionPayload) HashTreeRoot() ([32]byte, error) { - return e.p.HashTreeRoot() -} - -// HashTreeRootWith -- -func (e executionPayload) HashTreeRootWith(hh *fastssz.Hasher) error { - return e.p.HashTreeRootWith(hh) -} - -// Proto -- -func (e executionPayload) Proto() proto.Message { - return e.p -} - -// ParentHash -- -func (e executionPayload) ParentHash() []byte { - return e.p.ParentHash -} - -// FeeRecipient -- -func (e executionPayload) FeeRecipient() []byte { - return e.p.FeeRecipient -} - -// StateRoot -- -func (e executionPayload) StateRoot() []byte { - return e.p.StateRoot -} - -// ReceiptsRoot -- -func (e executionPayload) ReceiptsRoot() []byte { - return e.p.ReceiptsRoot -} - -// LogsBloom -- -func (e executionPayload) LogsBloom() []byte { - return e.p.LogsBloom -} - -// PrevRandao -- -func (e executionPayload) PrevRandao() []byte { - return e.p.PrevRandao -} - -// BlockNumber -- -func (e executionPayload) BlockNumber() uint64 { - return e.p.BlockNumber -} - -// GasLimit -- -func (e executionPayload) GasLimit() uint64 { - return e.p.GasLimit -} - -// GasUsed -- -func (e executionPayload) GasUsed() uint64 { - return e.p.GasUsed -} - -// Timestamp -- -func (e executionPayload) Timestamp() uint64 { - return e.p.Timestamp -} - -// ExtraData -- -func (e executionPayload) ExtraData() []byte { - return e.p.ExtraData -} - -// BaseFeePerGas -- -func (e executionPayload) BaseFeePerGas() []byte { - return e.p.BaseFeePerGas -} - -// BlockHash -- -func (e executionPayload) BlockHash() []byte { - return e.p.BlockHash -} - -// Transactions -- -func (e executionPayload) Transactions() ([][]byte, error) { - return e.p.Transactions, nil -} - -// 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. -type executionPayloadHeader struct { - p *enginev1.ExecutionPayloadHeader -} - -// WrappedExecutionPayloadHeader is a constructor which wraps a protobuf execution header into an interface. -func WrappedExecutionPayloadHeader(p *enginev1.ExecutionPayloadHeader) (interfaces.ExecutionData, error) { - w := executionPayloadHeader{p: p} - if w.IsNil() { - return nil, ErrNilObjectWrapped - } - return w, nil -} - -// IsNil checks if the underlying data is nil. -func (e executionPayloadHeader) IsNil() bool { - return e.p == nil -} - -// MarshalSSZ -- -func (e executionPayloadHeader) MarshalSSZ() ([]byte, error) { - return e.p.MarshalSSZ() -} - -// MarshalSSZTo -- -func (e executionPayloadHeader) MarshalSSZTo(dst []byte) ([]byte, error) { - return e.p.MarshalSSZTo(dst) -} - -// SizeSSZ -- -func (e executionPayloadHeader) SizeSSZ() int { - return e.p.SizeSSZ() -} - -// UnmarshalSSZ -- -func (e executionPayloadHeader) UnmarshalSSZ(buf []byte) error { - return e.p.UnmarshalSSZ(buf) -} - -// HashTreeRoot -- -func (e executionPayloadHeader) HashTreeRoot() ([32]byte, error) { - return e.p.HashTreeRoot() -} - -// HashTreeRootWith -- -func (e executionPayloadHeader) HashTreeRootWith(hh *fastssz.Hasher) error { - return e.p.HashTreeRootWith(hh) -} - -// Proto -- -func (e executionPayloadHeader) Proto() proto.Message { - return e.p -} - -// ParentHash -- -func (e executionPayloadHeader) ParentHash() []byte { - return e.p.ParentHash -} - -// FeeRecipient -- -func (e executionPayloadHeader) FeeRecipient() []byte { - return e.p.FeeRecipient -} - -// StateRoot -- -func (e executionPayloadHeader) StateRoot() []byte { - return e.p.StateRoot -} - -// ReceiptsRoot -- -func (e executionPayloadHeader) ReceiptsRoot() []byte { - return e.p.ReceiptsRoot -} - -// LogsBloom -- -func (e executionPayloadHeader) LogsBloom() []byte { - return e.p.LogsBloom -} - -// PrevRandao -- -func (e executionPayloadHeader) PrevRandao() []byte { - return e.p.PrevRandao -} - -// BlockNumber -- -func (e executionPayloadHeader) BlockNumber() uint64 { - return e.p.BlockNumber -} - -// GasLimit -- -func (e executionPayloadHeader) GasLimit() uint64 { - return e.p.GasLimit -} - -// GasUsed -- -func (e executionPayloadHeader) GasUsed() uint64 { - return e.p.GasUsed -} - -// Timestamp -- -func (e executionPayloadHeader) Timestamp() uint64 { - return e.p.Timestamp -} - -// ExtraData -- -func (e executionPayloadHeader) ExtraData() []byte { - return e.p.ExtraData -} - -// BaseFeePerGas -- -func (e executionPayloadHeader) BaseFeePerGas() []byte { - return e.p.BaseFeePerGas -} - -// BlockHash -- -func (e executionPayloadHeader) BlockHash() []byte { - return e.p.BlockHash -} - -// Transactions -- -func (executionPayloadHeader) Transactions() ([][]byte, error) { - return nil, ErrUnsupportedField -} - -// PayloadToHeader converts `payload` into execution payload header format. -func PayloadToHeader(payload interfaces.ExecutionData) (*enginev1.ExecutionPayloadHeader, error) { - txs, err := payload.Transactions() - if err != nil { - return nil, err - } - txRoot, err := ssz.TransactionsRoot(txs) - if err != nil { - return nil, err - } - return &enginev1.ExecutionPayloadHeader{ - ParentHash: bytesutil.SafeCopyBytes(payload.ParentHash()), - FeeRecipient: bytesutil.SafeCopyBytes(payload.FeeRecipient()), - StateRoot: bytesutil.SafeCopyBytes(payload.StateRoot()), - ReceiptsRoot: bytesutil.SafeCopyBytes(payload.ReceiptsRoot()), - LogsBloom: bytesutil.SafeCopyBytes(payload.LogsBloom()), - PrevRandao: bytesutil.SafeCopyBytes(payload.PrevRandao()), - BlockNumber: payload.BlockNumber(), - GasLimit: payload.GasLimit(), - GasUsed: payload.GasUsed(), - Timestamp: payload.Timestamp(), - ExtraData: bytesutil.SafeCopyBytes(payload.ExtraData()), - BaseFeePerGas: bytesutil.SafeCopyBytes(payload.BaseFeePerGas()), - BlockHash: bytesutil.SafeCopyBytes(payload.BlockHash()), - TransactionsRoot: txRoot[:], - }, nil -} - -// IsEmptyExecutionData checks if an execution data is empty underneath. If a single field has -// a non-zero value, this function will return false. -func IsEmptyExecutionData(data interfaces.ExecutionData) (bool, error) { - if !bytes.Equal(data.ParentHash(), make([]byte, fieldparams.RootLength)) { - return false, nil - } - if !bytes.Equal(data.FeeRecipient(), make([]byte, fieldparams.FeeRecipientLength)) { - return false, nil - } - if !bytes.Equal(data.StateRoot(), make([]byte, fieldparams.RootLength)) { - return false, nil - } - if !bytes.Equal(data.ReceiptsRoot(), make([]byte, fieldparams.RootLength)) { - return false, nil - } - if !bytes.Equal(data.LogsBloom(), make([]byte, fieldparams.LogsBloomLength)) { - return false, nil - } - if !bytes.Equal(data.PrevRandao(), make([]byte, fieldparams.RootLength)) { - return false, nil - } - if !bytes.Equal(data.BaseFeePerGas(), make([]byte, fieldparams.RootLength)) { - return false, nil - } - if !bytes.Equal(data.BlockHash(), make([]byte, fieldparams.RootLength)) { - return false, nil - } - - txs, err := data.Transactions() - switch { - case errors.Is(err, ErrUnsupportedField): - case err != nil: - return false, err - default: - if len(txs) != 0 { - return false, nil - } - } - - if len(data.ExtraData()) != 0 { - return false, nil - } - if data.BlockNumber() != 0 { - return false, nil - } - if data.GasLimit() != 0 { - return false, nil - } - if data.GasUsed() != 0 { - return false, nil - } - if data.Timestamp() != 0 { - return false, nil - } - return true, nil -} diff --git a/consensus-types/wrapper/execution_test.go b/consensus-types/wrapper/execution_test.go deleted file mode 100644 index 1aefea1bc2..0000000000 --- a/consensus-types/wrapper/execution_test.go +++ /dev/null @@ -1,124 +0,0 @@ -package wrapper_test - -import ( - "testing" - - fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - "github.com/prysmaticlabs/prysm/consensus-types/wrapper" - enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1" - "github.com/prysmaticlabs/prysm/testing/assert" - "github.com/prysmaticlabs/prysm/testing/require" -) - -func TestWrapExecutionPayload(t *testing.T) { - data := &enginev1.ExecutionPayload{GasUsed: 54} - wsb, err := wrapper.WrappedExecutionPayload(data) - require.NoError(t, err) - - assert.DeepEqual(t, data, wsb.Proto()) -} - -func TestWrapExecutionPayloadHeader(t *testing.T) { - data := &enginev1.ExecutionPayloadHeader{GasUsed: 54} - wsb, err := wrapper.WrappedExecutionPayloadHeader(data) - require.NoError(t, err) - - assert.DeepEqual(t, data, wsb.Proto()) -} - -func TestWrapExecutionPayload_IsNil(t *testing.T) { - _, err := wrapper.WrappedExecutionPayload(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - data := &enginev1.ExecutionPayload{GasUsed: 54} - wsb, err := wrapper.WrappedExecutionPayload(data) - require.NoError(t, err) - - assert.Equal(t, false, wsb.IsNil()) -} - -func TestWrapExecutionPayloadHeader_IsNil(t *testing.T) { - _, err := wrapper.WrappedExecutionPayloadHeader(nil) - require.Equal(t, wrapper.ErrNilObjectWrapped, err) - - data := &enginev1.ExecutionPayloadHeader{GasUsed: 54} - wsb, err := wrapper.WrappedExecutionPayloadHeader(data) - require.NoError(t, err) - - assert.Equal(t, false, wsb.IsNil()) -} - -func TestWrapExecutionPayload_SSZ(t *testing.T) { - wsb := createWrappedPayload(t) - rt, err := wsb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) - - var b []byte - b, err = wsb.MarshalSSZTo(b) - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - encoded, err := wsb.MarshalSSZ() - require.NoError(t, err) - assert.NotEqual(t, 0, wsb.SizeSSZ()) - assert.NoError(t, wsb.UnmarshalSSZ(encoded)) -} - -func TestWrapExecutionPayloadHeader_SSZ(t *testing.T) { - wsb := createWrappedPayloadHeader(t) - rt, err := wsb.HashTreeRoot() - assert.NoError(t, err) - assert.NotEmpty(t, rt) - - var b []byte - b, err = wsb.MarshalSSZTo(b) - assert.NoError(t, err) - assert.NotEqual(t, 0, len(b)) - encoded, err := wsb.MarshalSSZ() - require.NoError(t, err) - assert.NotEqual(t, 0, wsb.SizeSSZ()) - assert.NoError(t, wsb.UnmarshalSSZ(encoded)) -} - -func createWrappedPayload(t testing.TB) interfaces.ExecutionData { - wsb, err := wrapper.WrappedExecutionPayload(&enginev1.ExecutionPayload{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, fieldparams.LogsBloomLength), - PrevRandao: make([]byte, fieldparams.RootLength), - BlockNumber: 0, - GasLimit: 0, - GasUsed: 0, - Timestamp: 0, - ExtraData: make([]byte, 0), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - Transactions: make([][]byte, 0), - }) - require.NoError(t, err) - return wsb -} - -func createWrappedPayloadHeader(t testing.TB) interfaces.ExecutionData { - wsb, err := wrapper.WrappedExecutionPayloadHeader(&enginev1.ExecutionPayloadHeader{ - ParentHash: make([]byte, fieldparams.RootLength), - FeeRecipient: make([]byte, fieldparams.FeeRecipientLength), - StateRoot: make([]byte, fieldparams.RootLength), - ReceiptsRoot: make([]byte, fieldparams.RootLength), - LogsBloom: make([]byte, fieldparams.LogsBloomLength), - PrevRandao: make([]byte, fieldparams.RootLength), - BlockNumber: 0, - GasLimit: 0, - GasUsed: 0, - Timestamp: 0, - ExtraData: make([]byte, 0), - BaseFeePerGas: make([]byte, fieldparams.RootLength), - BlockHash: make([]byte, fieldparams.RootLength), - TransactionsRoot: make([]byte, fieldparams.RootLength), - }) - require.NoError(t, err) - return wsb -} diff --git a/consensus-types/wrapper/mutator.go b/consensus-types/wrapper/mutator.go deleted file mode 100644 index 9db5f54980..0000000000 --- a/consensus-types/wrapper/mutator.go +++ /dev/null @@ -1,77 +0,0 @@ -package wrapper - -import ( - "fmt" - - "github.com/pkg/errors" - "github.com/prysmaticlabs/prysm/consensus-types/interfaces" - types "github.com/prysmaticlabs/prysm/consensus-types/primitives" - eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1" - "github.com/prysmaticlabs/prysm/runtime/version" -) - -type BlockMutator struct { - Phase0 func(beaconBlock *eth.SignedBeaconBlock) - Altair func(beaconBlock *eth.SignedBeaconBlockAltair) - Bellatrix func(beaconBlock *eth.SignedBeaconBlockBellatrix) -} - -func (m BlockMutator) Apply(b interfaces.SignedBeaconBlock) error { - switch b.Version() { - case version.Phase0: - bb, err := b.PbPhase0Block() - if err != nil { - return err - } - m.Phase0(bb) - return nil - case version.Altair: - bb, err := b.PbAltairBlock() - if err != nil { - return err - } - m.Altair(bb) - return nil - case version.Bellatrix: - bb, err := b.PbBellatrixBlock() - if err != nil { - return err - } - m.Bellatrix(bb) - return nil - } - msg := fmt.Sprintf("version %d = %s", b.Version(), version.String(b.Version())) - return errors.Wrap(ErrUnsupportedSignedBeaconBlock, msg) -} - -func SetBlockStateRoot(b interfaces.SignedBeaconBlock, sr [32]byte) error { - return BlockMutator{ - Phase0: func(bb *eth.SignedBeaconBlock) { bb.Block.StateRoot = sr[:] }, - Altair: func(bb *eth.SignedBeaconBlockAltair) { bb.Block.StateRoot = sr[:] }, - Bellatrix: func(bb *eth.SignedBeaconBlockBellatrix) { bb.Block.StateRoot = sr[:] }, - }.Apply(b) -} - -func SetBlockParentRoot(b interfaces.SignedBeaconBlock, pr [32]byte) error { - return BlockMutator{ - Phase0: func(bb *eth.SignedBeaconBlock) { bb.Block.ParentRoot = pr[:] }, - Altair: func(bb *eth.SignedBeaconBlockAltair) { bb.Block.ParentRoot = pr[:] }, - Bellatrix: func(bb *eth.SignedBeaconBlockBellatrix) { bb.Block.ParentRoot = pr[:] }, - }.Apply(b) -} - -func SetBlockSlot(b interfaces.SignedBeaconBlock, s types.Slot) error { - return BlockMutator{ - Phase0: func(bb *eth.SignedBeaconBlock) { bb.Block.Slot = s }, - Altair: func(bb *eth.SignedBeaconBlockAltair) { bb.Block.Slot = s }, - Bellatrix: func(bb *eth.SignedBeaconBlockBellatrix) { bb.Block.Slot = s }, - }.Apply(b) -} - -func SetProposerIndex(b interfaces.SignedBeaconBlock, idx types.ValidatorIndex) error { - return BlockMutator{ - Phase0: func(bb *eth.SignedBeaconBlock) { bb.Block.ProposerIndex = idx }, - Altair: func(bb *eth.SignedBeaconBlockAltair) { bb.Block.ProposerIndex = idx }, - Bellatrix: func(bb *eth.SignedBeaconBlockBellatrix) { bb.Block.ProposerIndex = idx }, - }.Apply(b) -}