Enable Committee Cache By Default (#2323)

This commit is contained in:
Raul Jordan
2019-04-22 08:37:42 -05:00
committed by terence tsao
parent 68a9a42611
commit 51d4965388
14 changed files with 43 additions and 140 deletions

View File

@@ -11,14 +11,11 @@ go_library(
deps = [
"//beacon-chain/cache:go_default_library",
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/db:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/event:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/messagehandler:go_default_library",
"//shared/params:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
@@ -37,7 +34,6 @@ go_test(
"//beacon-chain/internal:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",

View File

@@ -6,9 +6,6 @@ import (
"fmt"
"sync"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/gogo/protobuf/proto"
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
@@ -17,7 +14,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/bitutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/event"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
handler "github.com/prysmaticlabs/prysm/shared/messagehandler"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
@@ -173,15 +169,7 @@ func (a *Service) UpdateLatestAttestation(ctx context.Context, attestation *pb.A
// Potential improvement, instead of getting the state,
// we could get a mapping of validator index to public key.
beaconState, err := a.beaconDB.HeadState(ctx)
if err != nil {
return err
}
head, err := a.beaconDB.ChainHead()
if err != nil {
return err
}
headRoot, err := hashutil.HashBeaconBlock(head)
state, err := a.beaconDB.HeadState(ctx)
if err != nil {
return err
}
@@ -190,36 +178,19 @@ func (a *Service) UpdateLatestAttestation(ctx context.Context, attestation *pb.A
var cachedCommittees *cache.CommitteesInSlot
slot := attestation.Data.Slot
for beaconState.Slot < slot {
beaconState, err = state.ExecuteStateTransition(
ctx, beaconState, nil /* block */, headRoot, &state.TransitionConfig{},
)
if err != nil {
return fmt.Errorf("could not execute head transition: %v", err)
}
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return err
}
if featureconfig.FeatureConfig().EnableCommitteesCache {
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return err
}
if cachedCommittees == nil {
crosslinkCommittees, err := helpers.CrosslinkCommitteesAtSlot(beaconState, slot, false /* registryChange */)
if err != nil {
return err
}
cachedCommittees = helpers.ToCommitteeCache(slot, crosslinkCommittees)
if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return err
}
}
} else {
crosslinkCommittees, err := helpers.CrosslinkCommitteesAtSlot(beaconState, slot, false /* registryChange */)
if cachedCommittees == nil {
crosslinkCommittees, err := helpers.CrosslinkCommitteesAtSlot(state, slot, false /* registryChange */)
if err != nil {
return err
}
cachedCommittees = helpers.ToCommitteeCache(slot, crosslinkCommittees)
if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return err
}
}
// Find committee for shard.
@@ -257,7 +228,7 @@ func (a *Service) UpdateLatestAttestation(ctx context.Context, attestation *pb.A
// If the attestation came from this attester. We use the slot committee to find the
// validator's actual index.
pubkey := bytesutil.ToBytes48(beaconState.ValidatorRegistry[committee[i]].Pubkey)
pubkey := bytesutil.ToBytes48(state.ValidatorRegistry[committee[i]].Pubkey)
newAttestationSlot := attestation.Data.Slot
currentAttestationSlot := uint64(0)
a.store.Lock()

View File

@@ -11,7 +11,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/internal"
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/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
@@ -19,9 +18,6 @@ import (
func init() {
logrus.SetLevel(logrus.DebugLevel)
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: false,
})
}
func TestUpdateLatestAttestation_UpdatesLatest(t *testing.T) {
@@ -258,9 +254,6 @@ func TestLatestAttestationTarget_ReturnsLatestAttestedBlock(t *testing.T) {
}
func TestUpdateLatestAttestation_CacheEnabledAndMiss(t *testing.T) {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: true,
})
beaconDB := internal.SetupDB(t)
defer internal.TeardownDB(t, beaconDB)
@@ -344,9 +337,6 @@ func TestUpdateLatestAttestation_CacheEnabledAndMiss(t *testing.T) {
}
func TestUpdateLatestAttestation_CacheEnabledAndHit(t *testing.T) {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: true,
})
var validators []*pb.Validator
for i := 0; i < 64; i++ {

View File

@@ -20,7 +20,6 @@ go_test(
embed = [":go_default_library"],
deps = [
"//proto/beacon/p2p/v1:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/params:go_default_library",
],
)

View File

@@ -5,16 +5,9 @@ import (
"testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
)
func init() {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: false,
})
}
func TestFFGSrcRewardsPenalties_AccurateBalances(t *testing.T) {
tests := []struct {
voted []uint64

View File

@@ -7,6 +7,7 @@ import (
"strings"
"testing"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/params"
)
@@ -353,6 +354,8 @@ func TestWinningRoot_EmptyParticipantBitfield(t *testing.T) {
},
}
helpers.RestartCommitteeCache()
want := fmt.Sprintf("wanted participants bitfield length %d, got: %d", 16, 0)
if _, err := winningRoot(state, 0, attestations, nil); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
@@ -374,6 +377,8 @@ func TestAttestingValidators_MatchActive(t *testing.T) {
attestations = append(attestations, attestation)
}
helpers.RestartCommitteeCache()
attestedValidators, err := AttestingValidators(
state,
0,
@@ -400,6 +405,8 @@ func TestAttestingValidators_EmptyWinningRoot(t *testing.T) {
AggregationBitfield: []byte{},
}
helpers.RestartCommitteeCache()
want := fmt.Sprintf("wanted participants bitfield length %d, got: %d", 16, 0)
if _, err := AttestingValidators(state, 0, []*pb.PendingAttestation{attestation}, nil); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)
@@ -424,6 +431,8 @@ func TestTotalAttestingBalance_CorrectBalance(t *testing.T) {
attestations = append(attestations, attestation)
}
helpers.RestartCommitteeCache()
attestedBalance, err := TotalAttestingBalance(
state,
0,
@@ -449,6 +458,8 @@ func TestTotalAttestingBalance_EmptyWinningRoot(t *testing.T) {
AggregationBitfield: []byte{},
}
helpers.RestartCommitteeCache()
want := fmt.Sprintf("wanted participants bitfield length %d, got: %d", 16, 0)
if _, err := TotalAttestingBalance(state, 0, []*pb.PendingAttestation{attestation}, nil); !strings.Contains(err.Error(), want) {
t.Errorf("Expected %s, received %v", want, err)

View File

@@ -18,7 +18,6 @@ go_library(
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/mathutil:go_default_library",
"//shared/params:go_default_library",
@@ -42,7 +41,6 @@ go_test(
deps = [
"//beacon-chain/cache:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/params:go_default_library",
"@com_github_gogo_protobuf//proto:go_default_library",
"@org_golang_google_grpc//codes:go_default_library",

View File

@@ -12,7 +12,6 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bitutil"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/mathutil"
"github.com/prysmaticlabs/prysm/shared/params"
"google.golang.org/grpc/codes"
@@ -265,32 +264,21 @@ func AttestationParticipants(
// When enabling committee cache, we fetch the committees using slot.
// If it's not prev cached, we compute for the committees of slot and
// add it to the cache.
if featureconfig.FeatureConfig().EnableCommitteesCache {
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return nil, err
}
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return nil, err
}
if cachedCommittees == nil {
crosslinkCommittees, err := CrosslinkCommitteesAtSlot(state, slot, false /* registryChange */)
if err != nil {
return nil, err
}
cachedCommittees = ToCommitteeCache(slot, crosslinkCommittees)
if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return nil, err
}
}
} else {
// When the committee cache is disabled, we calculate crosslink committees
// every time when AttestationParticipants gets called.
if cachedCommittees == nil {
crosslinkCommittees, err := CrosslinkCommitteesAtSlot(state, slot, false /* registryChange */)
if err != nil {
return nil, err
}
cachedCommittees = ToCommitteeCache(slot, crosslinkCommittees)
if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return nil, err
}
}
var selectedCommittee []uint64
@@ -428,29 +416,20 @@ func CommitteeAssignment(
startSlot := StartSlot(wantedEpoch)
for slot := startSlot; slot < startSlot+params.BeaconConfig().SlotsPerEpoch; slot++ {
if featureconfig.FeatureConfig().EnableCommitteesCache {
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return []uint64{}, 0, 0, false, err
}
if cachedCommittees == nil {
crosslinkCommittees, err := CrosslinkCommitteesAtSlot(
state, slot, registryChange)
if err != nil {
return []uint64{}, 0, 0, false, fmt.Errorf("could not get crosslink committee: %v", err)
}
cachedCommittees = ToCommitteeCache(slot, crosslinkCommittees)
if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return []uint64{}, 0, 0, false, err
}
}
} else {
cachedCommittees, err = committeeCache.CommitteesInfoBySlot(slot)
if err != nil {
return []uint64{}, 0, 0, false, err
}
if cachedCommittees == nil {
crosslinkCommittees, err := CrosslinkCommitteesAtSlot(
state, slot, registryChange)
if err != nil {
return []uint64{}, 0, 0, false, fmt.Errorf("could not get crosslink committee: %v", err)
}
cachedCommittees = ToCommitteeCache(slot, crosslinkCommittees)
if err := committeeCache.AddCommittees(cachedCommittees); err != nil {
return []uint64{}, 0, 0, false, err
}
}
for _, committee := range cachedCommittees.Committees {
for _, idx := range committee.Committee {
@@ -658,6 +637,11 @@ func crosslinkCommittees(state *pb.BeaconState, input *shufflingInput) ([]*Cross
return crosslinkCommittees, nil
}
// RestartCommitteeCache restarts the committee cache from scratch.
func RestartCommitteeCache() {
committeeCache = cache.NewCommitteesCache()
}
// ToCommitteeCache converts crosslink committee object
// into a cache format, to be saved in cache.
func ToCommitteeCache(slot uint64, crosslinkCommittees []*CrosslinkCommittee) *cache.CommitteesInSlot {

View File

@@ -8,7 +8,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
@@ -20,12 +19,6 @@ var validator = &pb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
func init() {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: false,
})
}
func populateValidatorsMax() {
for i := 0; i < len(validatorsUpperBound); i++ {
validatorsUpperBound[i] = validator
@@ -517,9 +510,6 @@ func TestCommitteeAssignment_CantFindValidator(t *testing.T) {
}
func TestAttestationParticipants_CommitteeCacheHit(t *testing.T) {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: true,
})
slotOffset := uint64(1111)
csInSlot := &cache.CommitteesInSlot{
Slot: params.BeaconConfig().GenesisSlot + slotOffset,
@@ -552,9 +542,6 @@ func TestAttestationParticipants_CommitteeCacheHit(t *testing.T) {
}
func TestAttestationParticipants_CommitteeCacheMissSaved(t *testing.T) {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: true,
})
validators := make([]*pb.Validator, 2*params.BeaconConfig().SlotsPerEpoch)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.Validator{
@@ -602,9 +589,6 @@ func TestAttestationParticipants_CommitteeCacheMissSaved(t *testing.T) {
}
func TestCommitteeAssignment_CommitteeCacheHit(t *testing.T) {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: true,
})
slotOffset := uint64(1111)
csInSlot := &cache.CommitteesInSlot{
Slot: params.BeaconConfig().GenesisSlot + slotOffset,
@@ -644,9 +628,6 @@ func TestCommitteeAssignment_CommitteeCacheHit(t *testing.T) {
}
func TestCommitteeAssignment_CommitteeCacheMissSaved(t *testing.T) {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: true,
})
validators := make([]*pb.Validator, 2*params.BeaconConfig().SlotsPerEpoch)
for i := 0; i < len(validators); i++ {

View File

@@ -24,7 +24,6 @@ go_test(
"//beacon-chain/core/state/stateutils:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bitutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/params:go_default_library",
],
)

View File

@@ -11,16 +11,9 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bitutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
)
func init() {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
EnableCommitteesCache: false,
})
}
func TestHasVoted_OK(t *testing.T) {
// Setting bit field to 11111111.
pendingAttestation := &pb.Attestation{

View File

@@ -55,7 +55,6 @@ spec:
- --tracing-endpoint=http://jaeger-collector.istio-system.svc.cluster.local:14268
- --trace-sample-fraction=1.0
- --datadir=/data
- --enable-committees-cache
# Disabling gossip sub until a larger beacon chain deployment.
- --disable-gossip-sub
resources:

View File

@@ -30,7 +30,6 @@ type FeatureFlagConfig struct {
EnableCrosslinks bool // EnableCrosslinks in epoch processing.
EnableCheckBlockStateRoot bool // EnableCheckBlockStateRoot in block processing.
EnableHistoricalStatePruning bool // EnableHistoricalStatePruning when updating finalized states.
EnableCommitteesCache bool // EnableCommitteesCache for state transition.
EnableBlockAncestorCache bool //EnableBlockAncestorCache for fork choice optimization.
DisableGossipSub bool // DisableGossipSub in p2p messaging.
}
@@ -74,10 +73,6 @@ func ConfigureBeaconFeatures(ctx *cli.Context) {
log.Info("Enabled historical state pruning")
cfg.EnableHistoricalStatePruning = true
}
if ctx.GlobalBool(EnableCommitteesCacheFlag.Name) {
log.Info("Enabled committees cache")
cfg.EnableCommitteesCache = true
}
if ctx.GlobalBool(EnableBlockAncestorCacheFlag.Name) {
log.Info("Enabled block ancestor cache")
cfg.EnableBlockAncestorCache = true

View File

@@ -25,11 +25,6 @@ var (
Name: "enable-crosslinks",
Usage: "Enable crosslinks in epoch processing, default is disabled.",
}
// EnableCommitteesCacheFlag enables crosslink committees cache for state transition. It is disabled by default.
EnableCommitteesCacheFlag = cli.BoolFlag{
Name: "enable-committees-cache",
Usage: "Enable crosslink committees cache for state transition, default is disabled.",
}
// EnableBlockAncestorCacheFlag enables block ancestor cache for LMD GHOST fork choice optimization. I
// it is disabled by default.
EnableBlockAncestorCacheFlag = cli.BoolFlag{
@@ -60,7 +55,6 @@ var ValidatorFlags = []cli.Flag{}
var BeaconChainFlags = []cli.Flag{
EnableComputeStateRootFlag,
EnableCrosslinksFlag,
EnableCommitteesCacheFlag,
EnableCheckBlockStateRootFlag,
EnableHistoricalStatePruningFlag,
EnableBlockAncestorCacheFlag,