Blockchain: Use get_block to retrieve block (#10688)

* Use get block

* Remove unused errNilParentInDB

* Fix TestVerifyBlkDescendant

* Update beacon-chain/blockchain/service.go

Co-authored-by: Potuz <potuz@prysmaticlabs.com>

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
This commit is contained in:
terencechain 2022-05-14 19:23:01 -07:00 committed by GitHub
parent 8cd43d216f
commit a90335b15e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 26 deletions

View File

@ -11,8 +11,6 @@ var (
errNilFinalizedInStore = errors.New("nil finalized checkpoint returned from store") errNilFinalizedInStore = errors.New("nil finalized checkpoint returned from store")
// errInvalidNilSummary is returned when a nil summary is returned from the DB. // errInvalidNilSummary is returned when a nil summary is returned from the DB.
errInvalidNilSummary = errors.New("nil summary 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 // errWrongBlockCount is returned when the wrong number of blocks or
// block roots is used // block roots is used
errWrongBlockCount = errors.New("wrong number of blocks or block roots") errWrongBlockCount = errors.New("wrong number of blocks or block roots")

View File

@ -235,14 +235,10 @@ func (s *Service) optimisticCandidateBlock(ctx context.Context, blk interfaces.B
if blk.Slot()+params.BeaconConfig().SafeSlotsToImportOptimistically <= s.CurrentSlot() { if blk.Slot()+params.BeaconConfig().SafeSlotsToImportOptimistically <= s.CurrentSlot() {
return nil return nil
} }
parent, err := s.getBlock(ctx, bytesutil.ToBytes32(blk.ParentRoot()))
parent, err := s.cfg.BeaconDB.Block(ctx, bytesutil.ToBytes32(blk.ParentRoot()))
if err != nil { if err != nil {
return err return err
} }
if parent == nil || parent.IsNil() {
return errNilParentInDB
}
parentIsExecutionBlock, err := blocks.IsExecutionBlock(parent.Block().Body()) parentIsExecutionBlock, err := blocks.IsExecutionBlock(parent.Block().Body())
if err != nil { if err != nil {
return err return err

View File

@ -40,7 +40,7 @@ func (s *Service) UpdateAndSaveHeadWithBalances(ctx context.Context) error {
if err != nil { if err != nil {
return errors.Wrap(err, "could not update head") 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 { if err != nil {
return err 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. // re-initiate fork choice store using the latest justified info.
// This recovers a fatal condition and should not happen in run time. // This recovers a fatal condition and should not happen in run time.
if !s.cfg.ForkChoiceStore.HasNode(headStartRoot) { if !s.cfg.ForkChoiceStore.HasNode(headStartRoot) {
jb, err := s.cfg.BeaconDB.Block(ctx, headStartRoot) jb, err := s.getBlock(ctx, headStartRoot)
if err != nil { if err != nil {
return [32]byte{}, err 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 // 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. // defense so invalid attestations don't flow into the attestation pool.
func (s *Service) saveOrphanedAtts(ctx context.Context, orphanedRoot [32]byte) error { 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 { if err != nil {
return err return err
} }

View File

@ -97,13 +97,10 @@ func (s *Service) VerifyFinalizedBlkDescendant(ctx context.Context, root [32]byt
return errNilFinalizedInStore return errNilFinalizedInStore
} }
fRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(finalized.Root)) fRoot := s.ensureRootNotZeros(bytesutil.ToBytes32(finalized.Root))
finalizedBlkSigned, err := s.cfg.BeaconDB.Block(ctx, fRoot) finalizedBlkSigned, err := s.getBlock(ctx, fRoot)
if err != nil { if err != nil {
return err return err
} }
if finalizedBlkSigned == nil || finalizedBlkSigned.IsNil() || finalizedBlkSigned.Block().IsNil() {
return errors.New("nil finalized block")
}
finalizedBlk := finalizedBlkSigned.Block() finalizedBlk := finalizedBlkSigned.Block()
bFinalizedRoot, err := s.ancestor(ctx, root[:], finalizedBlk.Slot()) bFinalizedRoot, err := s.ancestor(ctx, root[:], finalizedBlk.Slot())
if err != nil { if err != nil {
@ -358,7 +355,7 @@ func (s *Service) fillInForkChoiceMissingBlocks(ctx context.Context, blk interfa
higherThanFinalized := slot > fSlot higherThanFinalized := slot > fSlot
// As long as parent node is not in fork choice store, and parent node is in DB. // 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 { 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 { if err != nil {
return err return err
} }

View File

@ -1327,7 +1327,7 @@ func TestVerifyBlkDescendant(t *testing.T) {
args: args{ args: args{
finalizedRoot: [32]byte{'a'}, 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", name: "could not get finalized block root in DB",

View File

@ -201,13 +201,10 @@ func (s *Service) StartFromSavedState(saved state.BeaconState) error {
forkChoicer = protoarray.New(justified.Epoch, finalized.Epoch, fRoot) forkChoicer = protoarray.New(justified.Epoch, finalized.Epoch, fRoot)
} }
s.cfg.ForkChoiceStore = forkChoicer 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 { if err != nil {
return errors.Wrap(err, "could not get finalized checkpoint block") return errors.Wrap(err, "could not get finalized checkpoint block")
} }
if fb == nil || fb.IsNil() {
return errNilFinalizedInStore
}
payloadHash, err := getBlockPayloadHash(fb.Block()) payloadHash, err := getBlockPayloadHash(fb.Block())
if err != nil { if err != nil {
return errors.Wrap(err, "could not get execution payload hash") 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) finalizedState.Slot(), flags.HeadSync.Name)
} }
} }
if finalizedState == nil || finalizedState.IsNil() {
finalizedBlock, err := s.cfg.BeaconDB.Block(ctx, finalizedRoot) return errors.New("finalized state can't be nil")
if err != nil {
return errors.Wrap(err, "could not get finalized block from db")
} }
if finalizedState == nil || finalizedState.IsNil() || finalizedBlock == nil || finalizedBlock.IsNil() { finalizedBlock, err := s.getBlock(ctx, finalizedRoot)
return errors.New("finalized state and block can't be nil") if err != nil {
return errors.Wrap(err, "could not get finalized block")
} }
s.setHead(finalizedRoot, finalizedBlock, finalizedState) s.setHead(finalizedRoot, finalizedBlock, finalizedState)