prysm-pulse/third_party/com_github_prysmaticlabs_go_ssz.patch
Preston Van Loon 7c110e54f0
Add ssz marshal and unmarshal for most data structures (#5121)
* Add ssz marshal and unmarshal for most data structures
* Merge refs/heads/master into ssz-stuff
* Merge refs/heads/master into ssz-stuff
* Merge refs/heads/master into ssz-stuff
* Merge refs/heads/master into ssz-stuff
* Merge refs/heads/master into ssz-stuff
* Merge refs/heads/master into ssz-stuff
* Update ferran SSZ
* Update ferran's SSZ
* Merge refs/heads/master into ssz-stuff
* fix tests
* Merge branch 'ssz-stuff' of github.com:prysmaticlabs/prysm into ssz-stuff
* gaz
2020-03-19 02:39:23 +00:00

47 lines
1.4 KiB
Diff

diff --git a/BUILD.bazel b/BUILD.bazel
index e502174..1c29b7a 100644
--- a/BUILD.bazel
+++ b/BUILD.bazel
@@ -61,6 +61,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//types:go_default_library",
+ "@com_github_ferranbt_fastssz//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_go_bitfield//:go_default_library",
],
diff --git a/ssz.go b/ssz.go
index 61fb9ef..24755fc 100644
--- a/ssz.go
+++ b/ssz.go
@@ -5,6 +5,7 @@ import (
"reflect"
"strings"
+ fssz "github.com/ferranbt/fastssz"
"github.com/pkg/errors"
"github.com/prysmaticlabs/go-bitfield"
"github.com/prysmaticlabs/go-ssz/types"
@@ -48,6 +49,11 @@ func Marshal(val interface{}) ([]byte, error) {
if val == nil {
return nil, errors.New("untyped-value nil cannot be marshaled")
}
+
+ if v, ok := val.(fssz.Marshaler); ok {
+ return v.MarshalSSZ()
+ }
+
rval := reflect.ValueOf(val)
// We pre-allocate a buffer-size depending on the value's calculated total byte size.
@@ -87,6 +93,9 @@ func Unmarshal(input []byte, val interface{}) error {
if val == nil {
return errors.New("cannot unmarshal into untyped, nil value")
}
+ if v, ok := val.(fssz.Unmarshaler); ok {
+ return v.UnmarshalSSZ(input)
+ }
if len(input) == 0 {
return errors.New("no data to unmarshal from, input is an empty byte slice []byte{}")
}