3

I have some questions that about custom data structure in ink!.

https://paritytech.github.io/ink-docs/faq#what-are-the-scaleencode-and-scaledecode-traits. I already implemented some features with pallet and using the Encode, Decode trait is straight forward to me.

However, there is also a section using some advance storage feature of ink!. https://paritytech.github.io/ink-docs/datastructures/custom-datastructure

I’m kinda confused that which approach to use?

5
  • Does this answer your question? How to have a mapping in a custom structure inside an ink! contract?
    – Nuke
    Apr 17, 2022 at 19:06
  • If not, what are you trying to do?
    – Nuke
    Apr 17, 2022 at 19:07
  • I already checked that post, but it not really answered my question What I'm trying to ask is what is the correct way to create a custom struct: "Using Encode, Decode" or "Implement some storage layout trait". And what the difference between these two approaches.
    – Daniel Lam
    Apr 18, 2022 at 1:43
  • As with most design decisions, what you are trying to do (accomplish) is important to drive your tools & techniques used. I meant to ask that above: discuss what you are trying to do so the approach to use can be concretely based on goals.
    – Nuke
    Apr 18, 2022 at 18:12
  • The use case is very simple: Have a custom struct and a field with that struct in contract storage. However, in the past few days, I learnt more about storage layout. So what I guess for the simple use case I describe above: The Encode, Decode allows serialization, deserialization of a struct into single bytes as we know, which also means that if we don't derive SpreadLayout for that custom struct, the default storage layout for that struct is PackedLayout and the struct data is packed into one single cell. Can you confirm this? Because that is exactly what I want to know. @DanShield
    – Daniel Lam
    Apr 19, 2022 at 14:44

1 Answer 1

2

The Encode, Decode allows serialization, deserialization of a struct into single bytes as we know, which also means that if we don't derive SpreadLayout for that custom struct, the default storage layout for that struct is PackedLayout and the struct data is packed into one single cell.

Encode/Decode does not influence the storage layout ink! uses. By default the struct is SpreadLayout ‒ so every struct field is put into its own single storage cell.

This page from the ink! docs has some more details: https://paritytech.github.io/ink-docs/datastructures/spread-storage-layout.

We're currently working on ink! 4.0 which will greatly simplify the concepts of SpreadLayout and PackedLayout.

1
  • 1. Can I ask what are the use cases of Encode/Decode if it doesn't influence the storage layout? The only case I can think of is message parameters and returns. 2. I understands SpreadLayout but can't understand SpreadAllocate and why is it important for Mapping. Thanks
    – Daniel Lam
    May 3, 2022 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.