mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -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>
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package testutil
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
ssz "github.com/prysmaticlabs/fastssz"
|
|
)
|
|
|
|
// marshalAny marshals any value into SSZ format.
|
|
func marshalAny(value any) ([]byte, error) {
|
|
// First check if it implements ssz.Marshaler (this catches custom types like primitives.Epoch)
|
|
if marshaler, ok := value.(ssz.Marshaler); ok {
|
|
return marshaler.MarshalSSZ()
|
|
}
|
|
|
|
// Handle custom type aliases by checking if they're based on primitive types
|
|
valueType := reflect.TypeOf(value)
|
|
if valueType.PkgPath() != "" {
|
|
switch valueType.Kind() {
|
|
case reflect.Uint64:
|
|
return ssz.MarshalUint64(make([]byte, 0), reflect.ValueOf(value).Uint()), nil
|
|
case reflect.Uint32:
|
|
return ssz.MarshalUint32(make([]byte, 0), uint32(reflect.ValueOf(value).Uint())), nil
|
|
case reflect.Bool:
|
|
return ssz.MarshalBool(make([]byte, 0), reflect.ValueOf(value).Bool()), nil
|
|
}
|
|
}
|
|
|
|
switch v := value.(type) {
|
|
case []byte:
|
|
return v, nil
|
|
case []uint64:
|
|
buf := make([]byte, 0, len(v)*8)
|
|
for _, val := range v {
|
|
buf = ssz.MarshalUint64(buf, val)
|
|
}
|
|
return buf, nil
|
|
case uint64:
|
|
return ssz.MarshalUint64(make([]byte, 0), v), nil
|
|
case uint32:
|
|
return ssz.MarshalUint32(make([]byte, 0), v), nil
|
|
case bool:
|
|
return ssz.MarshalBool(make([]byte, 0), v), nil
|
|
|
|
default:
|
|
return nil, fmt.Errorf("unsupported type for SSZ marshalling: %T", value)
|
|
}
|
|
}
|