2022-05-09 13:02:34 +00:00
|
|
|
package state_native
|
2022-01-13 11:23:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-08-16 12:20:13 +00:00
|
|
|
customtypes "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native/custom-types"
|
|
|
|
nativetypes "github.com/prysmaticlabs/prysm/v3/beacon-chain/state/state-native/types"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/state/stateutil"
|
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/v3/config/fieldparams"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
|
2022-01-13 11:23:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetLatestBlockHeader in the beacon state.
|
|
|
|
func (b *BeaconState) SetLatestBlockHeader(val *ethpb.BeaconBlockHeader) error {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
b.latestBlockHeader = ethpb.CopyBeaconBlockHeader(val)
|
2022-05-09 13:02:34 +00:00
|
|
|
b.markFieldAsDirty(nativetypes.LatestBlockHeader)
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBlockRoots for the beacon state. Updates the entire
|
|
|
|
// list to a new value by overwriting the previous one.
|
|
|
|
func (b *BeaconState) SetBlockRoots(val [][]byte) error {
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2022-05-09 13:02:34 +00:00
|
|
|
b.sharedFieldReferences[nativetypes.BlockRoots].MinusRef()
|
|
|
|
b.sharedFieldReferences[nativetypes.BlockRoots] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
var rootsArr [fieldparams.BlockRootsLength][32]byte
|
|
|
|
for i := 0; i < len(rootsArr); i++ {
|
|
|
|
copy(rootsArr[i][:], val[i])
|
|
|
|
}
|
|
|
|
roots := customtypes.BlockRoots(rootsArr)
|
|
|
|
b.blockRoots = &roots
|
2022-05-09 13:02:34 +00:00
|
|
|
b.markFieldAsDirty(nativetypes.BlockRoots)
|
|
|
|
b.rebuildTrie[nativetypes.BlockRoots] = true
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateBlockRootAtIndex for the beacon state. Updates the block root
|
|
|
|
// at a specific index to a new value.
|
|
|
|
func (b *BeaconState) UpdateBlockRootAtIndex(idx uint64, blockRoot [32]byte) error {
|
2022-01-24 10:24:38 +00:00
|
|
|
if uint64(len(b.blockRoots)) <= idx {
|
2022-01-13 11:23:53 +00:00
|
|
|
return fmt.Errorf("invalid index provided %d", idx)
|
|
|
|
}
|
|
|
|
b.lock.Lock()
|
|
|
|
defer b.lock.Unlock()
|
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
r := b.blockRoots
|
2022-05-09 13:02:34 +00:00
|
|
|
if ref := b.sharedFieldReferences[nativetypes.BlockRoots]; 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.blockRoots
|
|
|
|
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.BlockRoots] = stateutil.NewRef(1)
|
2022-01-13 11:23:53 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:24:38 +00:00
|
|
|
r[idx] = blockRoot
|
|
|
|
b.blockRoots = r
|
2022-01-13 11:23:53 +00:00
|
|
|
|
2022-05-09 13:02:34 +00:00
|
|
|
b.markFieldAsDirty(nativetypes.BlockRoots)
|
|
|
|
b.addDirtyIndices(nativetypes.BlockRoots, []uint64{idx})
|
2022-01-13 11:23:53 +00:00
|
|
|
return nil
|
|
|
|
}
|