Beacon api: propoerly submit blind block (#11483)

* Beacon api: propoerly submit blind block

* Gazelle

* Fix SubmitBlockSSZ

* Update beacon-chain/rpc/eth/beacon/blocks.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update beacon-chain/rpc/eth/beacon/blocks.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
terencechain 2022-09-22 13:13:43 -07:00 committed by GitHub
parent 20e99fd1f9
commit 7720d98764
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 21 deletions

View File

@ -82,6 +82,7 @@ go_test(
deps = [ deps = [
"//api/grpc:go_default_library", "//api/grpc:go_default_library",
"//beacon-chain/blockchain/testing:go_default_library", "//beacon-chain/blockchain/testing:go_default_library",
"//beacon-chain/builder/testing:go_default_library",
"//beacon-chain/core/signing:go_default_library", "//beacon-chain/core/signing:go_default_library",
"//beacon-chain/core/transition:go_default_library", "//beacon-chain/core/transition:go_default_library",
"//beacon-chain/db:go_default_library", "//beacon-chain/db:go_default_library",

View File

@ -23,6 +23,7 @@ import (
ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1" ethpbv1 "github.com/prysmaticlabs/prysm/v3/proto/eth/v1"
ethpbv2 "github.com/prysmaticlabs/prysm/v3/proto/eth/v2" ethpbv2 "github.com/prysmaticlabs/prysm/v3/proto/eth/v2"
"github.com/prysmaticlabs/prysm/v3/proto/migration" "github.com/prysmaticlabs/prysm/v3/proto/migration"
eth "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v3/time/slots" "github.com/prysmaticlabs/prysm/v3/time/slots"
"go.opencensus.io/trace" "go.opencensus.io/trace"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@ -342,6 +343,19 @@ func (bs *Server) SubmitBlindedBlockSSZ(ctx context.Context, req *ethpbv2.SSZCon
if err != nil { if err != nil {
return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not unmarshal request data into block: %v", err) return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not unmarshal request data into block: %v", err)
} }
if block.IsBlinded() {
b, err := block.PbBlindedBellatrixBlock()
if err != nil {
return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not get blinded block: %v", err)
}
bb, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(b.Block)
if err != nil {
return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not migrate block: %v", err)
}
return &emptypb.Empty{}, bs.submitBlindedBellatrixBlock(ctx, bb, b.Signature)
}
root, err := block.Block().HashTreeRoot() root, err := block.Block().HashTreeRoot()
if err != nil { if err != nil {
return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not compute block's hash tree root: %v", err) return &emptypb.Empty{}, status.Errorf(codes.Internal, "Could not compute block's hash tree root: %v", err)
@ -875,24 +889,22 @@ func (bs *Server) submitBellatrixBlock(ctx context.Context, bellatrixBlk *ethpbv
} }
func (bs *Server) submitBlindedBellatrixBlock(ctx context.Context, blindedBellatrixBlk *ethpbv2.BlindedBeaconBlockBellatrix, sig []byte) error { func (bs *Server) submitBlindedBellatrixBlock(ctx context.Context, blindedBellatrixBlk *ethpbv2.BlindedBeaconBlockBellatrix, sig []byte) error {
v1alpha1SignedBlk, err := migration.BlindedBellatrixToV1Alpha1SignedBlock(&ethpbv2.SignedBlindedBeaconBlockBellatrix{ b, err := migration.BlindedBellatrixToV1Alpha1SignedBlock(&ethpbv2.SignedBlindedBeaconBlockBellatrix{
Message: blindedBellatrixBlk, Message: blindedBellatrixBlk,
Signature: sig, Signature: sig,
}) })
if err != nil { if err != nil {
return status.Errorf(codes.Internal, "Could not get blinded block: %v", err) return status.Errorf(codes.Internal, "Could not get blinded block: %v", err)
} }
wrappedBellatrixSignedBlk, err := blocks.NewSignedBeaconBlock(v1alpha1SignedBlk) _, err = bs.V1Alpha1ValidatorServer.ProposeBeaconBlock(ctx, &eth.GenericSignedBeaconBlock{
Block: &eth.GenericSignedBeaconBlock_BlindedBellatrix{
BlindedBellatrix: b,
},
})
if err != nil { if err != nil {
return status.Errorf(codes.Internal, "Could not get blinded block: %v", err) return status.Errorf(codes.Internal, "Could not propose blinded block: %v", err)
} }
return nil
root, err := blindedBellatrixBlk.HashTreeRoot()
if err != nil {
return status.Errorf(codes.InvalidArgument, "Could not tree hash block: %v", err)
}
return bs.submitBlock(ctx, root, wrappedBellatrixSignedBlk)
} }
func (bs *Server) submitBlock(ctx context.Context, blockRoot [fieldparams.RootLength]byte, block interfaces.SignedBeaconBlock) error { func (bs *Server) submitBlock(ctx context.Context, blockRoot [fieldparams.RootLength]byte, block interfaces.SignedBeaconBlock) error {

View File

@ -7,10 +7,13 @@ import (
"testing" "testing"
mock "github.com/prysmaticlabs/prysm/v3/beacon-chain/blockchain/testing" mock "github.com/prysmaticlabs/prysm/v3/beacon-chain/blockchain/testing"
builderTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/builder/testing"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/db" "github.com/prysmaticlabs/prysm/v3/beacon-chain/db"
dbTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing" dbTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/db/testing"
executionTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/execution/testing" executionTest "github.com/prysmaticlabs/prysm/v3/beacon-chain/execution/testing"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/operations/synccommittee"
mockp2p "github.com/prysmaticlabs/prysm/v3/beacon-chain/p2p/testing" mockp2p "github.com/prysmaticlabs/prysm/v3/beacon-chain/p2p/testing"
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/prysm/v1alpha1/validator"
"github.com/prysmaticlabs/prysm/v3/config/params" "github.com/prysmaticlabs/prysm/v3/config/params"
"github.com/prysmaticlabs/prysm/v3/consensus-types/blocks" "github.com/prysmaticlabs/prysm/v3/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v3/consensus-types/interfaces"
@ -770,13 +773,21 @@ func TestServer_SubmitBlindedBlockSSZ_OK(t *testing.T) {
require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state")
c := &mock.ChainService{Root: bsRoot[:], State: beaconState} c := &mock.ChainService{Root: bsRoot[:], State: beaconState}
alphaServer := &validator.Server{
SyncCommitteePool: synccommittee.NewStore(),
P2P: &mockp2p.MockBroadcaster{},
BlockBuilder: &builderTest.MockBuilderService{},
BlockReceiver: c,
BlockNotifier: &mock.MockBlockNotifier{},
}
beaconChainServer := &Server{ beaconChainServer := &Server{
BeaconDB: beaconDB, BeaconDB: beaconDB,
BlockReceiver: c, BlockReceiver: c,
ChainInfoFetcher: c, ChainInfoFetcher: c,
BlockNotifier: c.BlockNotifier(), BlockNotifier: c.BlockNotifier(),
Broadcaster: mockp2p.NewTestP2P(t), Broadcaster: mockp2p.NewTestP2P(t),
HeadFetcher: c, HeadFetcher: c,
V1Alpha1ValidatorServer: alphaServer,
} }
req := util.NewBlindedBeaconBlockBellatrix() req := util.NewBlindedBeaconBlockBellatrix()
req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().BellatrixForkEpoch)) req.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().BellatrixForkEpoch))
@ -890,12 +901,20 @@ func TestSubmitBlindedBlock(t *testing.T) {
require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state") require.NoError(t, beaconDB.SaveState(ctx, beaconState, genesisRoot), "Could not save genesis state")
c := &mock.ChainService{Root: bsRoot[:], State: beaconState} c := &mock.ChainService{Root: bsRoot[:], State: beaconState}
alphaServer := &validator.Server{
SyncCommitteePool: synccommittee.NewStore(),
P2P: &mockp2p.MockBroadcaster{},
BlockBuilder: &builderTest.MockBuilderService{},
BlockReceiver: c,
BlockNotifier: &mock.MockBlockNotifier{},
}
beaconChainServer := &Server{ beaconChainServer := &Server{
BeaconDB: beaconDB, BeaconDB: beaconDB,
BlockReceiver: c, BlockReceiver: c,
ChainInfoFetcher: c, ChainInfoFetcher: c,
BlockNotifier: c.BlockNotifier(), BlockNotifier: c.BlockNotifier(),
Broadcaster: mockp2p.NewTestP2P(t), Broadcaster: mockp2p.NewTestP2P(t),
V1Alpha1ValidatorServer: alphaServer,
} }
blk := util.NewBeaconBlockBellatrix() blk := util.NewBeaconBlockBellatrix()