From 13125d4f75050255fb35a17b4740024432ba2722 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Fri, 28 Jun 2024 16:06:26 +0100 Subject: [PATCH 1/2] Fix missing import for external []byte alias --- sszgen/generator/generator.go | 3 + sszgen/generator/unmarshal.go | 10 +-- sszgen/testcases/issue_166.go | 9 +++ sszgen/testcases/issue_166_encoding.go | 106 +++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 sszgen/testcases/issue_166.go create mode 100644 sszgen/testcases/issue_166_encoding.go diff --git a/sszgen/generator/generator.go b/sszgen/generator/generator.go index 9422e6f..a90f580 100644 --- a/sszgen/generator/generator.go +++ b/sszgen/generator/generator.go @@ -459,6 +459,9 @@ func (e *env) print(order []string) (string, bool, error) { } data["objs"] = objs + fmt.Println("----") + fmt.Println(valuesImported) + imports := []string{} for _, v := range valuesImported { imports = appendWithoutRepeated(imports, []string{detectImports(v)}) diff --git a/sszgen/generator/unmarshal.go b/sszgen/generator/unmarshal.go index 4c8b947..a1a2ea3 100644 --- a/sszgen/generator/unmarshal.go +++ b/sszgen/generator/unmarshal.go @@ -39,14 +39,9 @@ func (v *Value) unmarshal(dst string) string { validate = fmt.Sprintf("if len(%s) > %d { return ssz.ErrBytesLength }\n", dst, v.m) } - refName := "" - if v.ref != "" { - refName = v.ref + "." + v.obj - } - // both fixed and dynamic are decoded equally tmpl := `{{.validate}}if cap(::.{{.name}}) == 0 { - {{if .refName}} ::.{{.name}} = {{ .refName }}(make([]byte, 0, len({{.dst}}))) {{ else }} ::.{{.name}} = make([]byte, 0, len({{.dst}})) {{ end }} + {{if .isRef}} ::.{{.name}} = {{ ref .obj }}(make([]byte, 0, len({{.dst}}))) {{ else }} ::.{{.name}} = make([]byte, 0, len({{.dst}})) {{ end }} } ::.{{.name}} = append(::.{{.name}}, {{.dst}}...)` return execTmpl(tmpl, map[string]interface{}{ @@ -54,7 +49,8 @@ func (v *Value) unmarshal(dst string) string { "name": v.name, "dst": dst, "size": v.m, - "refName": refName, + "isRef": v.ref != "", + "obj": v, }) case TypeUint: diff --git a/sszgen/testcases/issue_166.go b/sszgen/testcases/issue_166.go new file mode 100644 index 0000000..ba9b91d --- /dev/null +++ b/sszgen/testcases/issue_166.go @@ -0,0 +1,9 @@ +package testcases + +//go:generate go run ../main.go --path issue_165.go --include ./other + +import "github.com/ferranbt/fastssz/sszgen/testcases/other" + +type Issue165 struct { + A other.Case4Bytes +} diff --git a/sszgen/testcases/issue_166_encoding.go b/sszgen/testcases/issue_166_encoding.go new file mode 100644 index 0000000..43d5d29 --- /dev/null +++ b/sszgen/testcases/issue_166_encoding.go @@ -0,0 +1,106 @@ +// Code generated by fastssz. DO NOT EDIT. +// Hash: 4f5884df993ce08e7f54a316e276c821ff2259053841aadaf21fd7497a7a7806 +// Version: 0.1.3 +package testcases + +import ( + ssz "github.com/ferranbt/fastssz" + "github.com/ferranbt/fastssz/sszgen/testcases/other" +) + +// MarshalSSZ ssz marshals the Issue165 object +func (i *Issue165) MarshalSSZ() ([]byte, error) { + return ssz.MarshalSSZ(i) +} + +// MarshalSSZTo ssz marshals the Issue165 object to a target array +func (i *Issue165) MarshalSSZTo(buf []byte) (dst []byte, err error) { + dst = buf + offset := int(4) + + // Offset (0) 'A' + dst = ssz.WriteOffset(dst, offset) + + // Field (0) 'A' + if size := len(i.A); size > 0 { + err = ssz.ErrBytesLengthFn("Issue165.A", size, 0) + return + } + dst = append(dst, i.A...) + + return +} + +// UnmarshalSSZ ssz unmarshals the Issue165 object +func (i *Issue165) UnmarshalSSZ(buf []byte) error { + var err error + size := uint64(len(buf)) + if size < 4 { + return ssz.ErrSize + } + + tail := buf + var o0 uint64 + + // Offset (0) 'A' + if o0 = ssz.ReadOffset(buf[0:4]); o0 > size { + return ssz.ErrOffset + } + + if o0 < 4 { + return ssz.ErrInvalidVariableOffset + } + + // Field (0) 'A' + { + buf = tail[o0:] + if len(buf) > 0 { + return ssz.ErrBytesLength + } + if cap(i.A) == 0 { + i.A = other.Case4Bytes(make([]byte, 0, len(buf))) + } + i.A = append(i.A, buf...) + } + return err +} + +// SizeSSZ returns the ssz encoded size in bytes for the Issue165 object +func (i *Issue165) SizeSSZ() (size int) { + size = 4 + + // Field (0) 'A' + size += len(i.A) + + return +} + +// HashTreeRoot ssz hashes the Issue165 object +func (i *Issue165) HashTreeRoot() ([32]byte, error) { + return ssz.HashWithDefaultHasher(i) +} + +// HashTreeRootWith ssz hashes the Issue165 object with a hasher +func (i *Issue165) HashTreeRootWith(hh ssz.HashWalker) (err error) { + indx := hh.Index() + + // Field (0) 'A' + { + elemIndx := hh.Index() + byteLen := uint64(len(i.A)) + if byteLen > 0 { + err = ssz.ErrIncorrectListSize + return + } + hh.Append(i.A) + hh.MerkleizeWithMixin(elemIndx, byteLen, (0+31)/32) + } + + hh.Merkleize(indx) + return +} + +// GetTree ssz hashes the Issue165 object +func (i *Issue165) GetTree() (*ssz.Node, error) { + return ssz.ProofTree(i) +} From dbbdbf38037d11b847fcfc73c13360f8cfcf8d01 Mon Sep 17 00:00:00 2001 From: Ferran Borreguero Date: Fri, 28 Jun 2024 16:09:03 +0100 Subject: [PATCH 2/2] Remove comment --- sszgen/generator/generator.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/sszgen/generator/generator.go b/sszgen/generator/generator.go index a90f580..9422e6f 100644 --- a/sszgen/generator/generator.go +++ b/sszgen/generator/generator.go @@ -459,9 +459,6 @@ func (e *env) print(order []string) (string, bool, error) { } data["objs"] = objs - fmt.Println("----") - fmt.Println(valuesImported) - imports := []string{} for _, v := range valuesImported { imports = appendWithoutRepeated(imports, []string{detectImports(v)})