-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dynamic-ssz prototype from attestantio/go-eth2-client#123
- Loading branch information
Showing
9 changed files
with
1,053 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
Oops, something went wrong.