mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-15 06:28:20 +00:00
21bdbd548a
* v0 * getters/setters * init and copy * hasher * all the nice stuff * make bazel happy * remove tests for smaller PR * remove old states * move files * import fixes * custom MarshalSSZ * fixed deadlock * copy version when copying state * correct issues in state_trie * fix Copy() * better e2e comment * add code to minimal state * spectest test * Revert "Auxiliary commit to revert individual files from 84154423464e8372f7e0a03367403656ac5cd78e" This reverts commit 9602599d183081291dfa0ba4f1036430f63a7822. * native state assert * always error * always log * more native state usage * cleanup * remove empty line * Revert "spectests" This reverts commit 1c49bed5d1cf6224afaf21e18562bf72fae5d2b6. # Conflicts: # beacon-chain/powchain/service.go # beacon-chain/state/v1/state_trie.go # beacon-chain/state/v2/state_trie.go # beacon-chain/state/v3/state_trie.go # testing/spectest/shared/phase0/finality/BUILD.bazel # testing/spectest/shared/phase0/finality/runner.go * dedup field trie * fix test issues * cleanup * use correct field num in FinalizedRootProof * use existing version constant * halfway there * "working" version * some fixes * fix field nums in tests * rename v0types to nativetypes * Revert "Auxiliary commit to revert individual files from dc549b1cf8e724bd08cee1ecc760ff3771d5592d" This reverts commit 7254d3070d8693b283fc686a2e01a822ecbac1b3. * uncomment code * remove map size * Revert "Revert "spectests"" This reverts commit 39c271ae6b57d6a3737e2c202cd8407857475e56. * use reverse map * Revert "Revert "Revert "spectests""" This reverts commit 19ba8cf95cdca689357c8234a262e08cccbafef4. * finally found the bug (cherry picked from commit a5414c4be1bdb61a50b391ea5301895e772cc5e9) * simplify populateFieldIndexes * fix copy (cherry picked from commit 7da4fb8cf51557ef931bb781872ea52fc6731af5) * remove native state from e2e * remove index map * unsupported functions * Use ProtobufBeaconState() from native state * tests * typo * reduce complexity of `SaveStatesEfficient` * remove unused receiver name * update doc.go * fix test assertion * fix test assertion 2 * Phase0 justification bits * bring back state tests * rename fieldIndexRev * versioning of ToProto * remove version check from unexported function * hasher tests * don't return error from JustificationBits * extract fieldConvertersNative * helper error function * use fieldConvertersNative * Introduce RealPosition method on FieldIndex * use RealPosition in hasher * remove unused fields * remove TestAppendBeyondIndicesLimit (cherry picked from commit 3017e700282969c30006b64c95c21ffe6b166f8b) * simplify RealPosition * rename field interface * use helper in proofs.go * Update beacon-chain/core/altair/upgrade.go Co-authored-by: Nishant Das <nishdas93@gmail.com> Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
243 lines
7.2 KiB
Go
243 lines
7.2 KiB
Go
package state_native
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
nativetypes "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/types"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
types "github.com/prysmaticlabs/prysm/consensus-types/primitives"
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/runtime/version"
|
|
)
|
|
|
|
// SetValidators for the beacon state. Updates the entire
|
|
// to a new value by overwriting the previous one.
|
|
func (b *BeaconState) SetValidators(val []*ethpb.Validator) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
b.validators = val
|
|
b.sharedFieldReferences[nativetypes.Validators].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Validators] = stateutil.NewRef(1)
|
|
b.markFieldAsDirty(nativetypes.Validators)
|
|
b.rebuildTrie[nativetypes.Validators] = true
|
|
b.valMapHandler = stateutil.NewValMapHandler(b.validators)
|
|
return nil
|
|
}
|
|
|
|
// ApplyToEveryValidator applies the provided callback function to each validator in the
|
|
// validator registry.
|
|
func (b *BeaconState) ApplyToEveryValidator(f func(idx int, val *ethpb.Validator) (bool, *ethpb.Validator, error)) error {
|
|
b.lock.Lock()
|
|
v := b.validators
|
|
if ref := b.sharedFieldReferences[nativetypes.Validators]; ref.Refs() > 1 {
|
|
v = b.validatorsReferences()
|
|
ref.MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Validators] = stateutil.NewRef(1)
|
|
}
|
|
b.lock.Unlock()
|
|
var changedVals []uint64
|
|
for i, val := range v {
|
|
changed, newVal, err := f(i, val)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if changed {
|
|
changedVals = append(changedVals, uint64(i))
|
|
v[i] = newVal
|
|
}
|
|
}
|
|
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
b.validators = v
|
|
b.markFieldAsDirty(nativetypes.Validators)
|
|
b.addDirtyIndices(nativetypes.Validators, changedVals)
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateValidatorAtIndex for the beacon state. Updates the validator
|
|
// at a specific index to a new value.
|
|
func (b *BeaconState) UpdateValidatorAtIndex(idx types.ValidatorIndex, val *ethpb.Validator) error {
|
|
if uint64(len(b.validators)) <= uint64(idx) {
|
|
return errors.Errorf("invalid index provided %d", idx)
|
|
}
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
v := b.validators
|
|
if ref := b.sharedFieldReferences[nativetypes.Validators]; ref.Refs() > 1 {
|
|
v = b.validatorsReferences()
|
|
ref.MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Validators] = stateutil.NewRef(1)
|
|
}
|
|
|
|
v[idx] = val
|
|
b.validators = v
|
|
b.markFieldAsDirty(nativetypes.Validators)
|
|
b.addDirtyIndices(nativetypes.Validators, []uint64{uint64(idx)})
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetBalances for the beacon state. Updates the entire
|
|
// list to a new value by overwriting the previous one.
|
|
func (b *BeaconState) SetBalances(val []uint64) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
b.sharedFieldReferences[nativetypes.Balances].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Balances] = stateutil.NewRef(1)
|
|
|
|
b.balances = val
|
|
b.markFieldAsDirty(nativetypes.Balances)
|
|
b.rebuildTrie[nativetypes.Balances] = true
|
|
return nil
|
|
}
|
|
|
|
// UpdateBalancesAtIndex for the beacon state. This method updates the balance
|
|
// at a specific index to a new value.
|
|
func (b *BeaconState) UpdateBalancesAtIndex(idx types.ValidatorIndex, val uint64) error {
|
|
if uint64(len(b.balances)) <= uint64(idx) {
|
|
return errors.Errorf("invalid index provided %d", idx)
|
|
}
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
bals := b.balances
|
|
if b.sharedFieldReferences[nativetypes.Balances].Refs() > 1 {
|
|
bals = b.balancesVal()
|
|
b.sharedFieldReferences[nativetypes.Balances].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Balances] = stateutil.NewRef(1)
|
|
}
|
|
|
|
bals[idx] = val
|
|
b.balances = bals
|
|
b.markFieldAsDirty(nativetypes.Balances)
|
|
b.addDirtyIndices(nativetypes.Balances, []uint64{uint64(idx)})
|
|
return nil
|
|
}
|
|
|
|
// SetSlashings for the beacon state. Updates the entire
|
|
// list to a new value by overwriting the previous one.
|
|
func (b *BeaconState) SetSlashings(val []uint64) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
b.sharedFieldReferences[nativetypes.Slashings].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Slashings] = stateutil.NewRef(1)
|
|
|
|
b.slashings = val
|
|
b.markFieldAsDirty(nativetypes.Slashings)
|
|
return nil
|
|
}
|
|
|
|
// UpdateSlashingsAtIndex for the beacon state. Updates the slashings
|
|
// at a specific index to a new value.
|
|
func (b *BeaconState) UpdateSlashingsAtIndex(idx, val uint64) error {
|
|
if uint64(len(b.slashings)) <= idx {
|
|
return errors.Errorf("invalid index provided %d", idx)
|
|
}
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
s := b.slashings
|
|
if b.sharedFieldReferences[nativetypes.Slashings].Refs() > 1 {
|
|
s = b.slashingsVal()
|
|
b.sharedFieldReferences[nativetypes.Slashings].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Slashings] = stateutil.NewRef(1)
|
|
}
|
|
|
|
s[idx] = val
|
|
|
|
b.slashings = s
|
|
|
|
b.markFieldAsDirty(nativetypes.Slashings)
|
|
return nil
|
|
}
|
|
|
|
// AppendValidator for the beacon state. Appends the new value
|
|
// to the the end of list.
|
|
func (b *BeaconState) AppendValidator(val *ethpb.Validator) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
vals := b.validators
|
|
if b.sharedFieldReferences[nativetypes.Validators].Refs() > 1 {
|
|
vals = b.validatorsReferences()
|
|
b.sharedFieldReferences[nativetypes.Validators].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Validators] = stateutil.NewRef(1)
|
|
}
|
|
|
|
// append validator to slice
|
|
b.validators = append(vals, val)
|
|
valIdx := types.ValidatorIndex(len(b.validators) - 1)
|
|
|
|
b.valMapHandler.Set(bytesutil.ToBytes48(val.PublicKey), valIdx)
|
|
|
|
b.markFieldAsDirty(nativetypes.Validators)
|
|
b.addDirtyIndices(nativetypes.Validators, []uint64{uint64(valIdx)})
|
|
return nil
|
|
}
|
|
|
|
// AppendBalance for the beacon state. Appends the new value
|
|
// to the the end of list.
|
|
func (b *BeaconState) AppendBalance(bal uint64) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
bals := b.balances
|
|
if b.sharedFieldReferences[nativetypes.Balances].Refs() > 1 {
|
|
bals = b.balancesVal()
|
|
b.sharedFieldReferences[nativetypes.Balances].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.Balances] = stateutil.NewRef(1)
|
|
}
|
|
|
|
b.balances = append(bals, bal)
|
|
balIdx := len(b.balances) - 1
|
|
b.markFieldAsDirty(nativetypes.Balances)
|
|
b.addDirtyIndices(nativetypes.Balances, []uint64{uint64(balIdx)})
|
|
return nil
|
|
}
|
|
|
|
// AppendInactivityScore for the beacon state.
|
|
func (b *BeaconState) AppendInactivityScore(s uint64) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
if b.version == version.Phase0 {
|
|
return errNotSupported("AppendInactivityScore", b.version)
|
|
}
|
|
|
|
scores := b.inactivityScores
|
|
if b.sharedFieldReferences[nativetypes.InactivityScores].Refs() > 1 {
|
|
scores = b.inactivityScoresVal()
|
|
b.sharedFieldReferences[nativetypes.InactivityScores].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.InactivityScores] = stateutil.NewRef(1)
|
|
}
|
|
|
|
b.inactivityScores = append(scores, s)
|
|
b.markFieldAsDirty(nativetypes.InactivityScores)
|
|
return nil
|
|
}
|
|
|
|
// SetInactivityScores for the beacon state. Updates the entire
|
|
// list to a new value by overwriting the previous one.
|
|
func (b *BeaconState) SetInactivityScores(val []uint64) error {
|
|
b.lock.Lock()
|
|
defer b.lock.Unlock()
|
|
|
|
if b.version == version.Phase0 {
|
|
return errNotSupported("SetInactivityScores", b.version)
|
|
}
|
|
|
|
b.sharedFieldReferences[nativetypes.InactivityScores].MinusRef()
|
|
b.sharedFieldReferences[nativetypes.InactivityScores] = stateutil.NewRef(1)
|
|
|
|
b.inactivityScores = val
|
|
b.markFieldAsDirty(nativetypes.InactivityScores)
|
|
return nil
|
|
}
|