Skip to content

storage_type: Strip key proper hash and entry bytes (32 instead of 16)#1522

Merged
lexnv merged 4 commits intomasterfrom
lexnv/dyn-storage-iter
Apr 9, 2024
Merged

storage_type: Strip key proper hash and entry bytes (32 instead of 16)#1522
lexnv merged 4 commits intomasterfrom
lexnv/dyn-storage-iter

Conversation

@lexnv
Copy link
Copy Markdown
Collaborator

@lexnv lexnv commented Apr 8, 2024

This PR ensures that we can properly decode storage key addresses.

There was an issue confirmed by #1517, where the decoding of keys for dynamic storage queries would always fail at:

let keys = <Address::Keys as StorageKey>::decode_storage_key(
cursor,
&mut hashers.iter(),
metadata.types(),
)?;

After investigating the code path, the error is coming from the following lines:

// We've consumed all of the hashers, so we expect to also consume all of the bytes:
if !bytes.is_empty() {
return Err(StorageAddressError::TooManyBytes.into());
}

The check appeared to be correct, as well as consuming hasher keys via consume_hash_returning_key_bytes.

After inspecting the key address bytes, we wrongly assumed the storage prefix bytes length.

Our storage keys start with the following:

twox_128(storage prefix) ++ twox_128(storage entry name)

In the issue, it is twox_128(System) ++ twox_128(Accounts).

The output of the twox is: 16 bytes, not 8 as stated and assumed in our code base.

Testing Done

  • reproduced the issue via storage_iter_dynamic example
  • Compared addr bytes with expected prefix:
    • twox_128("System"): 26aa394eea5630e07c48ae0c9558cef7
    • twox_128("Account"): b99d880ec681799c0cf30e8886371da9

Closes: #1517

lexnv added 2 commits April 9, 2024 00:11
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
@lexnv lexnv requested a review from a team as a code owner April 8, 2024 21:18
Copy link
Copy Markdown
Contributor

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test for iterating over storage keys would be nice :)

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
@jsdw
Copy link
Copy Markdown
Collaborator

jsdw commented Apr 9, 2024

Awesome; thanks for digging into this!

@lexnv lexnv merged commit c07ee0c into master Apr 9, 2024
@lexnv lexnv deleted the lexnv/dyn-storage-iter branch April 9, 2024 11:08
lexnv added a commit that referenced this pull request Apr 9, 2024
#1522)

* storage_type: Strip key proper hash and entry bytes (32 instead of 16)

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage_type: Fix typo

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* storage/tests: Check keys decode properly and don't cause errors

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>

* Update testing/integration-tests/src/full_client/storage/mod.rs

Co-authored-by: James Wilson <james@jsdw.me>

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
Co-authored-by: James Wilson <james@jsdw.me>
@jsdw jsdw mentioned this pull request May 16, 2024
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

Successfully merging this pull request may close these issues.

Dynamic storage iter example no longer works

3 participants