diff --git a/beacon-chain/cache/active_count.go b/beacon-chain/cache/active_count.go index d11e0761fc96..cda0e83e7113 100644 --- a/beacon-chain/cache/active_count.go +++ b/beacon-chain/cache/active_count.go @@ -7,6 +7,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" "k8s.io/client-go/tools/cache" ) @@ -62,6 +63,9 @@ func NewActiveCountCache() *ActiveCountCache { // ActiveCountInEpoch fetches ActiveCountByEpoch by epoch. Returns true with a // reference to the ActiveCountInEpoch info, if exists. Otherwise returns false, nil. func (c *ActiveCountCache) ActiveCountInEpoch(epoch uint64) (uint64, error) { + if !featureconfig.Get().EnableActiveCountCache { + return params.BeaconConfig().FarFutureEpoch, nil + } c.lock.RLock() defer c.lock.RUnlock() obj, exists, err := c.activeCountCache.GetByKey(strconv.Itoa(int(epoch))) diff --git a/beacon-chain/cache/active_indices.go b/beacon-chain/cache/active_indices.go index 4c62b97e0b62..6fe18be717b7 100644 --- a/beacon-chain/cache/active_indices.go +++ b/beacon-chain/cache/active_indices.go @@ -7,6 +7,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "k8s.io/client-go/tools/cache" ) @@ -61,6 +62,9 @@ func NewActiveIndicesCache() *ActiveIndicesCache { // ActiveIndicesInEpoch fetches ActiveIndicesByEpoch by epoch. Returns true with a // reference to the ActiveIndicesInEpoch info, if exists. Otherwise returns false, nil. func (c *ActiveIndicesCache) ActiveIndicesInEpoch(epoch uint64) ([]uint64, error) { + if !featureconfig.Get().EnableActiveIndicesCache { + return nil, nil + } c.lock.RLock() defer c.lock.RUnlock() obj, exists, err := c.activeIndicesCache.GetByKey(strconv.Itoa(int(epoch))) diff --git a/beacon-chain/cache/committee.go b/beacon-chain/cache/committee.go index 0b1ae83a5e08..ed17b81c6167 100644 --- a/beacon-chain/cache/committee.go +++ b/beacon-chain/cache/committee.go @@ -7,6 +7,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" "k8s.io/client-go/tools/cache" @@ -67,6 +68,9 @@ func NewCommitteeCache() *CommitteeCache { // ShuffledIndices fetches the shuffled indices by epoch and shard. Every list of indices // represent one committee. Returns true if the list exists with epoch and shard. Otherwise returns false, nil. func (c *CommitteeCache) ShuffledIndices(epoch uint64, shard uint64) ([]uint64, error) { + if !featureconfig.Get().EnableShuffledIndexCache && !featureconfig.Get().EnableNewCache { + return nil, nil + } c.lock.RLock() defer c.lock.RUnlock() obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch))) @@ -94,6 +98,9 @@ func (c *CommitteeCache) ShuffledIndices(epoch uint64, shard uint64) ([]uint64, // AddCommitteeShuffledList adds Committee shuffled list object to the cache. T // his method also trims the least recently list if the cache size has ready the max cache size limit. func (c *CommitteeCache) AddCommitteeShuffledList(committee *Committee) error { + if !featureconfig.Get().EnableShuffledIndexCache && !featureconfig.Get().EnableNewCache { + return nil + } c.lock.Lock() defer c.lock.Unlock() if err := c.CommitteeCache.AddIfNotPresent(committee); err != nil { @@ -105,6 +112,9 @@ func (c *CommitteeCache) AddCommitteeShuffledList(committee *Committee) error { // Epochs returns the epochs stored in the committee cache. These are the keys to the cache. func (c *CommitteeCache) Epochs() ([]uint64, error) { + if !featureconfig.Get().EnableShuffledIndexCache { + return nil, nil + } c.lock.RLock() defer c.lock.RUnlock() @@ -121,6 +131,9 @@ func (c *CommitteeCache) Epochs() ([]uint64, error) { // EpochInCache returns true if an input epoch is part of keys in cache. func (c *CommitteeCache) EpochInCache(wantedEpoch uint64) (bool, error) { + if !featureconfig.Get().EnableShuffledIndexCache && !featureconfig.Get().EnableNewCache { + return false, nil + } c.lock.RLock() defer c.lock.RUnlock() @@ -138,6 +151,9 @@ func (c *CommitteeCache) EpochInCache(wantedEpoch uint64) (bool, error) { // CommitteeCount returns the total number of committees in a given epoch as stored in cache. func (c *CommitteeCache) CommitteeCount(epoch uint64) (uint64, bool, error) { + if !featureconfig.Get().EnableShuffledIndexCache && !featureconfig.Get().EnableNewCache { + return 0, false, nil + } c.lock.RLock() defer c.lock.RUnlock() obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch))) @@ -162,6 +178,9 @@ func (c *CommitteeCache) CommitteeCount(epoch uint64) (uint64, bool, error) { // StartShard returns the start shard number in a given epoch as stored in cache. func (c *CommitteeCache) StartShard(epoch uint64) (uint64, bool, error) { + if !featureconfig.Get().EnableShuffledIndexCache && !featureconfig.Get().EnableNewCache { + return 0, false, nil + } c.lock.RLock() defer c.lock.RUnlock() obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch))) @@ -186,6 +205,10 @@ func (c *CommitteeCache) StartShard(epoch uint64) (uint64, bool, error) { // ActiveIndices returns the active indices of a given epoch stored in cache. func (c *CommitteeCache) ActiveIndices(epoch uint64) ([]uint64, error) { + if !featureconfig.Get().EnableShuffledIndexCache && !featureconfig.Get().EnableNewCache { + return nil, nil + } + c.lock.RLock() defer c.lock.RUnlock() obj, exists, err := c.CommitteeCache.GetByKey(strconv.Itoa(int(epoch))) diff --git a/beacon-chain/cache/feature_flag_test.go b/beacon-chain/cache/feature_flag_test.go index 36e9dcac996e..6710a4703216 100644 --- a/beacon-chain/cache/feature_flag_test.go +++ b/beacon-chain/cache/feature_flag_test.go @@ -4,7 +4,11 @@ import "github.com/prysmaticlabs/prysm/shared/featureconfig" func init() { featureconfig.Init(&featureconfig.Flag{ - EnableAttestationCache: true, - EnableEth1DataVoteCache: true, + EnableAttestationCache: true, + EnableEth1DataVoteCache: true, + EnableShuffledIndexCache: true, + EnableCommitteeCache: true, + EnableActiveCountCache: true, + EnableActiveIndicesCache: true, }) } diff --git a/beacon-chain/core/epoch/epoch_processing_test.go b/beacon-chain/core/epoch/epoch_processing_test.go index c1280b5ab3fb..517ee5f29e21 100644 --- a/beacon-chain/core/epoch/epoch_processing_test.go +++ b/beacon-chain/core/epoch/epoch_processing_test.go @@ -1096,33 +1096,6 @@ func TestAttestationDelta_CantGetAttestation(t *testing.T) { } } -func TestAttestationDelta_CantGetAttestationIndices(t *testing.T) { - e := params.BeaconConfig().SlotsPerEpoch - - state := buildState(e+2, 1) - atts := make([]*pb.PendingAttestation, 2) - for i := 0; i < len(atts); i++ { - atts[i] = &pb.PendingAttestation{ - Data: ðpb.AttestationData{ - Crosslink: ðpb.Crosslink{ - Shard: uint64(i), - }, - Target: ðpb.Checkpoint{}, - Source: ðpb.Checkpoint{}, - }, - InclusionDelay: uint64(i + 100), - AggregationBits: bitfield.Bitlist{0xFF, 0x01}, - } - } - state.PreviousEpochAttestations = atts - - _, _, err := attestationDelta(state) - wanted := "could not get attestation indices" - if !strings.Contains(err.Error(), wanted) { - t.Fatalf("Got: %v, want: %v", err.Error(), wanted) - } -} - func TestAttestationDelta_NoOneAttested(t *testing.T) { e := params.BeaconConfig().SlotsPerEpoch validatorCount := params.BeaconConfig().MinGenesisActiveValidatorCount / 32 diff --git a/beacon-chain/core/helpers/committee_test.go b/beacon-chain/core/helpers/committee_test.go index c40e7b00407d..e4da4d18e491 100644 --- a/beacon-chain/core/helpers/committee_test.go +++ b/beacon-chain/core/helpers/committee_test.go @@ -799,6 +799,11 @@ func TestShuffledIndices_ShuffleRightLength(t *testing.T) { } func TestUpdateCommitteeCache_CanUpdate(t *testing.T) { + c := featureconfig.Get() + c.EnableShuffledIndexCache = true + featureconfig.Init(c) + defer featureconfig.Init(nil) + ClearAllCaches() validatorCount := int(params.BeaconConfig().MinGenesisActiveValidatorCount) @@ -833,7 +838,7 @@ func TestUpdateCommitteeCache_CanUpdate(t *testing.T) { t.Fatal(err) } if len(indices) != int(params.BeaconConfig().TargetCommitteeSize) { - t.Error("Did not save correct indices lengths") + t.Errorf("Did not save correct indices lengths, got %d wanted %d", len(indices), params.BeaconConfig().TargetCommitteeSize) } } diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index 9a66f8c03d9a..50339e946404 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -36,12 +36,15 @@ type Flag struct { PruneFinalizedStates bool // PruneFinalizedStates from the database. // Cache toggles. - EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106. - EnableEth1DataVoteCache bool // EnableEth1DataVoteCache; see https://github.com/prysmaticlabs/prysm/issues/3106. - EnableNewCache bool // EnableNewCache enables the node to use the new caching scheme. - EnableBLSPubkeyCache bool // EnableBLSPubkeyCache to improve wall time of PubkeyFromBytes. - EnableShuffledIndexCache bool // EnableShuffledIndexCache to cache expensive shuffled index computation. + EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106. + EnableEth1DataVoteCache bool // EnableEth1DataVoteCache; see https://github.com/prysmaticlabs/prysm/issues/3106. + EnableNewCache bool // EnableNewCache enables the node to use the new caching scheme. + EnableBLSPubkeyCache bool // EnableBLSPubkeyCache to improve wall time of PubkeyFromBytes. + EnableShuffledIndexCache bool // EnableShuffledIndexCache to cache expensive shuffled index computation. EnableSkipSlotsCache bool // EnableSkipSlotsCache caches the state in skipped slots. + EnableCommitteeCache bool // EnableCommitteeCache to cache committee computation. + EnableActiveIndicesCache bool // EnableActiveIndicesCache. + EnableActiveCountCache bool // EnableActiveCountCache. } var featureConfig *Flag @@ -124,6 +127,18 @@ func ConfigureBeaconChain(ctx *cli.Context) { log.Warn("Enabled skip slots cache.") cfg.EnableSkipSlotsCache = true } + if ctx.GlobalBool(enableCommitteeCacheFlag.Name) { + log.Warn("Enabled committee cache.") + cfg.EnableCommitteeCache = true + } + if ctx.GlobalBool(enableActiveIndicesCacheFlag.Name) { + log.Warn("Enabled active indices cache.") + cfg.EnableActiveIndicesCache = true + } + if ctx.GlobalBool(enableActiveCountCacheFlag.Name) { + log.Warn("Enabled active count cache.") + cfg.EnableActiveCountCache = true + } Init(cfg) } diff --git a/shared/featureconfig/flags.go b/shared/featureconfig/flags.go index 82d0dfcd24b3..49aa0a454cec 100644 --- a/shared/featureconfig/flags.go +++ b/shared/featureconfig/flags.go @@ -33,6 +33,18 @@ var ( Name: "enable-shuffled-index-cache", Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106", } + enableCommitteeCacheFlag = cli.BoolFlag{ + Name: "enable-committee-cache", + Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106", + } + enableActiveIndicesCacheFlag = cli.BoolFlag{ + Name: "enable-active-indices-cache", + Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106", + } + enableActiveCountCacheFlag = cli.BoolFlag{ + Name: "enable-active-count-cache", + Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106", + } // InitSyncNoVerifyFlag enables the initial sync no verify configuration. InitSyncNoVerifyFlag = cli.BoolFlag{ Name: "init-sync-no-verify", @@ -118,6 +130,9 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{ enableBackupWebhookFlag, enableBLSPubkeyCacheFlag, enableShuffledIndexCache, + enableCommitteeCacheFlag, + enableActiveIndicesCacheFlag, + enableActiveCountCacheFlag, pruneFinalizedStatesFlag, enableSkipSlotsCache, }...)