Skip to content

Commit

Permalink
add dynamic-ssz prototype from attestantio/go-eth2-client#123
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Mar 30, 2024
1 parent fd8b9d5 commit ea3b4a1
Show file tree
Hide file tree
Showing 9 changed files with 1,053 additions and 0 deletions.
11 changes: 11 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ssz

import (
"reflect"

fastssz "github.com/ferranbt/fastssz"
)

var byteType = reflect.TypeOf(byte(0))
var sszMarshallerType = reflect.TypeOf((*fastssz.Marshaler)(nil)).Elem()
var sszUnmarshallerType = reflect.TypeOf((*fastssz.Unmarshaler)(nil)).Elem()
7 changes: 7 additions & 0 deletions debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ssz

import "strings"

func indent(c int) string {
return strings.Repeat(" ", c)
}
85 changes: 85 additions & 0 deletions dynssz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package ssz

import (
"fmt"
"reflect"
)

type DynSsz struct {
typesWithSpecVals map[reflect.Type]uint8
SpecValues map[string]any
NoFastSsz bool
}

const (
unknownSpecValued uint8 = iota
noSpecValues
hasSpecValues
)

func NewDynSsz(specs map[string]any) *DynSsz {
if specs == nil {
specs = map[string]any{}
}
return &DynSsz{
typesWithSpecVals: map[reflect.Type]uint8{},
SpecValues: specs,
}
}

func (d *DynSsz) MarshalSSZ(source any) ([]byte, error) {
sourceType := reflect.TypeOf(source)
sourceValue := reflect.ValueOf(source)

size, err := d.getSszValueSize(sourceType, sourceValue)
if err != nil {
return nil, err
}

buf := make([]byte, size)
newBuf, err := d.marshalType(sourceType, sourceValue, buf[:0], []sszSizeHint{}, 0)
if err != nil {
return nil, err
}

if len(newBuf) != size {
return nil, fmt.Errorf("ssz length does not match expected length (expected: %v, got: %v)", size, len(newBuf))
}

return newBuf, nil
}

func (d *DynSsz) MarshalSSZTo(source any, buf []byte) ([]byte, error) {
sourceType := reflect.TypeOf(source)
sourceValue := reflect.ValueOf(source)

newBuf, err := d.marshalType(sourceType, sourceValue, buf, []sszSizeHint{}, 0)
if err != nil {
return nil, err
}

return newBuf, nil
}

func (d *DynSsz) SizeSSZ(source any) (int, error) {
sourceType := reflect.TypeOf(source)
sourceValue := reflect.ValueOf(source)

return d.getSszValueSize(sourceType, sourceValue)
}

func (d *DynSsz) UnmarshalSSZ(target any, ssz []byte) error {
targetType := reflect.TypeOf(target)
targetValue := reflect.ValueOf(target)

consumedBytes, err := d.unmarshalType(targetType, targetValue, ssz, []sszSizeHint{}, 0)
if err != nil {
return err
}

if consumedBytes != len(ssz) {
return fmt.Errorf("did not consume full ssz range (consumed: %v, ssz size: %v)", consumedBytes, len(ssz))
}

return nil
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/pk910/dynamic-ssz

go 1.21.1

require (
github.com/ferranbt/fastssz v0.1.3 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.3.2 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/ferranbt/fastssz v0.1.3 h1:ZI+z3JH05h4kgmFXdHuR1aWYsgrg7o+Fw7/NCzM16Mo=
github.com/ferranbt/fastssz v0.1.3/go.mod h1:0Y9TEd/9XuFlh7mskMPfXiI2Dkw4Ddg9EyXt1W7MRvE=
github.com/klauspost/cpuid/v2 v2.0.4/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Loading

0 comments on commit ea3b4a1

Please sign in to comment.