-
Notifications
You must be signed in to change notification settings - Fork 255
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
Typed Storage Keys #1419
Typed Storage Keys #1419
Changes from 35 commits
eeded2c
4f052d0
e4e4437
3f20d77
3f0d8f0
28e1b77
489ccdc
23203da
7a36ce3
d7c658d
b21e94d
252e31e
db12bd7
5313f6a
8e1f77f
a2ad8d1
cef1f6b
62fd01f
394b206
b131b0f
1b856bc
b0ddafa
3202601
0b4f2ea
4c750f4
a404c85
e87eb3c
9c27b47
585c01c
4bfaa4c
bd01aab
a284ed8
5a68e8e
e4195b2
97962ee
8f0eb04
9b33cbc
b7a6b41
aa5e703
1eaa75d
56f1864
c5e071d
4a3b936
a660a67
f7a530c
26947b5
81cd16e
1a42c9e
809331e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -39,15 +39,21 @@ pub enum CodegenError { | |||||
#[error("Call variant for type {0} must have all named fields. Make sure you are providing a valid substrate-based metadata")] | ||||||
InvalidCallVariant(u32), | ||||||
/// Type should be an variant/enum. | ||||||
#[error( | ||||||
"{0} type should be an variant/enum type. Make sure you are providing a valid substrate-based metadata" | ||||||
)] | ||||||
#[error("{0} type should be an variant/enum type. Make sure you are providing a valid substrate-based metadata")] | ||||||
InvalidType(String), | ||||||
/// Extrinsic call type could not be found. | ||||||
#[error( | ||||||
"Extrinsic call type could not be found. Make sure you are providing a valid substrate-based metadata" | ||||||
)] | ||||||
#[error("Extrinsic call type could not be found. Make sure you are providing a valid substrate-based metadata")] | ||||||
MissingCallType, | ||||||
/// There are too many or too few hashers. | ||||||
#[error("Could not Generate functions for storage entry {storage_entry_name}. There are {key_count} keys, but only {hasher_count} hashers. The number of hashers must equal the number of keys or be exactly 1.")] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
InvalidStorageHasherCount { | ||||||
/// The name of the storage entry | ||||||
storage_entry_name: String, | ||||||
/// Number of keys | ||||||
key_count: usize, | ||||||
/// Number of hashers | ||||||
hasher_count: usize, | ||||||
}, | ||||||
/// Cannot generate types. | ||||||
#[error("Type Generation failed: {0}")] | ||||||
TypeGeneration(#[from] TypegenError), | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -475,6 +475,35 @@ pub enum StorageHasher { | |
Identity, | ||
} | ||
|
||
impl StorageHasher { | ||
/// The hash produced by a [`StorageHasher`] can have these two components, in order: | ||
/// | ||
/// 1. A fixed size hash. (not present for [`StorageHasher::Identity`]). | ||
/// 2. The SCALE encoded key that was used as an input to the hasher (only present for | ||
/// [`StorageHasher::Twox64Concat`], [`StorageHasher::Blake2_128Concat`] or [`StorageHasher::Identity`]). | ||
/// | ||
/// This function returns the number of bytes used to represent the first of these. | ||
pub fn len_excluding_key(&self) -> usize { | ||
match self { | ||
StorageHasher::Blake2_128Concat => 16, | ||
StorageHasher::Twox64Concat => 8, | ||
StorageHasher::Blake2_128 => 16, | ||
StorageHasher::Blake2_256 => 32, | ||
StorageHasher::Twox128 => 16, | ||
StorageHasher::Twox256 => 32, | ||
StorageHasher::Identity => 0, | ||
} | ||
} | ||
|
||
/// Returns true if the key used to produce the hash is appended to the hash itself. | ||
pub fn ends_with_key(&self) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I find this is a bit hard to hard to read, maybe we could do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chanegd it from
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Either looks good to me :D |
||
matches!( | ||
self, | ||
StorageHasher::Blake2_128Concat | StorageHasher::Twox64Concat | StorageHasher::Identity | ||
) | ||
} | ||
} | ||
|
||
/// Is the storage entry optional, or does it have a default value. | ||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
pub enum StorageEntryModifier { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,11 +38,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
// Get back an iterator of results. | ||
let mut results = api.storage().at_latest().await?.iter(storage_query).await?; | ||
|
||
while let Some(Ok((key, value))) = results.next().await { | ||
println!("Key: 0x{}", hex::encode(&key)); | ||
println!("Value: {:?}", value); | ||
while let Some(Ok(kv)) = results.next().await { | ||
println!("Keys decoded: {:?}", kv.keys); | ||
println!("Key: 0x{}", hex::encode(&kv.key_bytes)); | ||
println!("Value: {:?}", kv.value); | ||
Comment on lines
+42
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The examples look great! |
||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
key_count and hasher_count can't be 0 right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in reality, this is unlikely, but if they were, we just return
vec![]
below.