Skip to content

Commit

Permalink
removing typed pb accessors
Browse files Browse the repository at this point in the history
  • Loading branch information
kasey committed Apr 30, 2024
1 parent ae16d5f commit 47d6afa
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 88 deletions.
4 changes: 3 additions & 1 deletion beacon-chain/rpc/lookup/blocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ func TestGetBlock(t *testing.T) {
return
}
require.NoError(t, err)
pbBlock, err := result.PbPhase0Block()
pb, err := result.Proto()
require.NoError(t, err)
pbBlock, ok := pb.(*ethpbalpha.SignedBeaconBlock)
require.Equal(t, true, ok)
if !reflect.DeepEqual(pbBlock, tt.want) {
t.Error("Expected blocks to equal")
}
Expand Down
79 changes: 23 additions & 56 deletions beacon-chain/rpc/prysm/v1alpha1/beacon/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -90,64 +89,32 @@ func convertToBlockContainer(blk interfaces.ReadOnlySignedBeaconBlock, root [32]
Canonical: isCanonical,
}

switch blk.Version() {
case version.Phase0:
rBlk, err := blk.PbPhase0Block()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_Phase0Block{Phase0Block: rBlk}
case version.Altair:
rBlk, err := blk.PbAltairBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_AltairBlock{AltairBlock: rBlk}
case version.Bellatrix:
if blk.IsBlinded() {
rBlk, err := blk.PbBlindedBellatrixBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_BlindedBellatrixBlock{BlindedBellatrixBlock: rBlk}
} else {
rBlk, err := blk.PbBellatrixBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_BellatrixBlock{BellatrixBlock: rBlk}
}
case version.Capella:
if blk.IsBlinded() {
rBlk, err := blk.PbBlindedCapellaBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_BlindedCapellaBlock{BlindedCapellaBlock: rBlk}
} else {
rBlk, err := blk.PbCapellaBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_CapellaBlock{CapellaBlock: rBlk}
}
case version.Deneb:
if blk.IsBlinded() {
rBlk, err := blk.PbBlindedDenebBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_BlindedDenebBlock{BlindedDenebBlock: rBlk}
} else {
rBlk, err := blk.PbDenebBlock()
if err != nil {
return nil, err
}
ctr.Block = &ethpb.BeaconBlockContainer_DenebBlock{DenebBlock: rBlk}
}
pb, err := blk.Proto()
if err != nil {
return nil, err
}

switch pbStruct := pb.(type) {
case *ethpb.SignedBeaconBlock:
ctr.Block = &ethpb.BeaconBlockContainer_Phase0Block{Phase0Block: pbStruct}
case *ethpb.SignedBeaconBlockAltair:
ctr.Block = &ethpb.BeaconBlockContainer_AltairBlock{AltairBlock: pbStruct}
case *ethpb.SignedBlindedBeaconBlockBellatrix:
ctr.Block = &ethpb.BeaconBlockContainer_BlindedBellatrixBlock{BlindedBellatrixBlock: pbStruct}
case *ethpb.SignedBeaconBlockBellatrix:
ctr.Block = &ethpb.BeaconBlockContainer_BellatrixBlock{BellatrixBlock: pbStruct}
case *ethpb.SignedBlindedBeaconBlockCapella:
ctr.Block = &ethpb.BeaconBlockContainer_BlindedCapellaBlock{BlindedCapellaBlock: pbStruct}
case *ethpb.SignedBeaconBlockCapella:
ctr.Block = &ethpb.BeaconBlockContainer_CapellaBlock{CapellaBlock: pbStruct}
case *ethpb.SignedBlindedBeaconBlockDeneb:
ctr.Block = &ethpb.BeaconBlockContainer_BlindedDenebBlock{BlindedDenebBlock: pbStruct}
case *ethpb.SignedBeaconBlockDeneb:
ctr.Block = &ethpb.BeaconBlockContainer_DenebBlock{DenebBlock: pbStruct}
default:
return nil, errors.Errorf("block type is not recognized: %d", blk.Version())
}

return ctr, nil
}

Expand Down
1 change: 0 additions & 1 deletion consensus-types/blocks/testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ go_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",
],
)
44 changes: 14 additions & 30 deletions consensus-types/blocks/testing/mutator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
eth "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/runtime/version"
)

type blockMutator struct {
Expand All @@ -16,38 +15,23 @@ type blockMutator struct {
}

func (m blockMutator) apply(b interfaces.SignedBeaconBlock) (interfaces.SignedBeaconBlock, error) {
switch b.Version() {
case version.Phase0:
bb, err := b.PbPhase0Block()
if err != nil {
return nil, err
}
m.Phase0(bb)
return blocks.NewSignedBeaconBlock(bb)
case version.Altair:
bb, err := b.PbAltairBlock()
if err != nil {
return nil, err
}
m.Altair(bb)
return blocks.NewSignedBeaconBlock(bb)
case version.Bellatrix:
bb, err := b.PbBellatrixBlock()
if err != nil {
return nil, err
}
m.Bellatrix(bb)
return blocks.NewSignedBeaconBlock(bb)
case version.Capella:
bb, err := b.PbCapellaBlock()
if err != nil {
return nil, err
}
m.Capella(bb)
return blocks.NewSignedBeaconBlock(bb)
pb, err := b.Proto()
if err != nil {
return nil, err
}
switch pbStruct := pb.(type) {
case *eth.SignedBeaconBlock:
m.Phase0(pbStruct)
case *eth.SignedBeaconBlockAltair:
m.Altair(pbStruct)
case *eth.SignedBeaconBlockBellatrix:
m.Bellatrix(pbStruct)
case *eth.SignedBeaconBlockCapella:
m.Capella(pbStruct)
default:
return nil, blocks.ErrUnsupportedSignedBeaconBlock
}
return blocks.NewSignedBeaconBlock(pb)
}

// SetBlockStateRoot modifies the block's state root.
Expand Down

0 comments on commit 47d6afa

Please sign in to comment.