Release Proposer Index Cache (#4717)

* release cache

* gaz

* fix all tests

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Nishant Das 2020-02-04 00:23:04 +08:00 committed by GitHub
parent 00a6361c66
commit fb7a75d2c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 51 additions and 43 deletions

View File

@ -84,6 +84,7 @@ go_test(
"//beacon-chain/db/testing:go_default_library",
"//beacon-chain/p2p:go_default_library",
"//beacon-chain/powchain:go_default_library",
"//proto/beacon/db:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/event:go_default_library",

View File

@ -26,6 +26,7 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
beaconstate "github.com/prysmaticlabs/prysm/beacon-chain/state"
protodb "github.com/prysmaticlabs/prysm/proto/beacon/db"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/event"
@ -104,6 +105,25 @@ func setupBeaconChain(t *testing.T, beaconDB db.Database) *Service {
ctx := context.Background()
var web3Service *powchain.Service
var err error
bState, _ := testutil.DeterministicGenesisState(t, 10)
err = beaconDB.SavePowchainData(ctx, &protodb.ETH1ChainData{
BeaconState: bState.InnerStateUnsafe(),
Trie: &protodb.SparseMerkleTrie{},
CurrentEth1Data: &protodb.LatestETH1Data{
BlockHash: make([]byte, 32),
},
ChainstartData: &protodb.ChainStartData{
Eth1Data: &ethpb.Eth1Data{
DepositRoot: make([]byte, 32),
DepositCount: 0,
BlockHash: make([]byte, 32),
},
},
DepositContainers: []*protodb.DepositContainer{},
})
if err != nil {
t.Fatal(err)
}
web3Service, err = powchain.NewService(ctx, &powchain.Web3ServiceConfig{
BeaconDB: beaconDB,
ETH1Endpoint: endpoint,

View File

@ -27,7 +27,6 @@ go_library(
"//proto/beacon/p2p/v1:go_default_library",
"//shared/bls:go_default_library",
"//shared/bytesutil:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/roughtime:go_default_library",

View File

@ -11,7 +11,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/cache"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
"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/prysmaticlabs/prysm/shared/sliceutil"
@ -429,9 +428,6 @@ func UpdateCommitteeCache(state *stateTrie.BeaconState, epoch uint64) error {
// UpdateProposerIndicesInCache updates proposer indices entry of the committee cache.
func UpdateProposerIndicesInCache(state *stateTrie.BeaconState, epoch uint64) error {
if !featureconfig.Get().EnableProposerIndexCache {
return nil
}
indices, err := ActiveValidatorIndices(state, epoch)
if err != nil {

View File

@ -7,7 +7,6 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
@ -150,21 +149,19 @@ func ValidatorChurnLimit(activeValidatorCount uint64) (uint64, error) {
func BeaconProposerIndex(state *stateTrie.BeaconState) (uint64, error) {
e := CurrentEpoch(state)
if featureconfig.Get().EnableProposerIndexCache {
seed, err := Seed(state, e, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
return 0, errors.Wrap(err, "could not generate seed")
}
proposerIndices, err := committeeCache.ProposerIndices(seed)
if err != nil {
return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil {
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
}
seed, err := Seed(state, e, params.BeaconConfig().DomainBeaconAttester)
if err != nil {
return 0, errors.Wrap(err, "could not generate seed")
}
proposerIndices, err := committeeCache.ProposerIndices(seed)
if err != nil {
return 0, errors.Wrap(err, "could not interface with committee cache")
}
if proposerIndices != nil {
return proposerIndices[state.Slot()%params.BeaconConfig().SlotsPerEpoch], nil
}
seed, err := Seed(state, e, params.BeaconConfig().DomainBeaconProposer)
seed, err = Seed(state, e, params.BeaconConfig().DomainBeaconProposer)
if err != nil {
return 0, errors.Wrap(err, "could not generate seed")
}
@ -177,10 +174,8 @@ func BeaconProposerIndex(state *stateTrie.BeaconState) (uint64, error) {
return 0, errors.Wrap(err, "could not get active indices")
}
if featureconfig.Get().EnableProposerIndexCache {
if err := UpdateProposerIndicesInCache(state, CurrentEpoch(state)); err != nil {
return 0, errors.Wrap(err, "could not update committee cache")
}
if err := UpdateProposerIndicesInCache(state, CurrentEpoch(state)); err != nil {
return 0, errors.Wrap(err, "could not update committee cache")
}
return ComputeProposerIndex(state.Validators(), indices, seedWithSlotHash)

View File

@ -53,8 +53,7 @@ func BenchmarkExecuteStateTransition_FullBlock(b *testing.B) {
func BenchmarkExecuteStateTransition_WithCache(b *testing.B) {
config := &featureconfig.Flags{
EnableProposerIndexCache: true,
EnableAttestationCache: true,
EnableAttestationCache: true,
}
featureconfig.Init(config)
benchutil.SetBenchmarkConfig()
@ -93,8 +92,7 @@ func BenchmarkExecuteStateTransition_WithCache(b *testing.B) {
func BenchmarkProcessEpoch_2FullEpochs(b *testing.B) {
config := &featureconfig.Flags{
EnableProposerIndexCache: true,
EnableAttestationCache: true,
EnableAttestationCache: true,
}
featureconfig.Init(config)
benchutil.SetBenchmarkConfig()

View File

@ -423,6 +423,9 @@ func (s *Service) initDataFromContract() error {
}
func (s *Service) initDepositCaches(ctx context.Context, ctrs []*protodb.DepositContainer) error {
if ctrs == nil || len(ctrs) == 0 {
return nil
}
s.depositCache.InsertDepositContainers(ctx, ctrs)
currentState, err := s.beaconDB.HeadState(ctx)
if err != nil {

View File

@ -48,13 +48,12 @@ type Flags struct {
DisableForkChoice bool
// Cache toggles.
EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableSSZCache bool // EnableSSZCache see https://github.com/prysmaticlabs/prysm/pull/4558.
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.
EnableProposerIndexCache bool // EnableProposerIndexCache enable caching of proposer index.
EnableAttestationCache bool // EnableAttestationCache; see https://github.com/prysmaticlabs/prysm/issues/3106.
EnableSSZCache bool // EnableSSZCache see https://github.com/prysmaticlabs/prysm/pull/4558.
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
@ -140,10 +139,6 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Enabled filtered block tree cache for fork choice.")
cfg.EnableBlockTreeCache = true
}
if ctx.GlobalBool(cacheProposerIndicesFlag.Name) {
log.Warn("Enabled proposer index caching.")
cfg.EnableProposerIndexCache = true
}
if ctx.GlobalBool(protoArrayForkChoice.Name) {
log.Warn("Enabled using proto array fork choice over spec fork choice.")
cfg.ProtoArrayForkChoice = true

View File

@ -80,10 +80,6 @@ var (
Usage: "Cache filtered block tree by maintaining it rather than continually recalculating on the fly, " +
"this is used for fork choice.",
}
cacheProposerIndicesFlag = cli.BoolFlag{
Name: "cache-proposer-indices",
Usage: "Cache proposer indices on per epoch basis.",
}
protectProposerFlag = cli.BoolFlag{
Name: "protect-proposer",
Usage: "Prevent the validator client from signing and broadcasting 2 different block " +
@ -185,6 +181,11 @@ var (
Usage: deprecatedUsage,
Hidden: true,
}
deprecatedCacheProposerIndicesFlag = cli.BoolFlag{
Name: "cache-proposer-indices",
Usage: deprecatedUsage,
Hidden: true,
}
)
var deprecatedFlags = []cli.Flag{
@ -204,6 +205,7 @@ var deprecatedFlags = []cli.Flag{
deprecatedNewCacheFlag,
deprecatedEnableShuffledIndexCacheFlag,
deprecatedSaveDepositDataFlag,
deprecatedCacheProposerIndicesFlag,
}
// ValidatorFlags contains a list of all the feature flags that apply to the validator client.
@ -236,7 +238,6 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableSkipSlotsCacheFlag,
enableSlasherFlag,
cacheFilteredBlockTreeFlag,
cacheProposerIndicesFlag,
protoArrayForkChoice,
}...)