Skip to content

Commit

Permalink
fix(generator): Unmarshaling of fixed sized custom types is generated…
Browse files Browse the repository at this point in the history
… incorrectly. (ferranbt#152)

* fix

* update control flow

* bug fix

* add test case

---------

Co-authored-by: ocnc <anocpublic@gmail.com>
  • Loading branch information
itsdevbear and ocnc authored Apr 5, 2024
1 parent 3fad963 commit b8fd7b4
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 8 deletions.
5 changes: 3 additions & 2 deletions sszgen/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func detectImports(v *Value) string {
ref = v.ref
case TypeList, TypeVector:
ref = v.e.ref
case TypeBytes:
ref = v.ref
default:
ref = v.ref
}
Expand All @@ -223,7 +225,6 @@ func (v *Value) objRef() string {
if v.ref == "" {
return v.obj
}

valuesImported = append(valuesImported, v)
return v.ref + "." + v.obj
}
Expand Down Expand Up @@ -322,7 +323,6 @@ type env struct {

func (e *env) generateOutputEncodings(output string) (map[string]string, error) {
out := map[string]string{}

keys := make([]string, 0, len(e.order))
for k := range e.order {
keys = append(keys, k)
Expand Down Expand Up @@ -444,6 +444,7 @@ func (e *env) print(order []string) (string, bool, error) {
// require the sszgen functions.
continue
}

objs = append(objs, &Obj{
HashTreeRoot: e.hashTreeRoot(name, obj),
GetTree: e.getTree(name, obj),
Expand Down
15 changes: 9 additions & 6 deletions sszgen/generator/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,21 @@ func (v *Value) createSlice(useNumVariable bool) string {
return fmt.Sprintf("::.%s = make([]%s%s, %s)", v.name, ptr, v.e.objRef(), size)

case TypeBytes:
// [][]byte
if v.c {
return ""
}

// Check for a type alias.
ref := v.e.objRef()
if ref != "" {
return fmt.Sprintf("::.%s = make([]%s, %s)", v.name, ref, size)
}

if v.e.c {
return fmt.Sprintf("::.%s = make([][%d]byte, %s)", v.name, v.e.s, size)
}
ref := v.e.objRef()
if ref == "" {
ref = "[]byte"
}
return fmt.Sprintf("::.%s = make([]%s, %s)", v.name, ref, size)

return fmt.Sprintf("::.%s = make([][]byte, %s)", v.name, size)

default:
panic(fmt.Sprintf("create not implemented for %s type %s", v.name, v.e.t.String()))
Expand Down
11 changes: 11 additions & 0 deletions sszgen/testcases/pr_152.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package testcases

// Data152 is a byte array with 48 elements
//
//go:generate go run ../main.go --path pr_152.go
type Data152 [48]byte

// PR1512 is a struct with a Data152 field
type PR1512 struct {
D []Data152 `ssz-max:"32"`
}
133 changes: 133 additions & 0 deletions sszgen/testcases/pr_152_encoding.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b8fd7b4

Please sign in to comment.