From 512ef281135a0b7e18c9617b6aa05681ccc72a42 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 25 Mar 2020 16:59:26 -0700 Subject: [PATCH 01/39] Starting a quick PoC --- beacon-chain/blockchain/process_block.go | 10 +++++++--- beacon-chain/blockchain/service.go | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index a8d77726f1ab..9cfc84a65cd7 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -210,9 +210,8 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed return errors.Wrap(err, "could not execute state transition") } - if err := s.beaconDB.SaveBlock(ctx, signed); err != nil { - return errors.Wrapf(err, "could not save block from slot %d", b.Slot) - } + s.initSyncBlocks = append(s.initSyncBlocks, signed) + root, err := ssz.HashTreeRoot(b) if err != nil { return errors.Wrapf(err, "could not get signing root of block %d", b.Slot) @@ -264,6 +263,11 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed } } + if err := s.beaconDB.SaveBlocks(ctx, s.initSyncBlocks); err != nil { + return err + } + s.initSyncBlocks = []*ethpb.SignedBeaconBlock{} + if err := s.beaconDB.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint()); err != nil { return errors.Wrap(err, "could not save finalized checkpoint") } diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 9e28e76c4d4e..5c219f1fc706 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -75,6 +75,7 @@ type Service struct { checkpointStateLock sync.Mutex stateGen *stategen.State opsService *attestations.Service + initSyncBlocks []*ethpb.SignedBeaconBlock } // Config options for the service. @@ -117,6 +118,7 @@ func NewService(ctx context.Context, cfg *Config) (*Service, error) { checkpointState: cache.NewCheckpointStateCache(), opsService: cfg.OpsService, stateGen: cfg.StateGen, + initSyncBlocks: []*ethpb.SignedBeaconBlock{}, }, nil } From 7a370b01f90ee5648b11fa7856a13a624d4fce1e Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 25 Mar 2020 17:19:41 -0700 Subject: [PATCH 02/39] Rate limit to one epoch worth of blocks in memory --- beacon-chain/blockchain/process_block.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 9cfc84a65cd7..0bc6fc249b81 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -15,6 +15,7 @@ import ( "github.com/prysmaticlabs/prysm/shared/attestationutil" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" + "github.com/prysmaticlabs/prysm/shared/params" "github.com/sirupsen/logrus" "go.opencensus.io/trace" ) @@ -246,6 +247,14 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed } } + // Rate limit how many blocks we keep in the memory. + if len(s.initSyncBlocks) > int(params.BeaconConfig().SlotsPerEpoch) { + if err := s.beaconDB.SaveBlocks(ctx, s.initSyncBlocks); err != nil { + return err + } + s.initSyncBlocks = []*ethpb.SignedBeaconBlock{} + } + // Update finalized check point. Prune the block cache and helper caches on every new finalized epoch. if postState.FinalizedCheckpointEpoch() > s.finalizedCheckpt.Epoch { if !featureconfig.Get().NewStateMgmt { From 5ef3f0bd4ab2ecb112797d638ef6344cab005926 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 25 Mar 2020 17:51:19 -0700 Subject: [PATCH 03/39] Proof of concept working --- beacon-chain/blockchain/process_block.go | 8 ++++---- beacon-chain/blockchain/process_block_helpers.go | 10 ++++++---- beacon-chain/db/kv/checkpoint.go | 3 ++- beacon-chain/sync/initial-sync-old/round_robin.go | 5 ----- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 0bc6fc249b81..f1c0186f5a2c 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -248,7 +248,7 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed } // Rate limit how many blocks we keep in the memory. - if len(s.initSyncBlocks) > int(params.BeaconConfig().SlotsPerEpoch) { + if len(s.initSyncBlocks) > 2*int(params.BeaconConfig().SlotsPerEpoch) { if err := s.beaconDB.SaveBlocks(ctx, s.initSyncBlocks); err != nil { return err } @@ -284,9 +284,9 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed s.prevFinalizedCheckpt = s.finalizedCheckpt s.finalizedCheckpt = postState.FinalizedCheckpoint() - if err := s.finalizedImpliesNewJustified(ctx, postState); err != nil { - return errors.Wrap(err, "could not save new justified") - } + //if err := s.finalizedImpliesNewJustified(ctx, postState); err != nil { + // return errors.Wrap(err, "could not save new justified") + //} if featureconfig.Get().NewStateMgmt { fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root) diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index ee05f601df59..60768d9edb4c 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -263,10 +263,11 @@ func (s *Service) updateJustified(ctx context.Context, state *stateTrie.BeaconSt if cpt.Epoch > s.bestJustifiedCheckpt.Epoch { s.bestJustifiedCheckpt = cpt } - canUpdate, err := s.shouldUpdateCurrentJustified(ctx, cpt) - if err != nil { - return err - } + //canUpdate, err := s.shouldUpdateCurrentJustified(ctx, cpt) + //if err != nil { + // return err + //} + canUpdate := true if canUpdate { s.prevJustifiedCheckpt = s.justifiedCheckpt s.justifiedCheckpt = cpt @@ -278,6 +279,7 @@ func (s *Service) updateJustified(ctx context.Context, state *stateTrie.BeaconSt justifiedState := s.initSyncState[justifiedRoot] // If justified state is nil, resume back to normal syncing process and save // justified check point. + var err error if justifiedState == nil { if s.beaconDB.HasState(ctx, justifiedRoot) { return s.beaconDB.SaveJustifiedCheckpoint(ctx, cpt) diff --git a/beacon-chain/db/kv/checkpoint.go b/beacon-chain/db/kv/checkpoint.go index d1d769b17781..fe7408c15c52 100644 --- a/beacon-chain/db/kv/checkpoint.go +++ b/beacon-chain/db/kv/checkpoint.go @@ -109,6 +109,7 @@ func (k *Store) SaveFinalizedCheckpoint(ctx context.Context, checkpoint *ethpb.C if err := bucket.Put(finalizedCheckpointKey, enc); err != nil { return err } - return k.updateFinalizedBlockRoots(ctx, tx, checkpoint) + return nil + //return k.updateFinalizedBlockRoots(ctx, tx, checkpoint) }) } diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index 89508724ef57..a36fadfae771 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -19,7 +19,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/flags" prysmsync "github.com/prysmaticlabs/prysm/beacon-chain/sync" p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" - "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/mathutil" "github.com/prysmaticlabs/prysm/shared/params" @@ -209,10 +208,6 @@ func (s *Service) roundRobinSync(genesis time.Time) error { for _, blk := range blocks { s.logSyncStatus(genesis, blk.Block, peers, counter) - if !s.db.HasBlock(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) { - log.Debugf("Beacon node doesn't have a block in db with root %#x", blk.Block.ParentRoot) - continue - } s.blockNotifier.BlockFeed().Send(&feed.Event{ Type: blockfeed.ReceivedBlock, Data: &blockfeed.ReceivedBlockData{SignedBlock: blk}, From 96d78eab596d06640f674117e47841d2fd8c93fc Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 25 Mar 2020 17:53:57 -0700 Subject: [PATCH 04/39] Quick comment out --- beacon-chain/sync/initial-sync-old/round_robin.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index a36fadfae771..c2efe96bf716 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -208,6 +208,10 @@ func (s *Service) roundRobinSync(genesis time.Time) error { for _, blk := range blocks { s.logSyncStatus(genesis, blk.Block, peers, counter) + //if !s.db.HasBlock(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) { + // log.Debugf("Beacon node doesn't have a block in db with root %#x", blk.Block.ParentRoot) + // continue + //} s.blockNotifier.BlockFeed().Send(&feed.Event{ Type: blockfeed.ReceivedBlock, Data: &blockfeed.ReceivedBlockData{SignedBlock: blk}, From 4197c1db95c14d0842a883a750237588c0324cb6 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Wed, 25 Mar 2020 23:49:22 -0700 Subject: [PATCH 05/39] Save previous finalized checkpoint --- beacon-chain/db/iface/interface.go | 1 + beacon-chain/db/kv/checkpoint.go | 20 +++++++++++++++++++ beacon-chain/db/kv/finalized_block_roots.go | 1 + .../sync/initial-sync-old/round_robin.go | 11 +++++----- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index a926709f4416..bba88384f8a3 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -47,6 +47,7 @@ type ReadOnlyDatabase interface { // Checkpoint operations. JustifiedCheckpoint(ctx context.Context) (*eth.Checkpoint, error) FinalizedCheckpoint(ctx context.Context) (*eth.Checkpoint, error) + PrevFinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) // Archival data handlers for storing/retrieving historical beacon node information. ArchivedActiveValidatorChanges(ctx context.Context, epoch uint64) (*ethereum_beacon_p2p_v1.ArchivedActiveSetChanges, error) ArchivedCommitteeInfo(ctx context.Context, epoch uint64) (*ethereum_beacon_p2p_v1.ArchivedCommitteeInfo, error) diff --git a/beacon-chain/db/kv/checkpoint.go b/beacon-chain/db/kv/checkpoint.go index 82da461d0228..1edbb2c2ca5e 100644 --- a/beacon-chain/db/kv/checkpoint.go +++ b/beacon-chain/db/kv/checkpoint.go @@ -53,6 +53,26 @@ func (k *Store) FinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, err return checkpoint, err } +// PrevFinalizedCheckpoint returns the previous latest finalized checkpoint in beacon chain. +func (k *Store) PrevFinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) { + ctx, span := trace.StartSpan(ctx, "PrevFinalizedCheckpoint.FinalizedCheckpoint") + defer span.End() + var checkpoint *ethpb.Checkpoint + err := k.db.View(func(tx *bolt.Tx) error { + bkt := tx.Bucket(finalizedBlockRootsIndexBucket) + enc := bkt.Get(previousFinalizedCheckpointKey) + if enc == nil { + blockBucket := tx.Bucket(blocksBucket) + genesisRoot := blockBucket.Get(genesisBlockRootKey) + checkpoint = ðpb.Checkpoint{Root: genesisRoot} + return nil + } + checkpoint = ðpb.Checkpoint{} + return decode(enc, checkpoint) + }) + return checkpoint, err +} + // SaveJustifiedCheckpoint saves justified checkpoint in beacon chain. func (k *Store) SaveJustifiedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveJustifiedCheckpoint") diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index a7fc0bb6bc76..a38332be1510 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -55,6 +55,7 @@ func (k *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec return err } } + fmt.Println(previousFinalizedCheckpoint.Epoch, checkpoint.Epoch) blockRoots, err := k.BlockRoots(ctx, filters.NewFilter(). SetStartEpoch(previousFinalizedCheckpoint.Epoch). SetEndEpoch(checkpoint.Epoch+1), diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index 89508724ef57..b267c5c54635 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -188,12 +188,13 @@ func (s *Service) roundRobinSync(genesis time.Time) error { break } + c, _ := s.db.PrevFinalizedCheckpoint(ctx) blocks, err := request( - s.chain.HeadSlot()+1, // start - 1, // step - blockBatchSize, // count - peers, // peers - 0, // remainder + helpers.StartSlot(c.Epoch)+1, // start + 1, // step + blockBatchSize, // count + peers, // peers + 0, // remainder ) if err != nil { log.WithError(err).Error("Round robing sync request failed") From cf92a434f1b9e49b75df70c5811e24e8a925e4a7 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Thu, 26 Mar 2020 00:08:36 -0700 Subject: [PATCH 06/39] Test --- beacon-chain/db/kv/checkpoint.go | 4 +- beacon-chain/db/kv/finalized_block_roots.go | 117 ++++++++++---------- 2 files changed, 59 insertions(+), 62 deletions(-) diff --git a/beacon-chain/db/kv/checkpoint.go b/beacon-chain/db/kv/checkpoint.go index c1576ee4cc0e..0018d6ad01e6 100644 --- a/beacon-chain/db/kv/checkpoint.go +++ b/beacon-chain/db/kv/checkpoint.go @@ -109,7 +109,7 @@ func (k *Store) SaveFinalizedCheckpoint(ctx context.Context, checkpoint *ethpb.C if err := bucket.Put(finalizedCheckpointKey, enc); err != nil { return err } - return nil - //return k.updateFinalizedBlockRoots(ctx, tx, checkpoint) + + return k.updateFinalizedBlockRoots(ctx, tx, checkpoint) }) } diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index a7fc0bb6bc76..1f9b7ee73164 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -3,12 +3,9 @@ package kv import ( "bytes" "context" - "fmt" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" - dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" - "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/traceutil" bolt "go.etcd.io/bbolt" "go.opencensus.io/trace" @@ -43,9 +40,9 @@ func (k *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec bkt := tx.Bucket(finalizedBlockRootsIndexBucket) - root := checkpoint.Root - var previousRoot []byte - genesisRoot := tx.Bucket(blocksBucket).Get(genesisBlockRootKey) + //root := checkpoint.Root + //var previousRoot []byte + //genesisRoot := tx.Bucket(blocksBucket).Get(genesisBlockRootKey) // De-index recent finalized block roots, to be re-indexed. previousFinalizedCheckpoint := ðpb.Checkpoint{} @@ -72,60 +69,60 @@ func (k *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec // Walk up the ancestry chain until we reach a block root present in the finalized block roots // index bucket or genesis block root. - for { - if bytes.Equal(root, genesisRoot) { - break - } - - signedBlock, err := k.Block(ctx, bytesutil.ToBytes32(root)) - if err != nil { - traceutil.AnnotateError(span, err) - return err - } - if signedBlock == nil || signedBlock.Block == nil { - err := fmt.Errorf("missing block in database: block root=%#x", root) - traceutil.AnnotateError(span, err) - return err - } - block := signedBlock.Block - - container := &dbpb.FinalizedBlockRootContainer{ - ParentRoot: block.ParentRoot, - ChildRoot: previousRoot, - } - - enc, err := encode(container) - if err != nil { - traceutil.AnnotateError(span, err) - return err - } - if err := bkt.Put(root, enc); err != nil { - traceutil.AnnotateError(span, err) - return err - } - - // Found parent, loop exit condition. - if parentBytes := bkt.Get(block.ParentRoot); parentBytes != nil { - parent := &dbpb.FinalizedBlockRootContainer{} - if err := decode(parentBytes, parent); err != nil { - traceutil.AnnotateError(span, err) - return err - } - parent.ChildRoot = root - enc, err := encode(parent) - if err != nil { - traceutil.AnnotateError(span, err) - return err - } - if err := bkt.Put(block.ParentRoot, enc); err != nil { - traceutil.AnnotateError(span, err) - return err - } - break - } - previousRoot = root - root = block.ParentRoot - } + //for { + // if bytes.Equal(root, genesisRoot) { + // break + // } + // + // signedBlock, err := k.Block(ctx, bytesutil.ToBytes32(root)) + // if err != nil { + // traceutil.AnnotateError(span, err) + // return err + // } + // if signedBlock == nil || signedBlock.Block == nil { + // err := fmt.Errorf("missing block in database: block root=%#x", root) + // traceutil.AnnotateError(span, err) + // return err + // } + // block := signedBlock.Block + // + // container := &dbpb.FinalizedBlockRootContainer{ + // ParentRoot: block.ParentRoot, + // ChildRoot: previousRoot, + // } + // + // enc, err := encode(container) + // if err != nil { + // traceutil.AnnotateError(span, err) + // return err + // } + // if err := bkt.Put(root, enc); err != nil { + // traceutil.AnnotateError(span, err) + // return err + // } + // + // // Found parent, loop exit condition. + // if parentBytes := bkt.Get(block.ParentRoot); parentBytes != nil { + // parent := &dbpb.FinalizedBlockRootContainer{} + // if err := decode(parentBytes, parent); err != nil { + // traceutil.AnnotateError(span, err) + // return err + // } + // parent.ChildRoot = root + // enc, err := encode(parent) + // if err != nil { + // traceutil.AnnotateError(span, err) + // return err + // } + // if err := bkt.Put(block.ParentRoot, enc); err != nil { + // traceutil.AnnotateError(span, err) + // return err + // } + // break + // } + // previousRoot = root + // root = block.ParentRoot + //} // Upsert blocks from the current finalized epoch. roots, err := k.BlockRoots(ctx, filters.NewFilter().SetStartEpoch(checkpoint.Epoch).SetEndEpoch(checkpoint.Epoch+1)) From 7e8c0b92d6af55e0a987679e100bb9d48bbda75c Mon Sep 17 00:00:00 2001 From: terence tsao Date: Thu, 26 Mar 2020 07:11:19 -0700 Subject: [PATCH 07/39] Minor fixes --- beacon-chain/db/kv/finalized_block_roots.go | 1 + beacon-chain/sync/initial-sync-old/round_robin.go | 12 ++++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index 94a44f072d50..9e6cc4226d91 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -3,6 +3,7 @@ package kv import ( "bytes" "context" + "fmt" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index 14e53ab68361..f31768f91642 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -187,13 +187,13 @@ func (s *Service) roundRobinSync(genesis time.Time) error { break } - c, _ := s.db.PrevFinalizedCheckpoint(ctx) + //c, _ := s.db.PrevFinalizedCheckpoint(ctx) blocks, err := request( - helpers.StartSlot(c.Epoch)+1, // start - 1, // step - blockBatchSize, // count - peers, // peers - 0, // remainder + s.chain.HeadSlot(), // start + 1, // step + blockBatchSize, // count + peers, // peers + 0, // remainder ) if err != nil { log.WithError(err).Error("Round robing sync request failed") From 7e7273abae589413272f4210d3ac15945853ff5e Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 27 Mar 2020 23:32:14 -0700 Subject: [PATCH 08/39] More run time fixes --- .../blockchain/init_sync_process_block.go | 15 ++++++++++----- beacon-chain/blockchain/process_block_helpers.go | 13 ++++++++++--- beacon-chain/db/kv/blocks.go | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index 3cc99fc6ba68..555a80ec2a2a 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -2,7 +2,6 @@ package blockchain import ( "context" - "fmt" "sort" "github.com/pkg/errors" @@ -172,10 +171,17 @@ func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot if preState == nil { return nil, errors.New("finalized state does not exist in db") } - endBlock, err := s.beaconDB.Block(ctx, endRoot) - if err != nil { - return nil, err + + var endBlock *ethpb.SignedBeaconBlock + if s.hasInitSyncBlock(endRoot) { + endBlock = s.getInitSyncBlock(endRoot) + } else { + endBlock, err = s.beaconDB.Block(ctx, endRoot) + if err != nil { + return nil, err + } } + if endBlock == nil { return nil, errors.New("provided block root does not have block saved in the db") } @@ -226,6 +232,5 @@ func (s *Service) getInitSyncBlocks() []*ethpb.SignedBeaconBlock { func (s *Service) clearInitSyncBlocks() { s.initSyncBlocksLock.Lock() defer s.initSyncBlocksLock.Unlock() - fmt.Println("Clearing the cache!") s.initSyncBlocks = make(map[[32]byte]*ethpb.SignedBeaconBlock) } diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index da216075fcc0..5ef20a2ef8de 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -247,10 +247,17 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified if newJustifiedBlock.Slot <= helpers.StartSlot(s.justifiedCheckpt.Epoch) { return false, nil } - justifiedBlockSigned, err := s.beaconDB.Block(ctx, bytesutil.ToBytes32(s.justifiedCheckpt.Root)) - if err != nil { - return false, err + var justifiedBlockSigned *ethpb.SignedBeaconBlock + cachedJustifiedRoot := bytesutil.ToBytes32(s.justifiedCheckpt.Root) + if s.hasInitSyncBlock(cachedJustifiedRoot) { + justifiedBlockSigned = s.getInitSyncBlock(cachedJustifiedRoot) + } else { + justifiedBlockSigned, err = s.beaconDB.Block(ctx, cachedJustifiedRoot) + if err != nil { + return false, err + } } + if justifiedBlockSigned == nil || justifiedBlockSigned.Block == nil { return false, errors.New("nil justified block") } diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index 478568eada2f..a07a0f137186 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -237,7 +237,7 @@ func (k *Store) SaveBlocks(ctx context.Context, blocks []*ethpb.SignedBeaconBloc } if existingBlock := bkt.Get(blockRoot[:]); existingBlock != nil { - return nil + continue } enc, err := encode(block) if err != nil { From 9ba5b119f78dd47106af7fbe938c6303bbbbd8ec Mon Sep 17 00:00:00 2001 From: terence tsao Date: Fri, 27 Mar 2020 23:32:46 -0700 Subject: [PATCH 09/39] Remove panic --- beacon-chain/sync/initial-sync-old/round_robin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index e2af546567fc..e5bf7e2d63fb 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -218,7 +218,7 @@ func (s *Service) roundRobinSync(genesis time.Time) error { parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) if !s.db.HasBlock(ctx, parentRoot) && !s.chain.HasInitSyncBlock(parentRoot) { log.Warnf("Beacon node doesn't have a block in db or cache with root %#x", parentRoot) - panic("fuck") + continue } s.blockNotifier.BlockFeed().Send(&feed.Event{ From 0281a5fdf000a906cb0b305116cb51b05f2354a1 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 28 Mar 2020 09:15:46 -0700 Subject: [PATCH 10/39] Feature flag --- .../blockchain/init_sync_process_block.go | 11 +- beacon-chain/blockchain/process_block.go | 18 ++- .../blockchain/process_block_helpers.go | 7 +- beacon-chain/db/kv/finalized_block_roots.go | 117 +++++++++--------- .../sync/initial-sync-old/round_robin.go | 42 +++++-- shared/featureconfig/config.go | 5 + shared/featureconfig/flags.go | 6 + 7 files changed, 127 insertions(+), 79 deletions(-) diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index 555a80ec2a2a..c306ec2744a3 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/shared/bytesutil" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" ) @@ -173,7 +174,7 @@ func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot } var endBlock *ethpb.SignedBeaconBlock - if s.hasInitSyncBlock(endRoot) { + if featureconfig.Get().InitSyncBatchSaveBlocks && s.hasInitSyncBlock(endRoot) { endBlock = s.getInitSyncBlock(endRoot) } else { endBlock, err = s.beaconDB.Block(ctx, endRoot) @@ -198,12 +199,15 @@ func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot return postState, nil } +// This saves a beacon block to the initial sync blocks cache. func (s *Service) saveInitSyncBlock(r [32]byte, b *ethpb.SignedBeaconBlock) { s.initSyncBlocksLock.Lock() defer s.initSyncBlocksLock.Unlock() s.initSyncBlocks[r] = b } +// This checks if a beacon block exists in the initial sync blocks cache using the root +// of the block. func (s *Service) hasInitSyncBlock(r [32]byte) bool { s.initSyncBlocksLock.RLock() defer s.initSyncBlocksLock.RUnlock() @@ -211,6 +215,8 @@ func (s *Service) hasInitSyncBlock(r [32]byte) bool { return ok } +// This retrieves a beacon block from the initial sync blocks cache using the root of +// the block. func (s *Service) getInitSyncBlock(r [32]byte) *ethpb.SignedBeaconBlock { s.initSyncBlocksLock.RLock() defer s.initSyncBlocksLock.RUnlock() @@ -218,6 +224,8 @@ func (s *Service) getInitSyncBlock(r [32]byte) *ethpb.SignedBeaconBlock { return b } +// This retrieves all the beacon blocks from the initial sync blocks cache, the returned +// blocks are unordered. func (s *Service) getInitSyncBlocks() []*ethpb.SignedBeaconBlock { s.initSyncBlocksLock.RLock() defer s.initSyncBlocksLock.RUnlock() @@ -229,6 +237,7 @@ func (s *Service) getInitSyncBlocks() []*ethpb.SignedBeaconBlock { return blks } +// This clears out the initial sync blocks cache. func (s *Service) clearInitSyncBlocks() { s.initSyncBlocksLock.Lock() defer s.initSyncBlocksLock.Unlock() diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index d86d5529c8e9..57c827f93630 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -215,7 +215,13 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed if err != nil { return errors.Wrapf(err, "could not get signing root of block %d", b.Slot) } - s.saveInitSyncBlock(root, signed) + if featureconfig.Get().InitSyncBatchSaveBlocks { + s.saveInitSyncBlock(root, signed) + } else { + if err := s.beaconDB.SaveBlock(ctx, signed); err != nil { + return errors.Wrapf(err, "could not save block from slot %d", b.Slot) + } + } if err := s.insertBlockToForkChoiceStore(ctx, b, root, postState); err != nil { return errors.Wrapf(err, "could not insert block %d to fork choice store", b.Slot) @@ -246,7 +252,7 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed } } - // Rate limit how many blocks we keep in the memory. + // Rate limit how many blocks (2 epochs worth of blocks) a node keeps in the memory. if len(s.getInitSyncBlocks()) > 2*int(params.BeaconConfig().SlotsPerEpoch) { if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { return err @@ -271,10 +277,12 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed } } - if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { - return err + if featureconfig.Get().InitSyncBatchSaveBlocks { + if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { + return err + } + s.clearInitSyncBlocks() } - s.clearInitSyncBlocks() if err := s.beaconDB.SaveFinalizedCheckpoint(ctx, postState.FinalizedCheckpoint()); err != nil { return errors.Wrap(err, "could not save finalized checkpoint") diff --git a/beacon-chain/blockchain/process_block_helpers.go b/beacon-chain/blockchain/process_block_helpers.go index 5ef20a2ef8de..99d8b2843c9a 100644 --- a/beacon-chain/blockchain/process_block_helpers.go +++ b/beacon-chain/blockchain/process_block_helpers.go @@ -232,7 +232,7 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified var newJustifiedBlockSigned *ethpb.SignedBeaconBlock justifiedRoot := bytesutil.ToBytes32(newJustifiedCheckpt.Root) var err error - if s.hasInitSyncBlock(justifiedRoot) { + if featureconfig.Get().InitSyncBatchSaveBlocks && s.hasInitSyncBlock(justifiedRoot) { newJustifiedBlockSigned = s.getInitSyncBlock(justifiedRoot) } else { newJustifiedBlockSigned, err = s.beaconDB.Block(ctx, justifiedRoot) @@ -243,13 +243,14 @@ func (s *Service) shouldUpdateCurrentJustified(ctx context.Context, newJustified if newJustifiedBlockSigned == nil || newJustifiedBlockSigned.Block == nil { return false, errors.New("nil new justified block") } + newJustifiedBlock := newJustifiedBlockSigned.Block if newJustifiedBlock.Slot <= helpers.StartSlot(s.justifiedCheckpt.Epoch) { return false, nil } var justifiedBlockSigned *ethpb.SignedBeaconBlock cachedJustifiedRoot := bytesutil.ToBytes32(s.justifiedCheckpt.Root) - if s.hasInitSyncBlock(cachedJustifiedRoot) { + if featureconfig.Get().InitSyncBatchSaveBlocks && s.hasInitSyncBlock(cachedJustifiedRoot) { justifiedBlockSigned = s.getInitSyncBlock(cachedJustifiedRoot) } else { justifiedBlockSigned, err = s.beaconDB.Block(ctx, cachedJustifiedRoot) @@ -393,7 +394,7 @@ func (s *Service) ancestor(ctx context.Context, root []byte, slot uint64) ([]byt return nil, errors.Wrap(err, "could not get ancestor block") } - if s.hasInitSyncBlock(bytesutil.ToBytes32(root)) { + if featureconfig.Get().InitSyncBatchSaveBlocks && s.hasInitSyncBlock(bytesutil.ToBytes32(root)) { signed = s.getInitSyncBlock(bytesutil.ToBytes32(root)) } diff --git a/beacon-chain/db/kv/finalized_block_roots.go b/beacon-chain/db/kv/finalized_block_roots.go index 16c62f745efd..cdfbec325d4b 100644 --- a/beacon-chain/db/kv/finalized_block_roots.go +++ b/beacon-chain/db/kv/finalized_block_roots.go @@ -3,9 +3,12 @@ package kv import ( "bytes" "context" + "fmt" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" + dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/traceutil" bolt "go.etcd.io/bbolt" "go.opencensus.io/trace" @@ -40,9 +43,9 @@ func (k *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec bkt := tx.Bucket(finalizedBlockRootsIndexBucket) - //root := checkpoint.Root - //var previousRoot []byte - //genesisRoot := tx.Bucket(blocksBucket).Get(genesisBlockRootKey) + root := checkpoint.Root + var previousRoot []byte + genesisRoot := tx.Bucket(blocksBucket).Get(genesisBlockRootKey) // De-index recent finalized block roots, to be re-indexed. previousFinalizedCheckpoint := ðpb.Checkpoint{} @@ -70,60 +73,60 @@ func (k *Store) updateFinalizedBlockRoots(ctx context.Context, tx *bolt.Tx, chec // Walk up the ancestry chain until we reach a block root present in the finalized block roots // index bucket or genesis block root. - //for { - // if bytes.Equal(root, genesisRoot) { - // break - // } - // - // signedBlock, err := k.Block(ctx, bytesutil.ToBytes32(root)) - // if err != nil { - // traceutil.AnnotateError(span, err) - // return err - // } - // if signedBlock == nil || signedBlock.Block == nil { - // err := fmt.Errorf("missing block in database: block root=%#x", root) - // traceutil.AnnotateError(span, err) - // return err - // } - // block := signedBlock.Block - // - // container := &dbpb.FinalizedBlockRootContainer{ - // ParentRoot: block.ParentRoot, - // ChildRoot: previousRoot, - // } - // - // enc, err := encode(container) - // if err != nil { - // traceutil.AnnotateError(span, err) - // return err - // } - // if err := bkt.Put(root, enc); err != nil { - // traceutil.AnnotateError(span, err) - // return err - // } - // - // // Found parent, loop exit condition. - // if parentBytes := bkt.Get(block.ParentRoot); parentBytes != nil { - // parent := &dbpb.FinalizedBlockRootContainer{} - // if err := decode(parentBytes, parent); err != nil { - // traceutil.AnnotateError(span, err) - // return err - // } - // parent.ChildRoot = root - // enc, err := encode(parent) - // if err != nil { - // traceutil.AnnotateError(span, err) - // return err - // } - // if err := bkt.Put(block.ParentRoot, enc); err != nil { - // traceutil.AnnotateError(span, err) - // return err - // } - // break - // } - // previousRoot = root - // root = block.ParentRoot - //} + for { + if bytes.Equal(root, genesisRoot) { + break + } + + signedBlock, err := k.Block(ctx, bytesutil.ToBytes32(root)) + if err != nil { + traceutil.AnnotateError(span, err) + return err + } + if signedBlock == nil || signedBlock.Block == nil { + err := fmt.Errorf("missing block in database: block root=%#x", root) + traceutil.AnnotateError(span, err) + return err + } + block := signedBlock.Block + + container := &dbpb.FinalizedBlockRootContainer{ + ParentRoot: block.ParentRoot, + ChildRoot: previousRoot, + } + + enc, err := encode(container) + if err != nil { + traceutil.AnnotateError(span, err) + return err + } + if err := bkt.Put(root, enc); err != nil { + traceutil.AnnotateError(span, err) + return err + } + + // Found parent, loop exit condition. + if parentBytes := bkt.Get(block.ParentRoot); parentBytes != nil { + parent := &dbpb.FinalizedBlockRootContainer{} + if err := decode(parentBytes, parent); err != nil { + traceutil.AnnotateError(span, err) + return err + } + parent.ChildRoot = root + enc, err := encode(parent) + if err != nil { + traceutil.AnnotateError(span, err) + return err + } + if err := bkt.Put(block.ParentRoot, enc); err != nil { + traceutil.AnnotateError(span, err) + return err + } + break + } + previousRoot = root + root = block.ParentRoot + } // Upsert blocks from the current finalized epoch. roots, err := k.BlockRoots(ctx, filters.NewFilter().SetStartEpoch(checkpoint.Epoch).SetEndEpoch(checkpoint.Epoch+1)) diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index e5bf7e2d63fb..6ae22747cfa5 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -181,26 +181,42 @@ func (s *Service) roundRobinSync(genesis time.Time) error { } } } - lastFinalizedEpoch := s.chain.FinalizedCheckpt().Epoch - lastFinalizedState, err := s.db.HighestSlotStatesBelow(ctx, helpers.StartSlot(lastFinalizedEpoch)) - if err != nil { - return err + var startBlock uint64 + if featureconfig.Get().InitSyncBatchSaveBlocks { + lastFinalizedEpoch := s.chain.FinalizedCheckpt().Epoch + lastFinalizedState, err := s.db.HighestSlotStatesBelow(ctx, helpers.StartSlot(lastFinalizedEpoch)) + if err != nil { + return err + } + startBlock = lastFinalizedState[0].Slot() + 1 + } else { + startBlock = s.chain.HeadSlot() + 1 } - startBlock := lastFinalizedState[0].Slot() + 1 skippedBlocks := blockBatchSize * uint64(lastEmptyRequests*len(peers)) if startBlock+skippedBlocks > helpers.StartSlot(finalizedEpoch+1) { log.WithField("finalizedEpoch", finalizedEpoch).Debug("Requested block range is greater than the finalized epoch") break } - //c, _ := s.db.PrevFinalizedCheckpoint(ctx) - blocks, err := request( - s.chain.HeadSlot(), // start - 1, // step - blockBatchSize, // count - peers, // peers - 0, // remainder - ) + var blocks []*eth.SignedBeaconBlock + var err error + if featureconfig.Get().InitSyncBatchSaveBlocks { + blocks, err = request( + s.chain.HeadSlot()+1, // start + 1, // step + blockBatchSize, // count + peers, // peers + 0, + ) + } else { + blocks, err = request( + s.chain.HeadSlot()+1, // start + 1, // step + blockBatchSize, // count + peers, // peers + 0, + ) + } if err != nil { log.WithError(err).Error("Round robing sync request failed") continue diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index 2a780e3fed4e..285f06424302 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -53,6 +53,7 @@ type Flags struct { EnableInitSyncQueue bool // EnableInitSyncQueue enables the new initial sync implementation. EnableFieldTrie bool // EnableFieldTrie enables the state from using field specific tries when computing the root. EnableBlockHTR bool // EnableBlockHTR enables custom hashing of our beacon blocks. + InitSyncBatchSaveBlocks bool // InitSyncBatchSaveBlocks enables batch save blocks mode during initial syncing. // DisableForkChoice disables using LMD-GHOST fork choice to update // the head of the chain based on attestations and instead accepts any valid received block // as the chain head. UNSAFE, use with caution. @@ -190,6 +191,10 @@ func ConfigureBeaconChain(ctx *cli.Context) { log.Warn("Enabling custom block hashing") cfg.EnableBlockHTR = true } + if ctx.Bool(initSyncBatchSaveBlocks.Name) { + log.Warn("Enabling init sync batch save blocks mode") + cfg.InitSyncBatchSaveBlocks = true + } Init(cfg) } diff --git a/shared/featureconfig/flags.go b/shared/featureconfig/flags.go index 0f855039f4f3..a6200aaac234 100644 --- a/shared/featureconfig/flags.go +++ b/shared/featureconfig/flags.go @@ -141,6 +141,11 @@ var ( Name: "enable-custom-block-htr", Usage: "Enables the usage of a custom hashing method for our block", } + initSyncBatchSaveBlocks = &cli.BoolFlag{ + Name: "init-sync-batch-save-blocks", + Usage: "Instead of saving one block per slot to the DB during initial syncing, this enables batch saving" + + " of epochs worth of blocks to the DB", + } ) // Deprecated flags list. @@ -323,6 +328,7 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{ enableInitSyncQueue, enableFieldTrie, enableCustomBlockHTR, + initSyncBatchSaveBlocks, }...) // E2EBeaconChainFlags contains a list of the beacon chain feature flags to be tested in E2E. From 8df754c323a168aad7cccfb82052c763ffac313b Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 28 Mar 2020 09:21:59 -0700 Subject: [PATCH 11/39] Removed unused methods --- beacon-chain/blockchain/testing/mock.go | 5 +++++ beacon-chain/db/iface/interface.go | 1 - beacon-chain/db/kv/checkpoint.go | 20 -------------------- 3 files changed, 5 insertions(+), 21 deletions(-) diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index b98baa622db0..f98dc501487c 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -234,3 +234,8 @@ func (ms *ChainService) IsValidAttestation(ctx context.Context, att *ethpb.Attes // ClearCachedStates does nothing. func (ms *ChainService) ClearCachedStates() {} + +// HasInitSyncBlock mocks the same method in the chain service. +func (ms *ChainService) HasInitSyncBlock(root [32]byte) bool { + return true +} diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index f78c78a42c53..3267645fa485 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -51,7 +51,6 @@ type ReadOnlyDatabase interface { // Checkpoint operations. JustifiedCheckpoint(ctx context.Context) (*eth.Checkpoint, error) FinalizedCheckpoint(ctx context.Context) (*eth.Checkpoint, error) - PrevFinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) // Archival data handlers for storing/retrieving historical beacon node information. ArchivedActiveValidatorChanges(ctx context.Context, epoch uint64) (*ethereum_beacon_p2p_v1.ArchivedActiveSetChanges, error) ArchivedCommitteeInfo(ctx context.Context, epoch uint64) (*ethereum_beacon_p2p_v1.ArchivedCommitteeInfo, error) diff --git a/beacon-chain/db/kv/checkpoint.go b/beacon-chain/db/kv/checkpoint.go index eafe18d529e7..0018d6ad01e6 100644 --- a/beacon-chain/db/kv/checkpoint.go +++ b/beacon-chain/db/kv/checkpoint.go @@ -53,26 +53,6 @@ func (k *Store) FinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, err return checkpoint, err } -// PrevFinalizedCheckpoint returns the previous latest finalized checkpoint in beacon chain. -func (k *Store) PrevFinalizedCheckpoint(ctx context.Context) (*ethpb.Checkpoint, error) { - ctx, span := trace.StartSpan(ctx, "PrevFinalizedCheckpoint.FinalizedCheckpoint") - defer span.End() - var checkpoint *ethpb.Checkpoint - err := k.db.View(func(tx *bolt.Tx) error { - bkt := tx.Bucket(finalizedBlockRootsIndexBucket) - enc := bkt.Get(previousFinalizedCheckpointKey) - if enc == nil { - blockBucket := tx.Bucket(blocksBucket) - genesisRoot := blockBucket.Get(genesisBlockRootKey) - checkpoint = ðpb.Checkpoint{Root: genesisRoot} - return nil - } - checkpoint = ðpb.Checkpoint{} - return decode(enc, checkpoint) - }) - return checkpoint, err -} - // SaveJustifiedCheckpoint saves justified checkpoint in beacon chain. func (k *Store) SaveJustifiedCheckpoint(ctx context.Context, checkpoint *ethpb.Checkpoint) error { ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveJustifiedCheckpoint") From 1b6f64c00f4a697374cd60fdf100eccc18defb0e Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 28 Mar 2020 09:39:05 -0700 Subject: [PATCH 12/39] Fixed tests --- beacon-chain/blockchain/testing/mock.go | 2 +- .../sync/initial-sync-old/round_robin.go | 28 ++++++------------- 2 files changed, 9 insertions(+), 21 deletions(-) diff --git a/beacon-chain/blockchain/testing/mock.go b/beacon-chain/blockchain/testing/mock.go index f98dc501487c..45613b9e96a2 100644 --- a/beacon-chain/blockchain/testing/mock.go +++ b/beacon-chain/blockchain/testing/mock.go @@ -237,5 +237,5 @@ func (ms *ChainService) ClearCachedStates() {} // HasInitSyncBlock mocks the same method in the chain service. func (ms *ChainService) HasInitSyncBlock(root [32]byte) bool { - return true + return false } diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index 6ae22747cfa5..028fb207c1bc 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -198,25 +198,13 @@ func (s *Service) roundRobinSync(genesis time.Time) error { break } - var blocks []*eth.SignedBeaconBlock - var err error - if featureconfig.Get().InitSyncBatchSaveBlocks { - blocks, err = request( - s.chain.HeadSlot()+1, // start - 1, // step - blockBatchSize, // count - peers, // peers - 0, - ) - } else { - blocks, err = request( - s.chain.HeadSlot()+1, // start - 1, // step - blockBatchSize, // count - peers, // peers - 0, - ) - } + blocks, err := request( + s.chain.HeadSlot()+1, // start + 1, // step + blockBatchSize, // count + peers, // peers + 0, + ) if err != nil { log.WithError(err).Error("Round robing sync request failed") continue @@ -233,7 +221,7 @@ func (s *Service) roundRobinSync(genesis time.Time) error { s.logSyncStatus(genesis, blk.Block, peers, counter) parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) if !s.db.HasBlock(ctx, parentRoot) && !s.chain.HasInitSyncBlock(parentRoot) { - log.Warnf("Beacon node doesn't have a block in db or cache with root %#x", parentRoot) + log.Debugf("Beacon node doesn't have a block in db or cache with root %#x", parentRoot) continue } From 09729aaa70045704f2accfe0b15378b4d5ee1892 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 28 Mar 2020 09:45:44 -0700 Subject: [PATCH 13/39] E2e test --- shared/featureconfig/flags.go | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/featureconfig/flags.go b/shared/featureconfig/flags.go index a6200aaac234..1f070c9b261f 100644 --- a/shared/featureconfig/flags.go +++ b/shared/featureconfig/flags.go @@ -342,6 +342,7 @@ var E2EBeaconChainFlags = []string{ "--check-head-state", "--enable-initial-sync-queue", "--enable-state-field-trie", + "--init-sync-batch-save-blocks", // TODO(5123): This flag currently fails E2E. Commenting until it's resolved. //"--enable-dynamic-committee-subnets", } From 1b7f61439e394ff27cbec094934fffd0d22fe1e5 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 28 Mar 2020 10:36:31 -0700 Subject: [PATCH 14/39] comment --- beacon-chain/sync/initial-sync-old/round_robin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index 028fb207c1bc..5d11df19ccc8 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -203,7 +203,7 @@ func (s *Service) roundRobinSync(genesis time.Time) error { 1, // step blockBatchSize, // count peers, // peers - 0, + 0, // reminder ) if err != nil { log.WithError(err).Error("Round robing sync request failed") From 9e88161519504a97d002234622c57886dd6614ca Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sat, 28 Mar 2020 14:24:20 -0700 Subject: [PATCH 15/39] Compatible with current initial sync --- beacon-chain/blockchain/init_sync_process_block.go | 6 ++++++ beacon-chain/sync/initial-sync/round_robin.go | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index c306ec2744a3..1981d1b6953f 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -176,6 +176,12 @@ func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot var endBlock *ethpb.SignedBeaconBlock if featureconfig.Get().InitSyncBatchSaveBlocks && s.hasInitSyncBlock(endRoot) { endBlock = s.getInitSyncBlock(endRoot) + if featureconfig.Get().InitSyncBatchSaveBlocks { + if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { + return nil, err + } + s.clearInitSyncBlocks() + } } else { endBlock, err = s.beaconDB.Block(ctx, endRoot) if err != nil { diff --git a/beacon-chain/sync/initial-sync/round_robin.go b/beacon-chain/sync/initial-sync/round_robin.go index 2db6911f20c4..3fe9554c3f11 100644 --- a/beacon-chain/sync/initial-sync/round_robin.go +++ b/beacon-chain/sync/initial-sync/round_robin.go @@ -198,7 +198,8 @@ func (s *Service) logSyncStatus(genesis time.Time, blk *eth.BeaconBlock, counter } func (s *Service) processBlock(ctx context.Context, blk *eth.SignedBeaconBlock) error { - if !s.db.HasBlock(ctx, bytesutil.ToBytes32(blk.Block.ParentRoot)) { + parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) + if !s.db.HasBlock(ctx, parentRoot) && !s.chain.HasInitSyncBlock(parentRoot) { return fmt.Errorf("beacon node doesn't have a block in db with root %#x", blk.Block.ParentRoot) } s.blockNotifier.BlockFeed().Send(&feed.Event{ From 2a617b55cd52c5b4fe4c1835603a406a5dc01f63 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 09:46:33 -0700 Subject: [PATCH 16/39] Starting --- beacon-chain/state/stategen/hot.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/beacon-chain/state/stategen/hot.go b/beacon-chain/state/stategen/hot.go index 6e38ed011a00..e29afc9262a1 100644 --- a/beacon-chain/state/stategen/hot.go +++ b/beacon-chain/state/stategen/hot.go @@ -56,12 +56,17 @@ func (s *State) loadHotStateByRoot(ctx context.Context, blockRoot [32]byte) (*st ctx, span := trace.StartSpan(ctx, "stateGen.loadHotStateByRoot") defer span.End() - // Load the hot state cache. + // Load the hot state from cache. cachedState := s.hotStateCache.Get(blockRoot) if cachedState != nil { return cachedState, nil } + // Load the hot state from DB. + if s.beaconDB.HasState(ctx, blockRoot) { + return s.beaconDB.State(ctx, blockRoot) + } + summary, err := s.beaconDB.StateSummary(ctx, blockRoot) if err != nil { return nil, err @@ -70,13 +75,14 @@ func (s *State) loadHotStateByRoot(ctx context.Context, blockRoot [32]byte) (*st return nil, errUnknownStateSummary } - startState, err := s.lastSavedState(ctx, helpers.StartSlot(helpers.SlotToEpoch(summary.Slot))) + startState, err := s.lastSavedState(ctx, summary.Slot) if err != nil { return nil, err } if startState == nil { return nil, errUnknownBoundaryState } + log.Warnf("Replaying for block at slot %d, start slot %d", summary.Slot, startState.Slot()) // Don't need to replay the blocks if start state is the same state for the block root. var hotState *state.BeaconState From f8cdf331d10bd5b2866bf7db3e8ab0220dc96548 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 10:02:21 -0700 Subject: [PATCH 17/39] New cache --- beacon-chain/blockchain/service.go | 109 +++++++++++++++-------------- 1 file changed, 56 insertions(+), 53 deletions(-) diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 560a2616e05b..75818db47231 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -43,40 +43,42 @@ import ( // Service represents a service that handles the internal // logic of managing the full PoS beacon chain. type Service struct { - ctx context.Context - cancel context.CancelFunc - beaconDB db.HeadAccessDatabase - depositCache *depositcache.DepositCache - chainStartFetcher powchain.ChainStartFetcher - attPool attestations.Pool - slashingPool *slashings.Pool - exitPool *voluntaryexits.Pool - genesisTime time.Time - p2p p2p.Broadcaster - maxRoutines int64 - head *head - headLock sync.RWMutex - stateNotifier statefeed.Notifier - genesisRoot [32]byte - epochParticipation map[uint64]*precompute.Balance - epochParticipationLock sync.RWMutex - forkChoiceStore f.ForkChoicer - justifiedCheckpt *ethpb.Checkpoint - prevJustifiedCheckpt *ethpb.Checkpoint - bestJustifiedCheckpt *ethpb.Checkpoint - finalizedCheckpt *ethpb.Checkpoint - prevFinalizedCheckpt *ethpb.Checkpoint - nextEpochBoundarySlot uint64 - voteLock sync.RWMutex - initSyncState map[[32]byte]*stateTrie.BeaconState - boundaryRoots [][32]byte - initSyncStateLock sync.RWMutex - checkpointState *cache.CheckpointStateCache - checkpointStateLock sync.Mutex - stateGen *stategen.State - opsService *attestations.Service - initSyncBlocks map[[32]byte]*ethpb.SignedBeaconBlock - initSyncBlocksLock sync.RWMutex + ctx context.Context + cancel context.CancelFunc + beaconDB db.HeadAccessDatabase + depositCache *depositcache.DepositCache + chainStartFetcher powchain.ChainStartFetcher + attPool attestations.Pool + slashingPool *slashings.Pool + exitPool *voluntaryexits.Pool + genesisTime time.Time + p2p p2p.Broadcaster + maxRoutines int64 + head *head + headLock sync.RWMutex + stateNotifier statefeed.Notifier + genesisRoot [32]byte + epochParticipation map[uint64]*precompute.Balance + epochParticipationLock sync.RWMutex + forkChoiceStore f.ForkChoicer + justifiedCheckpt *ethpb.Checkpoint + prevJustifiedCheckpt *ethpb.Checkpoint + bestJustifiedCheckpt *ethpb.Checkpoint + finalizedCheckpt *ethpb.Checkpoint + prevFinalizedCheckpt *ethpb.Checkpoint + nextEpochBoundarySlot uint64 + voteLock sync.RWMutex + initSyncState map[[32]byte]*stateTrie.BeaconState + boundaryRoots [][32]byte + initSyncStateLock sync.RWMutex + checkpointState *cache.CheckpointStateCache + checkpointStateLock sync.Mutex + stateGen *stategen.State + opsService *attestations.Service + initSyncBlocks map[[32]byte]*ethpb.SignedBeaconBlock + initSyncBlocksLock sync.RWMutex + initSyncStateSummaries map[[32]byte]*pb.StateSummary + initSyncStateSummariesLock sync.RWMutex } // Config options for the service. @@ -101,25 +103,26 @@ type Config struct { func NewService(ctx context.Context, cfg *Config) (*Service, error) { ctx, cancel := context.WithCancel(ctx) return &Service{ - ctx: ctx, - cancel: cancel, - beaconDB: cfg.BeaconDB, - depositCache: cfg.DepositCache, - chainStartFetcher: cfg.ChainStartFetcher, - attPool: cfg.AttPool, - exitPool: cfg.ExitPool, - slashingPool: cfg.SlashingPool, - p2p: cfg.P2p, - maxRoutines: cfg.MaxRoutines, - stateNotifier: cfg.StateNotifier, - epochParticipation: make(map[uint64]*precompute.Balance), - forkChoiceStore: cfg.ForkChoiceStore, - initSyncState: make(map[[32]byte]*stateTrie.BeaconState), - boundaryRoots: [][32]byte{}, - checkpointState: cache.NewCheckpointStateCache(), - opsService: cfg.OpsService, - stateGen: cfg.StateGen, - initSyncBlocks: make(map[[32]byte]*ethpb.SignedBeaconBlock), + ctx: ctx, + cancel: cancel, + beaconDB: cfg.BeaconDB, + depositCache: cfg.DepositCache, + chainStartFetcher: cfg.ChainStartFetcher, + attPool: cfg.AttPool, + exitPool: cfg.ExitPool, + slashingPool: cfg.SlashingPool, + p2p: cfg.P2p, + maxRoutines: cfg.MaxRoutines, + stateNotifier: cfg.StateNotifier, + epochParticipation: make(map[uint64]*precompute.Balance), + forkChoiceStore: cfg.ForkChoiceStore, + initSyncState: make(map[[32]byte]*stateTrie.BeaconState), + boundaryRoots: [][32]byte{}, + checkpointState: cache.NewCheckpointStateCache(), + opsService: cfg.OpsService, + stateGen: cfg.StateGen, + initSyncBlocks: make(map[[32]byte]*ethpb.SignedBeaconBlock), + initSyncStateSummaries: make(map[[32]byte]*pb.StateSummary), }, nil } From 2d810ac1de271802e9bb9b14fb7cebe1b8c92928 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 10:02:26 -0700 Subject: [PATCH 18/39] Cache getters and setters --- .../blockchain/init_sync_process_block.go | 39 +++++++++++++++++++ beacon-chain/blockchain/receive_block.go | 6 +++ 2 files changed, 45 insertions(+) diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index 1981d1b6953f..5cf6d78e0d63 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -8,6 +8,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" @@ -249,3 +250,41 @@ func (s *Service) clearInitSyncBlocks() { defer s.initSyncBlocksLock.Unlock() s.initSyncBlocks = make(map[[32]byte]*ethpb.SignedBeaconBlock) } + +// This checks if a state summary exists in the initial sync state summaries cache using the root +// of the block. +func (s *Service) hasInitSyncStateSummary(r [32]byte) bool { + s.initSyncStateSummariesLock.RLock() + defer s.initSyncStateSummariesLock.RUnlock() + _, ok := s.initSyncStateSummaries[r] + return ok +} + +// This retrieves a state summary from the initial sync state summaries cache using the root of +// the block. +func (s *Service) getInitSyncStateSummary(r [32]byte) *pb.StateSummary { + s.initSyncStateSummariesLock.RLock() + defer s.initSyncStateSummariesLock.RUnlock() + b := s.initSyncStateSummaries[r] + return b +} + +// This retrieves all the beacon state summaries from the initial sync state summaries cache, the returned +// state summaries are unordered. +func (s *Service) getInitSyncStateSummaries() []*pb.StateSummary { + s.initSyncStateSummariesLock.RLock() + defer s.initSyncStateSummariesLock.RUnlock() + + blks := make([]*pb.StateSummary, 0, len(s.initSyncStateSummaries)) + for _, b := range s.initSyncStateSummaries { + blks = append(blks, b) + } + return blks +} + +// This clears out the initial sync state summaries cache. +func (s *Service) clearInitSyncStateSummaries() { + s.initSyncStateSummariesLock.Lock() + defer s.initSyncStateSummariesLock.Unlock() + s.initSyncStateSummaries = make(map[[32]byte]*epb.StateSummary) +} diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index 23d87862d0d6..ec45aa2962a2 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -27,6 +27,7 @@ type BlockReceiver interface { ReceiveBlockNoPubsubForkchoice(ctx context.Context, block *ethpb.SignedBeaconBlock) error ReceiveBlockNoVerify(ctx context.Context, block *ethpb.SignedBeaconBlock) error HasInitSyncBlock(root [32]byte) bool + HasInitSyncStateSummary(root [32]byte) bool } // ReceiveBlock is a function that defines the operations that are preformed on @@ -242,3 +243,8 @@ func (s *Service) ReceiveBlockNoVerify(ctx context.Context, block *ethpb.SignedB func (s *Service) HasInitSyncBlock(root [32]byte) bool { return s.hasInitSyncBlock(root) } + +// HasInitSyncStateSummary returns true if the state summary of the input root exists in initial sync state summaries cache. +func (s *Service) HasInitSyncStateSummary(root [32]byte) bool { + return s.hasInitStateSummary(root) +} From 70cbaf80bd6981e680945f01be4c0896c236772e Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 10:32:52 -0700 Subject: [PATCH 19/39] It should be part of state gen --- .../blockchain/init_sync_process_block.go | 39 ------- beacon-chain/blockchain/service.go | 109 +++++++++--------- beacon-chain/cache/BUILD.bazel | 2 + beacon-chain/cache/state_summary.go | 65 +++++++++++ beacon-chain/state/stategen/service.go | 2 + 5 files changed, 122 insertions(+), 95 deletions(-) create mode 100644 beacon-chain/cache/state_summary.go diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index 5cf6d78e0d63..1981d1b6953f 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -8,7 +8,6 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" - pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" @@ -250,41 +249,3 @@ func (s *Service) clearInitSyncBlocks() { defer s.initSyncBlocksLock.Unlock() s.initSyncBlocks = make(map[[32]byte]*ethpb.SignedBeaconBlock) } - -// This checks if a state summary exists in the initial sync state summaries cache using the root -// of the block. -func (s *Service) hasInitSyncStateSummary(r [32]byte) bool { - s.initSyncStateSummariesLock.RLock() - defer s.initSyncStateSummariesLock.RUnlock() - _, ok := s.initSyncStateSummaries[r] - return ok -} - -// This retrieves a state summary from the initial sync state summaries cache using the root of -// the block. -func (s *Service) getInitSyncStateSummary(r [32]byte) *pb.StateSummary { - s.initSyncStateSummariesLock.RLock() - defer s.initSyncStateSummariesLock.RUnlock() - b := s.initSyncStateSummaries[r] - return b -} - -// This retrieves all the beacon state summaries from the initial sync state summaries cache, the returned -// state summaries are unordered. -func (s *Service) getInitSyncStateSummaries() []*pb.StateSummary { - s.initSyncStateSummariesLock.RLock() - defer s.initSyncStateSummariesLock.RUnlock() - - blks := make([]*pb.StateSummary, 0, len(s.initSyncStateSummaries)) - for _, b := range s.initSyncStateSummaries { - blks = append(blks, b) - } - return blks -} - -// This clears out the initial sync state summaries cache. -func (s *Service) clearInitSyncStateSummaries() { - s.initSyncStateSummariesLock.Lock() - defer s.initSyncStateSummariesLock.Unlock() - s.initSyncStateSummaries = make(map[[32]byte]*epb.StateSummary) -} diff --git a/beacon-chain/blockchain/service.go b/beacon-chain/blockchain/service.go index 75818db47231..560a2616e05b 100644 --- a/beacon-chain/blockchain/service.go +++ b/beacon-chain/blockchain/service.go @@ -43,42 +43,40 @@ import ( // Service represents a service that handles the internal // logic of managing the full PoS beacon chain. type Service struct { - ctx context.Context - cancel context.CancelFunc - beaconDB db.HeadAccessDatabase - depositCache *depositcache.DepositCache - chainStartFetcher powchain.ChainStartFetcher - attPool attestations.Pool - slashingPool *slashings.Pool - exitPool *voluntaryexits.Pool - genesisTime time.Time - p2p p2p.Broadcaster - maxRoutines int64 - head *head - headLock sync.RWMutex - stateNotifier statefeed.Notifier - genesisRoot [32]byte - epochParticipation map[uint64]*precompute.Balance - epochParticipationLock sync.RWMutex - forkChoiceStore f.ForkChoicer - justifiedCheckpt *ethpb.Checkpoint - prevJustifiedCheckpt *ethpb.Checkpoint - bestJustifiedCheckpt *ethpb.Checkpoint - finalizedCheckpt *ethpb.Checkpoint - prevFinalizedCheckpt *ethpb.Checkpoint - nextEpochBoundarySlot uint64 - voteLock sync.RWMutex - initSyncState map[[32]byte]*stateTrie.BeaconState - boundaryRoots [][32]byte - initSyncStateLock sync.RWMutex - checkpointState *cache.CheckpointStateCache - checkpointStateLock sync.Mutex - stateGen *stategen.State - opsService *attestations.Service - initSyncBlocks map[[32]byte]*ethpb.SignedBeaconBlock - initSyncBlocksLock sync.RWMutex - initSyncStateSummaries map[[32]byte]*pb.StateSummary - initSyncStateSummariesLock sync.RWMutex + ctx context.Context + cancel context.CancelFunc + beaconDB db.HeadAccessDatabase + depositCache *depositcache.DepositCache + chainStartFetcher powchain.ChainStartFetcher + attPool attestations.Pool + slashingPool *slashings.Pool + exitPool *voluntaryexits.Pool + genesisTime time.Time + p2p p2p.Broadcaster + maxRoutines int64 + head *head + headLock sync.RWMutex + stateNotifier statefeed.Notifier + genesisRoot [32]byte + epochParticipation map[uint64]*precompute.Balance + epochParticipationLock sync.RWMutex + forkChoiceStore f.ForkChoicer + justifiedCheckpt *ethpb.Checkpoint + prevJustifiedCheckpt *ethpb.Checkpoint + bestJustifiedCheckpt *ethpb.Checkpoint + finalizedCheckpt *ethpb.Checkpoint + prevFinalizedCheckpt *ethpb.Checkpoint + nextEpochBoundarySlot uint64 + voteLock sync.RWMutex + initSyncState map[[32]byte]*stateTrie.BeaconState + boundaryRoots [][32]byte + initSyncStateLock sync.RWMutex + checkpointState *cache.CheckpointStateCache + checkpointStateLock sync.Mutex + stateGen *stategen.State + opsService *attestations.Service + initSyncBlocks map[[32]byte]*ethpb.SignedBeaconBlock + initSyncBlocksLock sync.RWMutex } // Config options for the service. @@ -103,26 +101,25 @@ type Config struct { func NewService(ctx context.Context, cfg *Config) (*Service, error) { ctx, cancel := context.WithCancel(ctx) return &Service{ - ctx: ctx, - cancel: cancel, - beaconDB: cfg.BeaconDB, - depositCache: cfg.DepositCache, - chainStartFetcher: cfg.ChainStartFetcher, - attPool: cfg.AttPool, - exitPool: cfg.ExitPool, - slashingPool: cfg.SlashingPool, - p2p: cfg.P2p, - maxRoutines: cfg.MaxRoutines, - stateNotifier: cfg.StateNotifier, - epochParticipation: make(map[uint64]*precompute.Balance), - forkChoiceStore: cfg.ForkChoiceStore, - initSyncState: make(map[[32]byte]*stateTrie.BeaconState), - boundaryRoots: [][32]byte{}, - checkpointState: cache.NewCheckpointStateCache(), - opsService: cfg.OpsService, - stateGen: cfg.StateGen, - initSyncBlocks: make(map[[32]byte]*ethpb.SignedBeaconBlock), - initSyncStateSummaries: make(map[[32]byte]*pb.StateSummary), + ctx: ctx, + cancel: cancel, + beaconDB: cfg.BeaconDB, + depositCache: cfg.DepositCache, + chainStartFetcher: cfg.ChainStartFetcher, + attPool: cfg.AttPool, + exitPool: cfg.ExitPool, + slashingPool: cfg.SlashingPool, + p2p: cfg.P2p, + maxRoutines: cfg.MaxRoutines, + stateNotifier: cfg.StateNotifier, + epochParticipation: make(map[uint64]*precompute.Balance), + forkChoiceStore: cfg.ForkChoiceStore, + initSyncState: make(map[[32]byte]*stateTrie.BeaconState), + boundaryRoots: [][32]byte{}, + checkpointState: cache.NewCheckpointStateCache(), + opsService: cfg.OpsService, + stateGen: cfg.StateGen, + initSyncBlocks: make(map[[32]byte]*ethpb.SignedBeaconBlock), }, nil } diff --git a/beacon-chain/cache/BUILD.bazel b/beacon-chain/cache/BUILD.bazel index feed8782ca22..0e36471c035c 100644 --- a/beacon-chain/cache/BUILD.bazel +++ b/beacon-chain/cache/BUILD.bazel @@ -11,11 +11,13 @@ go_library( "eth1_data.go", "hot_state_cache.go", "skip_slot_cache.go", + "state_summary.go", ], importpath = "github.com/prysmaticlabs/prysm/beacon-chain/cache", visibility = ["//beacon-chain:__subpackages__"], deps = [ "//beacon-chain/state:go_default_library", + "//proto/beacon/p2p/v1:go_default_library", "//shared/featureconfig:go_default_library", "//shared/hashutil:go_default_library", "//shared/params:go_default_library", diff --git a/beacon-chain/cache/state_summary.go b/beacon-chain/cache/state_summary.go new file mode 100644 index 000000000000..a7cda4f07d8e --- /dev/null +++ b/beacon-chain/cache/state_summary.go @@ -0,0 +1,65 @@ +package cache + +import ( + "sync" + + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" +) + +// StateSummaryCache caches state summary object. +type StateSummaryCache struct { + initSyncStateSummaries map[[32]byte]*pb.StateSummary + initSyncStateSummariesLock sync.RWMutex +} + +// NewStateSummaryCache creates a new state summary cache. +func NewStateSummaryCache() *StateSummaryCache { + return &StateSummaryCache{ + initSyncStateSummaries: make(map[[32]byte]*pb.StateSummary), + } +} + +// Put saves a state summary to the initial sync state summaries cache. +func (s *StateSummaryCache) Put(r [32]byte, b *pb.StateSummary) { + s.initSyncStateSummariesLock.Lock() + defer s.initSyncStateSummariesLock.Unlock() + s.initSyncStateSummaries[r] = b +} + +// Has checks if a state summary exists in the initial sync state summaries cache using the root +// of the block. +func (s *StateSummaryCache) Has(r [32]byte) bool { + s.initSyncStateSummariesLock.RLock() + defer s.initSyncStateSummariesLock.RUnlock() + _, ok := s.initSyncStateSummaries[r] + return ok +} + +// Get retrieves a state summary from the initial sync state summaries cache using the root of +// the block. +func (s *StateSummaryCache) Get(r [32]byte) *pb.StateSummary { + s.initSyncStateSummariesLock.RLock() + defer s.initSyncStateSummariesLock.RUnlock() + b := s.initSyncStateSummaries[r] + return b +} + +// GetAll retrieves all the beacon state summaries from the initial sync state summaries cache, the returned +// state summaries are unordered. +func (s *StateSummaryCache) GetAll() []*pb.StateSummary { + s.initSyncStateSummariesLock.RLock() + defer s.initSyncStateSummariesLock.RUnlock() + + blks := make([]*pb.StateSummary, 0, len(s.initSyncStateSummaries)) + for _, b := range s.initSyncStateSummaries { + blks = append(blks, b) + } + return blks +} + +// Clear clears out the initial sync state summaries cache. +func (s *StateSummaryCache) Clear() { + s.initSyncStateSummariesLock.Lock() + defer s.initSyncStateSummariesLock.Unlock() + s.initSyncStateSummaries = make(map[[32]byte]*pb.StateSummary) +} diff --git a/beacon-chain/state/stategen/service.go b/beacon-chain/state/stategen/service.go index c1064fba9f78..a885a0603332 100644 --- a/beacon-chain/state/stategen/service.go +++ b/beacon-chain/state/stategen/service.go @@ -23,6 +23,7 @@ type State struct { epochBoundaryLock sync.RWMutex hotStateCache *cache.HotStateCache splitInfo *splitSlotAndRoot + initSyncStateSummaries *cache.StateSummaryCache } // This tracks the split point. The point where slot and the block root of @@ -40,6 +41,7 @@ func New(db db.NoHeadAccessDatabase) *State { hotStateCache: cache.NewHotStateCache(), splitInfo: &splitSlotAndRoot{slot: 0, root: params.BeaconConfig().ZeroHash}, slotsPerArchivedPoint: archivedInterval, + initSyncStateSummaries: cache.NewStateSummaryCache(), } } From 785a6054314b7183c5211b10742ecbecf44479d0 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 11:47:34 -0700 Subject: [PATCH 20/39] Need to use cache for DB --- beacon-chain/blockchain/receive_block.go | 6 --- beacon-chain/db/BUILD.bazel | 1 + beacon-chain/db/db.go | 9 ++-- beacon-chain/db/iface/interface.go | 1 + beacon-chain/db/kv/BUILD.bazel | 1 + beacon-chain/db/kv/checkpoint.go | 5 ++- beacon-chain/db/kv/kv.go | 5 ++- beacon-chain/db/kv/state_summary.go | 20 +++++++++ beacon-chain/node/BUILD.bazel | 1 + beacon-chain/node/node.go | 55 +++++++++++++----------- beacon-chain/state/stategen/cold.go | 16 +++---- beacon-chain/state/stategen/getter.go | 22 +++++++++- beacon-chain/state/stategen/hot.go | 13 ++---- beacon-chain/state/stategen/migrate.go | 6 +++ beacon-chain/state/stategen/service.go | 6 +-- 15 files changed, 105 insertions(+), 62 deletions(-) diff --git a/beacon-chain/blockchain/receive_block.go b/beacon-chain/blockchain/receive_block.go index ec45aa2962a2..23d87862d0d6 100644 --- a/beacon-chain/blockchain/receive_block.go +++ b/beacon-chain/blockchain/receive_block.go @@ -27,7 +27,6 @@ type BlockReceiver interface { ReceiveBlockNoPubsubForkchoice(ctx context.Context, block *ethpb.SignedBeaconBlock) error ReceiveBlockNoVerify(ctx context.Context, block *ethpb.SignedBeaconBlock) error HasInitSyncBlock(root [32]byte) bool - HasInitSyncStateSummary(root [32]byte) bool } // ReceiveBlock is a function that defines the operations that are preformed on @@ -243,8 +242,3 @@ func (s *Service) ReceiveBlockNoVerify(ctx context.Context, block *ethpb.SignedB func (s *Service) HasInitSyncBlock(root [32]byte) bool { return s.hasInitSyncBlock(root) } - -// HasInitSyncStateSummary returns true if the state summary of the input root exists in initial sync state summaries cache. -func (s *Service) HasInitSyncStateSummary(root [32]byte) bool { - return s.hasInitStateSummary(root) -} diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index 11c0ee81f75b..dbebc3618e50 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -26,6 +26,7 @@ go_library( "//tools:__subpackages__", ], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/db/iface:go_default_library", "//beacon-chain/db/kv:go_default_library", "@com_github_sirupsen_logrus//:go_default_library", diff --git a/beacon-chain/db/db.go b/beacon-chain/db/db.go index 1d0ccf7fe40e..95b19a60507c 100644 --- a/beacon-chain/db/db.go +++ b/beacon-chain/db/db.go @@ -1,8 +1,11 @@ package db -import "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" +import ( + "github.com/prysmaticlabs/prysm/beacon-chain/cache" + "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" +) // NewDB initializes a new DB. -func NewDB(dirPath string) (Database, error) { - return kv.NewKVStore(dirPath) +func NewDB(dirPath string, stateSummaryCache *cache.StateSummaryCache) (Database, error) { + return kv.NewKVStore(dirPath, stateSummaryCache) } diff --git a/beacon-chain/db/iface/interface.go b/beacon-chain/db/iface/interface.go index 3267645fa485..37f00aa6281a 100644 --- a/beacon-chain/db/iface/interface.go +++ b/beacon-chain/db/iface/interface.go @@ -90,6 +90,7 @@ type NoHeadAccessDatabase interface { DeleteState(ctx context.Context, blockRoot [32]byte) error DeleteStates(ctx context.Context, blockRoots [][32]byte) error SaveStateSummary(ctx context.Context, summary *ethereum_beacon_p2p_v1.StateSummary) error + SaveStateSummaries(ctx context.Context, summaries []*ethereum_beacon_p2p_v1.StateSummary) error // Slashing operations. SaveProposerSlashing(ctx context.Context, slashing *eth.ProposerSlashing) error SaveAttesterSlashing(ctx context.Context, slashing *eth.AttesterSlashing) error diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index cf8883a2cf23..f4092bb0cfa2 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -25,6 +25,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db/kv", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/db/filters:go_default_library", "//beacon-chain/db/iface:go_default_library", diff --git a/beacon-chain/db/kv/checkpoint.go b/beacon-chain/db/kv/checkpoint.go index 0018d6ad01e6..0b91a8d5ce74 100644 --- a/beacon-chain/db/kv/checkpoint.go +++ b/beacon-chain/db/kv/checkpoint.go @@ -5,6 +5,7 @@ import ( "errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/traceutil" bolt "go.etcd.io/bbolt" @@ -65,7 +66,7 @@ func (k *Store) SaveJustifiedCheckpoint(ctx context.Context, checkpoint *ethpb.C return k.db.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket(checkpointBucket) if featureconfig.Get().NewStateMgmt { - if tx.Bucket(stateSummaryBucket).Get(checkpoint.Root) == nil { + if tx.Bucket(stateSummaryBucket).Get(checkpoint.Root) == nil && !k.stateSummaryCache.Has(bytesutil.ToBytes32(checkpoint.Root)) { return errors.New("missing state summary for finalized root") } } else { @@ -93,7 +94,7 @@ func (k *Store) SaveFinalizedCheckpoint(ctx context.Context, checkpoint *ethpb.C return k.db.Update(func(tx *bolt.Tx) error { bucket := tx.Bucket(checkpointBucket) if featureconfig.Get().NewStateMgmt { - if tx.Bucket(stateSummaryBucket).Get(checkpoint.Root) == nil { + if tx.Bucket(stateSummaryBucket).Get(checkpoint.Root) == nil && !k.stateSummaryCache.Has(bytesutil.ToBytes32(checkpoint.Root)) { return errors.New("missing state summary for finalized root") } } else { diff --git a/beacon-chain/db/kv/kv.go b/beacon-chain/db/kv/kv.go index 4aca817bbaa6..f48879ab96bb 100644 --- a/beacon-chain/db/kv/kv.go +++ b/beacon-chain/db/kv/kv.go @@ -10,6 +10,7 @@ import ( "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" prombolt "github.com/prysmaticlabs/prombbolt" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/db/iface" bolt "go.etcd.io/bbolt" ) @@ -38,12 +39,13 @@ type Store struct { validatorIndexCache *ristretto.Cache stateSlotBitLock sync.Mutex blockSlotBitLock sync.Mutex + stateSummaryCache *cache.StateSummaryCache } // NewKVStore initializes a new boltDB key-value store at the directory // path specified, creates the kv-buckets based on the schema, and stores // an open connection db object as a property of the Store struct. -func NewKVStore(dirPath string) (*Store, error) { +func NewKVStore(dirPath string, stateSummaryCache *cache.StateSummaryCache) (*Store, error) { if err := os.MkdirAll(dirPath, 0700); err != nil { return nil, err } @@ -79,6 +81,7 @@ func NewKVStore(dirPath string) (*Store, error) { databasePath: dirPath, blockCache: blockCache, validatorIndexCache: validatorCache, + stateSummaryCache: stateSummaryCache, } if err := kv.db.Update(func(tx *bolt.Tx) error { diff --git a/beacon-chain/db/kv/state_summary.go b/beacon-chain/db/kv/state_summary.go index a4b6317832a0..2f9f2ffa5e1c 100644 --- a/beacon-chain/db/kv/state_summary.go +++ b/beacon-chain/db/kv/state_summary.go @@ -23,6 +23,26 @@ func (k *Store) SaveStateSummary(ctx context.Context, summary *pb.StateSummary) }) } +// SaveStateSummaries saves state summary objects to the DB. +func (k *Store) SaveStateSummaries(ctx context.Context, summaries []*pb.StateSummary) error { + ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStateSummaries") + defer span.End() + + return k.db.Update(func(tx *bolt.Tx) error { + bucket := tx.Bucket(stateSummaryBucket) + for _, summary := range summaries { + enc, err := encode(summary) + if err != nil { + return err + } + if err := bucket.Put(summary.Root, enc); err != nil { + return err + } + } + return nil + }) +} + // StateSummary returns the state summary object from the db using input block root. func (k *Store) StateSummary(ctx context.Context, blockRoot [32]byte) (*pb.StateSummary, error) { ctx, span := trace.StartSpan(ctx, "BeaconDB.StateSummary") diff --git a/beacon-chain/node/BUILD.bazel b/beacon-chain/node/BUILD.bazel index 4c4a78a33441..ee8ce766cefb 100644 --- a/beacon-chain/node/BUILD.bazel +++ b/beacon-chain/node/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//beacon-chain/archiver:go_default_library", "//beacon-chain/blockchain:go_default_library", + "//beacon-chain/cache:go_default_library", "//beacon-chain/cache/depositcache:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/flags:go_default_library", diff --git a/beacon-chain/node/node.go b/beacon-chain/node/node.go index b730fd823fc5..301957dc3633 100644 --- a/beacon-chain/node/node.go +++ b/beacon-chain/node/node.go @@ -18,6 +18,7 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/archiver" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/flags" @@ -58,20 +59,21 @@ const testSkipPowFlag = "test-skip-pow" // full PoS node. It handles the lifecycle of the entire system and registers // services to a service registry. type BeaconNode struct { - ctx *cli.Context - services *shared.ServiceRegistry - lock sync.RWMutex - stop chan struct{} // Channel to wait for termination notifications. - db db.Database - attestationPool attestations.Pool - exitPool *voluntaryexits.Pool - slashingsPool *slashings.Pool - depositCache *depositcache.DepositCache - stateFeed *event.Feed - blockFeed *event.Feed - opFeed *event.Feed - forkChoiceStore forkchoice.ForkChoicer - stateGen *stategen.State + ctx *cli.Context + services *shared.ServiceRegistry + lock sync.RWMutex + stop chan struct{} // Channel to wait for termination notifications. + db db.Database + stateSummaryCache *cache.StateSummaryCache + attestationPool attestations.Pool + exitPool *voluntaryexits.Pool + slashingsPool *slashings.Pool + depositCache *depositcache.DepositCache + stateFeed *event.Feed + blockFeed *event.Feed + opFeed *event.Feed + forkChoiceStore forkchoice.ForkChoicer + stateGen *stategen.State } // NewBeaconNode creates a new node instance, sets up configuration options, and registers @@ -92,15 +94,16 @@ func NewBeaconNode(ctx *cli.Context) (*BeaconNode, error) { registry := shared.NewServiceRegistry() beacon := &BeaconNode{ - ctx: ctx, - services: registry, - stop: make(chan struct{}), - stateFeed: new(event.Feed), - blockFeed: new(event.Feed), - opFeed: new(event.Feed), - attestationPool: attestations.NewPool(), - exitPool: voluntaryexits.NewPool(), - slashingsPool: slashings.NewPool(), + ctx: ctx, + services: registry, + stop: make(chan struct{}), + stateFeed: new(event.Feed), + blockFeed: new(event.Feed), + opFeed: new(event.Feed), + attestationPool: attestations.NewPool(), + exitPool: voluntaryexits.NewPool(), + slashingsPool: slashings.NewPool(), + stateSummaryCache: cache.NewStateSummaryCache(), } if err := beacon.startDB(ctx); err != nil { @@ -233,7 +236,7 @@ func (b *BeaconNode) startDB(ctx *cli.Context) error { clearDB := ctx.Bool(cmd.ClearDB.Name) forceClearDB := ctx.Bool(cmd.ForceClearDB.Name) - d, err := db.NewDB(dbPath) + d, err := db.NewDB(dbPath, b.stateSummaryCache) if err != nil { return err } @@ -252,7 +255,7 @@ func (b *BeaconNode) startDB(ctx *cli.Context) error { if err := d.ClearDB(); err != nil { return err } - d, err = db.NewDB(dbPath) + d, err = db.NewDB(dbPath, b.stateSummaryCache) if err != nil { return err } @@ -264,7 +267,7 @@ func (b *BeaconNode) startDB(ctx *cli.Context) error { } func (b *BeaconNode) startStateGen() { - b.stateGen = stategen.New(b.db) + b.stateGen = stategen.New(b.db, b.stateSummaryCache) } func (b *BeaconNode) registerP2P(ctx *cli.Context) error { diff --git a/beacon-chain/state/stategen/cold.go b/beacon-chain/state/stategen/cold.go index bdae6ea03ec5..60ffa3d60d0c 100644 --- a/beacon-chain/state/stategen/cold.go +++ b/beacon-chain/state/stategen/cold.go @@ -44,12 +44,9 @@ func (s *State) loadColdStateByRoot(ctx context.Context, blockRoot [32]byte) (*s ctx, span := trace.StartSpan(ctx, "stateGen.loadColdStateByRoot") defer span.End() - summary, err := s.beaconDB.StateSummary(ctx, blockRoot) + summary, err := s.stateSummary(ctx, blockRoot) if err != nil { - return nil, err - } - if summary == nil { - return nil, errUnknownStateSummary + return nil, errors.Wrap(err, "could not get state summary") } // Use the archived point state if the summary slot lies on top of the archived point. @@ -195,13 +192,10 @@ func (s *State) blockRootSlot(ctx context.Context, blockRoot [32]byte) (uint64, ctx, span := trace.StartSpan(ctx, "stateGen.blockRootSlot") defer span.End() - if s.beaconDB.HasStateSummary(ctx, blockRoot) { - summary, err := s.beaconDB.StateSummary(ctx, blockRoot) + if s.StateSummaryExists(ctx, blockRoot) { + summary, err := s.stateSummary(ctx, blockRoot) if err != nil { - return 0, nil - } - if summary == nil { - return 0, errUnknownStateSummary + return 0, err } return summary.Slot, nil } diff --git a/beacon-chain/state/stategen/getter.go b/beacon-chain/state/stategen/getter.go index 4f53f717b715..3b71430b50bf 100644 --- a/beacon-chain/state/stategen/getter.go +++ b/beacon-chain/state/stategen/getter.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" "github.com/prysmaticlabs/prysm/beacon-chain/state" + pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" "go.opencensus.io/trace" ) @@ -52,5 +53,24 @@ func (s *State) StateBySlot(ctx context.Context, slot uint64) (*state.BeaconStat // StateSummaryExists returns true if the corresponding state of the input block either // exists in the DB or it can be generated by state gen. func (s *State) StateSummaryExists(ctx context.Context, blockRoot [32]byte) bool { - return s.beaconDB.HasStateSummary(ctx, blockRoot) + return s.beaconDB.HasStateSummary(ctx, blockRoot) || s.stateSummaryCache.Has(blockRoot) +} + +// This returns the state summary object of a given block root, it first checks the cache +// then checks the DB. An error is returned if state summary object is nil. +func (s *State) stateSummary(ctx context.Context, blockRoot [32]byte) (*pb.StateSummary, error) { + var summary *pb.StateSummary + var err error + if s.stateSummaryCache.Has(blockRoot) { + summary = s.stateSummaryCache.Get(blockRoot) + } else { + summary, err = s.beaconDB.StateSummary(ctx, blockRoot) + if err != nil { + return nil, err + } + } + if summary == nil { + return nil, errUnknownStateSummary + } + return summary, nil } diff --git a/beacon-chain/state/stategen/hot.go b/beacon-chain/state/stategen/hot.go index e29afc9262a1..38e736e07481 100644 --- a/beacon-chain/state/stategen/hot.go +++ b/beacon-chain/state/stategen/hot.go @@ -36,12 +36,10 @@ func (s *State) saveHotState(ctx context.Context, blockRoot [32]byte, state *sta } // On an intermediate slots, save the hot state summary. - if err := s.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{ + s.stateSummaryCache.Put(blockRoot, &pb.StateSummary{ Slot: state.Slot(), Root: blockRoot[:], - }); err != nil { - return err - } + }) // Store the copied state in the cache. s.hotStateCache.Put(blockRoot, state) @@ -67,12 +65,9 @@ func (s *State) loadHotStateByRoot(ctx context.Context, blockRoot [32]byte) (*st return s.beaconDB.State(ctx, blockRoot) } - summary, err := s.beaconDB.StateSummary(ctx, blockRoot) + summary, err := s.stateSummary(ctx, blockRoot) if err != nil { - return nil, err - } - if summary == nil { - return nil, errUnknownStateSummary + return nil, errors.Wrap(err, "could not get state summary") } startState, err := s.lastSavedState(ctx, summary.Slot) diff --git a/beacon-chain/state/stategen/migrate.go b/beacon-chain/state/stategen/migrate.go index 364d61af3661..cd4272546acc 100644 --- a/beacon-chain/state/stategen/migrate.go +++ b/beacon-chain/state/stategen/migrate.go @@ -29,6 +29,12 @@ func (s *State) MigrateToCold(ctx context.Context, finalizedState *state.BeaconS return nil } + // Migrate all state summary objects from cache to DB. + if err := s.beaconDB.SaveStateSummaries(ctx, s.stateSummaryCache.GetAll()); err != nil { + return err + } + s.stateSummaryCache.Clear() + // Move the states between split slot to finalized slot from hot section to the cold section. filter := filters.NewFilter().SetStartSlot(currentSplitSlot).SetEndSlot(finalizedState.Slot() - 1) blockRoots, err := s.beaconDB.BlockRoots(ctx, filter) diff --git a/beacon-chain/state/stategen/service.go b/beacon-chain/state/stategen/service.go index a885a0603332..0727aae41284 100644 --- a/beacon-chain/state/stategen/service.go +++ b/beacon-chain/state/stategen/service.go @@ -23,7 +23,7 @@ type State struct { epochBoundaryLock sync.RWMutex hotStateCache *cache.HotStateCache splitInfo *splitSlotAndRoot - initSyncStateSummaries *cache.StateSummaryCache + stateSummaryCache *cache.StateSummaryCache } // This tracks the split point. The point where slot and the block root of @@ -34,14 +34,14 @@ type splitSlotAndRoot struct { } // New returns a new state management object. -func New(db db.NoHeadAccessDatabase) *State { +func New(db db.NoHeadAccessDatabase, stateSummaryCache *cache.StateSummaryCache) *State { return &State{ beaconDB: db, epochBoundarySlotToRoot: make(map[uint64][32]byte), hotStateCache: cache.NewHotStateCache(), splitInfo: &splitSlotAndRoot{slot: 0, root: params.BeaconConfig().ZeroHash}, slotsPerArchivedPoint: archivedInterval, - initSyncStateSummaries: cache.NewStateSummaryCache(), + stateSummaryCache: stateSummaryCache, } } From 1ea753cf798b758bb163b59cb98c9c1aef3d4544 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 14:42:31 -0700 Subject: [PATCH 21/39] Don't have to use finalized state --- beacon-chain/blockchain/process_block.go | 11 +++-- beacon-chain/cache/finalized_state.go | 61 ++++++++++++++++++++++++ beacon-chain/state/stategen/migrate.go | 11 ++--- 3 files changed, 72 insertions(+), 11 deletions(-) create mode 100644 beacon-chain/cache/finalized_state.go diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 57c827f93630..5d14df1f16b9 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -137,11 +137,12 @@ func (s *Service) onBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) } if featureconfig.Get().NewStateMgmt { - finalizedState, err := s.stateGen.StateByRoot(ctx, fRoot) + fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root) + finalizedSummary, err := s.beaconDB.StateSummary(ctx, fRoot) if err != nil { return nil, err } - if err := s.stateGen.MigrateToCold(ctx, finalizedState, fRoot); err != nil { + if err := s.stateGen.MigrateToCold(ctx, finalizedSummary.Slot, fRoot); err != nil { return nil, err } } @@ -297,11 +298,11 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed if featureconfig.Get().NewStateMgmt { fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root) - finalizedState, err := s.stateGen.StateByRoot(ctx, fRoot) + fBlock, err := s.beaconDB.Block(ctx, fRoot) if err != nil { - return errors.Wrap(err, "could not get state by root for migration") + return err } - if err := s.stateGen.MigrateToCold(ctx, finalizedState, fRoot); err != nil { + if err := s.stateGen.MigrateToCold(ctx, fBlock.Block.Slot, fRoot); err != nil { return errors.Wrap(err, "could not migrate with new finalized root") } diff --git a/beacon-chain/cache/finalized_state.go b/beacon-chain/cache/finalized_state.go new file mode 100644 index 000000000000..1d61121fc661 --- /dev/null +++ b/beacon-chain/cache/finalized_state.go @@ -0,0 +1,61 @@ +package cache + +import ( + lru "github.com/hashicorp/golang-lru" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" + stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" +) + +var ( + // hotStateCacheSize defines the max number of hot state this can cache. + hotStateCacheSize = 16 + // Metrics + hotStateCacheHit = promauto.NewCounter(prometheus.CounterOpts{ + Name: "hot_state_cache_hit", + Help: "The total number of cache hits on the hot state cache.", + }) + hotStateCacheMiss = promauto.NewCounter(prometheus.CounterOpts{ + Name: "hot_state_cache_miss", + Help: "The total number of cache misses on the hot state cache.", + }) +) + +// HotStateCache is used to store the processed beacon state after finalized check point.. +type HotStateCache struct { + cache *lru.Cache +} + +// NewHotStateCache initializes the map and underlying cache. +func NewHotStateCache() *HotStateCache { + cache, err := lru.New(hotStateCacheSize) + if err != nil { + panic(err) + } + return &HotStateCache{ + cache: cache, + } +} + +// Get returns a cached response via input block root, if any. +// The response is copied by default. +func (c *HotStateCache) Get(root [32]byte) *stateTrie.BeaconState { + item, exists := c.cache.Get(root) + + if exists && item != nil { + hotStateCacheHit.Inc() + return item.(*stateTrie.BeaconState).Copy() + } + hotStateCacheMiss.Inc() + return nil +} + +// Put the response in the cache. +func (c *HotStateCache) Put(root [32]byte, state *stateTrie.BeaconState) { + c.cache.Add(root, state) +} + +// Has returns true if the key exists in the cache. +func (c *HotStateCache) Has(root [32]byte) bool { + return c.cache.Contains(root) +} diff --git a/beacon-chain/state/stategen/migrate.go b/beacon-chain/state/stategen/migrate.go index cd4272546acc..919c1a5c7a89 100644 --- a/beacon-chain/state/stategen/migrate.go +++ b/beacon-chain/state/stategen/migrate.go @@ -6,7 +6,6 @@ import ( "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" - "github.com/prysmaticlabs/prysm/beacon-chain/state" "github.com/prysmaticlabs/prysm/shared/bytesutil" "github.com/sirupsen/logrus" "go.opencensus.io/trace" @@ -15,17 +14,17 @@ import ( // MigrateToCold advances the split point in between the cold and hot state sections. // It moves the recent finalized states from the hot section to the cold section and // only preserve the ones that's on archived point. -func (s *State) MigrateToCold(ctx context.Context, finalizedState *state.BeaconState, finalizedRoot [32]byte) error { +func (s *State) MigrateToCold(ctx context.Context, finalizedSlot uint64, finalizedRoot [32]byte) error { ctx, span := trace.StartSpan(ctx, "stateGen.MigrateToCold") defer span.End() // Verify migration is sensible. The new finalized point must increase the current split slot, and // on an epoch boundary for hot state summary scheme to work. currentSplitSlot := s.splitInfo.slot - if currentSplitSlot > finalizedState.Slot() { + if currentSplitSlot > finalizedSlot { return nil } - if !helpers.IsEpochStart(finalizedState.Slot()) { + if !helpers.IsEpochStart(finalizedSlot) { return nil } @@ -36,7 +35,7 @@ func (s *State) MigrateToCold(ctx context.Context, finalizedState *state.BeaconS s.stateSummaryCache.Clear() // Move the states between split slot to finalized slot from hot section to the cold section. - filter := filters.NewFilter().SetStartSlot(currentSplitSlot).SetEndSlot(finalizedState.Slot() - 1) + filter := filters.NewFilter().SetStartSlot(currentSplitSlot).SetEndSlot(finalizedSlot - 1) blockRoots, err := s.beaconDB.BlockRoots(ctx, filter) if err != nil { return err @@ -90,7 +89,7 @@ func (s *State) MigrateToCold(ctx context.Context, finalizedState *state.BeaconS } // Update the split slot and root. - s.splitInfo = &splitSlotAndRoot{slot: finalizedState.Slot(), root: finalizedRoot} + s.splitInfo = &splitSlotAndRoot{slot: finalizedSlot, root: finalizedRoot} log.WithFields(logrus.Fields{ "slot": s.splitInfo.slot, "root": hex.EncodeToString(bytesutil.Trunc(s.splitInfo.root[:])), From e4a5a641b36b09b805b7fd242ffbcc67b183634d Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 14:42:43 -0700 Subject: [PATCH 22/39] Rm unused file --- beacon-chain/cache/finalized_state.go | 61 --------------------------- 1 file changed, 61 deletions(-) delete mode 100644 beacon-chain/cache/finalized_state.go diff --git a/beacon-chain/cache/finalized_state.go b/beacon-chain/cache/finalized_state.go deleted file mode 100644 index 1d61121fc661..000000000000 --- a/beacon-chain/cache/finalized_state.go +++ /dev/null @@ -1,61 +0,0 @@ -package cache - -import ( - lru "github.com/hashicorp/golang-lru" - "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promauto" - stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state" -) - -var ( - // hotStateCacheSize defines the max number of hot state this can cache. - hotStateCacheSize = 16 - // Metrics - hotStateCacheHit = promauto.NewCounter(prometheus.CounterOpts{ - Name: "hot_state_cache_hit", - Help: "The total number of cache hits on the hot state cache.", - }) - hotStateCacheMiss = promauto.NewCounter(prometheus.CounterOpts{ - Name: "hot_state_cache_miss", - Help: "The total number of cache misses on the hot state cache.", - }) -) - -// HotStateCache is used to store the processed beacon state after finalized check point.. -type HotStateCache struct { - cache *lru.Cache -} - -// NewHotStateCache initializes the map and underlying cache. -func NewHotStateCache() *HotStateCache { - cache, err := lru.New(hotStateCacheSize) - if err != nil { - panic(err) - } - return &HotStateCache{ - cache: cache, - } -} - -// Get returns a cached response via input block root, if any. -// The response is copied by default. -func (c *HotStateCache) Get(root [32]byte) *stateTrie.BeaconState { - item, exists := c.cache.Get(root) - - if exists && item != nil { - hotStateCacheHit.Inc() - return item.(*stateTrie.BeaconState).Copy() - } - hotStateCacheMiss.Inc() - return nil -} - -// Put the response in the cache. -func (c *HotStateCache) Put(root [32]byte, state *stateTrie.BeaconState) { - c.cache.Add(root, state) -} - -// Has returns true if the key exists in the cache. -func (c *HotStateCache) Has(root [32]byte) bool { - return c.cache.Contains(root) -} From f5b3e7ff4714fef9f0397007f519a45fa259ad24 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Sun, 29 Mar 2020 16:12:41 -0700 Subject: [PATCH 23/39] some changes to memory mgmt when using mempool --- beacon-chain/state/field_trie.go | 2 +- beacon-chain/state/getters.go | 8 ++--- beacon-chain/state/state_trie.go | 22 ++++++++++--- shared/memorypool/memorypool.go | 53 ++++++++++++++++++++++++++++++-- 4 files changed, 72 insertions(+), 13 deletions(-) diff --git a/beacon-chain/state/field_trie.go b/beacon-chain/state/field_trie.go index 959e62f0157c..a6d3563a7b63 100644 --- a/beacon-chain/state/field_trie.go +++ b/beacon-chain/state/field_trie.go @@ -117,7 +117,7 @@ func (f *FieldTrie) CopyTrie() *FieldTrie { case stateRoots: dstFieldTrie = memorypool.GetStateRootsTrie(len(f.fieldLayers)) default: - dstFieldTrie = make([][]*[32]byte, len(f.fieldLayers)) + dstFieldTrie = memorypool.GetFieldTrie(len(f.fieldLayers)) } for i, layer := range f.fieldLayers { diff --git a/beacon-chain/state/getters.go b/beacon-chain/state/getters.go index cbf215292c89..02740a1ff573 100644 --- a/beacon-chain/state/getters.go +++ b/beacon-chain/state/getters.go @@ -222,7 +222,7 @@ func (b *BeaconState) BlockRoots() [][]byte { if b.state.BlockRoots == nil { return nil } - roots := make([][]byte, len(b.state.BlockRoots)) + roots := memorypool.GetDoubleByteSlice(len(b.state.BlockRoots)) for i, r := range b.state.BlockRoots { tmpRt := make([]byte, len(r)) copy(tmpRt, r) @@ -264,7 +264,7 @@ func (b *BeaconState) StateRoots() [][]byte { b.lock.RLock() defer b.lock.RUnlock() - roots := make([][]byte, len(b.state.StateRoots)) + roots := memorypool.GetDoubleByteSlice(len(b.state.StateRoots)) for i, r := range b.state.StateRoots { tmpRt := make([]byte, len(r)) copy(tmpRt, r) @@ -285,7 +285,7 @@ func (b *BeaconState) HistoricalRoots() [][]byte { b.lock.RLock() defer b.lock.RUnlock() - roots := make([][]byte, len(b.state.HistoricalRoots)) + roots := memorypool.GetDoubleByteSlice(len(b.state.HistoricalRoots)) for i, r := range b.state.HistoricalRoots { tmpRt := make([]byte, len(r)) copy(tmpRt, r) @@ -563,7 +563,7 @@ func (b *BeaconState) RandaoMixes() [][]byte { b.lock.RLock() defer b.lock.RUnlock() - mixes := memorypool.GetDoubleByteSlice(len(b.state.RandaoMixes)) + mixes := memorypool.GetRandoMixes(len(b.state.RandaoMixes)) for i, r := range b.state.RandaoMixes { tmpRt := make([]byte, len(r)) copy(tmpRt, r) diff --git a/beacon-chain/state/state_trie.go b/beacon-chain/state/state_trie.go index a73587b7902e..d8d68cfeaa6a 100644 --- a/beacon-chain/state/state_trie.go +++ b/beacon-chain/state/state_trie.go @@ -158,18 +158,30 @@ func (b *BeaconState) Copy() *BeaconState { v.refs-- if b.stateFieldLeaves[field].reference != nil { b.stateFieldLeaves[field].MinusRef() + if b.stateFieldLeaves[field].refs == 0 && field != randaoMixes && field != blockRoots && field != stateRoots && field != historicalRoots { + memorypool.PutFieldTrie(b.stateFieldLeaves[field].fieldLayers) + } } if field == randaoMixes && v.refs == 0 { - memorypool.PutDoubleByteSlice(b.state.RandaoMixes) + memorypool.PutRandaoMixes(b.state.RandaoMixes) if b.stateFieldLeaves[field].refs == 0 { memorypool.PutRandaoMixesTrie(b.stateFieldLeaves[randaoMixes].fieldLayers) } } - if field == blockRoots && v.refs == 0 && b.stateFieldLeaves[field].refs == 0 { - memorypool.PutBlockRootsTrie(b.stateFieldLeaves[blockRoots].fieldLayers) + if field == blockRoots && v.refs == 0 { + memorypool.PutDoubleByteSlice(b.state.BlockRoots) + if b.stateFieldLeaves[field].refs == 0 { + memorypool.PutBlockRootsTrie(b.stateFieldLeaves[blockRoots].fieldLayers) + } + } + if field == stateRoots && v.refs == 0 { + memorypool.PutDoubleByteSlice(b.state.StateRoots) + if b.stateFieldLeaves[field].refs == 0 { + memorypool.PutStateRootsTrie(b.stateFieldLeaves[stateRoots].fieldLayers) + } } - if field == stateRoots && v.refs == 0 && b.stateFieldLeaves[field].refs == 0 { - memorypool.PutStateRootsTrie(b.stateFieldLeaves[stateRoots].fieldLayers) + if field == historicalRoots && v.refs == 0 { + memorypool.PutDoubleByteSlice(b.state.HistoricalRoots) } } }) diff --git a/shared/memorypool/memorypool.go b/shared/memorypool/memorypool.go index 5e9eece06fe3..5f1e8df14738 100644 --- a/shared/memorypool/memorypool.go +++ b/shared/memorypool/memorypool.go @@ -22,6 +22,8 @@ var StateRootsMemoryPool = new(sync.Pool) // for randao mixes trie. var RandaoMixesMemoryPool = new(sync.Pool) +var GenericFieldFieldTriePool = new(sync.Pool) + // GetDoubleByteSlice retrieves the 2d byte slice of // the desired size from the memory pool. func GetDoubleByteSlice(size int) [][]byte { @@ -48,6 +50,29 @@ func PutDoubleByteSlice(data [][]byte) { } } +func GetRandoMixes(size int) [][]byte { + if !featureconfig.Get().EnableByteMempool { + return make([][]byte, size) + } + rawObj := RandaoMixesMemoryPool.Get() + if rawObj == nil { + return GetDoubleByteSlice(size) + } + + byteSlice := rawObj.([][]byte) + if len(byteSlice) >= size { + return byteSlice[:size] + } + return append(byteSlice, make([][]byte, size-len(byteSlice))...) +} + +func PutRandaoMixes(data [][]byte) { + if !featureconfig.Get().EnableByteMempool { + return + } + RandaoMixesMemoryPool.Put(data) +} + // GetBlockRootsTrie retrieves the 3d byte trie of // the desired size from the memory pool. func GetBlockRootsTrie(size int) [][]*[32]byte { @@ -57,7 +82,7 @@ func GetBlockRootsTrie(size int) [][]*[32]byte { rawObj := BlockRootsMemoryPool.Get() if rawObj == nil { - return make([][]*[32]byte, size) + return GetFieldTrie(size) } byteSlice := rawObj.([][]*[32]byte) if len(byteSlice) >= size { @@ -83,7 +108,7 @@ func GetStateRootsTrie(size int) [][]*[32]byte { rawObj := BlockRootsMemoryPool.Get() if rawObj == nil { - return make([][]*[32]byte, size) + return GetFieldTrie(size) } byteSlice := rawObj.([][]*[32]byte) if len(byteSlice) >= size { @@ -109,7 +134,7 @@ func GetRandaoMixesTrie(size int) [][]*[32]byte { rawObj := StateRootsMemoryPool.Get() if rawObj == nil { - return make([][]*[32]byte, size) + return GetFieldTrie(size) } byteSlice := rawObj.([][]*[32]byte) if len(byteSlice) >= size { @@ -125,3 +150,25 @@ func PutRandaoMixesTrie(data [][]*[32]byte) { StateRootsMemoryPool.Put(data) } } + +func GetFieldTrie(size int) [][]*[32]byte { + if !featureconfig.Get().EnableByteMempool { + return make([][]*[32]byte, size) + } + + rawObj := GenericFieldFieldTriePool.Get() + if rawObj == nil { + return make([][]*[32]byte, size) + } + byteSlice := rawObj.([][]*[32]byte) + if len(byteSlice) >= size { + return byteSlice[:size] + } + return append(byteSlice, make([][]*[32]byte, size-len(byteSlice))...) +} + +func PutFieldTrie(data [][]*[32]byte) { + if featureconfig.Get().EnableByteMempool { + GenericFieldFieldTriePool.Put(data) + } +} From 9b9ef915c911cb334ce9ef6cb0f5cc8773ccdaaa Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 19:45:49 -0700 Subject: [PATCH 24/39] More run time fixes --- beacon-chain/blockchain/process_block.go | 4 ++-- beacon-chain/db/kv/blocks.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 5d14df1f16b9..60d2b4925425 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -138,11 +138,11 @@ func (s *Service) onBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) if featureconfig.Get().NewStateMgmt { fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root) - finalizedSummary, err := s.beaconDB.StateSummary(ctx, fRoot) + fBlock, err := s.beaconDB.Block(ctx, fRoot) if err != nil { return nil, err } - if err := s.stateGen.MigrateToCold(ctx, finalizedSummary.Slot, fRoot); err != nil { + if err := s.stateGen.MigrateToCold(ctx, fBlock.Block.Slot, fRoot); err != nil { return nil, err } } diff --git a/beacon-chain/db/kv/blocks.go b/beacon-chain/db/kv/blocks.go index a07a0f137186..7cd956a85f43 100644 --- a/beacon-chain/db/kv/blocks.go +++ b/beacon-chain/db/kv/blocks.go @@ -263,7 +263,7 @@ func (k *Store) SaveHeadBlockRoot(ctx context.Context, blockRoot [32]byte) error defer span.End() return k.db.Update(func(tx *bolt.Tx) error { if featureconfig.Get().NewStateMgmt { - if tx.Bucket(stateSummaryBucket).Get(blockRoot[:]) == nil { + if tx.Bucket(stateSummaryBucket).Get(blockRoot[:]) == nil && !k.stateSummaryCache.Has(blockRoot) { return errors.New("no state summary found with head block root") } } else { From 1ad856604f7089082c8992af43d837860ede20b5 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Sun, 29 Mar 2020 21:36:17 -0700 Subject: [PATCH 25/39] Can sync to head --- beacon-chain/node/node.go | 1 + beacon-chain/sync/pending_attestations_queue.go | 2 +- beacon-chain/sync/service.go | 4 ++++ beacon-chain/sync/validate_aggregate_proof.go | 7 ++++--- .../sync/validate_committee_index_beacon_attestation.go | 7 ++++--- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/beacon-chain/node/node.go b/beacon-chain/node/node.go index 301957dc3633..f09af7a3d7fd 100644 --- a/beacon-chain/node/node.go +++ b/beacon-chain/node/node.go @@ -440,6 +440,7 @@ func (b *BeaconNode) registerSyncService(ctx *cli.Context) error { AttPool: b.attestationPool, ExitPool: b.exitPool, SlashingPool: b.slashingsPool, + StateSummaryCache: b.stateSummaryCache, }) return b.services.RegisterService(rs) diff --git a/beacon-chain/sync/pending_attestations_queue.go b/beacon-chain/sync/pending_attestations_queue.go index bfb54748c933..5ba160fa4f14 100644 --- a/beacon-chain/sync/pending_attestations_queue.go +++ b/beacon-chain/sync/pending_attestations_queue.go @@ -62,7 +62,7 @@ func (s *Service) processPendingAtts(ctx context.Context) error { attestations := s.blkRootToPendingAtts[bRoot] s.pendingAttsLock.RUnlock() // Has the pending attestation's missing block arrived and the node processed block yet? - hasStateSummary := featureconfig.Get().NewStateMgmt && s.db.HasStateSummary(ctx, bRoot) + hasStateSummary := featureconfig.Get().NewStateMgmt && s.db.HasStateSummary(ctx, bRoot) || s.stateSummaryCache.Has(bRoot) if s.db.HasBlock(ctx, bRoot) && (s.db.HasState(ctx, bRoot) || hasStateSummary) { numberOfBlocksRecoveredFromAtt.Inc() for _, att := range attestations { diff --git a/beacon-chain/sync/service.go b/beacon-chain/sync/service.go index cbeef88cef43..5162349ab1ae 100644 --- a/beacon-chain/sync/service.go +++ b/beacon-chain/sync/service.go @@ -9,6 +9,7 @@ import ( "github.com/pkg/errors" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/prysm/beacon-chain/blockchain" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" blockfeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/block" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/operation" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" @@ -43,6 +44,7 @@ type Config struct { StateNotifier statefeed.Notifier BlockNotifier blockfeed.Notifier AttestationNotifier operation.Notifier + StateSummaryCache *cache.StateSummaryCache } // This defines the interface for interacting with block chain service @@ -74,6 +76,7 @@ func NewRegularSync(cfg *Config) *Service { blkRootToPendingAtts: make(map[[32]byte][]*ethpb.AggregateAttestationAndProof), stateNotifier: cfg.StateNotifier, blockNotifier: cfg.BlockNotifier, + stateSummaryCache: cfg.StateSummaryCache, blocksRateLimiter: leakybucket.NewCollector(allowedBlocksPerSecond, allowedBlocksBurst, false /* deleteEmptyBuckets */), } @@ -106,6 +109,7 @@ type Service struct { blockNotifier blockfeed.Notifier blocksRateLimiter *leakybucket.Collector attestationNotifier operation.Notifier + stateSummaryCache *cache.StateSummaryCache } // Start the regular sync service. diff --git a/beacon-chain/sync/validate_aggregate_proof.go b/beacon-chain/sync/validate_aggregate_proof.go index 44aa02769107..03255741206e 100644 --- a/beacon-chain/sync/validate_aggregate_proof.go +++ b/beacon-chain/sync/validate_aggregate_proof.go @@ -128,9 +128,10 @@ func (r *Service) validateAggregatedAtt(ctx context.Context, a *ethpb.AggregateA func (r *Service) validateBlockInAttestation(ctx context.Context, a *ethpb.AggregateAttestationAndProof) bool { // Verify the block being voted and the processed state is in DB. The block should have passed validation if it's in the DB. - hasStateSummary := featureconfig.Get().NewStateMgmt && r.db.HasStateSummary(ctx, bytesutil.ToBytes32(a.Aggregate.Data.BeaconBlockRoot)) - hasState := r.db.HasState(ctx, bytesutil.ToBytes32(a.Aggregate.Data.BeaconBlockRoot)) || hasStateSummary - hasBlock := r.db.HasBlock(ctx, bytesutil.ToBytes32(a.Aggregate.Data.BeaconBlockRoot)) + blockRoot := bytesutil.ToBytes32(a.Aggregate.Data.BeaconBlockRoot) + hasStateSummary := featureconfig.Get().NewStateMgmt && r.db.HasStateSummary(ctx, blockRoot) || r.stateSummaryCache.Has(blockRoot) + hasState := r.db.HasState(ctx, blockRoot) || hasStateSummary + hasBlock := r.db.HasBlock(ctx, blockRoot) if !(hasState && hasBlock) { // A node doesn't have the block, it'll request from peer while saving the pending attestation to a queue. r.savePendingAtt(a) diff --git a/beacon-chain/sync/validate_committee_index_beacon_attestation.go b/beacon-chain/sync/validate_committee_index_beacon_attestation.go index d816caa6739d..9e84c63f0c8f 100644 --- a/beacon-chain/sync/validate_committee_index_beacon_attestation.go +++ b/beacon-chain/sync/validate_committee_index_beacon_attestation.go @@ -74,9 +74,10 @@ func (s *Service) validateCommitteeIndexBeaconAttestation(ctx context.Context, p } // Verify the block being voted and the processed state is in DB and. The block should have passed validation if it's in the DB. - hasStateSummary := featureconfig.Get().NewStateMgmt && s.db.HasStateSummary(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) - hasState := s.db.HasState(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) || hasStateSummary - hasBlock := s.db.HasBlock(ctx, bytesutil.ToBytes32(att.Data.BeaconBlockRoot)) + blockRoot := bytesutil.ToBytes32(att.Data.BeaconBlockRoot) + hasStateSummary := featureconfig.Get().NewStateMgmt && s.db.HasStateSummary(ctx, blockRoot) || s.stateSummaryCache.Has(blockRoot) + hasState := s.db.HasState(ctx, blockRoot) || hasStateSummary + hasBlock := s.db.HasBlock(ctx, blockRoot) if !(hasState && hasBlock) { // A node doesn't have the block, it'll request from peer while saving the pending attestation to a queue. s.savePendingAtt(ð.AggregateAttestationAndProof{Aggregate: att}) From acfba2266fc488e6aaab888f99559124c81a236b Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 08:38:49 -0700 Subject: [PATCH 26/39] Feedback --- beacon-chain/blockchain/init_sync_process_block.go | 10 ++++------ beacon-chain/blockchain/process_block.go | 5 ++++- beacon-chain/sync/initial-sync-old/round_robin.go | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/beacon-chain/blockchain/init_sync_process_block.go b/beacon-chain/blockchain/init_sync_process_block.go index 1981d1b6953f..38bb2d74d90c 100644 --- a/beacon-chain/blockchain/init_sync_process_block.go +++ b/beacon-chain/blockchain/init_sync_process_block.go @@ -175,13 +175,11 @@ func (s *Service) generateState(ctx context.Context, startRoot [32]byte, endRoot var endBlock *ethpb.SignedBeaconBlock if featureconfig.Get().InitSyncBatchSaveBlocks && s.hasInitSyncBlock(endRoot) { - endBlock = s.getInitSyncBlock(endRoot) - if featureconfig.Get().InitSyncBatchSaveBlocks { - if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { - return nil, err - } - s.clearInitSyncBlocks() + if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { + return nil, err } + s.clearInitSyncBlocks() + endBlock = s.getInitSyncBlock(endRoot) } else { endBlock, err = s.beaconDB.Block(ctx, endRoot) if err != nil { diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index 57c827f93630..cff7ce5830d3 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -20,6 +20,9 @@ import ( "go.opencensus.io/trace" ) +// This defines size of the upper bound for initial sync block cache. +var initialSyncBlockCacheSize = 2 * params.BeaconConfig().SlotsPerEpoch + // onBlock is called when a gossip block is received. It runs regular state transition on the block. // // Spec pseudocode definition: @@ -253,7 +256,7 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed } // Rate limit how many blocks (2 epochs worth of blocks) a node keeps in the memory. - if len(s.getInitSyncBlocks()) > 2*int(params.BeaconConfig().SlotsPerEpoch) { + if len(s.getInitSyncBlocks()) > int(initialSyncBlockCacheSize) { if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil { return err } diff --git a/beacon-chain/sync/initial-sync-old/round_robin.go b/beacon-chain/sync/initial-sync-old/round_robin.go index 5d11df19ccc8..85d12ddbd441 100644 --- a/beacon-chain/sync/initial-sync-old/round_robin.go +++ b/beacon-chain/sync/initial-sync-old/round_robin.go @@ -203,7 +203,7 @@ func (s *Service) roundRobinSync(genesis time.Time) error { 1, // step blockBatchSize, // count peers, // peers - 0, // reminder + 0, // remainder ) if err != nil { log.WithError(err).Error("Round robing sync request failed") @@ -221,7 +221,7 @@ func (s *Service) roundRobinSync(genesis time.Time) error { s.logSyncStatus(genesis, blk.Block, peers, counter) parentRoot := bytesutil.ToBytes32(blk.Block.ParentRoot) if !s.db.HasBlock(ctx, parentRoot) && !s.chain.HasInitSyncBlock(parentRoot) { - log.Debugf("Beacon node doesn't have a block in db or cache with root %#x", parentRoot) + log.WithField("parentRoot", parentRoot).Debug("Beacon node doesn't have a block in DB or cache") continue } From b8cb68bfd4c4e014840748a9bb11a49b1f596fea Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 09:26:02 -0700 Subject: [PATCH 27/39] Revert "some changes to memory mgmt when using mempool" This reverts commit f5b3e7ff4714fef9f0397007f519a45fa259ad24. --- beacon-chain/state/field_trie.go | 2 +- beacon-chain/state/getters.go | 8 ++--- beacon-chain/state/state_trie.go | 22 +++---------- shared/memorypool/memorypool.go | 53 ++------------------------------ 4 files changed, 13 insertions(+), 72 deletions(-) diff --git a/beacon-chain/state/field_trie.go b/beacon-chain/state/field_trie.go index a6d3563a7b63..959e62f0157c 100644 --- a/beacon-chain/state/field_trie.go +++ b/beacon-chain/state/field_trie.go @@ -117,7 +117,7 @@ func (f *FieldTrie) CopyTrie() *FieldTrie { case stateRoots: dstFieldTrie = memorypool.GetStateRootsTrie(len(f.fieldLayers)) default: - dstFieldTrie = memorypool.GetFieldTrie(len(f.fieldLayers)) + dstFieldTrie = make([][]*[32]byte, len(f.fieldLayers)) } for i, layer := range f.fieldLayers { diff --git a/beacon-chain/state/getters.go b/beacon-chain/state/getters.go index 02740a1ff573..cbf215292c89 100644 --- a/beacon-chain/state/getters.go +++ b/beacon-chain/state/getters.go @@ -222,7 +222,7 @@ func (b *BeaconState) BlockRoots() [][]byte { if b.state.BlockRoots == nil { return nil } - roots := memorypool.GetDoubleByteSlice(len(b.state.BlockRoots)) + roots := make([][]byte, len(b.state.BlockRoots)) for i, r := range b.state.BlockRoots { tmpRt := make([]byte, len(r)) copy(tmpRt, r) @@ -264,7 +264,7 @@ func (b *BeaconState) StateRoots() [][]byte { b.lock.RLock() defer b.lock.RUnlock() - roots := memorypool.GetDoubleByteSlice(len(b.state.StateRoots)) + roots := make([][]byte, len(b.state.StateRoots)) for i, r := range b.state.StateRoots { tmpRt := make([]byte, len(r)) copy(tmpRt, r) @@ -285,7 +285,7 @@ func (b *BeaconState) HistoricalRoots() [][]byte { b.lock.RLock() defer b.lock.RUnlock() - roots := memorypool.GetDoubleByteSlice(len(b.state.HistoricalRoots)) + roots := make([][]byte, len(b.state.HistoricalRoots)) for i, r := range b.state.HistoricalRoots { tmpRt := make([]byte, len(r)) copy(tmpRt, r) @@ -563,7 +563,7 @@ func (b *BeaconState) RandaoMixes() [][]byte { b.lock.RLock() defer b.lock.RUnlock() - mixes := memorypool.GetRandoMixes(len(b.state.RandaoMixes)) + mixes := memorypool.GetDoubleByteSlice(len(b.state.RandaoMixes)) for i, r := range b.state.RandaoMixes { tmpRt := make([]byte, len(r)) copy(tmpRt, r) diff --git a/beacon-chain/state/state_trie.go b/beacon-chain/state/state_trie.go index d8d68cfeaa6a..a73587b7902e 100644 --- a/beacon-chain/state/state_trie.go +++ b/beacon-chain/state/state_trie.go @@ -158,30 +158,18 @@ func (b *BeaconState) Copy() *BeaconState { v.refs-- if b.stateFieldLeaves[field].reference != nil { b.stateFieldLeaves[field].MinusRef() - if b.stateFieldLeaves[field].refs == 0 && field != randaoMixes && field != blockRoots && field != stateRoots && field != historicalRoots { - memorypool.PutFieldTrie(b.stateFieldLeaves[field].fieldLayers) - } } if field == randaoMixes && v.refs == 0 { - memorypool.PutRandaoMixes(b.state.RandaoMixes) + memorypool.PutDoubleByteSlice(b.state.RandaoMixes) if b.stateFieldLeaves[field].refs == 0 { memorypool.PutRandaoMixesTrie(b.stateFieldLeaves[randaoMixes].fieldLayers) } } - if field == blockRoots && v.refs == 0 { - memorypool.PutDoubleByteSlice(b.state.BlockRoots) - if b.stateFieldLeaves[field].refs == 0 { - memorypool.PutBlockRootsTrie(b.stateFieldLeaves[blockRoots].fieldLayers) - } - } - if field == stateRoots && v.refs == 0 { - memorypool.PutDoubleByteSlice(b.state.StateRoots) - if b.stateFieldLeaves[field].refs == 0 { - memorypool.PutStateRootsTrie(b.stateFieldLeaves[stateRoots].fieldLayers) - } + if field == blockRoots && v.refs == 0 && b.stateFieldLeaves[field].refs == 0 { + memorypool.PutBlockRootsTrie(b.stateFieldLeaves[blockRoots].fieldLayers) } - if field == historicalRoots && v.refs == 0 { - memorypool.PutDoubleByteSlice(b.state.HistoricalRoots) + if field == stateRoots && v.refs == 0 && b.stateFieldLeaves[field].refs == 0 { + memorypool.PutStateRootsTrie(b.stateFieldLeaves[stateRoots].fieldLayers) } } }) diff --git a/shared/memorypool/memorypool.go b/shared/memorypool/memorypool.go index 5f1e8df14738..5e9eece06fe3 100644 --- a/shared/memorypool/memorypool.go +++ b/shared/memorypool/memorypool.go @@ -22,8 +22,6 @@ var StateRootsMemoryPool = new(sync.Pool) // for randao mixes trie. var RandaoMixesMemoryPool = new(sync.Pool) -var GenericFieldFieldTriePool = new(sync.Pool) - // GetDoubleByteSlice retrieves the 2d byte slice of // the desired size from the memory pool. func GetDoubleByteSlice(size int) [][]byte { @@ -50,29 +48,6 @@ func PutDoubleByteSlice(data [][]byte) { } } -func GetRandoMixes(size int) [][]byte { - if !featureconfig.Get().EnableByteMempool { - return make([][]byte, size) - } - rawObj := RandaoMixesMemoryPool.Get() - if rawObj == nil { - return GetDoubleByteSlice(size) - } - - byteSlice := rawObj.([][]byte) - if len(byteSlice) >= size { - return byteSlice[:size] - } - return append(byteSlice, make([][]byte, size-len(byteSlice))...) -} - -func PutRandaoMixes(data [][]byte) { - if !featureconfig.Get().EnableByteMempool { - return - } - RandaoMixesMemoryPool.Put(data) -} - // GetBlockRootsTrie retrieves the 3d byte trie of // the desired size from the memory pool. func GetBlockRootsTrie(size int) [][]*[32]byte { @@ -82,7 +57,7 @@ func GetBlockRootsTrie(size int) [][]*[32]byte { rawObj := BlockRootsMemoryPool.Get() if rawObj == nil { - return GetFieldTrie(size) + return make([][]*[32]byte, size) } byteSlice := rawObj.([][]*[32]byte) if len(byteSlice) >= size { @@ -108,7 +83,7 @@ func GetStateRootsTrie(size int) [][]*[32]byte { rawObj := BlockRootsMemoryPool.Get() if rawObj == nil { - return GetFieldTrie(size) + return make([][]*[32]byte, size) } byteSlice := rawObj.([][]*[32]byte) if len(byteSlice) >= size { @@ -134,7 +109,7 @@ func GetRandaoMixesTrie(size int) [][]*[32]byte { rawObj := StateRootsMemoryPool.Get() if rawObj == nil { - return GetFieldTrie(size) + return make([][]*[32]byte, size) } byteSlice := rawObj.([][]*[32]byte) if len(byteSlice) >= size { @@ -150,25 +125,3 @@ func PutRandaoMixesTrie(data [][]*[32]byte) { StateRootsMemoryPool.Put(data) } } - -func GetFieldTrie(size int) [][]*[32]byte { - if !featureconfig.Get().EnableByteMempool { - return make([][]*[32]byte, size) - } - - rawObj := GenericFieldFieldTriePool.Get() - if rawObj == nil { - return make([][]*[32]byte, size) - } - byteSlice := rawObj.([][]*[32]byte) - if len(byteSlice) >= size { - return byteSlice[:size] - } - return append(byteSlice, make([][]*[32]byte, size-len(byteSlice))...) -} - -func PutFieldTrie(data [][]*[32]byte) { - if featureconfig.Get().EnableByteMempool { - GenericFieldFieldTriePool.Put(data) - } -} From 85e831704d961a5b2b73d375ec3a300c275ffc74 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 09:50:10 -0700 Subject: [PATCH 28/39] Fixed sync tests --- .../init_sync_process_block_test.go | 3 ++- beacon-chain/db/db_kafka_wrapped.go | 3 ++- beacon-chain/db/kv/kv_test.go | 3 ++- beacon-chain/db/testing/BUILD.bazel | 1 + beacon-chain/db/testing/setup_db.go | 3 ++- beacon-chain/state/stategen/cold_test.go | 7 +++--- beacon-chain/state/stategen/getter_test.go | 11 ++++---- beacon-chain/state/stategen/hot_test.go | 15 +++++------ beacon-chain/state/stategen/migrate_test.go | 25 +++++++------------ beacon-chain/state/stategen/replay_test.go | 9 ++++--- beacon-chain/state/stategen/service_test.go | 3 ++- beacon-chain/state/stategen/setter_test.go | 7 +++--- .../sync/pending_attestations_queue_test.go | 4 +++ ...committee_index_beacon_attestation_test.go | 2 ++ .../sync/validate_aggregate_proof_test.go | 8 ++++-- ...committee_index_beacon_attestation_test.go | 2 ++ 16 files changed, 61 insertions(+), 45 deletions(-) diff --git a/beacon-chain/blockchain/init_sync_process_block_test.go b/beacon-chain/blockchain/init_sync_process_block_test.go index ae870403b55a..fdf0f7fcde63 100644 --- a/beacon-chain/blockchain/init_sync_process_block_test.go +++ b/beacon-chain/blockchain/init_sync_process_block_test.go @@ -6,6 +6,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/core/state" @@ -210,7 +211,7 @@ func TestPruneNonBoundary_CanPrune(t *testing.T) { func TestGenerateState_CorrectlyGenerated(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - cfg := &Config{BeaconDB: db, StateGen: stategen.New(db)} + cfg := &Config{BeaconDB: db, StateGen: stategen.New(db, cache.NewStateSummaryCache())} service, err := NewService(context.Background(), cfg) if err != nil { t.Fatal(err) diff --git a/beacon-chain/db/db_kafka_wrapped.go b/beacon-chain/db/db_kafka_wrapped.go index ee59b093cb07..2ceae8f0675d 100644 --- a/beacon-chain/db/db_kafka_wrapped.go +++ b/beacon-chain/db/db_kafka_wrapped.go @@ -1,13 +1,14 @@ package db import ( + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/db/kafka" "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" ) // NewDB initializes a new DB with kafka wrapper. func NewDB(dirPath string) (Database, error) { - db, err := kv.NewKVStore(dirPath) + db, err := kv.NewKVStore(dirPath, cache.NewStateSummaryCache()) if err != nil { return nil, err } diff --git a/beacon-chain/db/kv/kv_test.go b/beacon-chain/db/kv/kv_test.go index d08cfa6c3e0b..dd719fbb8c22 100644 --- a/beacon-chain/db/kv/kv_test.go +++ b/beacon-chain/db/kv/kv_test.go @@ -8,6 +8,7 @@ import ( "path" "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/shared/testutil" ) @@ -21,7 +22,7 @@ func setupDB(t testing.TB) *Store { if err := os.RemoveAll(path); err != nil { t.Fatalf("Failed to remove directory: %v", err) } - db, err := NewKVStore(path) + db, err := NewKVStore(path, cache.NewStateSummaryCache()) if err != nil { t.Fatalf("Failed to instantiate DB: %v", err) } diff --git a/beacon-chain/db/testing/BUILD.bazel b/beacon-chain/db/testing/BUILD.bazel index 2ca28958dedf..3df156edb95f 100644 --- a/beacon-chain/db/testing/BUILD.bazel +++ b/beacon-chain/db/testing/BUILD.bazel @@ -7,6 +7,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db/testing", visibility = ["//beacon-chain:__subpackages__"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/kv:go_default_library", "//shared/testutil:go_default_library", diff --git a/beacon-chain/db/testing/setup_db.go b/beacon-chain/db/testing/setup_db.go index 8e0c347b9e21..c1450767a830 100644 --- a/beacon-chain/db/testing/setup_db.go +++ b/beacon-chain/db/testing/setup_db.go @@ -8,6 +8,7 @@ import ( "path" "testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/kv" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -23,7 +24,7 @@ func SetupDB(t testing.TB) db.Database { if err := os.RemoveAll(p); err != nil { t.Fatalf("failed to remove directory: %v", err) } - s, err := kv.NewKVStore(p) + s, err := kv.NewKVStore(p, cache.NewStateSummaryCache()) if err != nil { t.Fatal(err) } diff --git a/beacon-chain/state/stategen/cold_test.go b/beacon-chain/state/stategen/cold_test.go index ab060c63bb87..6fb2508d0788 100644 --- a/beacon-chain/state/stategen/cold_test.go +++ b/beacon-chain/state/stategen/cold_test.go @@ -6,6 +6,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -16,7 +17,7 @@ func TestSaveColdState_NonArchivedPoint(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.slotsPerArchivedPoint = 2 beaconState, _ := testutil.DeterministicGenesisState(t, 32) beaconState.SetSlot(1) @@ -307,7 +308,7 @@ func TestBlockRootSlot_Exists(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) bRoot := [32]byte{'A'} bSlot := uint64(100) if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{ @@ -332,7 +333,7 @@ func TestBlockRootSlot_CanRecoverAndSave(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) bSlot := uint64(100) b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: bSlot}} bRoot, _ := ssz.HashTreeRoot(b.Block) diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index 48e287246c9d..e1129d2cef45 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -7,6 +7,7 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" @@ -18,7 +19,7 @@ func TestStateByRoot_ColdState(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.splitInfo.slot = 2 service.slotsPerArchivedPoint = 1 @@ -53,7 +54,7 @@ func TestStateByRoot_HotStateDB(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} @@ -85,7 +86,7 @@ func TestStateByRoot_HotStateCached(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) r := [32]byte{'A'} @@ -110,7 +111,7 @@ func TestStateBySlot_ColdState(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.slotsPerArchivedPoint = params.BeaconConfig().SlotsPerEpoch * 2 service.splitInfo.slot = service.slotsPerArchivedPoint + 1 @@ -155,7 +156,7 @@ func TestStateBySlot_HotStateDB(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} diff --git a/beacon-chain/state/stategen/hot_test.go b/beacon-chain/state/stategen/hot_test.go index 5cc6bddd83a2..2fc9ffe98211 100644 --- a/beacon-chain/state/stategen/hot_test.go +++ b/beacon-chain/state/stategen/hot_test.go @@ -7,6 +7,7 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" @@ -21,7 +22,7 @@ func TestSaveHotState_AlreadyHas(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch) @@ -48,7 +49,7 @@ func TestSaveHotState_CanSaveOnEpochBoundary(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch) @@ -73,7 +74,7 @@ func TestSaveHotState_NoSaveNotEpochBoundary(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch - 1) @@ -105,7 +106,7 @@ func TestLoadHoteStateByRoot_Cached(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) r := [32]byte{'A'} @@ -126,7 +127,7 @@ func TestLoadHoteStateByRoot_FromDBCanProcess(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} @@ -158,7 +159,7 @@ func TestLoadHoteStateByRoot_FromDBBoundaryCase(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) blk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} @@ -191,7 +192,7 @@ func TestLoadHoteStateBySlot_CanAdvanceSlotUsingDB(t *testing.T) { ctx := context.Background() db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) beaconState, _ := testutil.DeterministicGenesisState(t, 32) b := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} if err := service.beaconDB.SaveBlock(ctx, b); err != nil { diff --git a/beacon-chain/state/stategen/migrate_test.go b/beacon-chain/state/stategen/migrate_test.go index 133e23d0c9a6..9e0c806eb097 100644 --- a/beacon-chain/state/stategen/migrate_test.go +++ b/beacon-chain/state/stategen/migrate_test.go @@ -6,6 +6,7 @@ import ( ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" "github.com/prysmaticlabs/prysm/shared/params" @@ -19,11 +20,9 @@ func TestMigrateToCold_NoBlock(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) - beaconState, _ := testutil.DeterministicGenesisState(t, 32) - beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch) - if err := service.MigrateToCold(ctx, beaconState, [32]byte{}); err != nil { + if err := service.MigrateToCold(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{}); err != nil { t.Fatal(err) } @@ -36,12 +35,9 @@ func TestMigrateToCold_HigherSplitSlot(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.splitInfo.slot = 2 - - beaconState, _ := testutil.DeterministicGenesisState(t, 32) - beaconState.SetSlot(1) - if err := service.MigrateToCold(ctx, beaconState, [32]byte{}); err != nil { + if err := service.MigrateToCold(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{}); err != nil { t.Fatal(err) } @@ -54,11 +50,8 @@ func TestMigrateToCold_NotEpochStart(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) - - beaconState, _ := testutil.DeterministicGenesisState(t, 32) - beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch + 1) - if err := service.MigrateToCold(ctx, beaconState, [32]byte{}); err != nil { + service := New(db, cache.NewStateSummaryCache()) + if err := service.MigrateToCold(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{}); err != nil { t.Fatal(err) } @@ -71,7 +64,7 @@ func TestMigrateToCold_MigrationCompletes(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.slotsPerArchivedPoint = 2 beaconState, _ := testutil.DeterministicGenesisState(t, 32) @@ -106,7 +99,7 @@ func TestMigrateToCold_MigrationCompletes(t *testing.T) { t.Fatal(err) } - if err := service.MigrateToCold(ctx, beaconState, [32]byte{}); err != nil { + if err := service.MigrateToCold(ctx, newBeaconState.Slot(), [32]byte{}); err != nil { t.Fatal(err) } diff --git a/beacon-chain/state/stategen/replay_test.go b/beacon-chain/state/stategen/replay_test.go index aa3a80ffe243..bad2497386c3 100644 --- a/beacon-chain/state/stategen/replay_test.go +++ b/beacon-chain/state/stategen/replay_test.go @@ -8,6 +8,7 @@ import ( "github.com/gogo/protobuf/proto" ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks" "github.com/prysmaticlabs/prysm/beacon-chain/db" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" @@ -23,7 +24,7 @@ func TestComputeStateUpToSlot_GenesisState(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} gRoot, err := ssz.HashTreeRoot(gBlk.Block) @@ -56,7 +57,7 @@ func TestComputeStateUpToSlot_CanProcessUpTo(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) gBlk := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} gRoot, err := ssz.HashTreeRoot(gBlk.Block) @@ -109,7 +110,7 @@ func TestReplayBlocks_AllSkipSlots(t *testing.T) { beaconState.SetCurrentJustifiedCheckpoint(cp) beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) targetSlot := params.BeaconConfig().SlotsPerEpoch - 1 newState, err := service.ReplayBlocks(context.Background(), beaconState, []*ethpb.SignedBeaconBlock{}, targetSlot) if err != nil { @@ -145,7 +146,7 @@ func TestReplayBlocks_SameSlot(t *testing.T) { beaconState.SetCurrentJustifiedCheckpoint(cp) beaconState.SetCurrentEpochAttestations([]*pb.PendingAttestation{}) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) targetSlot := beaconState.Slot() newState, err := service.ReplayBlocks(context.Background(), beaconState, []*ethpb.SignedBeaconBlock{}, targetSlot) if err != nil { diff --git a/beacon-chain/state/stategen/service_test.go b/beacon-chain/state/stategen/service_test.go index 6d54fdaeac6a..796393f2db96 100644 --- a/beacon-chain/state/stategen/service_test.go +++ b/beacon-chain/state/stategen/service_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/gogo/protobuf/proto" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -15,7 +16,7 @@ func TestResume(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) root := [32]byte{'A'} beaconState, _ := testutil.DeterministicGenesisState(t, 32) beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch) diff --git a/beacon-chain/state/stategen/setter_test.go b/beacon-chain/state/stategen/setter_test.go index 7823564c3ff1..e78921f37696 100644 --- a/beacon-chain/state/stategen/setter_test.go +++ b/beacon-chain/state/stategen/setter_test.go @@ -5,6 +5,7 @@ import ( "testing" //"github.com/gogo/protobuf/proto" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/testutil" @@ -17,7 +18,7 @@ func TestSaveState_ColdStateCanBeSaved(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.slotsPerArchivedPoint = 1 beaconState, _ := testutil.DeterministicGenesisState(t, 32) @@ -48,7 +49,7 @@ func TestSaveState_HotStateCanBeSaved(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.slotsPerArchivedPoint = 1 beaconState, _ := testutil.DeterministicGenesisState(t, 32) // This goes to hot section, verify it can save on epoch boundary. @@ -75,7 +76,7 @@ func TestSaveState_HotStateCached(t *testing.T) { db := testDB.SetupDB(t) defer testDB.TeardownDB(t, db) - service := New(db) + service := New(db, cache.NewStateSummaryCache()) service.slotsPerArchivedPoint = 1 beaconState, _ := testutil.DeterministicGenesisState(t, 32) beaconState.SetSlot(params.BeaconConfig().SlotsPerEpoch) diff --git a/beacon-chain/sync/pending_attestations_queue_test.go b/beacon-chain/sync/pending_attestations_queue_test.go index baba3d755983..264e344c3069 100644 --- a/beacon-chain/sync/pending_attestations_queue_test.go +++ b/beacon-chain/sync/pending_attestations_queue_test.go @@ -11,6 +11,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" @@ -46,6 +47,7 @@ func TestProcessPendingAtts_NoBlockRequestBlock(t *testing.T) { db: db, chain: &mock.ChainService{Genesis: roughtime.Now()}, blkRootToPendingAtts: make(map[[32]byte][]*ethpb.AggregateAttestationAndProof), + stateSummaryCache: cache.NewStateSummaryCache(), } a := ðpb.AggregateAttestationAndProof{Aggregate: ðpb.Attestation{Data: ðpb.AttestationData{Target: ðpb.Checkpoint{}}}} @@ -69,6 +71,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { chain: &mock.ChainService{Genesis: roughtime.Now()}, blkRootToPendingAtts: make(map[[32]byte][]*ethpb.AggregateAttestationAndProof), attPool: attestations.NewPool(), + stateSummaryCache: cache.NewStateSummaryCache(), } a := ðpb.AggregateAttestationAndProof{ @@ -175,6 +178,7 @@ func TestProcessPendingAtts_HasBlockSaveAggregatedAtt(t *testing.T) { }}, blkRootToPendingAtts: make(map[[32]byte][]*ethpb.AggregateAttestationAndProof), attPool: attestations.NewPool(), + stateSummaryCache: cache.NewStateSummaryCache(), } sb = ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{}} diff --git a/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go b/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go index b856ae8586b8..579c55342f40 100644 --- a/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go +++ b/beacon-chain/sync/subscriber_committee_index_beacon_attestation_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/feed" statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state" dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" @@ -58,6 +59,7 @@ func TestService_committeeIndexBeaconAttestationSubscriber_ValidMessage(t *testi stateNotifier: (&mock.ChainService{}).StateNotifier(), attestationNotifier: (&mock.ChainService{}).OperationNotifier(), initialSync: &mockSync.Sync{IsSyncing: false}, + stateSummaryCache: cache.NewStateSummaryCache(), } r.registerSubscribers() r.stateNotifier.StateFeed().Send(&feed.Event{ diff --git a/beacon-chain/sync/validate_aggregate_proof_test.go b/beacon-chain/sync/validate_aggregate_proof_test.go index e379691a5d8b..1cd16b4d56c7 100644 --- a/beacon-chain/sync/validate_aggregate_proof_test.go +++ b/beacon-chain/sync/validate_aggregate_proof_test.go @@ -14,6 +14,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" mock "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" "github.com/prysmaticlabs/prysm/beacon-chain/operations/attestations" @@ -139,6 +140,7 @@ func TestValidateAggregateAndProof_NoBlock(t *testing.T) { initialSync: &mockSync.Sync{IsSyncing: false}, attPool: attestations.NewPool(), blkRootToPendingAtts: make(map[[32]byte][]*ethpb.AggregateAttestationAndProof), + stateSummaryCache: cache.NewStateSummaryCache(), } buf := new(bytes.Buffer) @@ -199,7 +201,8 @@ func TestValidateAggregateAndProof_NotWithinSlotRange(t *testing.T) { initialSync: &mockSync.Sync{IsSyncing: false}, chain: &mock.ChainService{Genesis: time.Now(), State: beaconState}, - attPool: attestations.NewPool(), + attPool: attestations.NewPool(), + stateSummaryCache: cache.NewStateSummaryCache(), } buf := new(bytes.Buffer) @@ -377,7 +380,8 @@ func TestValidateAggregateAndProof_CanValidate(t *testing.T) { FinalizedCheckPoint: ðpb.Checkpoint{ Epoch: 0, }}, - attPool: attestations.NewPool(), + attPool: attestations.NewPool(), + stateSummaryCache: cache.NewStateSummaryCache(), } buf := new(bytes.Buffer) diff --git a/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go b/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go index 67f8cc8340bc..89f6f179fdf8 100644 --- a/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go +++ b/beacon-chain/sync/validate_committee_index_beacon_attestation_test.go @@ -12,6 +12,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz" mockChain "github.com/prysmaticlabs/prysm/beacon-chain/blockchain/testing" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" dbtest "github.com/prysmaticlabs/prysm/beacon-chain/db/testing" p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing" beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state" @@ -36,6 +37,7 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { db: db, chain: chain, blkRootToPendingAtts: make(map[[32]byte][]*ethpb.AggregateAttestationAndProof), + stateSummaryCache: cache.NewStateSummaryCache(), } blk := ðpb.SignedBeaconBlock{ From 6707fb0f9e0edc632186dd523fca47963f6d35eb Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 11:12:38 -0700 Subject: [PATCH 29/39] Fixed existing tests --- beacon-chain/state/stategen/getter_test.go | 6 +++--- beacon-chain/state/stategen/hot_test.go | 9 +++++---- beacon-chain/state/stategen/migrate_test.go | 6 +++--- beacon-chain/state/stategen/setter_test.go | 2 +- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index e1129d2cef45..109c4d67ad99 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -64,15 +64,15 @@ func TestStateByRoot_HotStateDB(t *testing.T) { t.Fatal(err) } targetSlot := uint64(10) - + targetRoot := [32]byte{'a'} if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{ Slot: targetSlot, - Root: blkRoot[:], + Root: targetRoot[:], }); err != nil { t.Fatal(err) } - loadedState, err := service.StateByRoot(ctx, blkRoot) + loadedState, err := service.StateByRoot(ctx, targetRoot) if err != nil { t.Fatal(err) } diff --git a/beacon-chain/state/stategen/hot_test.go b/beacon-chain/state/stategen/hot_test.go index 2fc9ffe98211..3714c9e23d1a 100644 --- a/beacon-chain/state/stategen/hot_test.go +++ b/beacon-chain/state/stategen/hot_test.go @@ -63,7 +63,7 @@ func TestSaveHotState_CanSaveOnEpochBoundary(t *testing.T) { if !service.beaconDB.HasState(ctx, r) { t.Error("Should have saved the state") } - if !service.beaconDB.HasStateSummary(ctx, r) { + if !service.stateSummaryCache.Has(r) { t.Error("Should have saved the state summary") } testutil.AssertLogsContain(t, hook, "Saved full state on epoch boundary") @@ -96,7 +96,7 @@ func TestSaveHotState_NoSaveNotEpochBoundary(t *testing.T) { if service.beaconDB.HasState(ctx, r) { t.Error("Should not have saved the state") } - if !service.beaconDB.HasStateSummary(ctx, r) { + if !service.stateSummaryCache.Has(r) { t.Error("Should have saved the state summary") } testutil.AssertLogsDoNotContain(t, hook, "Saved full state on epoch boundary") @@ -137,15 +137,16 @@ func TestLoadHoteStateByRoot_FromDBCanProcess(t *testing.T) { t.Fatal(err) } targetSlot := uint64(10) + targetRoot := [32]byte{'a'} if err := service.beaconDB.SaveStateSummary(ctx, &pb.StateSummary{ Slot: targetSlot, - Root: blkRoot[:], + Root: targetRoot[:], }); err != nil { t.Fatal(err) } // This tests where hot state was not cached and needs processing. - loadedState, err := service.loadHotStateByRoot(ctx, blkRoot) + loadedState, err := service.loadHotStateByRoot(ctx, targetRoot) if err != nil { t.Fatal(err) } diff --git a/beacon-chain/state/stategen/migrate_test.go b/beacon-chain/state/stategen/migrate_test.go index 9e0c806eb097..a4638a6e811f 100644 --- a/beacon-chain/state/stategen/migrate_test.go +++ b/beacon-chain/state/stategen/migrate_test.go @@ -37,7 +37,7 @@ func TestMigrateToCold_HigherSplitSlot(t *testing.T) { service := New(db, cache.NewStateSummaryCache()) service.splitInfo.slot = 2 - if err := service.MigrateToCold(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{}); err != nil { + if err := service.MigrateToCold(ctx, 1, [32]byte{}); err != nil { t.Fatal(err) } @@ -51,7 +51,7 @@ func TestMigrateToCold_NotEpochStart(t *testing.T) { defer testDB.TeardownDB(t, db) service := New(db, cache.NewStateSummaryCache()) - if err := service.MigrateToCold(ctx, params.BeaconConfig().SlotsPerEpoch, [32]byte{}); err != nil { + if err := service.MigrateToCold(ctx, params.BeaconConfig().SlotsPerEpoch+1, [32]byte{}); err != nil { t.Fatal(err) } @@ -99,7 +99,7 @@ func TestMigrateToCold_MigrationCompletes(t *testing.T) { t.Fatal(err) } - if err := service.MigrateToCold(ctx, newBeaconState.Slot(), [32]byte{}); err != nil { + if err := service.MigrateToCold(ctx, beaconState.Slot(), [32]byte{}); err != nil { t.Fatal(err) } diff --git a/beacon-chain/state/stategen/setter_test.go b/beacon-chain/state/stategen/setter_test.go index e78921f37696..f956dfe9501a 100644 --- a/beacon-chain/state/stategen/setter_test.go +++ b/beacon-chain/state/stategen/setter_test.go @@ -64,7 +64,7 @@ func TestSaveState_HotStateCanBeSaved(t *testing.T) { if !service.beaconDB.HasState(ctx, r) { t.Error("Should have saved the state") } - if !service.beaconDB.HasStateSummary(ctx, r) { + if !service.stateSummaryCache.Has(r) { t.Error("Should have saved the state summary") } testutil.AssertLogsContain(t, hook, "Saved full state on epoch boundary") From c46ec8114f82975b713807707eeb0b7105180f7c Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 11:31:11 -0700 Subject: [PATCH 30/39] Test for state summary getter --- beacon-chain/blockchain/process_block.go | 9 +++-- beacon-chain/state/stategen/getter_test.go | 42 ++++++++++++++++++++++ beacon-chain/state/stategen/hot.go | 1 - 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/beacon-chain/blockchain/process_block.go b/beacon-chain/blockchain/process_block.go index d13e5199f0a0..81e766e78620 100644 --- a/beacon-chain/blockchain/process_block.go +++ b/beacon-chain/blockchain/process_block.go @@ -143,10 +143,10 @@ func (s *Service) onBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root) fBlock, err := s.beaconDB.Block(ctx, fRoot) if err != nil { - return nil, err + return nil, errors.Wrap(err, "could not get finalized block to migrate") } if err := s.stateGen.MigrateToCold(ctx, fBlock.Block.Slot, fRoot); err != nil { - return nil, err + return nil, errors.Wrap(err, "could not migrate to cold") } } } @@ -303,11 +303,10 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed fRoot := bytesutil.ToBytes32(postState.FinalizedCheckpoint().Root) fBlock, err := s.beaconDB.Block(ctx, fRoot) if err != nil { - return err + return errors.Wrap(err, "could not get finalized block to migrate") } if err := s.stateGen.MigrateToCold(ctx, fBlock.Block.Slot, fRoot); err != nil { - return errors.Wrap(err, "could not migrate with new finalized root") - + return errors.Wrap(err, "could not migrate to cold") } } } diff --git a/beacon-chain/state/stategen/getter_test.go b/beacon-chain/state/stategen/getter_test.go index 109c4d67ad99..43e30604abaf 100644 --- a/beacon-chain/state/stategen/getter_test.go +++ b/beacon-chain/state/stategen/getter_test.go @@ -178,3 +178,45 @@ func TestStateBySlot_HotStateDB(t *testing.T) { t.Error("Did not correctly load state") } } + +func TestStateSummary_CanGetFromCacheOrDB(t *testing.T) { + ctx := context.Background() + db := testDB.SetupDB(t) + defer testDB.TeardownDB(t, db) + + service := New(db, cache.NewStateSummaryCache()) + + r := [32]byte{'a'} + summary := &pb.StateSummary{Slot: 100} + _, err := service.stateSummary(ctx, r) + if err != errUnknownStateSummary { + t.Fatal("Did not get wanted error") + } + + service.stateSummaryCache.Put(r, summary) + got, err := service.stateSummary(ctx, r) + if err != nil { + t.Fatal(err) + } + if !proto.Equal(got, summary) { + t.Error("Did not get wanted summary") + } + + r = [32]byte{'b'} + summary = &pb.StateSummary{Root: r[:], Slot: 101} + _, err = service.stateSummary(ctx, r) + if err != errUnknownStateSummary { + t.Fatal("Did not get wanted error") + } + + if err := service.beaconDB.SaveStateSummary(ctx, summary); err != nil { + t.Fatal(err) + } + got, err = service.stateSummary(ctx, r) + if err != nil { + t.Fatal("Did not get wanted error") + } + if !proto.Equal(got, summary) { + t.Error("Did not get wanted summary") + } +} diff --git a/beacon-chain/state/stategen/hot.go b/beacon-chain/state/stategen/hot.go index 38e736e07481..4760769d60d1 100644 --- a/beacon-chain/state/stategen/hot.go +++ b/beacon-chain/state/stategen/hot.go @@ -77,7 +77,6 @@ func (s *State) loadHotStateByRoot(ctx context.Context, blockRoot [32]byte) (*st if startState == nil { return nil, errUnknownBoundaryState } - log.Warnf("Replaying for block at slot %d, start slot %d", summary.Slot, startState.Slot()) // Don't need to replay the blocks if start state is the same state for the block root. var hotState *state.BeaconState From 623166759e38c259ce6efb63a2c75650cabec193 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 11:32:00 -0700 Subject: [PATCH 31/39] Gaz --- beacon-chain/db/kv/BUILD.bazel | 1 + beacon-chain/state/stategen/BUILD.bazel | 1 + beacon-chain/sync/BUILD.bazel | 1 + 3 files changed, 3 insertions(+) diff --git a/beacon-chain/db/kv/BUILD.bazel b/beacon-chain/db/kv/BUILD.bazel index df8d980f74ff..3028065ad71b 100644 --- a/beacon-chain/db/kv/BUILD.bazel +++ b/beacon-chain/db/kv/BUILD.bazel @@ -75,6 +75,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/db/filters:go_default_library", "//beacon-chain/state:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/beacon-chain/state/stategen/BUILD.bazel b/beacon-chain/state/stategen/BUILD.bazel index 1aa72fcfe66b..8cfd418311fa 100644 --- a/beacon-chain/state/stategen/BUILD.bazel +++ b/beacon-chain/state/stategen/BUILD.bazel @@ -47,6 +47,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/blocks:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/testing:go_default_library", diff --git a/beacon-chain/sync/BUILD.bazel b/beacon-chain/sync/BUILD.bazel index 58b13c2b6f0b..bc54e2363fa2 100644 --- a/beacon-chain/sync/BUILD.bazel +++ b/beacon-chain/sync/BUILD.bazel @@ -108,6 +108,7 @@ go_test( shard_count = 4, deps = [ "//beacon-chain/blockchain/testing:go_default_library", + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/feed:go_default_library", "//beacon-chain/core/feed/state:go_default_library", "//beacon-chain/core/helpers:go_default_library", From 6a5bb43c40bbc3391ae49203c53e11b46cfc850a Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 11:40:53 -0700 Subject: [PATCH 32/39] Fix kafka passthrough --- beacon-chain/db/kafka/passthrough.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/beacon-chain/db/kafka/passthrough.go b/beacon-chain/db/kafka/passthrough.go index 9796e6bc91f9..431e9ac8f436 100644 --- a/beacon-chain/db/kafka/passthrough.go +++ b/beacon-chain/db/kafka/passthrough.go @@ -238,6 +238,11 @@ func (e Exporter) SaveStateSummary(ctx context.Context, summary *pb.StateSummary return e.db.SaveStateSummary(ctx, summary) } +// SaveStateSummaries -- passthrough. +func (e Exporter) SaveStateSummaries(ctx context.Context, summaries []*pb.StateSummary) error { + return e.db.SaveStateSummaries(ctx, summaries) +} + // SaveStates -- passthrough. func (e Exporter) SaveStates(ctx context.Context, states []*state.BeaconState, blockRoots [][32]byte) error { return e.db.SaveStates(ctx, states, blockRoots) From cf10be8129766b89d589f84ee155ae4798957666 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 11:59:44 -0700 Subject: [PATCH 33/39] Fixed inputs --- tools/blocktree/main.go | 3 ++- tools/extractor/main.go | 3 ++- tools/interop/export-genesis/main.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/blocktree/main.go b/tools/blocktree/main.go index 11c00f351cea..c55221e49ef4 100644 --- a/tools/blocktree/main.go +++ b/tools/blocktree/main.go @@ -17,6 +17,7 @@ import ( "github.com/emicklei/dot" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -43,7 +44,7 @@ func main() { params.UseDemoBeaconConfig() flag.Parse() - db, err := db.NewDB(*datadir) + db, err := db.NewDB(*datadir, cache.NewStateSummaryCache()) if err != nil { panic(err) } diff --git a/tools/extractor/main.go b/tools/extractor/main.go index df7c8144004a..6d9f05a9f8ce 100644 --- a/tools/extractor/main.go +++ b/tools/extractor/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -27,7 +28,7 @@ func init() { func main() { flag.Parse() fmt.Println("Starting process...") - d, err := db.NewDB(*datadir) + d, err := db.NewDB(*datadir, cache.NewStateSummaryCache()) if err != nil { panic(err) } diff --git a/tools/interop/export-genesis/main.go b/tools/interop/export-genesis/main.go index fc2e3bdd629c..d8fd9b581fe2 100644 --- a/tools/interop/export-genesis/main.go +++ b/tools/interop/export-genesis/main.go @@ -7,6 +7,7 @@ import ( "os" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/db" ) @@ -21,7 +22,7 @@ func main() { fmt.Printf("Reading db at %s and writing ssz output to %s.\n", os.Args[1], os.Args[2]) - d, err := db.NewDB(os.Args[1]) + d, err := db.NewDB(os.Args[1], cache.NewStateSummaryCache()) if err != nil { panic(err) } From 26b72a9c93da0899d3ef8a2e57dc93fef2f0c259 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 12:13:04 -0700 Subject: [PATCH 34/39] Gaz --- tools/blocktree/BUILD.bazel | 1 + tools/extractor/BUILD.bazel | 1 + tools/interop/export-genesis/BUILD.bazel | 1 + 3 files changed, 3 insertions(+) diff --git a/tools/blocktree/BUILD.bazel b/tools/blocktree/BUILD.bazel index e67e8e7c1d02..3d385bd46719 100644 --- a/tools/blocktree/BUILD.bazel +++ b/tools/blocktree/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/blocktree", visibility = ["//visibility:private"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/tools/extractor/BUILD.bazel b/tools/extractor/BUILD.bazel index 0a838619bbb9..ecefdbfa21fb 100644 --- a/tools/extractor/BUILD.bazel +++ b/tools/extractor/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/extractor", visibility = ["//visibility:public"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/state/interop:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/tools/interop/export-genesis/BUILD.bazel b/tools/interop/export-genesis/BUILD.bazel index 50c55fd93db6..1c02a2086204 100644 --- a/tools/interop/export-genesis/BUILD.bazel +++ b/tools/interop/export-genesis/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/interop/export-genesis", visibility = ["//visibility:private"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/db:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], From 736f71e6b6055e5114278e16a45de1b85a51b3f5 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 12:24:19 -0700 Subject: [PATCH 35/39] Fixed build --- tools/blocktree/BUILD.bazel | 1 - tools/blocktree/main.go | 3 +-- tools/extractor/BUILD.bazel | 1 - tools/extractor/main.go | 3 +-- tools/interop/export-genesis/BUILD.bazel | 1 - tools/interop/export-genesis/main.go | 3 +-- 6 files changed, 3 insertions(+), 9 deletions(-) diff --git a/tools/blocktree/BUILD.bazel b/tools/blocktree/BUILD.bazel index 3d385bd46719..e67e8e7c1d02 100644 --- a/tools/blocktree/BUILD.bazel +++ b/tools/blocktree/BUILD.bazel @@ -6,7 +6,6 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/blocktree", visibility = ["//visibility:private"], deps = [ - "//beacon-chain/cache:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/tools/blocktree/main.go b/tools/blocktree/main.go index c55221e49ef4..a2cb744f4554 100644 --- a/tools/blocktree/main.go +++ b/tools/blocktree/main.go @@ -17,7 +17,6 @@ import ( "github.com/emicklei/dot" "github.com/prysmaticlabs/go-ssz" - "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -44,7 +43,7 @@ func main() { params.UseDemoBeaconConfig() flag.Parse() - db, err := db.NewDB(*datadir, cache.NewStateSummaryCache()) + db, err := db.NewDB(*datadir, nil) if err != nil { panic(err) } diff --git a/tools/extractor/BUILD.bazel b/tools/extractor/BUILD.bazel index ecefdbfa21fb..0a838619bbb9 100644 --- a/tools/extractor/BUILD.bazel +++ b/tools/extractor/BUILD.bazel @@ -6,7 +6,6 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/extractor", visibility = ["//visibility:public"], deps = [ - "//beacon-chain/cache:go_default_library", "//beacon-chain/core/state/interop:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/tools/extractor/main.go b/tools/extractor/main.go index 6d9f05a9f8ce..9f07a160d081 100644 --- a/tools/extractor/main.go +++ b/tools/extractor/main.go @@ -5,7 +5,6 @@ import ( "flag" "fmt" - "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -28,7 +27,7 @@ func init() { func main() { flag.Parse() fmt.Println("Starting process...") - d, err := db.NewDB(*datadir, cache.NewStateSummaryCache()) + d, err := db.NewDB(*datadir, nil) if err != nil { panic(err) } diff --git a/tools/interop/export-genesis/BUILD.bazel b/tools/interop/export-genesis/BUILD.bazel index 1c02a2086204..50c55fd93db6 100644 --- a/tools/interop/export-genesis/BUILD.bazel +++ b/tools/interop/export-genesis/BUILD.bazel @@ -6,7 +6,6 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/interop/export-genesis", visibility = ["//visibility:private"], deps = [ - "//beacon-chain/cache:go_default_library", "//beacon-chain/db:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], diff --git a/tools/interop/export-genesis/main.go b/tools/interop/export-genesis/main.go index d8fd9b581fe2..6601e9535f1b 100644 --- a/tools/interop/export-genesis/main.go +++ b/tools/interop/export-genesis/main.go @@ -7,7 +7,6 @@ import ( "os" "github.com/prysmaticlabs/go-ssz" - "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/db" ) @@ -22,7 +21,7 @@ func main() { fmt.Printf("Reading db at %s and writing ssz output to %s.\n", os.Args[1], os.Args[2]) - d, err := db.NewDB(os.Args[1], cache.NewStateSummaryCache()) + d, err := db.NewDB(os.Args[1], nil) if err != nil { panic(err) } From a195f1c6ba2919890cb9693e49e37018c5e7a33e Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 12:43:42 -0700 Subject: [PATCH 36/39] Fixed visibility --- beacon-chain/cache/BUILD.bazel | 5 ++++- tools/blocktree/BUILD.bazel | 1 + tools/blocktree/main.go | 3 ++- tools/extractor/BUILD.bazel | 1 + tools/extractor/main.go | 3 ++- tools/interop/export-genesis/BUILD.bazel | 1 + tools/interop/export-genesis/main.go | 3 ++- 7 files changed, 13 insertions(+), 4 deletions(-) diff --git a/beacon-chain/cache/BUILD.bazel b/beacon-chain/cache/BUILD.bazel index 0e36471c035c..9b508498b301 100644 --- a/beacon-chain/cache/BUILD.bazel +++ b/beacon-chain/cache/BUILD.bazel @@ -14,7 +14,10 @@ go_library( "state_summary.go", ], importpath = "github.com/prysmaticlabs/prysm/beacon-chain/cache", - visibility = ["//beacon-chain:__subpackages__"], + visibility = [ + "//beacon-chain:__subpackages__", + "//tools:__subpackages__", + ], deps = [ "//beacon-chain/state:go_default_library", "//proto/beacon/p2p/v1:go_default_library", diff --git a/tools/blocktree/BUILD.bazel b/tools/blocktree/BUILD.bazel index e67e8e7c1d02..3d385bd46719 100644 --- a/tools/blocktree/BUILD.bazel +++ b/tools/blocktree/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/blocktree", visibility = ["//visibility:private"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/helpers:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/tools/blocktree/main.go b/tools/blocktree/main.go index a2cb744f4554..c55221e49ef4 100644 --- a/tools/blocktree/main.go +++ b/tools/blocktree/main.go @@ -17,6 +17,7 @@ import ( "github.com/emicklei/dot" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -43,7 +44,7 @@ func main() { params.UseDemoBeaconConfig() flag.Parse() - db, err := db.NewDB(*datadir, nil) + db, err := db.NewDB(*datadir, cache.NewStateSummaryCache()) if err != nil { panic(err) } diff --git a/tools/extractor/BUILD.bazel b/tools/extractor/BUILD.bazel index 0a838619bbb9..ecefdbfa21fb 100644 --- a/tools/extractor/BUILD.bazel +++ b/tools/extractor/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/extractor", visibility = ["//visibility:public"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/core/state/interop:go_default_library", "//beacon-chain/db:go_default_library", "//beacon-chain/db/filters:go_default_library", diff --git a/tools/extractor/main.go b/tools/extractor/main.go index 9f07a160d081..6d9f05a9f8ce 100644 --- a/tools/extractor/main.go +++ b/tools/extractor/main.go @@ -5,6 +5,7 @@ import ( "flag" "fmt" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/core/state/interop" "github.com/prysmaticlabs/prysm/beacon-chain/db" "github.com/prysmaticlabs/prysm/beacon-chain/db/filters" @@ -27,7 +28,7 @@ func init() { func main() { flag.Parse() fmt.Println("Starting process...") - d, err := db.NewDB(*datadir, nil) + d, err := db.NewDB(*datadir, cache.NewStateSummaryCache()) if err != nil { panic(err) } diff --git a/tools/interop/export-genesis/BUILD.bazel b/tools/interop/export-genesis/BUILD.bazel index 50c55fd93db6..1c02a2086204 100644 --- a/tools/interop/export-genesis/BUILD.bazel +++ b/tools/interop/export-genesis/BUILD.bazel @@ -6,6 +6,7 @@ go_library( importpath = "github.com/prysmaticlabs/prysm/tools/interop/export-genesis", visibility = ["//visibility:private"], deps = [ + "//beacon-chain/cache:go_default_library", "//beacon-chain/db:go_default_library", "@com_github_prysmaticlabs_go_ssz//:go_default_library", ], diff --git a/tools/interop/export-genesis/main.go b/tools/interop/export-genesis/main.go index 6601e9535f1b..d8fd9b581fe2 100644 --- a/tools/interop/export-genesis/main.go +++ b/tools/interop/export-genesis/main.go @@ -7,6 +7,7 @@ import ( "os" "github.com/prysmaticlabs/go-ssz" + "github.com/prysmaticlabs/prysm/beacon-chain/cache" "github.com/prysmaticlabs/prysm/beacon-chain/db" ) @@ -21,7 +22,7 @@ func main() { fmt.Printf("Reading db at %s and writing ssz output to %s.\n", os.Args[1], os.Args[2]) - d, err := db.NewDB(os.Args[1], nil) + d, err := db.NewDB(os.Args[1], cache.NewStateSummaryCache()) if err != nil { panic(err) } From d5a27446b9990f7a4d4d72458ea5bd429293e856 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 13:31:04 -0700 Subject: [PATCH 37/39] Trying without the ignore --- beacon-chain/db/BUILD.bazel | 1 - 1 file changed, 1 deletion(-) diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index dbebc3618e50..20a43f929100 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -6,7 +6,6 @@ config_setting( values = {"define": "kafka_enabled=false"}, ) -# gazelle:ignore db.go db_kafka_wrapped.go go_library( name = "go_default_library", srcs = [ From 2281a801ecde93ccba7c69471b31ee6e19609b26 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 13:34:54 -0700 Subject: [PATCH 38/39] Didn't work.. --- beacon-chain/db/BUILD.bazel | 1 + 1 file changed, 1 insertion(+) diff --git a/beacon-chain/db/BUILD.bazel b/beacon-chain/db/BUILD.bazel index 20a43f929100..dbebc3618e50 100644 --- a/beacon-chain/db/BUILD.bazel +++ b/beacon-chain/db/BUILD.bazel @@ -6,6 +6,7 @@ config_setting( values = {"define": "kafka_enabled=false"}, ) +# gazelle:ignore db.go db_kafka_wrapped.go go_library( name = "go_default_library", srcs = [ From 7e5ebccae7c37f83f86502802fa8747fa0649389 Mon Sep 17 00:00:00 2001 From: terence tsao Date: Mon, 30 Mar 2020 13:48:43 -0700 Subject: [PATCH 39/39] Fix kafka --- beacon-chain/db/db_kafka_wrapped.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beacon-chain/db/db_kafka_wrapped.go b/beacon-chain/db/db_kafka_wrapped.go index 2ceae8f0675d..d13faa91c140 100644 --- a/beacon-chain/db/db_kafka_wrapped.go +++ b/beacon-chain/db/db_kafka_wrapped.go @@ -7,8 +7,8 @@ import ( ) // NewDB initializes a new DB with kafka wrapper. -func NewDB(dirPath string) (Database, error) { - db, err := kv.NewKVStore(dirPath, cache.NewStateSummaryCache()) +func NewDB(dirPath string, stateSummaryCache *cache.StateSummaryCache) (Database, error) { + db, err := kv.NewKVStore(dirPath, stateSummaryCache) if err != nil { return nil, err }