From a2892b1ed560dde4c733a25f6d0cad4f45259160 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 24 Jan 2024 13:48:15 +0800 Subject: [PATCH] clean up validate beacon block (#13517) --- beacon-chain/sync/validate_beacon_blocks.go | 63 ++++++++++++--------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/beacon-chain/sync/validate_beacon_blocks.go b/beacon-chain/sync/validate_beacon_blocks.go index 107cb9dc2..a82b787fc 100644 --- a/beacon-chain/sync/validate_beacon_blocks.go +++ b/beacon-chain/sync/validate_beacon_blocks.go @@ -241,36 +241,11 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk interfaces.ReadOn return err } - if !s.cfg.chain.InForkchoice(blk.Block().ParentRoot()) { - s.setBadBlock(ctx, blockRoot) - return blockchain.ErrNotDescendantOfFinalized - } - - parentState, err := s.cfg.stateGen.StateByRoot(ctx, blk.Block().ParentRoot()) + parentState, err := s.validatePhase0Block(ctx, blk, blockRoot) if err != nil { return err } - if err := blocks.VerifyBlockSignatureUsingCurrentFork(parentState, blk); err != nil { - s.setBadBlock(ctx, blockRoot) - return err - } - // In the event the block is more than an epoch ahead from its - // parent state, we have to advance the state forward. - parentRoot := blk.Block().ParentRoot() - parentState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, parentState, parentRoot[:], blk.Block().Slot()) - if err != nil { - return err - } - idx, err := helpers.BeaconProposerIndex(ctx, parentState) - if err != nil { - return err - } - if blk.Block().ProposerIndex() != idx { - s.setBadBlock(ctx, blockRoot) - return errors.New("incorrect proposer index") - } - if err = s.validateBellatrixBeaconBlock(ctx, parentState, blk.Block()); err != nil { if errors.Is(err, ErrOptimisticParent) { return err @@ -282,6 +257,42 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk interfaces.ReadOn return nil } +// Validates beacon block according to phase 0 validity conditions. +// - Checks that the parent is in our forkchoice tree. +// - Validates that the proposer signature is valid. +// - Validates that the proposer index is valid. +func (s *Service) validatePhase0Block(ctx context.Context, blk interfaces.ReadOnlySignedBeaconBlock, blockRoot [32]byte) (state.BeaconState, error) { + if !s.cfg.chain.InForkchoice(blk.Block().ParentRoot()) { + s.setBadBlock(ctx, blockRoot) + return nil, blockchain.ErrNotDescendantOfFinalized + } + + parentState, err := s.cfg.stateGen.StateByRoot(ctx, blk.Block().ParentRoot()) + if err != nil { + return nil, err + } + + if err := blocks.VerifyBlockSignatureUsingCurrentFork(parentState, blk); err != nil { + return nil, err + } + // In the event the block is more than an epoch ahead from its + // parent state, we have to advance the state forward. + parentRoot := blk.Block().ParentRoot() + parentState, err = transition.ProcessSlotsUsingNextSlotCache(ctx, parentState, parentRoot[:], blk.Block().Slot()) + if err != nil { + return nil, err + } + idx, err := helpers.BeaconProposerIndex(ctx, parentState) + if err != nil { + return nil, err + } + if blk.Block().ProposerIndex() != idx { + s.setBadBlock(ctx, blockRoot) + return nil, errors.New("incorrect proposer index") + } + return parentState, nil +} + func validateDenebBeaconBlock(blk interfaces.ReadOnlyBeaconBlock) error { if blk.Version() < version.Deneb { return nil