Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache filtered block tree #4515

Merged
merged 12 commits into from Jan 13, 2020
8 changes: 8 additions & 0 deletions beacon-chain/blockchain/forkchoice/process_block.go
Expand Up @@ -93,6 +93,14 @@ 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.filteredBlockTree = tree
}

// Update justified check point.
if postState.CurrentJustifiedCheckpoint.Epoch > s.justifiedCheckpt.Epoch {
if err := s.updateJustified(ctx, postState); err != nil {
Expand Down
13 changes: 10 additions & 3 deletions beacon-chain/blockchain/forkchoice/service.go
Expand Up @@ -52,6 +52,7 @@ type Store struct {
initSyncState map[[32]byte]*pb.BeaconState
initSyncStateLock sync.RWMutex
nextEpochBoundarySlot uint64
filteredBlockTree map[[32]byte]*ethpb.BeaconBlock
}

// NewForkChoiceService instantiates a new service instance that will
Expand Down Expand Up @@ -262,9 +263,15 @@ 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 {
filteredBlocks = s.filteredBlockTree
} else {
filteredBlocks, err = s.getFilterBlockTree(ctx)
if err != nil {
return nil, err
}
}

justifiedSlot := helpers.StartSlot(s.justifiedCheckpt.Epoch)
Expand Down
5 changes: 5 additions & 0 deletions shared/featureconfig/config.go
Expand Up @@ -44,6 +44,7 @@ type Flags struct {
EnableShuffledIndexCache bool // EnableShuffledIndexCache to cache expensive shuffled index computation.
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 @@ -128,6 +129,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 @@ -78,6 +78,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," +
terencechain marked this conversation as resolved.
Show resolved Hide resolved
"this is used for fork choice.",
}
)

// Deprecated flags list.
Expand Down Expand Up @@ -190,4 +195,5 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableSkipSlotsCache,
saveDepositData,
enableSlasherFlag,
cacheFilteredBlockTree,
}...)