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
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.Mutex
}

// 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.Lock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RLock? Seems like you could process many readers in parallel with attestations coming in pubsub quickly

filteredBlocks = s.filteredBlockTree
s.filteredBlockTreeLock.Unlock()
} 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," +
terencechain marked this conversation as resolved.
Show resolved Hide resolved
"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,
}...)