Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Macro for constructing a high-level type-safe wrapper around substrate storage #78

Closed
rphmeier opened this issue Feb 18, 2018 · 3 comments

Comments

@rphmeier
Copy link
Contributor

rphmeier commented Feb 18, 2018

Goal: never reference or load storage items using the key string directly. It is arcane, bug-prone, and unreadable.

usage:

Using a trait Storage:

trait Storage {
    // panic if the type is wrong for the key.
    fn load<T: Decodable>(&self) -> Option<T>;
    fn store<T: Encodable>(&mut self, value: T);
}
storage_declarations! {
    Authorities: List(":auth" -> AuthorityId), // creates something like current `KeyedVec` using prefix ":auth"
    Code: ":code" -> Vec<u8>, // creates a single value. stored under that key.
    ...
}

Authorities::len_key() -> &'static [u8];
Authorities::load_len(&Storage) -> u32
Authorities::key_for(n) -> Vec<u8>;
Authorities::load_from(&Storage, n) -> Option<AuthorityId>;
// ... KeyedVec-like API

Code::load_from(&Storage) -> Option<Vec<u8>>; // assumes a `Decodable` trait where the `<[u8] as Decodable>::Decoded = Vec<u8>`
Code::store_in(&mut Storage, &[u8]); 
Code::key() -> &'static [u8]; // for low-level usage.

crate substrate_storage would define all storage values used in substrate.
crate polkadot_storage would define all storage values used in polkadot.

The "load"/"store" API is a little annoying, so under runtime-support we would provide a Storage implementation that calls out to the externalities and a trait to provide helpers that are more ergonomic: i.e. a load() and store(T) function which are usable only within the runtime.

Usage in runtime:

// assuming these declarations:
storage_declarations! {
    Authorities: List(":auth" -> AuthorityId), // creates something like current `KeyedVec` using prefix ":auth"
    Code: ":code" -> Vec<u8>, // creates a single value. stored under that key.
    ...
}

// ...

Authorities::len_key() -> &'static [u8];
Authorities::len() -> u32
Authorities::key_for(n) -> Vec<u8>;
Authorities::load(n) -> AuthorityId;
// ... KeyedVec-like API

Code::load() -> Option<Vec<u8>>; // assumes a `Decodable` trait where the `<[u8] as Decodable>::Decoded = Vec<u8>`
Code::store(&[u8]); 
Code::key() -> &'static [u8]; // for low-level usage.
@gavofyork
Copy link
Member

gavofyork commented Mar 4, 2018

crate substrate_storage would define all storage values used in substrate.

what is this fascination with collecting common lexicographical items into crates!?

while i agree with the general gist of this issue, storage elements should be declared (and exported as needed) in the modules that use them, not in some monolithic collection, just like constants, types, helper functions and tests.

@rphmeier
Copy link
Contributor Author

rphmeier commented Mar 4, 2018

@gavofyork sure, then those particular keys like :auth: and :code: should be defined in runtime-std as there isn't really anywhere else suitable.

@rphmeier
Copy link
Contributor Author

rphmeier commented Mar 4, 2018

In general the goal of extracting the constants out to a separate crate is to allow type-safe access of the storage without having to link to the entire runtime.

JoshOrndorff added a commit to moonbeam-foundation/substrate that referenced this issue Apr 21, 2021
* Adds H160 account

* Check in Cargo.lock

* Fix no_std build

* Fixes token-dealer account id

* Setup chain_specs for H160 AccountId

* Adds license

* Cleans H160 account.rs

* Clean warnings

* Fixes account reference

* format rust files

* More rust format fixes

* Fixes genesis spec for H160

* Adds test for polkadotjs balance check

* Fixes test specs

* Restore AccountId logic

* Replace AccountId20 by H160 + fix tests

* Clean formatting

* Fix extra whitespace

* Fixes warning messages

* Fixes errors in merge

* Adds dev/ops account to alphanet specs

Co-authored-by: Alan <alan@ip-172-31-37-206.us-east-2.compute.internal>
Co-authored-by: Joshy Orndorff <admin@joshyorndorff.com>
Co-authored-by: Alan Sapede <alan@scg-moonbuild-1.dev.purestake.tech>
iStrike7 referenced this issue in mangata-finance/substrate Aug 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants