From 1b8eb16fc7193d358441dc93cf1184dd39bbcc90 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 7 Dec 2019 14:45:39 -0800 Subject: [PATCH] --initial-sync-cache-state don't need to save head root (#4219) * Test * Run time works * Revert --- beacon-chain/blockchain/receive_block.go | 21 ++++++++++++++----- beacon-chain/blockchain/service.go | 26 ++++++++++++++++++++++++ beacon-chain/blockchain/service_test.go | 23 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 685fd227f..75d556ea4 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -12,6 +12,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/traceutil" "github.com/sirupsen/logrus" "go.opencensus.io/trace" @@ -189,11 +190,21 @@ func (s *Service) ReceiveBlockNoVerify(ctx context.Context, block *ethpb.BeaconB return errors.Wrap(err, "could not get signing root on received blockCopy") } - if !bytes.Equal(root[:], s.HeadRoot()) { - if err := s.saveHead(ctx, blockCopy, root); err != nil { - err := errors.Wrap(err, "could not save head") - traceutil.AnnotateError(span, err) - return err + if featureconfig.Get().InitSyncCacheState { + if !bytes.Equal(root[:], s.HeadRoot()) { + if err := s.saveHeadNoDB(ctx, blockCopy, root); err != nil { + err := errors.Wrap(err, "could not save head") + traceutil.AnnotateError(span, err) + return err + } + } + } else { + if !bytes.Equal(root[:], s.HeadRoot()) { + if err := s.saveHead(ctx, blockCopy, root); err != nil { + err := errors.Wrap(err, "could not save head") + traceutil.AnnotateError(span, err) + return err + } } } diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 14a8f6be2..ec9527e8b 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -255,6 +255,32 @@ func (s *Service) saveHead(ctx context.Context, b *ethpb.BeaconBlock, r [32]byte return nil } +// This gets called to update canonical root mapping. It does not save head block +// root in DB. With the inception of inital-sync-cache-state flag, it uses finalized +// check point as anchors to resume sync therefore head is no longer needed to be saved on per slot basis. +func (s *Service) saveHeadNoDB(ctx context.Context, b *ethpb.BeaconBlock, r [32]byte) error { + s.headLock.Lock() + defer s.headLock.Unlock() + + s.headSlot = b.Slot + + s.canonicalRoots[b.Slot] = r[:] + + s.headBlock = b + + headState, err := s.beaconDB.State(ctx, r) + if err != nil { + return errors.Wrap(err, "could not retrieve head state in DB") + } + s.headState = headState + + log.WithFields(logrus.Fields{ + "slot": b.Slot, + "headRoot": fmt.Sprintf("%#x", r), + }).Debug("Saved new head info") + return nil +} + // This gets called when beacon chain is first initialized to save validator indices and pubkeys in db func (s *Service) saveGenesisValidators(ctx context.Context, state *pb.BeaconState) error { for i, v := range state.Validators { diff --git a/beacon-chain/blockchain/service_test.go b/beacon-chain/blockchain/service_test.go index 206324049..007d7b9cb 100644 --- a/beacon-chain/blockchain/service_test.go +++ b/beacon-chain/blockchain/service_test.go @@ -325,3 +325,26 @@ func TestChainService_InitializeChainInfo(t *testing.T) { t.Error("head slot incorrect") } } + +func TestChainService_SaveHeadNoDB(t *testing.T) { + db := testDB.SetupDB(t) + defer testDB.TeardownDB(t, db) + ctx := context.Background() + s := &Service{ + beaconDB: db, + canonicalRoots: make(map[uint64][]byte), + } + b := ðpb.BeaconBlock{Slot: 1} + r, _ := ssz.SigningRoot(b) + if err := s.saveHeadNoDB(ctx, b, r); err != nil { + t.Fatal(err) + } + + newB, err := s.beaconDB.HeadBlock(ctx) + if err != nil { + t.Fatal(err) + } + if reflect.DeepEqual(newB, b) { + t.Error("head block should not be equal") + } +}