Skip to content

Commit

Permalink
Reduce attester state copies (#6025)
Browse files Browse the repository at this point in the history
* Test

* Fix

* Removed extra line

* Feature flag

* Gaz
  • Loading branch information
terencechain committed May 28, 2020
1 parent 7dd187c commit 3fe47c0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions beacon-chain/cache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ go_library(
deps = [
"//beacon-chain/state:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/featureconfig:go_default_library",
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/sliceutil:go_default_library",
Expand Down
8 changes: 8 additions & 0 deletions beacon-chain/cache/attestation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"k8s.io/client-go/tools/cache"
)

Expand Down Expand Up @@ -97,6 +99,9 @@ func (c *AttestationCache) Get(ctx context.Context, req *ethpb.AttestationDataRe

if exists && item != nil && item.(*attestationReqResWrapper).res != nil {
attestationCacheHit.Inc()
if featureconfig.Get().ReduceAttesterStateCopy {
return state.CopyAttestationData(item.(*attestationReqResWrapper).res), nil
}
return item.(*attestationReqResWrapper).res, nil
}
attestationCacheMiss.Inc()
Expand Down Expand Up @@ -162,6 +167,9 @@ func wrapperToKey(i interface{}) (string, error) {
}

func reqToKey(req *ethpb.AttestationDataRequest) (string, error) {
if featureconfig.Get().ReduceAttesterStateCopy {
return fmt.Sprintf("%d", req.Slot), nil
}
return fmt.Sprintf("%d-%d", req.CommitteeIndex, req.Slot), nil
}

Expand Down
6 changes: 6 additions & 0 deletions beacon-chain/rpc/validator/attester.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ func (vs *Server) GetAttestationData(ctx context.Context, req *ethpb.Attestation
return nil, status.Errorf(codes.Internal, "Could not retrieve data from attestation cache: %v", err)
}
if res != nil {
if featureconfig.Get().ReduceAttesterStateCopy {
res.CommitteeIndex = req.CommitteeIndex
}
return res, nil
}

Expand All @@ -59,6 +62,9 @@ func (vs *Server) GetAttestationData(ctx context.Context, req *ethpb.Attestation
if res == nil {
return nil, status.Error(codes.DataLoss, "A request was in progress and resolved to nil")
}
if featureconfig.Get().ReduceAttesterStateCopy {
res.CommitteeIndex = req.CommitteeIndex
}
return res, nil
}
return nil, status.Errorf(codes.Internal, "Could not mark attestation as in-progress: %v", err)
Expand Down
5 changes: 5 additions & 0 deletions shared/featureconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type Flags struct {
WaitForSynced bool // WaitForSynced uses WaitForSynced in validator startup to ensure it can communicate with the beacon node as soon as possible.
SkipRegenHistoricalStates bool // SkipRegenHistoricalState skips regenerating historical states from genesis to last finalized. This enables a quick switch over to using new-state-mgmt.
EnableInitSyncWeightedRoundRobin bool // EnableInitSyncWeightedRoundRobin enables weighted round robin fetching optimization in initial syncing.
ReduceAttesterStateCopy bool // ReduceAttesterStateCopy reduces head state copies for attester rpc.

// DisableForkChoice disables using LMD-GHOST fork choice to update
// the head of the chain based on attestations and instead accepts any valid received block
Expand Down Expand Up @@ -215,6 +216,10 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Disabling state reference copy")
cfg.EnableStateRefCopy = false
}
if ctx.Bool(reduceAttesterStateCopy.Name) {
log.Warn("Enabling feature that reduces attester state copy")
cfg.ReduceAttesterStateCopy = true
}
Init(cfg)
}

Expand Down
7 changes: 7 additions & 0 deletions shared/featureconfig/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,17 @@ var (
Name: "enable-init-sync-wrr",
Usage: "Enables weighted round robin fetching optimization",
}
reduceAttesterStateCopy = &cli.BoolFlag{
Name: "reduce-attester-state-copy",
Usage: "Reduces the amount of state copies for attester rpc",
}
)

// devModeFlags holds list of flags that are set when development mode is on.
var devModeFlags = []cli.Flag{
enableNewStateMgmt,
enableInitSyncWeightedRoundRobin,
reduceAttesterStateCopy,
}

// Deprecated flags list.
Expand Down Expand Up @@ -481,6 +486,7 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableInitSyncWeightedRoundRobin,
disableFieldTrie,
disableStateRefCopy,
reduceAttesterStateCopy,
}...)

// E2EBeaconChainFlags contains a list of the beacon chain feature flags to be tested in E2E.
Expand All @@ -490,4 +496,5 @@ var E2EBeaconChainFlags = []string{
"--check-head-state",
"--enable-new-state-mgmt",
"--enable-init-sync-wrr",
"--reduce-attester-state-copy",
}

0 comments on commit 3fe47c0

Please sign in to comment.