From 46bf360471c05bf246d9e34692bb1b7b5f8f8c2b Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Thu, 6 Feb 2020 08:06:27 -0800 Subject: [PATCH] Use balancesLength and randaoMixesLength to save copy on read (#4769) * Use balancesLength and randaoMixesLength to save copy on read * Update beacon-chain/state/getters.go Co-Authored-By: shayzluf Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> Co-authored-by: shayzluf --- beacon-chain/core/epoch/epoch_processing.go | 5 ++-- .../core/epoch/precompute/reward_penalty.go | 3 +-- beacon-chain/state/getters.go | 24 +++++++++++++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/beacon-chain/core/epoch/epoch_processing.go b/beacon-chain/core/epoch/epoch_processing.go index 1be30be576ba..34369a7b916a 100644 --- a/beacon-chain/core/epoch/epoch_processing.go +++ b/beacon-chain/core/epoch/epoch_processing.go @@ -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, ) } diff --git a/beacon-chain/core/epoch/precompute/reward_penalty.go b/beacon-chain/core/epoch/precompute/reward_penalty.go index afafa97d86db..aadd2e3c4e66 100644 --- a/beacon-chain/core/epoch/precompute/reward_penalty.go +++ b/beacon-chain/core/epoch/precompute/reward_penalty.go @@ -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") } diff --git a/beacon-chain/state/getters.go b/beacon-chain/state/getters.go index 132da19a471e..2df3f42e8a9f 100644 --- a/beacon-chain/state/getters.go +++ b/beacon-chain/state/getters.go @@ -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 { @@ -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 {