diff --git a/beacon-chain/blockchain/error.go b/beacon-chain/blockchain/error.go index 7cb99ae0e..7e64ccc34 100644 --- a/beacon-chain/blockchain/error.go +++ b/beacon-chain/blockchain/error.go @@ -11,8 +11,6 @@ var ( errNilFinalizedInStore = errors.New("nil finalized checkpoint returned from store") // errInvalidNilSummary is returned when a nil summary is returned from the DB. errInvalidNilSummary = errors.New("nil summary returned from the DB") - // errNilParentInDB is returned when a nil parent block is returned from the DB. - errNilParentInDB = errors.New("nil parent block in DB") // errWrongBlockCount is returned when the wrong number of blocks or // block roots is used errWrongBlockCount = errors.New("wrong number of blocks or block roots") diff --git a/beacon-chain/blockchain/execution_engine.go b/beacon-chain/blockchain/execution_engine.go index 788ce8183..e9aab3815 100644 --- a/beacon-chain/blockchain/execution_engine.go +++ b/beacon-chain/blockchain/execution_engine.go @@ -235,14 +235,10 @@ func (s *Service) optimisticCandidateBlock(ctx context.Context, blk interfaces.B if blk.Slot()+params.BeaconConfig().SafeSlotsToImportOptimistically <= s.CurrentSlot() { return nil } - - parent, err := s.cfg.BeaconDB.Block(ctx, bytesutil.ToBytes32(blk.ParentRoot())) + parent, err := s.getBlock(ctx, bytesutil.ToBytes32(blk.ParentRoot())) if err != nil { return err } - if parent == nil || parent.IsNil() { - return errNilParentInDB - } parentIsExecutionBlock, err := blocks.IsExecutionBlock(parent.Block().Body()) if err != nil { return err diff --git a/beacon-chain/blockchain/head.go b/beacon-chain/blockchain/head.go index a47191e23..f817e180b 100644 --- a/beacon-chain/blockchain/head.go +++ b/beacon-chain/blockchain/head.go @@ -40,7 +40,7 @@ func (s *Service) UpdateAndSaveHeadWithBalances(ctx context.Context) error { if err != nil { return errors.Wrap(err, "could not update head") } - headBlock, err := s.cfg.BeaconDB.Block(ctx, headRoot) + headBlock, err := s.getBlock(ctx, headRoot) if err != nil { return err } @@ -86,7 +86,7 @@ func (s *Service) updateHead(ctx context.Context, balances []uint64) ([32]byte, // re-initiate fork choice store using the latest justified info. // This recovers a fatal condition and should not happen in run time. if !s.cfg.ForkChoiceStore.HasNode(headStartRoot) { - jb, err := s.cfg.BeaconDB.Block(ctx, headStartRoot) + jb, err := s.getBlock(ctx, headStartRoot) if err != nil { return [32]byte{}, err } @@ -355,7 +355,7 @@ func (s *Service) notifyNewHeadEvent( // attestation pool. It also filters out the attestations that is one epoch older as a // defense so invalid attestations don't flow into the attestation pool. func (s *Service) saveOrphanedAtts(ctx context.Context, orphanedRoot [32]byte) error { - orphanedBlk, err := s.cfg.BeaconDB.Block(ctx, orphanedRoot) + orphanedBlk, err := s.getBlock(ctx, orphanedRoot) if err != nil { return err } diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index 5e6840de7..e03dca2cd 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -97,13 +97,10 @@ func (s *Service) VerifyFinalizedBlkDescendant(ctx context.Context, root [32]byt return errNilFinalizedInStore } fRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(finalized.Root)) - finalizedBlkSigned, err := s.cfg.BeaconDB.Block(ctx, fRoot) + finalizedBlkSigned, err := s.getBlock(ctx, fRoot) if err != nil { return err } - if finalizedBlkSigned == nil || finalizedBlkSigned.IsNil() || finalizedBlkSigned.Block().IsNil() { - return errors.New("nil finalized block") - } finalizedBlk := finalizedBlkSigned.Block() bFinalizedRoot, err := s.ancestor(ctx, root[:], finalizedBlk.Slot()) if err != nil { @@ -358,7 +355,7 @@ func (s *Service) fillInForkChoiceMissingBlocks(ctx context.Context, blk interfa higherThanFinalized := slot > fSlot // As long as parent node is not in fork choice store, and parent node is in DB. for !s.cfg.ForkChoiceStore.HasNode(parentRoot) && s.cfg.BeaconDB.HasBlock(ctx, parentRoot) && higherThanFinalized { - b, err := s.cfg.BeaconDB.Block(ctx, parentRoot) + b, err := s.getBlock(ctx, parentRoot) if err != nil { return err } diff --git a/beacon-chain/blockchain/process_block_test.go b/beacon-chain/blockchain/process_block_test.go index 195e36941..e139687ee 100644 --- a/beacon-chain/blockchain/process_block_test.go +++ b/beacon-chain/blockchain/process_block_test.go @@ -1327,7 +1327,7 @@ func TestVerifyBlkDescendant(t *testing.T) { args: args{ finalizedRoot: [32]byte{'a'}, }, - wantedErr: "nil finalized block", + wantedErr: "block not found in cache or db", }, { name: "could not get finalized block root in DB", diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 72970463a..cb094cf9f 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -201,13 +201,10 @@ func (s *Service) StartFromSavedState(saved state.BeaconState) error { forkChoicer = protoarray.New(justified.Epoch, finalized.Epoch, fRoot) } s.cfg.ForkChoiceStore = forkChoicer - fb, err := s.cfg.BeaconDB.Block(s.ctx, s.ensureRootNotZeros(fRoot)) + fb, err := s.getBlock(s.ctx, s.ensureRootNotZeros(fRoot)) if err != nil { return errors.Wrap(err, "could not get finalized checkpoint block") } - if fb == nil || fb.IsNil() { - return errNilFinalizedInStore - } payloadHash, err := getBlockPayloadHash(fb.Block()) if err != nil { return errors.Wrap(err, "could not get execution payload hash") @@ -339,14 +336,13 @@ func (s *Service) initializeHeadFromDB(ctx context.Context) error { finalizedState.Slot(), flags.HeadSync.Name) } } - - finalizedBlock, err := s.cfg.BeaconDB.Block(ctx, finalizedRoot) - if err != nil { - return errors.Wrap(err, "could not get finalized block from db") + if finalizedState == nil || finalizedState.IsNil() { + return errors.New("finalized state can't be nil") } - if finalizedState == nil || finalizedState.IsNil() || finalizedBlock == nil || finalizedBlock.IsNil() { - return errors.New("finalized state and block can't be nil") + finalizedBlock, err := s.getBlock(ctx, finalizedRoot) + if err != nil { + return errors.Wrap(err, "could not get finalized block") } s.setHead(finalizedRoot, finalizedBlock, finalizedState)