github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/stateutil/array_root.go (about)

     1  package stateutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/prysmaticlabs/prysm/shared/bytesutil"
     7  )
     8  
     9  // HandleByteArrays computes and returns byte arrays in a slice of root format.
    10  func HandleByteArrays(val [][]byte, indices []uint64, convertAll bool) ([][32]byte, error) {
    11  	length := len(indices)
    12  	if convertAll {
    13  		length = len(val)
    14  	}
    15  	roots := make([][32]byte, 0, length)
    16  	rootCreator := func(input []byte) {
    17  		newRoot := bytesutil.ToBytes32(input)
    18  		roots = append(roots, newRoot)
    19  	}
    20  	if convertAll {
    21  		for i := range val {
    22  			rootCreator(val[i])
    23  		}
    24  		return roots, nil
    25  	}
    26  	if len(val) > 0 {
    27  		for _, idx := range indices {
    28  			if idx > uint64(len(val))-1 {
    29  				return nil, fmt.Errorf("index %d greater than number of byte arrays %d", idx, len(val))
    30  			}
    31  			rootCreator(val[idx])
    32  		}
    33  	}
    34  	return roots, nil
    35  }