ferranbt / fastssz

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

Failure to create encoding for slice of types #99

Closed mcdee closed 2 years ago

mcdee commented 2 years ago

A simple struct using only primitive types:

type Struct1 struct {
    Data [][]byte `ssz-size:"128,20"`
}

encodes fine, but if the type is abstracted:

type Slice []byte
type Struct2 struct {
    Data []Slice `ssz-size:"128,20"`
}

An attempt to create the encoding with sszgen returns an error:

[ERR]: failed to encode Slice: no ssz-size or ssz-max tags found for element. tag=

Similarly, attempting to do the same with a fixed-size array:

type FixedSizeArray [20]byte
type Struct3 struct {
    Data []FixedSizeArray `ssz-size:"128,20"`
}

returns

[ERR]: failed to encode FixedSizeArray: no ssz-size or ssz-max tags found for element. tag=

This is using current master.

ferranbt commented 2 years ago

It tries to encode also the alias type but It cannot find the SSZ tags. Use this flag:

sszgen --exclude-objs FixedSizeArray,Slice ...
mcdee commented 2 years ago

Thanks, but that doesn't seem to fix the issue:


sszgen --exclude-objs FixedSizeArray,Slice --path . --objs Struct3
[ERR]: failed to encode Struct3: failed to encode FixedSizeArray: failed to encode FixedSizeArray: unexpected mismatch between ssz-size and array fixed size, name=FixedSizeArray, ssz-size=128, fixed=20```
ferranbt commented 2 years ago

I think this is the same as #55

ferranbt commented 2 years ago

However, I am unsure how easy it is to fix this issue. Alias management is pretty hard, especially for unmarshalling. Almost all of these cases have to be built ad-hoc without good abstractions so I am not sure to what extent this issue can be fixed.

92 was another example of aliases that got a simple solution without changes on the generator.

I will try to look for an easy solution for this case too. However, if your only intention for the alias is to do marshal/unmarshal of the JSON structures, there is another approach here that works better than aliases with custom encoding functions.

mcdee commented 2 years ago

Thanks, I'll consider how best to approach this in the specific cases I have. I suspect that a shadow copy of the structs with unaliased types would be the best short-term solution here.

ferranbt commented 2 years ago

Could you try this PR #103 @mcdee? It has a possible fix for the issue.