mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 15:37:56 -05:00
* Add VariableTestContainer in ssz_query.proto * Add listInfo * Use errors.New for making an error with a static string literal * Add listInfo field when analyzing the List type * Persist the field order in the container * Add actualOffset and goFieldName at fieldInfo * Add PopulateFromValue function & update test runner * Handle slice of ssz object for marshalling * Add CalculateOffsetAndLength test * Add comments for better doc * Changelog :) * Apply reviews from Radek * Remove actualOffset and update offset field instead * Add Nested container of variable-sized for testing nested path * Fix offset adding logics: for variable-sized field, always add 4 instead of its fixed size * Fix multiple import issue --------- Co-authored-by: Radosław Kapka <rkapka@wp.pl>
39 lines
934 B
Go
39 lines
934 B
Go
package query
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
)
|
|
|
|
// CalculateOffsetAndLength calculates the offset and length of a given path within the SSZ object.
|
|
// By walking the given path, it accumulates the offsets based on sszInfo.
|
|
func CalculateOffsetAndLength(sszInfo *sszInfo, path []PathElement) (*sszInfo, uint64, uint64, error) {
|
|
if sszInfo == nil {
|
|
return nil, 0, 0, errors.New("sszInfo is nil")
|
|
}
|
|
|
|
if len(path) == 0 {
|
|
return nil, 0, 0, errors.New("path is empty")
|
|
}
|
|
|
|
walk := sszInfo
|
|
offset := uint64(0)
|
|
|
|
for _, elem := range path {
|
|
containerInfo, err := walk.ContainerInfo()
|
|
if err != nil {
|
|
return nil, 0, 0, fmt.Errorf("could not get field infos: %w", err)
|
|
}
|
|
|
|
fieldInfo, exists := containerInfo.fields[elem.Name]
|
|
if !exists {
|
|
return nil, 0, 0, fmt.Errorf("field %s not found in containerInfo", elem.Name)
|
|
}
|
|
|
|
offset += fieldInfo.offset
|
|
walk = fieldInfo.sszInfo
|
|
}
|
|
|
|
return walk, offset, walk.Size(), nil
|
|
}
|