mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
BeaconState: Use copy on write for validator index map (#4713)
* Use copy on write for validator index map * Merge refs/heads/master into copy-on-write-2
This commit is contained in:
parent
b7e6012628
commit
f432f7851e
@ -324,6 +324,15 @@ func (b *BeaconState) ValidatorIndexByPubkey(key [48]byte) (uint64, bool) {
|
||||
return idx, ok
|
||||
}
|
||||
|
||||
func (b *BeaconState) validatorIndexMap() map[[48]byte]uint64 {
|
||||
m := make(map[[48]byte]uint64, len(b.valIdxMap))
|
||||
|
||||
for k, v := range b.valIdxMap {
|
||||
m[k] = v
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// PubkeyAtIndex returns the pubkey at the given
|
||||
// validator index.
|
||||
func (b *BeaconState) PubkeyAtIndex(idx uint64) [48]byte {
|
||||
|
@ -228,10 +228,10 @@ func (b *BeaconState) UpdateValidatorAtIndex(idx uint64, val *ethpb.Validator) e
|
||||
// SetValidatorIndexByPubkey updates the validator index mapping maintained internally to
|
||||
// a given input 48-byte, public key.
|
||||
func (b *BeaconState) SetValidatorIndexByPubkey(pubKey [48]byte, validatorIdx uint64) {
|
||||
b.lock.Lock()
|
||||
b.valIdxMap[pubKey] = validatorIdx
|
||||
b.markFieldAsDirty(validators)
|
||||
b.lock.Unlock()
|
||||
// Copy on write since this is a shared map.
|
||||
m := b.validatorIndexMap()
|
||||
m[pubKey] = validatorIdx
|
||||
b.valIdxMap = m
|
||||
}
|
||||
|
||||
// SetBalances for the beacon state. This PR updates the entire
|
||||
|
@ -84,17 +84,15 @@ func (b *BeaconState) Copy() *BeaconState {
|
||||
FinalizedCheckpoint: b.FinalizedCheckpoint(),
|
||||
},
|
||||
dirtyFields: make(map[fieldIndex]interface{}, 20),
|
||||
valIdxMap: make(map[[48]byte]uint64, len(b.valIdxMap)),
|
||||
|
||||
// Copy on write validator index map.
|
||||
valIdxMap: b.valIdxMap,
|
||||
}
|
||||
|
||||
for i := range b.dirtyFields {
|
||||
dst.dirtyFields[i] = true
|
||||
}
|
||||
|
||||
for i := range b.valIdxMap {
|
||||
dst.valIdxMap[i] = b.valIdxMap[i]
|
||||
}
|
||||
|
||||
dst.merkleLayers = make([][][]byte, len(b.merkleLayers))
|
||||
for i, layer := range b.merkleLayers {
|
||||
dst.merkleLayers[i] = make([][]byte, len(layer))
|
||||
|
Loading…
Reference in New Issue
Block a user