mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
* Add basic PathElement * Add ssz_type.go * Add basic sszInfo * Add containerInfo * Add basic analyzer without analyzing list/vector * Add analyzer for homogeneous collection types * Add offset/length calculator * Add testutil package in encoding/ssz/query * Add first round trip test for IndexedAttestationElectra * Go mod tidy * Add Print function for debugging purpose * Add changelog * Add testonly flag for testutil package & Nit for nogo * Apply reviews from Radek * Replace fastssz with prysmaticlabs one * Add proto/ssz_query package for testing purpose * Update encoding/ssz/query tests to decouple with beacon types * Use require.* instead of assert.* * Fix import name for proto ssz_query package * Remove uint8/uint16 and some byte arrays in FixedTestContainer * Add newline for files * Fix comment about byte array in ssz_query.proto --------- Co-authored-by: Radosław Kapka <rkapka@wp.pl>
38 lines
857 B
Go
38 lines
857 B
Go
package query
|
|
|
|
import "fmt"
|
|
|
|
func CalculateOffsetAndLength(sszInfo *sszInfo, path []PathElement) (*sszInfo, uint64, uint64, error) {
|
|
if sszInfo == nil {
|
|
return nil, 0, 0, fmt.Errorf("sszInfo is nil")
|
|
}
|
|
|
|
if len(path) == 0 {
|
|
return nil, 0, 0, fmt.Errorf("path is empty")
|
|
}
|
|
|
|
walk := sszInfo
|
|
currentOffset := uint64(0)
|
|
|
|
for _, elem := range path {
|
|
fieldInfos, err := walk.ContainerInfo()
|
|
if err != nil {
|
|
return nil, 0, 0, fmt.Errorf("could not get field infos: %w", err)
|
|
}
|
|
|
|
fieldInfo, exists := fieldInfos[elem.Name]
|
|
if !exists {
|
|
return nil, 0, 0, fmt.Errorf("field %s not found in fieldInfos", elem.Name)
|
|
}
|
|
|
|
currentOffset += fieldInfo.offset
|
|
walk = fieldInfo.sszInfo
|
|
}
|
|
|
|
if walk.isVariable {
|
|
return nil, 0, 0, fmt.Errorf("cannot calculate length for variable-sized type")
|
|
}
|
|
|
|
return walk, currentOffset, walk.Size(), nil
|
|
}
|