ferranbt / fastssz

Fast Ethereum2.0 SSZ encoder/decoder
MIT License
74 stars 44 forks source link

Allow to use alias to a byte array #55

Closed dshulyak closed 2 years ago

dshulyak commented 3 years ago

If we define alias to a byte array as:

type Alias [20]byte

type Example struct {
   Field []Alias
}

It will not be respected by generator, and we will get [][20]byte in the generated code.

For a work around i used:

diff --git a/sszgen/unmarshal.go b/sszgen/unmarshal.go
index 836fd3c..3222b6b 100644
--- a/sszgen/unmarshal.go
+++ b/sszgen/unmarshal.go
@@ -338,6 +338,9 @@ func (v *Value) createSlice() string {
                if v.c {
                        return ""
                }
+               if len(v.e.obj) > 0 {
+                       return fmt.Sprintf("::.%s = make([]%v, %s)", v.name, v.e.objRef(), size)
+               }
                if v.e.c {
                        return fmt.Sprintf("::.%s = make([][%d]byte, %s)", v.name, v.e.s, size)
                }