package beacon import ( "context" "testing" "github.com/golang/mock/gomock" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/prysmaticlabs/prysm/v4/api" mock "github.com/prysmaticlabs/prysm/v4/beacon-chain/blockchain/testing" "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/testutil" mockSync "github.com/prysmaticlabs/prysm/v4/beacon-chain/sync/initial-sync/testing" "github.com/prysmaticlabs/prysm/v4/config/params" "github.com/prysmaticlabs/prysm/v4/consensus-types/blocks" ethpbv1 "github.com/prysmaticlabs/prysm/v4/proto/eth/v1" ethpbv2 "github.com/prysmaticlabs/prysm/v4/proto/eth/v2" "github.com/prysmaticlabs/prysm/v4/proto/migration" "github.com/prysmaticlabs/prysm/v4/testing/assert" mock2 "github.com/prysmaticlabs/prysm/v4/testing/mock" "github.com/prysmaticlabs/prysm/v4/testing/require" "github.com/prysmaticlabs/prysm/v4/testing/util" "google.golang.org/grpc" "google.golang.org/grpc/metadata" ) func TestServer_GetBlindedBlock(t *testing.T) { stream := &runtime.ServerTransportStream{} ctx := grpc.NewContextWithServerTransportStream(context.Background(), stream) t.Run("Phase 0", func(t *testing.T) { b := util.NewBeaconBlock() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) bs := &Server{ FinalizationFetcher: &mock.ChainService{}, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } expected, err := migration.V1Alpha1ToV1SignedBlock(b) require.NoError(t, err) resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) phase0Block, ok := resp.Data.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_Phase0Block) require.Equal(t, true, ok) assert.DeepEqual(t, expected.Block, phase0Block.Phase0Block) assert.Equal(t, ethpbv2.Version_PHASE0, resp.Version) }) t.Run("Altair", func(t *testing.T) { b := util.NewBeaconBlockAltair() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) bs := &Server{ FinalizationFetcher: &mock.ChainService{}, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } expected, err := migration.V1Alpha1BeaconBlockAltairToV2(b.Block) require.NoError(t, err) resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) altairBlock, ok := resp.Data.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_AltairBlock) require.Equal(t, true, ok) assert.DeepEqual(t, expected, altairBlock.AltairBlock) assert.Equal(t, ethpbv2.Version_ALTAIR, resp.Version) }) t.Run("Bellatrix", func(t *testing.T) { b := util.NewBlindedBeaconBlockBellatrix() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) mockChainService := &mock.ChainService{} bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, OptimisticModeFetcher: mockChainService, } expected, err := migration.V1Alpha1BeaconBlockBlindedBellatrixToV2Blinded(b.Block) require.NoError(t, err) resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) bellatrixBlock, ok := resp.Data.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_BellatrixBlock) require.Equal(t, true, ok) assert.DeepEqual(t, expected, bellatrixBlock.BellatrixBlock) assert.Equal(t, ethpbv2.Version_BELLATRIX, resp.Version) }) t.Run("Capella", func(t *testing.T) { b := util.NewBlindedBeaconBlockCapella() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) mockChainService := &mock.ChainService{} bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, OptimisticModeFetcher: mockChainService, } expected, err := migration.V1Alpha1BeaconBlockBlindedCapellaToV2Blinded(b.Block) require.NoError(t, err) resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) capellaBlock, ok := resp.Data.Message.(*ethpbv2.SignedBlindedBeaconBlockContainer_CapellaBlock) require.Equal(t, true, ok) assert.DeepEqual(t, expected, capellaBlock.CapellaBlock) assert.Equal(t, ethpbv2.Version_CAPELLA, resp.Version) }) t.Run("execution optimistic", func(t *testing.T) { b := util.NewBlindedBeaconBlockBellatrix() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) r, err := blk.Block().HashTreeRoot() require.NoError(t, err) mockChainService := &mock.ChainService{ OptimisticRoots: map[[32]byte]bool{r: true}, } bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, OptimisticModeFetcher: mockChainService, } resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) assert.Equal(t, true, resp.ExecutionOptimistic) }) t.Run("finalized", func(t *testing.T) { b := util.NewBeaconBlock() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) root, err := blk.Block().HashTreeRoot() require.NoError(t, err) mockChainService := &mock.ChainService{ FinalizedRoots: map[[32]byte]bool{root: true}, } bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{BlockId: root[:]}) require.NoError(t, err) assert.Equal(t, true, resp.Finalized) }) t.Run("not finalized", func(t *testing.T) { b := util.NewBeaconBlock() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) root, err := blk.Block().HashTreeRoot() require.NoError(t, err) mockChainService := &mock.ChainService{ FinalizedRoots: map[[32]byte]bool{root: false}, } bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } resp, err := bs.GetBlindedBlock(ctx, ðpbv1.BlockRequest{BlockId: root[:]}) require.NoError(t, err) assert.Equal(t, false, resp.Finalized) }) } func TestServer_GetBlindedBlockSSZ(t *testing.T) { ctx := context.Background() t.Run("Phase 0", func(t *testing.T) { b := util.NewBeaconBlock() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) bs := &Server{ FinalizationFetcher: &mock.ChainService{}, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } expected, err := blk.MarshalSSZ() require.NoError(t, err) resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) assert.NotNil(t, resp) assert.DeepEqual(t, expected, resp.Data) assert.Equal(t, ethpbv2.Version_PHASE0, resp.Version) }) t.Run("Altair", func(t *testing.T) { b := util.NewBeaconBlockAltair() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) bs := &Server{ FinalizationFetcher: &mock.ChainService{}, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } expected, err := blk.MarshalSSZ() require.NoError(t, err) resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) assert.NotNil(t, resp) assert.DeepEqual(t, expected, resp.Data) assert.Equal(t, ethpbv2.Version_ALTAIR, resp.Version) }) t.Run("Bellatrix", func(t *testing.T) { b := util.NewBlindedBeaconBlockBellatrix() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) mockChainService := &mock.ChainService{} bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, OptimisticModeFetcher: mockChainService, } expected, err := blk.MarshalSSZ() require.NoError(t, err) resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) assert.NotNil(t, resp) assert.DeepEqual(t, expected, resp.Data) assert.Equal(t, ethpbv2.Version_BELLATRIX, resp.Version) }) t.Run("Capella", func(t *testing.T) { b := util.NewBlindedBeaconBlockCapella() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) mockChainService := &mock.ChainService{} bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, OptimisticModeFetcher: mockChainService, } expected, err := blk.MarshalSSZ() require.NoError(t, err) resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) assert.NotNil(t, resp) assert.DeepEqual(t, expected, resp.Data) assert.Equal(t, ethpbv2.Version_CAPELLA, resp.Version) }) t.Run("execution optimistic", func(t *testing.T) { b := util.NewBlindedBeaconBlockBellatrix() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) r, err := blk.Block().HashTreeRoot() require.NoError(t, err) mockChainService := &mock.ChainService{ OptimisticRoots: map[[32]byte]bool{r: true}, } bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, OptimisticModeFetcher: mockChainService, } resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{}) require.NoError(t, err) assert.Equal(t, true, resp.ExecutionOptimistic) }) t.Run("finalized", func(t *testing.T) { b := util.NewBeaconBlock() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) root, err := blk.Block().HashTreeRoot() require.NoError(t, err) mockChainService := &mock.ChainService{ FinalizedRoots: map[[32]byte]bool{root: true}, } bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: root[:]}) require.NoError(t, err) assert.Equal(t, true, resp.Finalized) }) t.Run("not finalized", func(t *testing.T) { b := util.NewBeaconBlock() blk, err := blocks.NewSignedBeaconBlock(b) require.NoError(t, err) root, err := blk.Block().HashTreeRoot() require.NoError(t, err) mockChainService := &mock.ChainService{ FinalizedRoots: map[[32]byte]bool{root: false}, } bs := &Server{ FinalizationFetcher: mockChainService, Blocker: &testutil.MockBlocker{BlockToReturn: blk}, } resp, err := bs.GetBlindedBlockSSZ(ctx, ðpbv1.BlockRequest{BlockId: root[:]}) require.NoError(t, err) assert.Equal(t, false, resp.Finalized) }) } func TestServer_SubmitBlindedBlockSSZ(t *testing.T) { ctrl := gomock.NewController(t) ctx := context.Background() t.Run("Phase 0", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } b := util.NewBeaconBlock() ssz, err := b.MarshalSSZ() require.NoError(t, err) blockReq := ðpbv2.SSZContainer{ Data: ssz, } md := metadata.MD{} md.Set(api.VersionHeader, "phase0") sszCtx := metadata.NewIncomingContext(ctx, md) _, err = server.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NoError(t, err) }) t.Run("Altair", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } b := util.NewBeaconBlockAltair() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().AltairForkEpoch)) ssz, err := b.MarshalSSZ() require.NoError(t, err) blockReq := ðpbv2.SSZContainer{ Data: ssz, } md := metadata.MD{} md.Set(api.VersionHeader, "altair") sszCtx := metadata.NewIncomingContext(ctx, md) _, err = server.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NoError(t, err) }) t.Run("Bellatrix", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } b := util.NewBlindedBeaconBlockBellatrix() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().BellatrixForkEpoch)) ssz, err := b.MarshalSSZ() require.NoError(t, err) blockReq := ðpbv2.SSZContainer{ Data: ssz, } md := metadata.MD{} md.Set(api.VersionHeader, "bellatrix") sszCtx := metadata.NewIncomingContext(ctx, md) _, err = server.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NoError(t, err) }) t.Run("Bellatrix full", func(t *testing.T) { server := &Server{ SyncChecker: &mockSync.Sync{IsSyncing: false}, } b := util.NewBeaconBlockBellatrix() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().BellatrixForkEpoch)) ssz, err := b.MarshalSSZ() require.NoError(t, err) blockReq := ðpbv2.SSZContainer{ Data: ssz, } md := metadata.MD{} md.Set(api.VersionHeader, "bellatrix") sszCtx := metadata.NewIncomingContext(ctx, md) _, err = server.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NotNil(t, err) }) t.Run("Capella", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } b := util.NewBlindedBeaconBlockCapella() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().CapellaForkEpoch)) ssz, err := b.MarshalSSZ() require.NoError(t, err) blockReq := ðpbv2.SSZContainer{ Data: ssz, } md := metadata.MD{} md.Set(api.VersionHeader, "capella") sszCtx := metadata.NewIncomingContext(ctx, md) _, err = server.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NoError(t, err) }) t.Run("Capella full", func(t *testing.T) { server := &Server{ SyncChecker: &mockSync.Sync{IsSyncing: false}, } b := util.NewBeaconBlockCapella() b.Block.Slot = params.BeaconConfig().SlotsPerEpoch.Mul(uint64(params.BeaconConfig().CapellaForkEpoch)) ssz, err := b.MarshalSSZ() require.NoError(t, err) blockReq := ðpbv2.SSZContainer{ Data: ssz, } md := metadata.MD{} md.Set(api.VersionHeader, "capella") sszCtx := metadata.NewIncomingContext(ctx, md) _, err = server.SubmitBlindedBlockSSZ(sszCtx, blockReq) assert.NotNil(t, err) }) t.Run("sync not ready", func(t *testing.T) { chainService := &mock.ChainService{} v1Server := &Server{ SyncChecker: &mockSync.Sync{IsSyncing: true}, HeadFetcher: chainService, TimeFetcher: chainService, OptimisticModeFetcher: chainService, } _, err := v1Server.SubmitBlindedBlockSSZ(context.Background(), nil) require.ErrorContains(t, "Syncing to latest head", err) }) } func TestSubmitBlindedBlock(t *testing.T) { ctrl := gomock.NewController(t) t.Run("Phase 0", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ Message: ðpbv2.SignedBlindedBeaconBlockContainer_Phase0Block{Phase0Block: ðpbv1.BeaconBlock{}}, Signature: []byte("sig"), } _, err := server.SubmitBlindedBlock(context.Background(), blockReq) assert.NoError(t, err) }) t.Run("Altair", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ Message: ðpbv2.SignedBlindedBeaconBlockContainer_AltairBlock{AltairBlock: ðpbv2.BeaconBlockAltair{}}, Signature: []byte("sig"), } _, err := server.SubmitBlindedBlock(context.Background(), blockReq) assert.NoError(t, err) }) t.Run("Bellatrix", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ Message: ðpbv2.SignedBlindedBeaconBlockContainer_BellatrixBlock{BellatrixBlock: ðpbv2.BlindedBeaconBlockBellatrix{}}, Signature: []byte("sig"), } _, err := server.SubmitBlindedBlock(context.Background(), blockReq) assert.NoError(t, err) }) t.Run("Capella", func(t *testing.T) { v1alpha1Server := mock2.NewMockBeaconNodeValidatorServer(ctrl) v1alpha1Server.EXPECT().ProposeBeaconBlock(gomock.Any(), gomock.Any()) server := &Server{ V1Alpha1ValidatorServer: v1alpha1Server, SyncChecker: &mockSync.Sync{IsSyncing: false}, } blockReq := ðpbv2.SignedBlindedBeaconBlockContainer{ Message: ðpbv2.SignedBlindedBeaconBlockContainer_CapellaBlock{CapellaBlock: ðpbv2.BlindedBeaconBlockCapella{}}, Signature: []byte("sig"), } _, err := server.SubmitBlindedBlock(context.Background(), blockReq) assert.NoError(t, err) }) t.Run("sync not ready", func(t *testing.T) { chainService := &mock.ChainService{} v1Server := &Server{ SyncChecker: &mockSync.Sync{IsSyncing: true}, HeadFetcher: chainService, TimeFetcher: chainService, OptimisticModeFetcher: chainService, } _, err := v1Server.SubmitBlindedBlock(context.Background(), nil) require.ErrorContains(t, "Syncing to latest head", err) }) }