ferranbt / fastssz

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

sszgen incorrect output with array of `[]byte` type #127

Closed mcdee closed 7 months ago

mcdee commented 1 year ago

If I create a simple type:

type Obj1 struct {
        T1 [][]byte `ssz-max:"1024,256" ssz-size:"?,?"`
}

sszgen generates code that handles the two separate maximums fine:

func (o *Obj1) MarshalSSZTo(buf []byte) (dst []byte, err error) {
    ...
    // Field (0) 'T1'
    if size := len(o.T1); size > 1024 {
        err = ssz.ErrListTooBigFn("Obj1.T1", size, 1024)
        return
    }
    ...
    for ii := 0; ii < len(o.T1); ii++ {
        if size := len(o.T1[ii]); size > 256 {
            err = ssz.ErrBytesLengthFn("Obj1.T1[ii]", size, 256)
            return
        }
        dst = append(dst, o.T1[ii]...)
    }
    ...
}

It can be seen that the two sizes (1024 and 256) are used correctly. However, if I add an intermediate type in to the object:

type Data []byte

type Obj2 struct {
        T1 []Data `ssz-max:"1024,256" ssz-size:"?,?"`
}

sszgen generates this:

func (o *Obj2) MarshalSSZTo(buf []byte) (dst []byte, err error) {
    // Field (0) 'T1'
    if size := len(o.T1); size > 256 {
        err = ssz.ErrListTooBigFn("Obj2.T1", size, 256)
        return
    }
    ...
    for ii := 0; ii < len(o.T1); ii++ {
        if size := len(o.T1[ii]); size > 256 {
            err = ssz.ErrBytesLengthFn("Obj2.T1[ii]", size, 256)
            return
        }
        dst = append(dst, o.T1[ii]...)
    }
    ...
}

Here it can be seen that in the first piece of code 256 is being used where it should still be 1024.

ferranbt commented 1 year ago

Hi @mcdee. Thank you for reporting the issue, I will take a look. There was a similar situation with placeholders before and it was easy to solve.

mcdee commented 1 year ago

Any luck with hunting this one down?

ferranbt commented 1 year ago

Nope, I give it a try every few weeks but I am not having luck, this was part of some rewrite to support nested bytes (i.e. [][]byte) that I did not write so I am having some hard time fixing it.