Native Blocks Ep. 1 - New types and functions (#10837)

* types and functions

* partially done tests

* refactor

* remaining Proto() tests

* remaining proto.go tests

* simplify UnmarshalSSZ and move BeaconBlockIsNil

* getters_test

* remove errAssertionFailed

* review feedback

* remove cloning protobuf

* fmt

* change IsNil

* fix tests
This commit is contained in:
Radosław Kapka
2022-06-09 15:13:02 +02:00
committed by GitHub
parent 5ee66a4a68
commit fce9e6883d
11 changed files with 2741 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
load("@prysm//tools/go:def.bzl", "go_library")
go_library(
name = "go_default_library",
testonly = True,
srcs = [
"factory.go",
"mutator.go",
],
importpath = "github.com/prysmaticlabs/prysm/consensus-types/blocks/testing",
visibility = ["//visibility:public"],
deps = [
"//consensus-types/blocks:go_default_library",
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",
"@com_github_pkg_errors//:go_default_library",
],
)

View File

@@ -0,0 +1,27 @@
package testing
import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/consensus-types/blocks"
eth "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
// NewSignedBeaconBlockFromGeneric creates a signed beacon block
// from a protobuf generic signed beacon block.
func NewSignedBeaconBlockFromGeneric(gb *eth.GenericSignedBeaconBlock) (*blocks.SignedBeaconBlock, error) {
if gb == nil {
return nil, blocks.ErrNilObjectWrapped
}
switch bb := gb.Block.(type) {
case *eth.GenericSignedBeaconBlock_Phase0:
return blocks.NewSignedBeaconBlock(bb.Phase0)
case *eth.GenericSignedBeaconBlock_Altair:
return blocks.NewSignedBeaconBlock(bb.Altair)
case *eth.GenericSignedBeaconBlock_Bellatrix:
return blocks.NewSignedBeaconBlock(bb.Bellatrix)
case *eth.GenericSignedBeaconBlock_BlindedBellatrix:
return blocks.NewSignedBeaconBlock(bb.BlindedBellatrix)
default:
return nil, errors.Wrapf(blocks.ErrUnsupportedSignedBeaconBlock, "unable to create block from type %T", gb)
}
}

View File

@@ -0,0 +1,79 @@
package testing
import (
"github.com/prysmaticlabs/prysm/consensus-types/blocks"
"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
default:
return blocks.ErrUnsupportedSignedBeaconBlock
}
}
// SetBlockStateRoot modifies the block's state root.
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)
}
// SetBlockParentRoot modifies the block's parent root.
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)
}
// SetBlockSlot modifies the block's slot.
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)
}
// SetProposerIndex modifies the block's proposer index.
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)
}