Use multi lock

This commit is contained in:
terence tsao
2021-09-24 21:01:39 -07:00
parent 45f82dffb3
commit 8b6cfc1819
2 changed files with 18 additions and 0 deletions

View File

@@ -36,6 +36,7 @@ go_library(
"//validator:__subpackages__",
],
deps = [
"//async:go_default_library",
"//beacon-chain/cache:go_default_library",
"//beacon-chain/core:go_default_library",
"//beacon-chain/state:go_default_library",

View File

@@ -2,9 +2,11 @@ package helpers
import (
"bytes"
"fmt"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/async"
"github.com/prysmaticlabs/prysm/beacon-chain/core"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
"github.com/prysmaticlabs/prysm/config/params"
@@ -85,6 +87,12 @@ func ActiveValidatorIndices(s state.ReadOnlyBeaconState, epoch types.Epoch) ([]t
if activeIndices != nil {
return activeIndices, nil
}
// Smoothing out committee cache update with a multi lock by seed.
mLock := async.NewMultilock(committeeCacheLockKey(seed))
mLock.Lock()
defer mLock.Unlock()
var indices []types.ValidatorIndex
if err := s.ReadFromEveryValidator(func(idx int, val state.ReadOnlyValidator) error {
if IsActiveValidatorUsingTrie(val, epoch) {
@@ -117,6 +125,11 @@ func ActiveValidatorCount(s state.ReadOnlyBeaconState, epoch types.Epoch) (uint6
return uint64(activeCount), nil
}
// Smoothing out committee cache update with a multi lock by seed.
mLock := async.NewMultilock(committeeCacheLockKey(seed))
mLock.Lock()
defer mLock.Unlock()
count := uint64(0)
if err := s.ReadFromEveryValidator(func(idx int, val state.ReadOnlyValidator) error {
if IsActiveValidatorUsingTrie(val, epoch) {
@@ -363,3 +376,7 @@ func isEligibleForActivation(activationEligibilityEpoch, activationEpoch, finali
return activationEligibilityEpoch <= finalizedEpoch &&
activationEpoch == params.BeaconConfig().FarFutureEpoch
}
func committeeCacheLockKey(seed [32]byte) string {
return fmt.Sprintf("%s-%s", "committee_cache_update", string(seed[:]))
}