diff --git a/beacon-chain/cache/shuffled_indices.go b/beacon-chain/cache/shuffled_indices.go index c9ef6d443d..7fb531458e 100644 --- a/beacon-chain/cache/shuffled_indices.go +++ b/beacon-chain/cache/shuffled_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" ) @@ -62,6 +63,9 @@ func NewShuffledIndicesCache() *ShuffledIndicesCache { // IndicesByIndexSeed fetches IndicesByIndexSeed by epoch and seed. Returns true with a // reference to the ShuffledIndicesInEpoch info, if exists. Otherwise returns false, nil. func (c *ShuffledIndicesCache) IndicesByIndexSeed(index uint64, seed []byte) ([]uint64, error) { + if !featureconfig.Get().EnableShuffledIndexCache { + return nil, nil + } c.lock.RLock() defer c.lock.RUnlock() key := string(seed) + strconv.Itoa(int(index)) @@ -88,6 +92,9 @@ func (c *ShuffledIndicesCache) IndicesByIndexSeed(index uint64, seed []byte) ([] // AddShuffledValidatorList adds IndicesByIndexSeed object to the cache. This method also trims the least // recently added IndicesByIndexSeed object if the cache size has ready the max cache size limit. func (c *ShuffledIndicesCache) AddShuffledValidatorList(shuffledIndices *IndicesByIndexSeed) error { + if !featureconfig.Get().EnableShuffledIndexCache { + return nil + } c.lock.Lock() defer c.lock.Unlock() if err := c.shuffledIndicesCache.AddIfNotPresent(shuffledIndices); err != nil { diff --git a/beacon-chain/cache/shuffled_indices_test.go b/beacon-chain/cache/shuffled_indices_test.go index 1d1f32273a..da2b6fe720 100644 --- a/beacon-chain/cache/shuffled_indices_test.go +++ b/beacon-chain/cache/shuffled_indices_test.go @@ -4,8 +4,16 @@ import ( "reflect" "strconv" "testing" + + "github.com/prysmaticlabs/prysm/shared/featureconfig" ) +func init() { + fc := featureconfig.Get() + fc.EnableShuffledIndexCache = true + featureconfig.Init(fc) +} + func TestShuffleKeyFn_OK(t *testing.T) { sInfo := &IndicesByIndexSeed{ Index: 999, diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index 5aa98df69e..f13fef7702 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -60,6 +60,7 @@ go_test( "//proto/eth/v1alpha1:go_default_library", "//shared/bls:go_default_library", "//shared/bytesutil:go_default_library", + "//shared/featureconfig:go_default_library", "//shared/params:go_default_library", "//shared/sliceutil:go_default_library", "//shared/testutil:go_default_library", diff --git a/beacon-chain/core/helpers/committee_test.go b/beacon-chain/core/helpers/committee_test.go index 0b6f90b21a..c40e7b0040 100644 --- a/beacon-chain/core/helpers/committee_test.go +++ b/beacon-chain/core/helpers/committee_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/go-bitfield" pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1" ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" + "github.com/prysmaticlabs/prysm/shared/featureconfig" "github.com/prysmaticlabs/prysm/shared/params" "github.com/prysmaticlabs/prysm/shared/sliceutil" "google.golang.org/grpc/codes" @@ -176,6 +177,11 @@ func TestComputeCommittee_WithoutCache(t *testing.T) { } func TestComputeCommittee_WithCache(t *testing.T) { + fc := featureconfig.Get() + fc.EnableShuffledIndexCache = true + featureconfig.Init(fc) + defer featureconfig.Init(nil) + // Create 10 committees committeeCount := uint64(10) validatorCount := committeeCount * params.BeaconConfig().TargetCommitteeSize diff --git a/shared/featureconfig/config.go b/shared/featureconfig/config.go index 6b85157706..0b6d0c3d04 100644 --- a/shared/featureconfig/config.go +++ b/shared/featureconfig/config.go @@ -36,10 +36,11 @@ type Flag struct { EnableFinalizedBlockRootIndex bool // EnableFinalizedBlockRootIndex in the database. This operation may be expensive at runtime. // 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. + 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. } var featureConfig *Flag @@ -114,6 +115,10 @@ func ConfigureBeaconChain(ctx *cli.Context) { log.Warn("Enabled finalized block root index") cfg.EnableFinalizedBlockRootIndex = true } + if ctx.GlobalBool(enableShuffledIndexCache.Name) { + log.Warn("Enabled shuffled index cache.") + cfg.EnableShuffledIndexCache = true + } Init(cfg) } diff --git a/shared/featureconfig/flags.go b/shared/featureconfig/flags.go index de51ed042b..6410529b0a 100644 --- a/shared/featureconfig/flags.go +++ b/shared/featureconfig/flags.go @@ -29,6 +29,10 @@ var ( Name: "enable-eth1-data-vote-cache", Usage: "Enable unsafe cache mechanism. See https://github.com/prysmaticlabs/prysm/issues/3106", } + enableShuffledIndexCache = cli.BoolFlag{ + Name: "enable-shuffled-index-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", @@ -99,6 +103,7 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{ OptimizeProcessEpoch, enableBackupWebhookFlag, enableBLSPubkeyCacheFlag, + enableShuffledIndexCache, pruneFinalizedStatesFlag, enableFinalizedBlockRootIndexFlag, }...)