-
Notifications
You must be signed in to change notification settings - Fork 30
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
Implement stable type id #182
Comments
Just to note that we do such hashing in Subxt already outside of In order to hash types in this way, one needs to iterate through them once the type registry has been built, so I wonder whether there is any utility in adding this logic to scale-info itself rather than some logic on top? Also, funnily enough we have a PR open for a |
One more requirement I would like to add: I want to use this in wasm runtime so it should be efficient. It will be great if the derive macro can just generate the hash at compile time and expose it as a const. |
I think I may have misunderstood this before, because there are (at least) two ways one could hash types:
Are you looking for something more like 1? Also, out of curiosity because I want to understand how it would be used better; do you have any examples for things you want to do with this hash? |
I am not 100% sure what’s my requirements so let me give you my use case and see if we can come up something together. I have this idea when working on open-web3-stack/open-runtime-module-library#927 Essentially I want a AnyMap that the value can be any type and yet I want some type safety. The normal Rust solution will be using std::TypeId but it could change across compiler version and therefore not suitable for persistent. So I want a stable TypeId and use it as the key for this storage map. This ensures I can always decode the value into the correct type. Another potential use case is store the type of along with encoded data and be able to detect unexpected format breaking change or bad decode at runtime. |
Aah ok, I think I see! To re-iterate (just to make sure I do understand):
Similar limitations to the rust I realise I was a little wrong above; I imagine that it would actually be possible to write a function that takes some arbitrary type which implements
I think instead, I'd keep this separate from fn get_type_hash<T: TypeInfo>() -> [u8; 32] {
// ... actual hashing logic here
}
trait TypeInfoHash: TypeInfo {
fn type_hash() -> [u8; 32] {
// default impl gets hash at runtime:
get_type_hash()
};
}
It would be quite a lot of work I think, but it's all work that would need doing one way or the other anyway; nothing is really saved by doing it in the |
I like your idea. For generic types, I guess we could do something like: hash( To calculate hash of In this way, we can calculate the hash of generic types at runtime relatively efficient because hash(T1) should be a constant (provided T1 does not have generic type parameter). But yeah this is indeed a lot more complicated than my initial estimation and I have found another solution to my original problem so I don't need this right now. |
Something similar to TypeId but that's stable. i.e. It never changes as long as the shape of the type renames the same.
This will be useful to detect unexpected scale codec breaking change, implement safe any type map, etc.
Basically this should be a deterministic secure hash of the
Type
withoutpath
.The text was updated successfully, but these errors were encountered: