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

Have all the special storage keys in a single place #2126

Open
xlc opened this issue Nov 1, 2023 · 16 comments
Open

Have all the special storage keys in a single place #2126

xlc opened this issue Nov 1, 2023 · 16 comments
Labels
C1-mentor A task where a mentor is available. Please indicate in the issue who the mentor could be. D1-medium Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase. I4-refactor Code needs refactoring.

Comments

@xlc
Copy link
Contributor

xlc commented Nov 1, 2023

I am working on a feature of Chopsticks to decode all the storage keys. There are some keys requires special handling as they are not included in metadata.

Currently we have

/// Wasm code of the runtime.
///
/// Stored as a raw byte vector. Required by substrate.
pub const CODE: &[u8] = b":code";
/// Number of wasm linear memory pages required for execution of the runtime.
///
/// The type of this value is encoded `u64`.
pub const HEAP_PAGES: &[u8] = b":heappages";
/// Current extrinsic index (u32) is stored under this key.
///
/// Encodes to `0x3a65787472696e7369635f696e646578`.
pub const EXTRINSIC_INDEX: &[u8] = b":extrinsic_index";
/// Current intra-block entropy (a universally unique `[u8; 32]` value) is stored here.
///
/// Encodes to `0x3a696e747261626c6f636b5f656e74726f7079`.
pub const INTRABLOCK_ENTROPY: &[u8] = b":intrablock_entropy";
/// Prefix of child storage keys.
pub const CHILD_STORAGE_KEY_PREFIX: &[u8] = b":child_storage:";
/// Prefix of the default child storage keys in the top trie.
pub const DEFAULT_CHILD_STORAGE_KEY_PREFIX: &[u8] = b":child_storage:default:";

/// The storage key postfix that is used to store the [`StorageVersion`] per pallet.
///
/// The full storage key is built by using:
/// Twox128([`PalletInfo::name`]) ++ Twox128([`STORAGE_VERSION_STORAGE_KEY_POSTFIX`])
pub const STORAGE_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__STORAGE_VERSION__:";

/// The key that is holds the current number of active layers.
///
/// Encodes to `0x3a7472616e73616374696f6e5f6c6576656c3a`.
pub const TRANSACTION_LEVEL_KEY: &[u8] = b":transaction_level:";

/// The storage key for the current set of weighted Grandpa authorities.
/// The value stored is an encoded VersionedAuthorityList.
pub const GRANDPA_AUTHORITIES_KEY: &[u8] = b":grandpa_authorities";

/// The upward message dispatch queue remaining capacity for the given para id.
///
/// The storage entry stores a tuple of two values:
///
/// - `count: u32`, the number of additional messages which may be enqueued for the given para,
/// - `total_size: u32`, the total size of additional messages which may be enqueued for the
/// given para.
pub fn relay_dispatch_queue_remaining_capacity(para_id: Id) -> WellKnownKey<(u32, u32)> {
(b":relay_dispatch_queue_remaining_capacity", para_id).encode().into()
}

Please let me know if I missed anything

This makes it really hard to implement a generic and all-catching storage decoder. Can we move all the Substrate well known keys to sp_core::storage::well_known_keys? The polkadot one are all in a single place so that's good.

@xlc
Copy link
Contributor Author

xlc commented Nov 1, 2023

The key format is also inconsistent. Some of them are :xxx and some of them are :xxx: but I guess we are already too late to change them.

We should also update the docs to include encoded hex and the expected value type.

@bkchr
Copy link
Member

bkchr commented Nov 2, 2023

The polkadot one are all in a single place so that's good.

The Polkadot ones are not special keys, these are just the normal FRAME storage keys.

@xlc
Copy link
Contributor Author

xlc commented Nov 2, 2023

:relay_dispatch_queue_remaining_capacity is definitely a special key but yeah everything else seems just storage keys encoded. Maybe we could refactor them in such why we code the string and have macro to convert them to hex in compile time? So it is more obvious where the hex is coming from.

@bkchr
Copy link
Member

bkchr commented Nov 2, 2023

:relay_dispatch_queue_remaining_capacity

Ahh you got me :P

Maybe we could refactor them in such why we code the string and have macro to convert them to hex in compile time?

In a perfect way we would not need them at all. Or better abstracted. But we already have the hash macros, so it should actually work.

@ggwpez
Copy link
Member

ggwpez commented Mar 13, 2024

I think we should use storage_alias to have type safety.

@ggwpez ggwpez added C1-mentor A task where a mentor is available. Please indicate in the issue who the mentor could be. I4-refactor Code needs refactoring. D1-medium Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase. labels Mar 13, 2024
bkchr pushed a commit that referenced this issue Apr 10, 2024
@dharjeezy
Copy link
Contributor

dharjeezy commented Sep 2, 2024

is this issue just all about creating a cratewell_known_keys in sp_storage and moving the keys specified into it and then refractoring? is there anything else to be done asides that? @ggwpez

@ggwpez
Copy link
Member

ggwpez commented Oct 7, 2024

It seems a bit odd to me that the keys for runtime specific things are specified in sp_storage and not a frame crate.
But since sp-storage cannot depend on frame, we could re-export all well known keys somewhere in frame-support.

Not sure if that is ideal, better ideas?

and then refractoring

Please first add a deprecation warning. If you want to remove something.

@dharjeezy
Copy link
Contributor

@ggwpez can we then put all these keys in the frame support storage crate?

@ggwpez
Copy link
Member

ggwpez commented Oct 28, 2024

We can re-export them there yes, but they are also needed in lower level crates, so they need to stay in there as well.

I guess a common re-export module is the best we can do now.

@dharjeezy
Copy link
Contributor

We can re-export them there yes, but they are also needed in lower level crates, so they need to stay in there as well.

I guess a common re-export module is the best we can do now.

i don't seem to quite get what you mean by a common re-export module? what do you mean? @ggwpez

@ggwpez
Copy link
Member

ggwpez commented Oct 28, 2024

mod well_known_keye {
    pub use sp_storage::...
    pub use sp_consensus_grandpa::...
}

and so on. Although it would probably not cover the ones from polkadot primitives, since what would create a dependency cycle again.

Not sure what @bkchr was thinking.

@bkchr
Copy link
Member

bkchr commented Oct 28, 2024

It seems a bit odd to me that the keys for runtime specific things are specified in sp_storage and not a frame crate.

Because these keys are not FRAME specific. These are Substrate well known keys for communicating data to the node. Things like the code should not require to first call into the runtime.

@bkchr
Copy link
Member

bkchr commented Oct 28, 2024

Generally, maybe just adding a page in the polkadot sdk docs is a better idea. I don't see any reason for adding them somewhere to re-export them, while this ticket is just about being able to find these keys/having a place to look them up.

@dharjeezy
Copy link
Contributor

so, no need to get this done anymore? @bkchr

@bkchr
Copy link
Member

bkchr commented Nov 5, 2024

As I said above, I think there should be a guide/reference doc added to the sdk docs.

@aurexav
Copy link
Contributor

aurexav commented Jan 11, 2025

Recently, I create this https://github.com/subscan-explorer/well-known-address-registry.

I'm interesting to add the well know storage keys there. It might be an option.

And also #324. Subscan team is struggling with the custom pallet account across different chains. 🤷🏻‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C1-mentor A task where a mentor is available. Please indicate in the issue who the mentor could be. D1-medium Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase. I4-refactor Code needs refactoring.
Projects
None yet
Development

No branches or pull requests

5 participants