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
|
|
|
customtypes "github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native/custom-types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/beacon-chain/state/stateutil"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v4/config/fieldparams"
|
|
|
|
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
|
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-01-26 14:40:12 +00:00
|
|
|
b.sharedFieldReferences[types.RandaoMixes].MinusRef()
|
|
|
|
b.sharedFieldReferences[types.RandaoMixes] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
var mixesArr [fieldparams.RandaoMixesLength][32]byte
|
|
|
|
for i := 0; i < len(mixesArr); i++ {
|
|
|
|
copy(mixesArr[i][:], val[i])
|
|
|
|
}
|
|
|
|
mixes := customtypes.RandaoMixes(mixesArr)
|
|
|
|
b.randaoMixes = &mixes
|
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.
|
|
|
|
func (b *BeaconState) UpdateRandaoMixesAtIndex(idx uint64, val []byte) error {
|
2022-01-24 10:24:38 +00:00
|
|
|
if uint64(len(b.randaoMixes)) <= idx {
|
2022-01-13 11:23:53 +00:00
|
|
|
return errors.Errorf("invalid index provided %d", idx)
|
|
|
|
}
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
mixes := b.randaoMixes
|
2023-01-26 14:40:12 +00:00
|
|
|
if refs := b.sharedFieldReferences[types.RandaoMixes].Refs(); refs > 1 {
|
2022-01-13 11:23:53 +00:00
|
|
|
// Copy elements in underlying array by reference.
|
2022-01-24 10:24:38 +00:00
|
|
|
m := *b.randaoMixes
|
|
|
|
mCopy := m
|
|
|
|
mixes = &mCopy
|
2023-01-26 14:40:12 +00:00
|
|
|
b.sharedFieldReferences[types.RandaoMixes].MinusRef()
|
|
|
|
b.sharedFieldReferences[types.RandaoMixes] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
mixes[idx] = bytesutil.ToBytes32(val)
|
|
|
|
b.randaoMixes = mixes
|
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
|
|
|
|
}
|