ferranbt / fastssz

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

Add support for variables #131

Open filoozom opened 1 year ago

filoozom commented 1 year ago

Hi,

Would it be conceivable to make ssz-size / ssz-max accept variable names that would get inserted in the generated code, instead of hardcoded values?

The idea behind this is that the Ethereum spec comes with configurable variables, and it's currently impossible (or at least quite complicated and repetitive) to write code that can be compatible with multiple presets at the same time.

For example, I'd like to be able to write something like this:

type ExecutionPayload struct {
    Withdrawals   []*Withdrawal           `ssz-max-var:"MAX_WITHDRAWALS_PER_PAYLOAD" ssz-var-pkg: config`
}

And it would then generate the file with MAX_WITHDRAWALS_PER_PAYLOAD in place of the hardcoded 16, as well as import the config package (although that might already be doable automatically?).

I'm guessing this could introduce some overhead in cases where calculations are required for buffer indexes etc, but for extremely high performance apps it would still be possible to use the current way.

ferranbt commented 7 months ago

Hello! Thank you for the proposal. This is not something I am planning to do in the short term. Unfortunately, the changes in the code generator to support this feature are not trivial to do.