2022-05-09 13:02:34 +00:00
|
|
|
package state_native
|
2022-01-13 11:23:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pkg/errors"
|
2022-01-24 10:24:38 +00:00
|
|
|
customtypes "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/custom-types"
|
2022-05-09 13:02:34 +00:00
|
|
|
nativetypes "github.com/prysmaticlabs/prysm/beacon-chain/state/state-native/types"
|
2022-01-13 11:23:53 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
2022-01-24 10:24:38 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
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()
|
|
|
|
|
2022-05-09 13:02:34 +00:00
|
|
|
b.sharedFieldReferences[nativetypes.StateRoots].MinusRef()
|
|
|
|
b.sharedFieldReferences[nativetypes.StateRoots] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
var rootsArr [fieldparams.StateRootsLength][32]byte
|
|
|
|
for i := 0; i < len(rootsArr); i++ {
|
|
|
|
copy(rootsArr[i][:], val[i])
|
|
|
|
}
|
|
|
|
roots := customtypes.StateRoots(rootsArr)
|
|
|
|
b.stateRoots = &roots
|
2022-05-09 13:02:34 +00:00
|
|
|
b.markFieldAsDirty(nativetypes.StateRoots)
|
|
|
|
b.rebuildTrie[nativetypes.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 {
|
|
|
|
b.lock.RLock()
|
2022-01-24 10:24:38 +00:00
|
|
|
if uint64(len(b.stateRoots)) <= idx {
|
2022-01-13 11:23:53 +00:00
|
|
|
b.lock.RUnlock()
|
|
|
|
return errors.Errorf("invalid index provided %d", idx)
|
|
|
|
}
|
|
|
|
b.lock.RUnlock()
|
|
|
|
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
|
|
|
// Check if we hold the only reference to the shared state roots slice.
|
2022-01-24 10:24:38 +00:00
|
|
|
r := b.stateRoots
|
2022-05-09 13:02:34 +00:00
|
|
|
if ref := b.sharedFieldReferences[nativetypes.StateRoots]; ref.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
|
|
|
roots := *b.stateRoots
|
|
|
|
rootsCopy := roots
|
|
|
|
r = &rootsCopy
|
2022-01-13 11:23:53 +00:00
|
|
|
ref.MinusRef()
|
2022-05-09 13:02:34 +00:00
|
|
|
b.sharedFieldReferences[nativetypes.StateRoots] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
r[idx] = stateRoot
|
|
|
|
b.stateRoots = r
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-05-09 13:02:34 +00:00
|
|
|
b.markFieldAsDirty(nativetypes.StateRoots)
|
|
|
|
b.addDirtyIndices(nativetypes.StateRoots, []uint64{idx})
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|