starknet-io / types-rs

🐺 Starknet Rust types 🦀
MIT License
66 stars 41 forks source link

dev: make sure fields of generated structs are sorted alphabetically #83

Closed tdelabro closed 4 months ago

tdelabro commented 4 months ago

In the process of computing ContractClass hashes (at least for the old cairo classes) the struct is serialized to json using a python hint and then the bytes of this string are fed to the StarknetKeccak hasher.

All implementations in other languages have to emulate the behaviors of the python serializer in order to be able to recompute the contract class hash. Part of this behavior is that fields of a struct are serialized in alphabetical order.

In Rust, using Serde, there is no simple way to force this. You have to recreate the structs yourself with fields defined in the correct order, convert or deserialize into this new struct, and serialize using those new types.

The previous lib used by the Starknet Rust ecosystem to define those types is Starknet-rs, which do not enforce alphabetical order for that struct's fields, forcing everyone to reorder the fields one way or the other.

It would be a real improvement if we could make sure that all the struct generated for the RPC servers do respect the alphabetical ordering

tdelabro commented 4 months ago

@maciejka is there something in the current codebase that enforces this?

tdelabro commented 4 months ago

It is not strictly required for all structs, just for all which are part of cairo contract classes

tdelabro commented 4 months ago

This should be enforced at the openRPC specs level rather than during the code generation. I'm on it

maciejka commented 4 months ago

@tdelabro are you able to identify which structs need to use alphabetical order for serialization?