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
|
|
|
)
|
|
|
|
|
|
|
|
// SetStateRoots for the beacon state. Updates the state roots
|
|
|
|
// to a new value by overwriting the previous value.
|
|
|
|
func (b *BeaconState) SetStateRoots(val [][]byte) error {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
if features.Get().EnableExperimentalState {
|
|
|
|
if b.stateRootsMultiValue != nil {
|
|
|
|
b.stateRootsMultiValue.Detach(b)
|
|
|
|
}
|
|
|
|
b.stateRootsMultiValue = NewMultiValueStateRoots(val)
|
|
|
|
} else {
|
|
|
|
b.sharedFieldReferences[types.StateRoots].MinusRef()
|
|
|
|
b.sharedFieldReferences[types.StateRoots] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
rootsArr := make([][32]byte, fieldparams.StateRootsLength)
|
|
|
|
for i := 0; i < len(rootsArr); i++ {
|
|
|
|
copy(rootsArr[i][:], val[i])
|
|
|
|
}
|
|
|
|
b.stateRoots = 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.StateRoots)
|
|
|
|
b.rebuildTrie[types.StateRoots] = true
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateStateRootAtIndex for the beacon state. Updates the state root
|
|
|
|
// at a specific index to a new value.
|
|
|
|
func (b *BeaconState) UpdateStateRootAtIndex(idx uint64, stateRoot [32]byte) error {
|
2023-10-06 01:10:05 +00:00
|
|
|
if features.Get().EnableExperimentalState {
|
|
|
|
if err := b.stateRootsMultiValue.UpdateAt(b, idx, stateRoot); err != nil {
|
|
|
|
return errors.Wrap(err, "could not update state roots")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if uint64(len(b.stateRoots)) <= idx {
|
|
|
|
return errors.Wrapf(consensus_types.ErrOutOfBounds, "state root index %d does not exist", idx)
|
|
|
|
}
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
b.lock.Lock()
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
r := b.stateRoots
|
|
|
|
if ref := b.sharedFieldReferences[types.StateRoots]; ref.Refs() > 1 {
|
|
|
|
// Copy elements in underlying array by reference.
|
|
|
|
r = make([][32]byte, len(b.stateRoots))
|
|
|
|
copy(r, b.stateRoots)
|
|
|
|
ref.MinusRef()
|
|
|
|
b.sharedFieldReferences[types.StateRoots] = stateutil.NewRef(1)
|
|
|
|
}
|
|
|
|
r[idx] = stateRoot
|
|
|
|
b.stateRoots = r
|
|
|
|
|
|
|
|
b.lock.Unlock()
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-06 01:10:05 +00:00
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
b.markFieldAsDirty(types.StateRoots)
|
|
|
|
b.addDirtyIndices(types.StateRoots, []uint64{idx})
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|