Skip to content

Commit

Permalink
Cache filtered block tree (#4515)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
terencechain authored and prylabs-bulldozer[bot] committed Jan 13, 2020
1 parent 7edca61 commit a8edfa4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
10 changes: 10 additions & 0 deletions beacon-chain/blockchain/forkchoice/process_block.go
Expand Up @@ -93,6 +93,16 @@ func (s *Store) OnBlock(ctx context.Context, signed *ethpb.SignedBeaconBlock) er
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.
if postState.CurrentJustifiedCheckpoint.Epoch > s.justifiedCheckpt.Epoch {
if err := s.updateJustified(ctx, postState); err != nil {
Expand Down
16 changes: 13 additions & 3 deletions beacon-chain/blockchain/forkchoice/service.go
Expand Up @@ -52,6 +52,8 @@ type Store struct {
initSyncState map[[32]byte]*pb.BeaconState
initSyncStateLock sync.RWMutex
nextEpochBoundarySlot uint64
filteredBlockTree map[[32]byte]*ethpb.BeaconBlock
filteredBlockTreeLock sync.RWMutex
}

// NewForkChoiceService instantiates a new service instance that will
Expand Down Expand Up @@ -262,9 +264,17 @@ func (s *Store) Head(ctx context.Context) ([]byte, error) {
defer span.End()

head := s.JustifiedCheckpt().Root
filteredBlocks, err := s.getFilterBlockTree(ctx)
if err != nil {
return nil, err
filteredBlocks := make(map[[32]byte]*ethpb.BeaconBlock)
var err error
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)
Expand Down
13 changes: 9 additions & 4 deletions shared/featureconfig/config.go
Expand Up @@ -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.

// Cache toggles.
EnableAttestationCache bool // EnableAttestationCache; 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.
EnableSlasherConnection bool // EnableSlasher enable retrieval of slashing events from a slasher instance.
EnableAttestationCache bool // EnableAttestationCache; 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.
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
Expand Down Expand Up @@ -118,6 +119,10 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Enable slasher connection.")
cfg.EnableSlasherConnection = true
}
if ctx.GlobalBool(cacheFilteredBlockTree.Name) {
log.Warn("Enabled filtered block tree cache for fork choice.")
cfg.EnableBlockTreeCache = true
}
Init(cfg)
}

Expand Down
6 changes: 6 additions & 0 deletions shared/featureconfig/flags.go
Expand Up @@ -69,6 +69,11 @@ var (
"triggered the genesis as the genesis time. This flag should be used for local " +
"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.
Expand Down Expand Up @@ -192,4 +197,5 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableSkipSlotsCache,
saveDepositData,
enableSlasherFlag,
cacheFilteredBlockTree,
}...)

0 comments on commit a8edfa4

Please sign in to comment.