Skip to content

Commit

Permalink
Use balancesLength and randaoMixesLength to save copy on read (prysma…
Browse files Browse the repository at this point in the history
…ticlabs#4769)

* Use balancesLength and randaoMixesLength to save copy on read

* Update beacon-chain/state/getters.go

Co-Authored-By: shayzluf <thezluf@gmail.com>

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: shayzluf <thezluf@gmail.com>
  • Loading branch information
3 people authored and cryptomental committed Feb 28, 2020
1 parent 7008f8f commit 46bf360
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
5 changes: 2 additions & 3 deletions beacon-chain/core/epoch/epoch_processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,10 @@ func ProcessFinalUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState,

// Set RANDAO mix.
randaoMixLength := params.BeaconConfig().EpochsPerHistoricalVector
mixes := state.RandaoMixes()
if len(mixes) != int(randaoMixLength) {
if state.RandaoMixesLength() != int(randaoMixLength) {
return nil, fmt.Errorf(
"state randao length %d different than EpochsPerHistoricalVector %d",
len(mixes),
state.RandaoMixesLength(),
randaoMixLength,
)
}
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/core/epoch/precompute/reward_penalty.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ func ProcessRewardsAndPenaltiesPrecompute(
}

numOfVals := state.NumValidators()
bals := state.Balances()
// Guard against an out-of-bounds using validator balance precompute.
if len(vp) != numOfVals || len(vp) != len(bals) {
if len(vp) != numOfVals || len(vp) != state.BalancesLength() {
return state, errors.New("precomputed registries not the same length as state registries")
}

Expand Down
24 changes: 24 additions & 0 deletions beacon-chain/state/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,18 @@ func (b *BeaconState) BalanceAtIndex(idx uint64) (uint64, error) {
return b.state.Balances[idx], nil
}

// BalancesLength returns the length of the balances slice.
func (b *BeaconState) BalancesLength() int {
if b.state.Balances == nil {
return 0
}

b.lock.RLock()
defer b.lock.RUnlock()

return len(b.state.Balances)
}

// RandaoMixes of block proposers on the beacon chain.
func (b *BeaconState) RandaoMixes() [][]byte {
if b.state.RandaoMixes == nil {
Expand Down Expand Up @@ -467,6 +479,18 @@ func (b *BeaconState) RandaoMixAtIndex(idx uint64) ([]byte, error) {
return root, nil
}

// RandaoMixesLength returns the length of the randao mixes slice.
func (b *BeaconState) RandaoMixesLength() int {
if b.state.RandaoMixes == nil {
return 0
}

b.lock.RLock()
defer b.lock.RUnlock()

return len(b.state.RandaoMixes)
}

// Slashings of validators on the beacon chain.
func (b *BeaconState) Slashings() []uint64 {
if b.state.Slashings == nil {
Expand Down

0 comments on commit 46bf360

Please sign in to comment.