Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substrate Storage Deep Dive Workshop/Tutorial #19

Open
shawntabrizi opened this issue May 29, 2019 · 6 comments
Open

Substrate Storage Deep Dive Workshop/Tutorial #19

shawntabrizi opened this issue May 29, 2019 · 6 comments

Comments

@shawntabrizi
Copy link

There are a lot of tricks and nuances to using storage in Substrate.

  • Supported storage types (found here)

    • StorageValue
    • StorageMap
    • StorageDoubleMap
    • HashedStorage/UnhashedStorage (This may be just an implementation detail we should briefly talk about, but not go deep into)
    • LinkedMap/EnumerableStorageMap
    • other...?
  • Implications of using different storage types for efficiency

  • Using Vec, implications of letting users input the value for this

  • Storage API: get, put, insert, take, append, etc...

  • Storing Structs (advance things)

  • Storage Upgrades or Migrations, how and when it is possible

  • Charging fees for users storing items

  • Genesis configuration, default value, "add extra genesis"

  • decl_storage macro in general

  • using the Polkadot UI for a number of different storage scenarios

  • more?

@thiolliere may be able to help us here with the details of some of these things if he is so kind :)

@kianenigma
Copy link

kianenigma commented May 29, 2019

I'd be happy to help with it but probably not as the main owner of the task.

An important idea worth noting:
Both Indices and Council use a chunked map to deal with long Vecs. I've made an issue to standardize it but might be a while until anyone does that (if ever). Until then, I think introducing this to our users is super important.

Another one that I've learned the hard way: StorageDoubleMap vs literally storing a map (x, y) => value.

@shawntabrizi
Copy link
Author

@kianenigma can you share a code sample to get an idea of what you mean?

@kianenigma
Copy link

@kianenigma can you share a code sample to get an idea of what you mean?

In cases where you have a very large vec, and you access indexes individually (no need to iterate all of them etc.), you can chunck the Vec into Sets (of 64 element per se) and fetch one set from mem. Indexing would simply be:

const SET_SIZE: u32 = 64;
decl_storage {  BigList map u32 => Vec<X> }

set_idx = global_idx / SET_SIZE;
vec_idxe = global_idx % SET_SIZE;
let set = Self::big_list(set_idx);
let target_value = set[vec_idx];

inserting and removing needs a bit bookkeeping though; one thing for sure is that you probably want a new storage item all the time that points to the next free set. Both Voters and ApprovalsOf have the same deal in council.

@ltfschoen
Copy link

ltfschoen commented May 29, 2019

And continue the conversation about off-chain storage, as we've already done here:

@JoshOrndorff
Copy link
Contributor

JoshOrndorff commented Jul 4, 2019

AFAIK, storing enums is not documented anywhere. TIL

// Can't derive Default like you can for structs.
#[derive(Encode, Decode, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub enum Status {
    Active,
    Sold,
    SellerReviewed,
    BuyerReviewed,
}

// Without a manual implementation, you can't use the enum as a storage value
impl Default for Status {
    fn default() -> Self {
        Self::Active
    }
}

@bkchr
Copy link

bkchr commented Jul 4, 2019

You can store it without a default. Either you specify the default yourself in the decl storage line or you store it as an optional.

@JoshOrndorff JoshOrndorff transferred this issue from polkadot-developers/substrate-developer-hub.github.io Mar 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants