Cache filtered block tree (#4515)

* Cache filtered block tree
* Merge refs/heads/master into cache-filtered-tree
* Merge refs/heads/master into cache-filtered-tree
* Add locks
* Merge branch 'cache-filtered-tree' of git+ssh://github.com/prysmaticlabs/prysm into cache-filtered-tree
* Confligt
* Merge refs/heads/master into cache-filtered-tree
* Merge refs/heads/master into cache-filtered-tree
* Update shared/featureconfig/flags.go

Co-Authored-By: Preston Van Loon <preston@prysmaticlabs.com>
* Rlock
* Merge branch 'master' of git+ssh://github.com/prysmaticlabs/prysm into cache-filtered-tree
* Merge branch 'cache-filtered-tree' of git+ssh://github.com/prysmaticlabs/prysm into cache-filtered-tree
This commit is contained in:
terence tsao 2020-01-12 20:12:50 -08:00 committed by prylabs-bulldozer[bot]
parent 7edca61e44
commit a8edfa42cc
4 changed files with 38 additions and 7 deletions

View File

@ -93,6 +93,16 @@ func (s *Store) OnBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) er
return errors.Wrap(err, "could not save state") return errors.Wrap(err, "could not save state")
} }
if featureconfig.Get().EnableBlockTreeCache {
tree, err := s.getFilterBlockTree(ctx)
if err != nil {
return errors.Wrap(err, "could not calculate filtered block tree")
}
s.filteredBlockTreeLock.Lock()
s.filteredBlockTree = tree
s.filteredBlockTreeLock.Unlock()
}
// Update justified check point. // Update justified check point.
if postState.CurrentJustifiedCheckpoint.Epoch > s.justifiedCheckpt.Epoch { if postState.CurrentJustifiedCheckpoint.Epoch > s.justifiedCheckpt.Epoch {
if err := s.updateJustified(ctx, postState); err != nil { if err := s.updateJustified(ctx, postState); err != nil {

View File

@ -52,6 +52,8 @@ type Store struct {
initSyncState map[[32]byte]*pb.BeaconState initSyncState map[[32]byte]*pb.BeaconState
initSyncStateLock sync.RWMutex initSyncStateLock sync.RWMutex
nextEpochBoundarySlot uint64 nextEpochBoundarySlot uint64
filteredBlockTree map[[32]byte]*ethpb.BeaconBlock
filteredBlockTreeLock sync.RWMutex
} }
// NewForkChoiceService instantiates a new service instance that will // NewForkChoiceService instantiates a new service instance that will
@ -262,9 +264,17 @@ func (s *Store) Head(ctx context.Context) ([]byte, error) {
defer span.End() defer span.End()
head := s.JustifiedCheckpt().Root head := s.JustifiedCheckpt().Root
filteredBlocks, err := s.getFilterBlockTree(ctx) filteredBlocks := make(map[[32]byte]*ethpb.BeaconBlock)
if err != nil { var err error
return nil, err if featureconfig.Get().EnableBlockTreeCache {
s.filteredBlockTreeLock.RLock()
filteredBlocks = s.filteredBlockTree
s.filteredBlockTreeLock.RUnlock()
} else {
filteredBlocks, err = s.getFilterBlockTree(ctx)
if err != nil {
return nil, err
}
} }
justifiedSlot := helpers.StartSlot(s.justifiedCheckpt.Epoch) justifiedSlot := helpers.StartSlot(s.justifiedCheckpt.Epoch)

View File

@ -38,10 +38,11 @@ type Flags struct {
EnableSavingOfDepositData bool // EnableSavingOfDepositData allows the saving of eth1 related data such as deposits,chain data to be saved. EnableSavingOfDepositData bool // EnableSavingOfDepositData allows the saving of eth1 related data such as deposits,chain data to be saved.
// Cache toggles. // Cache toggles.
EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106. EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableEth1DataVoteCache bool // EnableEth1DataVoteCache; see https://github.com/prysmaticlabs/prysm/issues/3106. EnableEth1DataVoteCache bool // EnableEth1DataVoteCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableSkipSlotsCache bool // EnableSkipSlotsCache caches the state in skipped slots. EnableSkipSlotsCache bool // EnableSkipSlotsCache caches the state in skipped slots.
EnableSlasherConnection bool // EnableSlasher enable retrieval of slashing events from a slasher instance. EnableSlasherConnection bool // EnableSlasher enable retrieval of slashing events from a slasher instance.
EnableBlockTreeCache bool // EnableBlockTreeCache enable fork choice service to maintain latest filtered block tree.
} }
var featureConfig *Flags var featureConfig *Flags
@ -118,6 +119,10 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Enable slasher connection.") log.Warn("Enable slasher connection.")
cfg.EnableSlasherConnection = true cfg.EnableSlasherConnection = true
} }
if ctx.GlobalBool(cacheFilteredBlockTree.Name) {
log.Warn("Enabled filtered block tree cache for fork choice.")
cfg.EnableBlockTreeCache = true
}
Init(cfg) Init(cfg)
} }

View File

@ -69,6 +69,11 @@ var (
"triggered the genesis as the genesis time. This flag should be used for local " + "triggered the genesis as the genesis time. This flag should be used for local " +
"development and testing only.", "development and testing only.",
} }
cacheFilteredBlockTree = cli.BoolFlag{
Name: "cache-filtered-block-tree",
Usage: "Cache filtered block tree by maintaining it rather than continually recalculating on the fly, " +
"this is used for fork choice.",
}
) )
// Deprecated flags list. // Deprecated flags list.
@ -192,4 +197,5 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableSkipSlotsCache, enableSkipSlotsCache,
saveDepositData, saveDepositData,
enableSlasherFlag, enableSlasherFlag,
cacheFilteredBlockTree,
}...) }...)