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 <thezluf@gmail.com>

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: shayzluf <thezluf@gmail.com>
This commit is contained in:
Preston Van Loon 2020-02-06 08:06:27 -08:00 committed by GitHub
parent 69c3d9dec2
commit a9f1de354b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View File

@ -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,
)
}

View File

@ -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")
}

View File

@ -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 {