2021-03-11 02:57:46 +00:00
|
|
|
package stateutil
|
|
|
|
|
|
|
|
import (
|
2021-06-21 08:33:06 +00:00
|
|
|
"sync"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
coreutils "github.com/prysmaticlabs/prysm/v3/beacon-chain/core/transition/stateutils"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
|
|
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2021-03-11 02:57:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ValidatorMapHandler is a container to hold the map and a reference tracker for how many
|
|
|
|
// states shared this.
|
|
|
|
type ValidatorMapHandler struct {
|
2022-01-06 17:33:08 +00:00
|
|
|
valIdxMap map[[fieldparams.BLSPubkeyLength]byte]types.ValidatorIndex
|
2021-03-11 02:57:46 +00:00
|
|
|
mapRef *Reference
|
2021-06-21 08:33:06 +00:00
|
|
|
*sync.RWMutex
|
2021-03-11 02:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewValMapHandler returns a new validator map handler.
|
|
|
|
func NewValMapHandler(vals []*ethpb.Validator) *ValidatorMapHandler {
|
|
|
|
return &ValidatorMapHandler{
|
|
|
|
valIdxMap: coreutils.ValidatorIndexMap(vals),
|
|
|
|
mapRef: &Reference{refs: 1},
|
2021-06-21 08:33:06 +00:00
|
|
|
RWMutex: new(sync.RWMutex),
|
2021-03-11 02:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 12:06:05 +00:00
|
|
|
// AddRef copies the whole map and returns a map handler with the copied map.
|
2021-03-11 02:57:46 +00:00
|
|
|
func (v *ValidatorMapHandler) AddRef() {
|
|
|
|
v.mapRef.AddRef()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsNil returns true if the underlying validator index map is nil.
|
|
|
|
func (v *ValidatorMapHandler) IsNil() bool {
|
2021-06-21 08:33:06 +00:00
|
|
|
return v.mapRef == nil || v.valIdxMap == nil
|
2021-03-11 02:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the whole map and returns a map handler with the copied map.
|
|
|
|
func (v *ValidatorMapHandler) Copy() *ValidatorMapHandler {
|
|
|
|
if v == nil || v.valIdxMap == nil {
|
2022-01-06 17:33:08 +00:00
|
|
|
return &ValidatorMapHandler{valIdxMap: map[[fieldparams.BLSPubkeyLength]byte]types.ValidatorIndex{}, mapRef: new(Reference), RWMutex: new(sync.RWMutex)}
|
2021-03-11 02:57:46 +00:00
|
|
|
}
|
2021-06-21 08:33:06 +00:00
|
|
|
v.RLock()
|
|
|
|
defer v.RUnlock()
|
2022-01-06 17:33:08 +00:00
|
|
|
m := make(map[[fieldparams.BLSPubkeyLength]byte]types.ValidatorIndex, len(v.valIdxMap))
|
2021-03-11 02:57:46 +00:00
|
|
|
for k, v := range v.valIdxMap {
|
|
|
|
m[k] = v
|
|
|
|
}
|
|
|
|
return &ValidatorMapHandler{
|
|
|
|
valIdxMap: m,
|
|
|
|
mapRef: &Reference{refs: 1},
|
2021-06-21 08:33:06 +00:00
|
|
|
RWMutex: new(sync.RWMutex),
|
2021-03-11 02:57:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the validator index using the corresponding public key.
|
2022-01-06 17:33:08 +00:00
|
|
|
func (v *ValidatorMapHandler) Get(key [fieldparams.BLSPubkeyLength]byte) (types.ValidatorIndex, bool) {
|
2021-06-21 08:33:06 +00:00
|
|
|
v.RLock()
|
|
|
|
defer v.RUnlock()
|
2021-03-11 02:57:46 +00:00
|
|
|
idx, ok := v.valIdxMap[key]
|
|
|
|
if !ok {
|
|
|
|
return 0, false
|
|
|
|
}
|
|
|
|
return idx, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the validator index using the corresponding public key.
|
2022-01-06 17:33:08 +00:00
|
|
|
func (v *ValidatorMapHandler) Set(key [fieldparams.BLSPubkeyLength]byte, index types.ValidatorIndex) {
|
2021-06-21 08:33:06 +00:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
2021-03-11 02:57:46 +00:00
|
|
|
v.valIdxMap[key] = index
|
|
|
|
}
|