Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redefined integers within slices fail to correctly generate ssz methods #92

Closed
lightclient opened this issue Jul 11, 2022 · 3 comments
Closed

Comments

@lightclient
Copy link

lightclient commented Jul 11, 2022

It seems like sszgen is not respecting the slice type when it is an alias of an integer. I think this has to do with one of the previous fixes that took advantage of the implicit conversion between the two.

package main

import (
        "encoding/json"
        "fmt"
        "strconv"
)

type U64Str uint64

func (i U64Str) MarshalJSON() ([]byte, error) {
        return json.Marshal(strconv.FormatUint(uint64(i), 10))
}

func (i *U64Str) UnmarshalJSON(b []byte) error {
        var s string
        if err := json.Unmarshal(b, &s); err != nil {
                return err
        }
        value, err := strconv.ParseInt(s, 10, 64)
        if err != nil {
                return err
        }
        *i = U64Str(value)
        return nil
}

type Foo struct {
        Bar []U64Str `json:"bar" ssz-max:"1024"`
}

func main() {
        input := `{"bar": ["1", "2", "3"]}`

        var val Foo
        if err := json.Unmarshal([]byte(input), &val); err != nil {
                panic(err)
        }
        fmt.Println("success")
}
$ sszgen --path main.go
$ go run .
./main_encoding.go:62:11: cannot use ssz.ExtendUint64(f.Bar, num) (value of type []uint64) as type []U64Str in assignment
./main_encoding.go:62:28: cannot use f.Bar (variable of type []U64Str) as type []uint64 in argument to ssz.ExtendUint64
./main_encoding.go:97:20: cannot use i (variable of type U64Str) as type uint64 in argument to hh.AppendUint64
@lightclient
Copy link
Author

This was originally found in flashbots/go-boost-utils#17

@lightclient lightclient changed the title Alias integers in slices fails Redefined integers within slices fail to correctly generate ssz methods Jul 11, 2022
@ferranbt
Copy link
Owner

I never expected that aliases would be that popular. I will take a look.

@ferranbt
Copy link
Owner

This is a hard issue to solve. The unmarshaller uses this function to efficiently allocate the array, but the function expects []uint type. I do not think the types can be casted.

However, something like this works:

type ArrayUint []uint64

type Case4 struct {
	Val ArrayUint  `ssz-size:"32"`
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants