Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,11 @@ func (b *BeaconNode) registerRPCService(router *mux.Router) error {
return err
}

var regSyncService *regularsync.Service
if err := b.services.FetchService(&regSyncService); err != nil {
return err
}

var slasherService *slasher.Service
if features.Get().EnableSlasher {
if err := b.services.FetchService(&slasherService); err != nil {
Expand Down Expand Up @@ -834,6 +839,7 @@ func (b *BeaconNode) registerRPCService(router *mux.Router) error {
ProposerIdsCache: b.proposerIdsCache,
BlockBuilder: b.fetchBuilderService(),
Router: router,
RegSyncService: regSyncService,
})

return b.services.RegisterService(rpcService)
Expand Down
37 changes: 25 additions & 12 deletions beacon-chain/rpc/eth/beacon/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/feed"
blockfeed "github.com/prysmaticlabs/prysm/v4/beacon-chain/core/feed/block"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/core/transition"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/db/filters"
rpchelpers "github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/eth/helpers"
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/lookup"
Expand Down Expand Up @@ -198,10 +199,9 @@ func (bs *Server) ListBlockHeaders(ctx context.Context, req *ethpbv1.BlockHeader
}

// SubmitBlock instructs the beacon node to broadcast a newly signed beacon block to the beacon network, to be
// included in the beacon chain. The beacon node is not required to validate the signed ReadOnlyBeaconBlock, and a successful
// response (20X) only indicates that the broadcast has been successful. The beacon node is expected to integrate the
// new block into its state, and therefore validate the block internally, however blocks which fail the validation are
// still broadcast but a different status code is returned (202).
// included in the beacon chain. The beacon node will validate the signed ReadOnlyBeaconBlock, and a successful
// response (20X) only indicates that the broadcast has been successful. The beacon node is not expected to integrate the
// new block into its state.
func (bs *Server) SubmitBlock(ctx context.Context, req *ethpbv2.SignedBeaconBlockContainer) (*emptypb.Empty, error) {
ctx, span := trace.StartSpan(ctx, "beacon.SubmitBlock")
defer span.End()
Expand Down Expand Up @@ -231,10 +231,9 @@ func (bs *Server) SubmitBlock(ctx context.Context, req *ethpbv2.SignedBeaconBloc
}

// SubmitBlockSSZ instructs the beacon node to broadcast a newly signed beacon block to the beacon network, to be
// included in the beacon chain. The beacon node is not required to validate the signed ReadOnlyBeaconBlock, and a successful
// response (20X) only indicates that the broadcast has been successful. The beacon node is expected to integrate the
// new block into its state, and therefore validate the block internally, however blocks which fail the validation are
// still broadcast but a different status code is returned (202).
// included in the beacon chain. The beacon node will validate the signed ReadOnlyBeaconBlock, and a successful
// response (20X) only indicates that the broadcast has been successful. The beacon node is not expected to integrate the
// new block into its state.
//
// The provided block must be SSZ-serialized.
func (bs *Server) SubmitBlockSSZ(ctx context.Context, req *ethpbv2.SSZContainer) (*emptypb.Empty, error) {
Expand Down Expand Up @@ -1019,6 +1018,24 @@ func (bs *Server) submitBlock(ctx context.Context, blockRoot [fieldparams.RootLe
})
}()

if block == nil || block.IsNil() {
return errors.New("nil block")
}
Comment on lines +1021 to +1023
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do this before the defer function?


b := block.Block()
parentState, err := bs.StateGenService.StateByRoot(ctx, b.ParentRoot())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our usual block processing pipeline we verify the block time disparity at this stage. Do we need to do it here?

if err != nil {
return errors.Wrap(err, "could not get parent state")
}
_, err = transition.ExecuteStateTransition(ctx, parentState, block)
if err != nil {
return errors.Wrap(err, "could not execute state transition")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better change the Godoc for SubmitBlockSSZ then.

}

if bs.EquivocationChecker.SeenProposerIndex(b.Slot(), b.ProposerIndex()) {
return fmt.Errorf("block exists in sync service, slot: %d, proposer index: %d", b.Slot(), b.ProposerIndex())
}

// Broadcast the new block to the network.
blockPb, err := block.Proto()
if err != nil {
Expand All @@ -1028,9 +1045,5 @@ func (bs *Server) submitBlock(ctx context.Context, blockRoot [fieldparams.RootLe
return status.Errorf(codes.Internal, "Could not broadcast block: %v", err)
}

if err := bs.BlockReceiver.ReceiveBlock(ctx, block, blockRoot); err != nil {
return status.Errorf(codes.Internal, "Could not process beacon block: %v", err)
}

return nil
}
1 change: 1 addition & 0 deletions beacon-chain/rpc/eth/beacon/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Server struct {
OptimisticModeFetcher blockchain.OptimisticModeFetcher
V1Alpha1ValidatorServer *v1alpha1validator.Server
SyncChecker sync.Checker
EquivocationChecker sync.EquivocationChecker
CanonicalHistory *stategen.CanonicalHistory
ExecutionPayloadReconstructor execution.ExecutionPayloadReconstructor
FinalizationFetcher blockchain.FinalizationFetcher
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/rpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ type Config struct {
SyncCommitteeObjectPool synccommittee.Pool
BLSChangesPool blstoexec.PoolManager
SyncService chainSync.Checker
RegSyncService chainSync.EquivocationChecker
Broadcaster p2p.Broadcaster
PeersFetcher p2p.PeersProvider
PeerManager p2p.PeerManager
Expand Down Expand Up @@ -336,6 +337,7 @@ func (s *Service) Start() {
ExecutionPayloadReconstructor: s.cfg.ExecutionPayloadReconstructor,
BLSChangesPool: s.cfg.BLSChangesPool,
FinalizationFetcher: s.cfg.FinalizationFetcher,
EquivocationChecker: s.cfg.RegSyncService,
}
ethpbv1alpha1.RegisterNodeServer(s.grpcServer, nodeServer)
ethpbservice.RegisterBeaconNodeServer(s.grpcServer, nodeServerV1)
Expand Down
23 changes: 15 additions & 8 deletions beacon-chain/sync/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stategen"
lruwrpr "github.com/prysmaticlabs/prysm/v4/cache/lru"
"github.com/prysmaticlabs/prysm/v4/config/params"
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/runtime"
prysmTime "github.com/prysmaticlabs/prysm/v4/time"
Expand Down Expand Up @@ -110,6 +111,7 @@ type Service struct {
ctx context.Context
cancel context.CancelFunc
slotToPendingBlocks *gcache.Cache
seenProposerIndexCache []primitives.ValidatorIndex
seenPendingBlocks map[[32]byte]bool
blkRootToPendingAtts map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof
subHandler *subTopicHandler
Expand Down Expand Up @@ -146,14 +148,15 @@ func NewService(ctx context.Context, opts ...Option) *Service {
c := gcache.New(pendingBlockExpTime /* exp time */, 2*pendingBlockExpTime /* prune time */)
ctx, cancel := context.WithCancel(ctx)
r := &Service{
ctx: ctx,
cancel: cancel,
chainStarted: abool.New(),
cfg: &config{},
slotToPendingBlocks: c,
seenPendingBlocks: make(map[[32]byte]bool),
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
signatureChan: make(chan *signatureVerifier, verifierLimit),
ctx: ctx,
cancel: cancel,
chainStarted: abool.New(),
cfg: &config{},
slotToPendingBlocks: c,
seenPendingBlocks: make(map[[32]byte]bool),
blkRootToPendingAtts: make(map[[32]byte][]*ethpb.SignedAggregateAttestationAndProof),
signatureChan: make(chan *signatureVerifier, verifierLimit),
seenProposerIndexCache: []primitives.ValidatorIndex{0}, // Index 0 is reserved for slot
}
for _, opt := range opts {
if err := opt(r); err != nil {
Expand Down Expand Up @@ -301,3 +304,7 @@ type Checker interface {
Status() error
Resync() error
}

type EquivocationChecker interface {
SeenProposerIndex(slot primitives.Slot, proposerIdx primitives.ValidatorIndex) bool
}
50 changes: 50 additions & 0 deletions beacon-chain/sync/validate_beacon_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func (s *Service) validateBeaconBlockPubSub(ctx context.Context, pid peer.ID, ms
return pubsub.ValidationReject, errors.New("block.Block is nil")
}

if err := s.verifySignatureAndInsertProposerIndex(ctx, blk); err != nil {
log.WithError(err).Error("could not verify sig and insert proposer index")
}

// Broadcast the block on a feed to notify other services in the beacon node
// of a received block (even if it does not process correctly through a state transition).
s.cfg.blockNotifier.BlockFeed().Send(&feed.Event{
Expand Down Expand Up @@ -384,3 +388,49 @@ func getBlockFields(b interfaces.ReadOnlySignedBeaconBlock) logrus.Fields {
"version": b.Block().Version(),
}
}

func (s *Service) verifySignatureAndInsertProposerIndex(ctx context.Context, blk interfaces.ReadOnlySignedBeaconBlock) error {
headState, err := s.cfg.chain.HeadStateReadOnly(ctx)
if err != nil {
return err
}
if err = blocks.VerifyBlockSignatureUsingCurrentFork(headState, blk); err != nil {
return err
}

s.insertProposerIndexCache(blk.Block().Slot(), blk.Block().ProposerIndex())
return nil
}

func (s *Service) insertProposerIndexCache(slot primitives.Slot, proposerIdx primitives.ValidatorIndex) {
if s.seenProposerIndexCache == nil {
return
}
if s.cfg.chain.CurrentSlot() != slot {
return
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nilcheck for seenProposerIndexCache

switch {
case uint64(s.seenProposerIndexCache[0]) == uint64(slot):
s.seenProposerIndexCache = append(s.seenProposerIndexCache, proposerIdx)
case uint64(slot) > uint64(s.seenProposerIndexCache[0]):
// Overwrite slot in proposer index cache if it's higher.
s.seenProposerIndexCache = []primitives.ValidatorIndex{primitives.ValidatorIndex(slot), proposerIdx}
}
}

func (s *Service) SeenProposerIndex(slot primitives.Slot, proposerIdx primitives.ValidatorIndex) bool {
if s.seenProposerIndexCache == nil {
return false
}

if s.seenProposerIndexCache[0] != primitives.ValidatorIndex(slot) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nilcheck for seenProposerIndexCache

return false
}
for _, i := range s.seenProposerIndexCache[1:] {
if i == proposerIdx {
return true
}
}
return false
}