Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup Validate Beacon Block #13517

Merged
merged 1 commit into from
Jan 24, 2024
Merged
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
45 changes: 28 additions & 17 deletions beacon-chain/sync/validate_beacon_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,45 +241,56 @@ func (s *Service) validateBeaconBlock(ctx context.Context, blk interfaces.ReadOn
return err
}

parentState, err := s.validatePhase0Block(ctx, blk, blockRoot)
if err != nil {
return err
}

if err = s.validateBellatrixBeaconBlock(ctx, parentState, blk.Block()); err != nil {
if errors.Is(err, ErrOptimisticParent) {
return err
}
// for other kinds of errors, set this block as a bad block.
s.setBadBlock(ctx, blockRoot)
return err
}
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 blockchain.ErrNotDescendantOfFinalized
return nil, blockchain.ErrNotDescendantOfFinalized
}

parentState, err := s.cfg.stateGen.StateByRoot(ctx, blk.Block().ParentRoot())
if err != nil {
return err
return nil, err
}

if err := blocks.VerifyBlockSignatureUsingCurrentFork(parentState, blk); err != nil {
s.setBadBlock(ctx, blockRoot)
return err
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 err
return nil, err
}
idx, err := helpers.BeaconProposerIndex(ctx, parentState)
if err != nil {
return err
return nil, err
}
if blk.Block().ProposerIndex() != idx {
s.setBadBlock(ctx, blockRoot)
return errors.New("incorrect proposer index")
return nil, errors.New("incorrect proposer index")
}

if err = s.validateBellatrixBeaconBlock(ctx, parentState, blk.Block()); err != nil {
if errors.Is(err, ErrOptimisticParent) {
return err
}
// for other kinds of errors, set this block as a bad block.
s.setBadBlock(ctx, blockRoot)
return err
}
return nil
return parentState, nil
}

func validateDenebBeaconBlock(blk interfaces.ReadOnlyBeaconBlock) error {
Expand Down
Loading