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

     1  package v1
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
     6  )
     7  
     8  // SetRandaoMixes for the beacon state. Updates the entire
     9  // randao mixes to a new value by overwriting the previous one.
    10  func (b *BeaconState) SetRandaoMixes(val [][]byte) error {
    11  	if !b.hasInnerState() {
    12  		return ErrNilInnerState
    13  	}
    14  	b.lock.Lock()
    15  	defer b.lock.Unlock()
    16  
    17  	b.sharedFieldReferences[randaoMixes].MinusRef()
    18  	b.sharedFieldReferences[randaoMixes] = stateutil.NewRef(1)
    19  
    20  	b.state.RandaoMixes = val
    21  	b.markFieldAsDirty(randaoMixes)
    22  	b.rebuildTrie[randaoMixes] = true
    23  	return nil
    24  }
    25  
    26  // UpdateRandaoMixesAtIndex for the beacon state. Updates the randao mixes
    27  // at a specific index to a new value.
    28  func (b *BeaconState) UpdateRandaoMixesAtIndex(idx uint64, val []byte) error {
    29  	if !b.hasInnerState() {
    30  		return ErrNilInnerState
    31  	}
    32  	if uint64(len(b.state.RandaoMixes)) <= idx {
    33  		return errors.Errorf("invalid index provided %d", idx)
    34  	}
    35  	b.lock.Lock()
    36  	defer b.lock.Unlock()
    37  
    38  	mixes := b.state.RandaoMixes
    39  	if refs := b.sharedFieldReferences[randaoMixes].Refs(); refs > 1 {
    40  		// Copy elements in underlying array by reference.
    41  		mixes = make([][]byte, len(b.state.RandaoMixes))
    42  		copy(mixes, b.state.RandaoMixes)
    43  		b.sharedFieldReferences[randaoMixes].MinusRef()
    44  		b.sharedFieldReferences[randaoMixes] = stateutil.NewRef(1)
    45  	}
    46  
    47  	mixes[idx] = val
    48  	b.state.RandaoMixes = mixes
    49  	b.markFieldAsDirty(randaoMixes)
    50  	b.addDirtyIndices(randaoMixes, []uint64{idx})
    51  
    52  	return nil
    53  }