Skip to content

Commit

Permalink
Revert "Release state-ref-copy flag (#6012)"
Browse files Browse the repository at this point in the history
This reverts commit ebf7122.
  • Loading branch information
terencechain committed May 27, 2020
1 parent ebf7122 commit 04f5265
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
10 changes: 10 additions & 0 deletions beacon-chain/state/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/go-bitfield"
p2ppb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
)

func TestStateReferenceSharing_Finalizer(t *testing.T) {
Expand Down Expand Up @@ -49,6 +50,9 @@ func TestStateReferenceSharing_Finalizer(t *testing.T) {
}

func TestStateReferenceCopy_NoUnexpectedRootsMutation(t *testing.T) {
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true})
defer resetCfg()

root1, root2 := bytesutil.ToBytes32([]byte("foo")), bytesutil.ToBytes32([]byte("bar"))
a, err := InitializeFromProtoUnsafe(&p2ppb.BeaconState{
BlockRoots: [][]byte{
Expand Down Expand Up @@ -137,6 +141,9 @@ func TestStateReferenceCopy_NoUnexpectedRootsMutation(t *testing.T) {
}

func TestStateReferenceCopy_NoUnexpectedRandaoMutation(t *testing.T) {
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true})
defer resetCfg()

val1, val2 := []byte("foo"), []byte("bar")
a, err := InitializeFromProtoUnsafe(&p2ppb.BeaconState{
RandaoMixes: [][]byte{
Expand Down Expand Up @@ -194,6 +201,9 @@ func TestStateReferenceCopy_NoUnexpectedRandaoMutation(t *testing.T) {
}

func TestStateReferenceCopy_NoUnexpectedAttestationsMutation(t *testing.T) {
resetCfg := featureconfig.InitWithReset(&featureconfig.Flags{EnableStateRefCopy: true})
defer resetCfg()

assertAttFound := func(vals []*p2ppb.PendingAttestation, val uint64) {
for i := range vals {
if reflect.DeepEqual(vals[i].AggregationBits, bitfield.NewBitlist(val)) {
Expand Down
62 changes: 43 additions & 19 deletions beacon-chain/state/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/prysmaticlabs/go-bitfield"
coreutils "github.com/prysmaticlabs/prysm/beacon-chain/core/state/stateutils"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
)

Expand Down Expand Up @@ -106,8 +107,12 @@ func (b *BeaconState) UpdateBlockRootAtIndex(idx uint64, blockRoot [32]byte) err
r := b.state.BlockRoots
if ref := b.sharedFieldReferences[blockRoots]; ref.refs > 1 {
// Copy on write since this is a shared array.
r = make([][]byte, len(b.state.BlockRoots))
copy(r, b.state.BlockRoots)
if featureconfig.Get().EnableStateRefCopy {
r = make([][]byte, len(b.state.BlockRoots))
copy(r, b.state.BlockRoots)
} else {
r = b.BlockRoots()
}

ref.MinusRef()
b.sharedFieldReferences[blockRoots] = &reference{refs: 1}
Expand Down Expand Up @@ -159,8 +164,12 @@ func (b *BeaconState) UpdateStateRootAtIndex(idx uint64, stateRoot [32]byte) err
r := b.state.StateRoots
if ref := b.sharedFieldReferences[stateRoots]; ref.refs > 1 {
// Perform a copy since this is a shared reference and we don't want to mutate others.
r = make([][]byte, len(b.state.StateRoots))
copy(r, b.state.StateRoots)
if featureconfig.Get().EnableStateRefCopy {
r = make([][]byte, len(b.state.StateRoots))
copy(r, b.state.StateRoots)
} else {
r = b.StateRoots()
}

ref.MinusRef()
b.sharedFieldReferences[stateRoots] = &reference{refs: 1}
Expand Down Expand Up @@ -236,9 +245,12 @@ func (b *BeaconState) AppendEth1DataVotes(val *ethpb.Eth1Data) error {
b.lock.RLock()
votes := b.state.Eth1DataVotes
if b.sharedFieldReferences[eth1DataVotes].refs > 1 {
votes = make([]*ethpb.Eth1Data, len(b.state.Eth1DataVotes))
copy(votes, b.state.Eth1DataVotes)

if featureconfig.Get().EnableStateRefCopy {
votes = make([]*ethpb.Eth1Data, len(b.state.Eth1DataVotes))
copy(votes, b.state.Eth1DataVotes)
} else {
votes = b.Eth1DataVotes()
}
b.sharedFieldReferences[eth1DataVotes].MinusRef()
b.sharedFieldReferences[eth1DataVotes] = &reference{refs: 1}
}
Expand Down Expand Up @@ -442,9 +454,12 @@ func (b *BeaconState) UpdateRandaoMixesAtIndex(idx uint64, val []byte) error {
b.lock.RLock()
mixes := b.state.RandaoMixes
if refs := b.sharedFieldReferences[randaoMixes].refs; refs > 1 {
mixes = make([][]byte, len(b.state.RandaoMixes))
copy(mixes, b.state.RandaoMixes)

if featureconfig.Get().EnableStateRefCopy {
mixes = make([][]byte, len(b.state.RandaoMixes))
copy(mixes, b.state.RandaoMixes)
} else {
mixes = b.RandaoMixes()
}
b.sharedFieldReferences[randaoMixes].MinusRef()
b.sharedFieldReferences[randaoMixes] = &reference{refs: 1}
}
Expand Down Expand Up @@ -553,9 +568,12 @@ func (b *BeaconState) AppendHistoricalRoots(root [32]byte) error {
b.lock.RLock()
roots := b.state.HistoricalRoots
if b.sharedFieldReferences[historicalRoots].refs > 1 {
roots = make([][]byte, len(b.state.HistoricalRoots))
copy(roots, b.state.HistoricalRoots)

if featureconfig.Get().EnableStateRefCopy {
roots = make([][]byte, len(b.state.HistoricalRoots))
copy(roots, b.state.HistoricalRoots)
} else {
roots = b.HistoricalRoots()
}
b.sharedFieldReferences[historicalRoots].MinusRef()
b.sharedFieldReferences[historicalRoots] = &reference{refs: 1}
}
Expand All @@ -579,9 +597,12 @@ func (b *BeaconState) AppendCurrentEpochAttestations(val *pbp2p.PendingAttestati

atts := b.state.CurrentEpochAttestations
if b.sharedFieldReferences[currentEpochAttestations].refs > 1 {
atts = make([]*pbp2p.PendingAttestation, len(b.state.CurrentEpochAttestations))
copy(atts, b.state.CurrentEpochAttestations)

if featureconfig.Get().EnableStateRefCopy {
atts = make([]*pbp2p.PendingAttestation, len(b.state.CurrentEpochAttestations))
copy(atts, b.state.CurrentEpochAttestations)
} else {
atts = b.CurrentEpochAttestations()
}
b.sharedFieldReferences[currentEpochAttestations].MinusRef()
b.sharedFieldReferences[currentEpochAttestations] = &reference{refs: 1}
}
Expand All @@ -605,9 +626,12 @@ func (b *BeaconState) AppendPreviousEpochAttestations(val *pbp2p.PendingAttestat
b.lock.RLock()
atts := b.state.PreviousEpochAttestations
if b.sharedFieldReferences[previousEpochAttestations].refs > 1 {
atts = make([]*pbp2p.PendingAttestation, len(b.state.PreviousEpochAttestations))
copy(atts, b.state.PreviousEpochAttestations)

if featureconfig.Get().EnableStateRefCopy {
atts = make([]*pbp2p.PendingAttestation, len(b.state.PreviousEpochAttestations))
copy(atts, b.state.PreviousEpochAttestations)
} else {
atts = b.PreviousEpochAttestations()
}
b.sharedFieldReferences[previousEpochAttestations].MinusRef()
b.sharedFieldReferences[previousEpochAttestations] = &reference{refs: 1}
}
Expand Down
5 changes: 5 additions & 0 deletions shared/featureconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type Flags struct {
NewStateMgmt bool // NewStateMgmt enables the new state mgmt service.
EnableFieldTrie bool // EnableFieldTrie enables the state from using field specific tries when computing the root.
NoInitSyncBatchSaveBlocks bool // NoInitSyncBatchSaveBlocks disables batch save blocks mode during initial syncing.
EnableStateRefCopy bool // EnableStateRefCopy copies the references to objects instead of the objects themselves when copying state fields.
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.
Expand Down Expand Up @@ -196,6 +197,10 @@ func ConfigureBeaconChain(ctx *cli.Context) {
log.Warn("Disabling init sync batch save blocks mode")
cfg.NoInitSyncBatchSaveBlocks = true
}
if ctx.Bool(enableStateRefCopy.Name) {
log.Warn("Enabling state reference copy")
cfg.EnableStateRefCopy = true
}
if ctx.Bool(disableBroadcastSlashingFlag.Name) {
log.Warn("Disabling slashing broadcasting to p2p network")
cfg.DisableBroadcastSlashings = true
Expand Down
13 changes: 7 additions & 6 deletions shared/featureconfig/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ var (
Name: "disable-init-sync-batch-save-blocks",
Usage: "Instead of saving batch blocks to the DB during initial syncing, this disables batch saving of blocks",
}
enableStateRefCopy = &cli.BoolFlag{
Name: "enable-state-ref-copy",
Usage: "Enables the usage of a new copying method for our state fields.",
}
waitForSyncedFlag = &cli.BoolFlag{
Name: "wait-for-synced",
Usage: "Uses WaitForSynced for validator startup, to ensure a validator is able to communicate with the beacon node as quick as possible",
Expand All @@ -156,6 +160,7 @@ var (

// devModeFlags holds list of flags that are set when development mode is on.
var devModeFlags = []cli.Flag{
enableStateRefCopy,
enableFieldTrie,
enableNewStateMgmt,
enableInitSyncWeightedRoundRobin,
Expand Down Expand Up @@ -366,11 +371,6 @@ var (
Usage: deprecatedUsage,
Hidden: true,
}
deprecateEnableStateRefCopy = &cli.BoolFlag{
Name: "enable-state-ref-copy",
Usage: deprecatedUsage,
Hidden: true,
}
)

var deprecatedFlags = []cli.Flag{
Expand Down Expand Up @@ -414,7 +414,6 @@ var deprecatedFlags = []cli.Flag{
deprecatedEnableByteMempool,
deprecatedBroadcastSlashingFlag,
deprecatedDisableHistoricalDetectionFlag,
deprecateEnableStateRefCopy,
}

// ValidatorFlags contains a list of all the feature flags that apply to the validator client.
Expand Down Expand Up @@ -468,6 +467,7 @@ var BeaconChainFlags = append(deprecatedFlags, []cli.Flag{
enableNewStateMgmt,
enableFieldTrie,
disableInitSyncBatchSaveBlocks,
enableStateRefCopy,
waitForSyncedFlag,
skipRegenHistoricalStates,
enableInitSyncWeightedRoundRobin,
Expand All @@ -479,6 +479,7 @@ var E2EBeaconChainFlags = []string{
"--enable-state-gen-sig-verify",
"--check-head-state",
"--enable-state-field-trie",
"--enable-state-ref-copy",
"--enable-new-state-mgmt",
"--enable-init-sync-wrr",
}

0 comments on commit 04f5265

Please sign in to comment.