github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/v1/setters_validator.go (about)

     1  package v1
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	types "github.com/prysmaticlabs/eth2-types"
     6  	"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
     7  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     8  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     9  )
    10  
    11  // SetValidators for the beacon state. Updates the entire
    12  // to a new value by overwriting the previous one.
    13  func (b *BeaconState) SetValidators(val []*ethpb.Validator) error {
    14  	if !b.hasInnerState() {
    15  		return ErrNilInnerState
    16  	}
    17  	b.lock.Lock()
    18  	defer b.lock.Unlock()
    19  
    20  	b.state.Validators = val
    21  	b.sharedFieldReferences[validators].MinusRef()
    22  	b.sharedFieldReferences[validators] = stateutil.NewRef(1)
    23  	b.markFieldAsDirty(validators)
    24  	b.rebuildTrie[validators] = true
    25  	b.valMapHandler = stateutil.NewValMapHandler(b.state.Validators)
    26  	return nil
    27  }
    28  
    29  // ApplyToEveryValidator applies the provided callback function to each validator in the
    30  // validator registry.
    31  func (b *BeaconState) ApplyToEveryValidator(f func(idx int, val *ethpb.Validator) (bool, *ethpb.Validator, error)) error {
    32  	if !b.hasInnerState() {
    33  		return ErrNilInnerState
    34  	}
    35  	b.lock.Lock()
    36  	v := b.state.Validators
    37  	if ref := b.sharedFieldReferences[validators]; ref.Refs() > 1 {
    38  		v = b.validatorsReferences()
    39  		ref.MinusRef()
    40  		b.sharedFieldReferences[validators] = stateutil.NewRef(1)
    41  	}
    42  	b.lock.Unlock()
    43  	var changedVals []uint64
    44  	for i, val := range v {
    45  		changed, newVal, err := f(i, val)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		if changed {
    50  			changedVals = append(changedVals, uint64(i))
    51  			v[i] = newVal
    52  		}
    53  	}
    54  
    55  	b.lock.Lock()
    56  	defer b.lock.Unlock()
    57  
    58  	b.state.Validators = v
    59  	b.markFieldAsDirty(validators)
    60  	b.addDirtyIndices(validators, changedVals)
    61  
    62  	return nil
    63  }
    64  
    65  // UpdateValidatorAtIndex for the beacon state. Updates the validator
    66  // at a specific index to a new value.
    67  func (b *BeaconState) UpdateValidatorAtIndex(idx types.ValidatorIndex, val *ethpb.Validator) error {
    68  	if !b.hasInnerState() {
    69  		return ErrNilInnerState
    70  	}
    71  	if uint64(len(b.state.Validators)) <= uint64(idx) {
    72  		return errors.Errorf("invalid index provided %d", idx)
    73  	}
    74  	b.lock.Lock()
    75  	defer b.lock.Unlock()
    76  
    77  	v := b.state.Validators
    78  	if ref := b.sharedFieldReferences[validators]; ref.Refs() > 1 {
    79  		v = b.validatorsReferences()
    80  		ref.MinusRef()
    81  		b.sharedFieldReferences[validators] = stateutil.NewRef(1)
    82  	}
    83  
    84  	v[idx] = val
    85  	b.state.Validators = v
    86  	b.markFieldAsDirty(validators)
    87  	b.addDirtyIndices(validators, []uint64{uint64(idx)})
    88  
    89  	return nil
    90  }
    91  
    92  // SetBalances for the beacon state. Updates the entire
    93  // list to a new value by overwriting the previous one.
    94  func (b *BeaconState) SetBalances(val []uint64) error {
    95  	if !b.hasInnerState() {
    96  		return ErrNilInnerState
    97  	}
    98  	b.lock.Lock()
    99  	defer b.lock.Unlock()
   100  
   101  	b.sharedFieldReferences[balances].MinusRef()
   102  	b.sharedFieldReferences[balances] = stateutil.NewRef(1)
   103  
   104  	b.state.Balances = val
   105  	b.markFieldAsDirty(balances)
   106  	return nil
   107  }
   108  
   109  // UpdateBalancesAtIndex for the beacon state. This method updates the balance
   110  // at a specific index to a new value.
   111  func (b *BeaconState) UpdateBalancesAtIndex(idx types.ValidatorIndex, val uint64) error {
   112  	if !b.hasInnerState() {
   113  		return ErrNilInnerState
   114  	}
   115  	if uint64(len(b.state.Balances)) <= uint64(idx) {
   116  		return errors.Errorf("invalid index provided %d", idx)
   117  	}
   118  	b.lock.Lock()
   119  	defer b.lock.Unlock()
   120  
   121  	bals := b.state.Balances
   122  	if b.sharedFieldReferences[balances].Refs() > 1 {
   123  		bals = b.balances()
   124  		b.sharedFieldReferences[balances].MinusRef()
   125  		b.sharedFieldReferences[balances] = stateutil.NewRef(1)
   126  	}
   127  
   128  	bals[idx] = val
   129  	b.state.Balances = bals
   130  	b.markFieldAsDirty(balances)
   131  	return nil
   132  }
   133  
   134  // SetSlashings for the beacon state. Updates the entire
   135  // list to a new value by overwriting the previous one.
   136  func (b *BeaconState) SetSlashings(val []uint64) error {
   137  	if !b.hasInnerState() {
   138  		return ErrNilInnerState
   139  	}
   140  	b.lock.Lock()
   141  	defer b.lock.Unlock()
   142  
   143  	b.sharedFieldReferences[slashings].MinusRef()
   144  	b.sharedFieldReferences[slashings] = stateutil.NewRef(1)
   145  
   146  	b.state.Slashings = val
   147  	b.markFieldAsDirty(slashings)
   148  	return nil
   149  }
   150  
   151  // UpdateSlashingsAtIndex for the beacon state. Updates the slashings
   152  // at a specific index to a new value.
   153  func (b *BeaconState) UpdateSlashingsAtIndex(idx, val uint64) error {
   154  	if !b.hasInnerState() {
   155  		return ErrNilInnerState
   156  	}
   157  	if uint64(len(b.state.Slashings)) <= idx {
   158  		return errors.Errorf("invalid index provided %d", idx)
   159  	}
   160  	b.lock.Lock()
   161  	defer b.lock.Unlock()
   162  
   163  	s := b.state.Slashings
   164  	if b.sharedFieldReferences[slashings].Refs() > 1 {
   165  		s = b.slashings()
   166  		b.sharedFieldReferences[slashings].MinusRef()
   167  		b.sharedFieldReferences[slashings] = stateutil.NewRef(1)
   168  	}
   169  
   170  	s[idx] = val
   171  
   172  	b.state.Slashings = s
   173  
   174  	b.markFieldAsDirty(slashings)
   175  	return nil
   176  }
   177  
   178  // AppendValidator for the beacon state. Appends the new value
   179  // to the the end of list.
   180  func (b *BeaconState) AppendValidator(val *ethpb.Validator) error {
   181  	if !b.hasInnerState() {
   182  		return ErrNilInnerState
   183  	}
   184  	b.lock.Lock()
   185  	defer b.lock.Unlock()
   186  
   187  	vals := b.state.Validators
   188  	if b.sharedFieldReferences[validators].Refs() > 1 {
   189  		vals = b.validatorsReferences()
   190  		b.sharedFieldReferences[validators].MinusRef()
   191  		b.sharedFieldReferences[validators] = stateutil.NewRef(1)
   192  	}
   193  
   194  	// append validator to slice
   195  	b.state.Validators = append(vals, val)
   196  	valIdx := types.ValidatorIndex(len(b.state.Validators) - 1)
   197  
   198  	b.valMapHandler.Set(bytesutil.ToBytes48(val.PublicKey), valIdx)
   199  
   200  	b.markFieldAsDirty(validators)
   201  	b.addDirtyIndices(validators, []uint64{uint64(valIdx)})
   202  	return nil
   203  }
   204  
   205  // AppendBalance for the beacon state. Appends the new value
   206  // to the the end of list.
   207  func (b *BeaconState) AppendBalance(bal uint64) error {
   208  	if !b.hasInnerState() {
   209  		return ErrNilInnerState
   210  	}
   211  	b.lock.Lock()
   212  	defer b.lock.Unlock()
   213  
   214  	bals := b.state.Balances
   215  	if b.sharedFieldReferences[balances].Refs() > 1 {
   216  		bals = b.balances()
   217  		b.sharedFieldReferences[balances].MinusRef()
   218  		b.sharedFieldReferences[balances] = stateutil.NewRef(1)
   219  	}
   220  
   221  	b.state.Balances = append(bals, bal)
   222  	b.markFieldAsDirty(balances)
   223  	return nil
   224  }