feat(soroban): Support structs #1843
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR brings support to user defined structs in Soroban's Solidity.
Here are some points to note:
In-memory structs were naturally supported by integrating a sorobanVm-friendly bump allocator that was introduced in Soroban: support dynamic memory arrays #1838
For storing structs in storage, we do as follows:
We encode struct fields as Soroban Vals, and store each one separately in storage where the key of each element is SorobanVec[storage_slot, field_index].
When we retrieve a whole struct, i.e
StructDef memory my_struct = my_structs_map[my_struct], we simply loop over all fields, fetch them from storage, and create an in memory instance.That is why we strongly advise to access a single struct field at a time instead of fetching it all from storage at once. This a complete example:
Here,
get_timelock_amount_expensive_opnot only retrieves all fields from storage, but puts in extra instructions construct the struct in-memory.get_timelock_amount_cheap_opon the other hand simply calls the soroban host functionget_contract_dataand doesn't need extra work.