2022-05-09 13:02:34 +00:00
|
|
|
package state_native
|
2022-01-13 11:23:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2023-03-17 18:52:56 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stateutil"
|
2023-10-06 01:10:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v4/config/features"
|
2023-03-17 18:52:56 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
2023-10-06 01:10:05 +00:00
|
|
|
consensus_types "github.com/prysmaticlabs/prysm/v4/consensus-types"
|
2022-01-13 11:23:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetRandaoMixes for the beacon state. Updates the entire
|
|
|
|
// randao mixes to a new value by overwriting the previous one.
|
|
|
|
func (b *BeaconState) SetRandaoMixes(val [][]byte) error {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
if features.Get().EnableExperimentalState {
|
|
|
|
if b.randaoMixesMultiValue != nil {
|
|
|
|
b.randaoMixesMultiValue.Detach(b)
|
|
|
|
}
|
|
|
|
b.randaoMixesMultiValue = NewMultiValueRandaoMixes(val)
|
|
|
|
} else {
|
|
|
|
b.sharedFieldReferences[types.RandaoMixes].MinusRef()
|
|
|
|
b.sharedFieldReferences[types.RandaoMixes] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
rootsArr := make([][32]byte, fieldparams.RandaoMixesLength)
|
|
|
|
for i := 0; i < len(rootsArr); i++ {
|
|
|
|
copy(rootsArr[i][:], val[i])
|
|
|
|
}
|
|
|
|
b.randaoMixes = rootsArr
|
2022-01-24 10:24:38 +00:00
|
|
|
}
|
2023-10-06 01:10:05 +00:00
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
b.markFieldAsDirty(types.RandaoMixes)
|
|
|
|
b.rebuildTrie[types.RandaoMixes] = true
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRandaoMixesAtIndex for the beacon state. Updates the randao mixes
|
|
|
|
// at a specific index to a new value.
|
2023-10-06 01:10:05 +00:00
|
|
|
func (b *BeaconState) UpdateRandaoMixesAtIndex(idx uint64, val [32]byte) error {
|
|
|
|
if features.Get().EnableExperimentalState {
|
|
|
|
if err := b.randaoMixesMultiValue.UpdateAt(b, idx, val); err != nil {
|
|
|
|
return errors.Wrap(err, "could not update randao mixes")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if uint64(len(b.randaoMixes)) <= idx {
|
|
|
|
return errors.Wrapf(consensus_types.ErrOutOfBounds, "randao mix index %d does not exist", idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.lock.Lock()
|
|
|
|
|
|
|
|
m := b.randaoMixes
|
|
|
|
if ref := b.sharedFieldReferences[types.RandaoMixes]; ref.Refs() > 1 {
|
|
|
|
// Copy elements in underlying array by reference.
|
|
|
|
m = make([][32]byte, len(b.randaoMixes))
|
|
|
|
copy(m, b.randaoMixes)
|
|
|
|
ref.MinusRef()
|
|
|
|
b.sharedFieldReferences[types.RandaoMixes] = stateutil.NewRef(1)
|
|
|
|
}
|
|
|
|
m[idx] = val
|
|
|
|
b.randaoMixes = m
|
|
|
|
|
|
|
|
b.lock.Unlock()
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
2023-10-06 01:10:05 +00:00
|
|
|
|
2022-01-13 11:23:53 +00:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
b.markFieldAsDirty(types.RandaoMixes)
|
|
|
|
b.addDirtyIndices(types.RandaoMixes, []uint64{idx})
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|