Files
prysm/beacon-chain/state/fieldtrie/field_trie_helpers.go
Preston Van Loon 2fd6bd8150 Add golang.org/x/tools modernize static analyzer and fix violations (#15946)
* Ran gopls modernize to fix everything

go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...

* Override rules_go provided dependency for golang.org/x/tools to v0.38.0.

To update this, checked out rules_go, then ran `bazel run //go/tools/releaser -- upgrade-dep -mirror=false org_golang_x_tools` and copied the patches.

* Fix buildtag violations and ignore buildtag violations in external

* Introduce modernize analyzer package.

* Add modernize "any" analyzer.

* Fix violations of any analyzer

* Add modernize "appendclipped" analyzer.

* Fix violations of appendclipped

* Add modernize "bloop" analyzer.

* Add modernize "fmtappendf" analyzer.

* Add modernize "forvar" analyzer.

* Add modernize "mapsloop" analyzer.

* Add modernize "minmax" analyzer.

* Fix violations of minmax analyzer

* Add modernize "omitzero" analyzer.

* Add modernize "rangeint" analyzer.

* Fix violations of rangeint.

* Add modernize "reflecttypefor" analyzer.

* Fix violations of reflecttypefor analyzer.

* Add modernize "slicescontains" analyzer.

* Add modernize "slicessort" analyzer.

* Add modernize "slicesdelete" analyzer. This is disabled by default for now. See https://go.dev/issue/73686.

* Add modernize "stringscutprefix" analyzer.

* Add modernize "stringsbuilder" analyzer.

* Fix violations of stringsbuilder analyzer.

* Add modernize "stringsseq" analyzer.

* Add modernize "testingcontext" analyzer.

* Add modernize "waitgroup" analyzer.

* Changelog fragment

* gofmt

* gazelle

* Add modernize "newexpr" analyzer.

* Disable newexpr until go1.26

* Add more details in WORKSPACE on how to update the override

* @nalepae feedback on min()

* gofmt

* Fix violations of forvar
2025-11-14 01:27:22 +00:00

316 lines
9.9 KiB
Go

package fieldtrie
import (
"encoding/binary"
"fmt"
"reflect"
customtypes "github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native/custom-types"
"github.com/OffchainLabs/prysm/v7/beacon-chain/state/state-native/types"
"github.com/OffchainLabs/prysm/v7/beacon-chain/state/stateutil"
multi_value_slice "github.com/OffchainLabs/prysm/v7/container/multi-value-slice"
pmath "github.com/OffchainLabs/prysm/v7/math"
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
"github.com/pkg/errors"
)
func (f *FieldTrie) validateIndices(idxs []uint64) error {
length := f.length
if f.dataType == types.CompressedArray {
comLength, err := f.field.ElemsInChunk()
if err != nil {
return err
}
length *= comLength
}
for _, idx := range idxs {
if idx >= length {
return errors.Errorf("invalid index for field %s: %d >= length %d", f.field.String(), idx, length)
}
}
return nil
}
func validateElements(field types.FieldIndex, fieldInfo types.DataType, elements any, length uint64) error {
if fieldInfo == types.CompressedArray {
comLength, err := field.ElemsInChunk()
if err != nil {
return err
}
length *= comLength
}
if val, ok := elements.(sliceAccessor); ok {
totalLen := val.Len(val.State())
if uint64(totalLen) > length {
return errors.Errorf("elements length is larger than expected for field %s: %d > %d", field.String(), totalLen, length)
}
return nil
}
val := reflect.Indirect(reflect.ValueOf(elements))
if uint64(val.Len()) > length {
return errors.Errorf("elements length is larger than expected for field %s: %d > %d", field.String(), val.Len(), length)
}
return nil
}
// fieldConverters converts the corresponding field and the provided elements to the appropriate roots.
func fieldConverters(field types.FieldIndex, indices []uint64, elements any, convertAll bool) ([][32]byte, error) {
switch field {
case types.BlockRoots, types.StateRoots, types.RandaoMixes:
return convertRoots(indices, elements, convertAll)
case types.Eth1DataVotes:
return convertEth1DataVotes(indices, elements, convertAll)
case types.Validators:
return convertValidators(indices, elements, convertAll)
case types.PreviousEpochAttestations, types.CurrentEpochAttestations:
return convertAttestations(indices, elements, convertAll)
case types.Balances:
return convertBalances(indices, elements, convertAll)
default:
return [][32]byte{}, errors.Errorf("got unsupported type of %v", reflect.TypeOf(elements).Name())
}
}
func convertRoots(indices []uint64, elements any, convertAll bool) ([][32]byte, error) {
switch castedType := elements.(type) {
case customtypes.BlockRoots:
return handle32ByteMVslice(multi_value_slice.BuildEmptyCompositeSlice[[32]byte](castedType), indices, convertAll)
case customtypes.StateRoots:
return handle32ByteMVslice(multi_value_slice.BuildEmptyCompositeSlice[[32]byte](castedType), indices, convertAll)
case customtypes.RandaoMixes:
return handle32ByteMVslice(multi_value_slice.BuildEmptyCompositeSlice[[32]byte](castedType), indices, convertAll)
case multi_value_slice.MultiValueSliceComposite[[32]byte]:
return handle32ByteMVslice(castedType, indices, convertAll)
default:
return nil, errors.Errorf("non-existent type provided %T", castedType)
}
}
func convertEth1DataVotes(indices []uint64, elements any, convertAll bool) ([][32]byte, error) {
val, ok := elements.([]*ethpb.Eth1Data)
if !ok {
return nil, errors.Errorf("Wanted type of %T but got %T", []*ethpb.Eth1Data{}, elements)
}
return handleEth1DataSlice(val, indices, convertAll)
}
func convertValidators(indices []uint64, elements any, convertAll bool) ([][32]byte, error) {
switch casted := elements.(type) {
case []*ethpb.Validator:
return handleValidatorMVSlice(multi_value_slice.BuildEmptyCompositeSlice[*ethpb.Validator](casted), indices, convertAll)
case multi_value_slice.MultiValueSliceComposite[*ethpb.Validator]:
return handleValidatorMVSlice(casted, indices, convertAll)
default:
return nil, errors.Errorf("Wanted type of %T but got %T", []*ethpb.Validator{}, elements)
}
}
func convertAttestations(indices []uint64, elements any, convertAll bool) ([][32]byte, error) {
val, ok := elements.([]*ethpb.PendingAttestation)
if !ok {
return nil, errors.Errorf("Wanted type of %T but got %T", []*ethpb.PendingAttestation{}, elements)
}
return handlePendingAttestationSlice(val, indices, convertAll)
}
func convertBalances(indices []uint64, elements any, convertAll bool) ([][32]byte, error) {
switch casted := elements.(type) {
case []uint64:
return handleBalanceMVSlice(multi_value_slice.BuildEmptyCompositeSlice[uint64](casted), indices, convertAll)
case multi_value_slice.MultiValueSliceComposite[uint64]:
return handleBalanceMVSlice(casted, indices, convertAll)
default:
return nil, errors.Errorf("Wanted type of %T but got %T", []uint64{}, elements)
}
}
// handle32ByteMVslice computes and returns 32 byte arrays in a slice of root format. This is modified
// to be used with multivalue slices.
func handle32ByteMVslice(mv multi_value_slice.MultiValueSliceComposite[[32]byte],
indices []uint64, convertAll bool) ([][32]byte, error) {
length := len(indices)
if convertAll {
length = mv.Len(mv.State())
}
roots := make([][32]byte, 0, length)
rootCreator := func(input [32]byte) {
roots = append(roots, input)
}
if convertAll {
val := mv.Value(mv.State())
for i := range val {
rootCreator(val[i])
}
return roots, nil
}
totalLen := mv.Len(mv.State())
if totalLen > 0 {
for _, idx := range indices {
if idx > uint64(totalLen)-1 {
return nil, fmt.Errorf("index %d greater than number of byte arrays %d", idx, totalLen)
}
val, err := mv.At(mv.State(), idx)
if err != nil {
return nil, err
}
rootCreator(val)
}
}
return roots, nil
}
// handleValidatorMVSlice returns the validator indices in a slice of root format.
func handleValidatorMVSlice(mv multi_value_slice.MultiValueSliceComposite[*ethpb.Validator], indices []uint64, convertAll bool) ([][32]byte, error) {
length := len(indices)
if convertAll {
return stateutil.OptimizedValidatorRoots(mv.Value(mv.State()))
}
roots := make([][32]byte, 0, length)
rootCreator := func(input *ethpb.Validator) error {
newRoot, err := stateutil.ValidatorRootWithHasher(input)
if err != nil {
return err
}
roots = append(roots, newRoot)
return nil
}
totalLen := mv.Len(mv.State())
if totalLen > 0 {
for _, idx := range indices {
if idx > uint64(totalLen)-1 {
return nil, fmt.Errorf("index %d greater than number of validators %d", idx, totalLen)
}
val, err := mv.At(mv.State(), idx)
if err != nil {
return nil, err
}
err = rootCreator(val)
if err != nil {
return nil, err
}
}
}
return roots, nil
}
// handleEth1DataSlice processes a list of eth1data and indices into the appropriate roots.
func handleEth1DataSlice(val []*ethpb.Eth1Data, indices []uint64, convertAll bool) ([][32]byte, error) {
length := len(indices)
if convertAll {
length = len(val)
}
roots := make([][32]byte, 0, length)
rootCreator := func(input *ethpb.Eth1Data) error {
newRoot, err := stateutil.Eth1DataRootWithHasher(input)
if err != nil {
return err
}
roots = append(roots, newRoot)
return nil
}
if convertAll {
for i := range val {
err := rootCreator(val[i])
if err != nil {
return nil, err
}
}
return roots, nil
}
if len(val) > 0 {
for _, idx := range indices {
if idx > uint64(len(val))-1 {
return nil, fmt.Errorf("index %d greater than number of items in eth1 data slice %d", idx, len(val))
}
err := rootCreator(val[idx])
if err != nil {
return nil, err
}
}
}
return roots, nil
}
// handlePendingAttestationSlice returns the root of a slice of pending attestations.
func handlePendingAttestationSlice(val []*ethpb.PendingAttestation, indices []uint64, convertAll bool) ([][32]byte, error) {
length := len(indices)
if convertAll {
length = len(val)
}
roots := make([][32]byte, 0, length)
rootCreator := func(input *ethpb.PendingAttestation) error {
newRoot, err := stateutil.PendingAttRootWithHasher(input)
if err != nil {
return err
}
roots = append(roots, newRoot)
return nil
}
if convertAll {
for i := range val {
err := rootCreator(val[i])
if err != nil {
return nil, err
}
}
return roots, nil
}
if len(val) > 0 {
for _, idx := range indices {
if idx > uint64(len(val))-1 {
return nil, fmt.Errorf("index %d greater than number of pending attestations %d", idx, len(val))
}
err := rootCreator(val[idx])
if err != nil {
return nil, err
}
}
}
return roots, nil
}
func handleBalanceMVSlice(mv multi_value_slice.MultiValueSliceComposite[uint64], indices []uint64, convertAll bool) ([][32]byte, error) {
if convertAll {
val := mv.Value(mv.State())
return stateutil.PackUint64IntoChunks(val)
}
totalLen := mv.Len(mv.State())
if totalLen > 0 {
numOfElems, err := types.Balances.ElemsInChunk()
if err != nil {
return nil, err
}
iNumOfElems, err := pmath.Int(numOfElems)
if err != nil {
return nil, err
}
var roots [][32]byte
for _, idx := range indices {
// We split the indexes into their relevant groups. Balances
// are compressed according to 4 values -> 1 chunk.
startIdx := idx / numOfElems
startGroup := startIdx * numOfElems
var chunk [32]byte
sizeOfElem := len(chunk) / iNumOfElems
for i, j := 0, startGroup; j < startGroup+numOfElems; i, j = i+sizeOfElem, j+1 {
wantedVal := uint64(0)
// We are adding chunks in sets of 4, if the set is at the edge of the array
// then you will need to zero out the rest of the chunk. Ex : 41 indexes,
// so 41 % 4 = 1 . There are 3 indexes, which do not exist yet but we
// have to add in as a root. These 3 indexes are then given a 'zero' value.
if j < uint64(totalLen) {
val, err := mv.At(mv.State(), j)
if err != nil {
return nil, err
}
wantedVal = val
}
binary.LittleEndian.PutUint64(chunk[i:i+sizeOfElem], wantedVal)
}
roots = append(roots, chunk)
}
return roots, nil
}
return [][32]byte{}, nil
}