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

Distinct handling for N fields + 1 hasher vs N fields + N hashers #458

Merged
merged 11 commits into from
Feb 21, 2022

Conversation

jsdw
Copy link
Collaborator

@jsdw jsdw commented Feb 18, 2022

Tests still needed before this merges (https://github.com/paritytech/substrate/blob/f318350b6410ef47e438aae24c0b4779373d0a7b/frame/assets/src/lib.rs#L274 is example of StorageNMap item).

I added a few general storage tests, and a specific one that is fixed by this PR and broken before it.

Closes #446


let key_impl = if hashers.len() == fields.len() {
// If the number of hashers matches the number of fields, we're dealing with a
// StorageNMap, and each field should be hashed separately according to the
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment not strictly accurate, as it would be possible (although useless) to have a single tuple key e.g. (KeyType,) for a regular StorageMap

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tweaked the comment to "something shaped like a StorageNMap"; does that make more sense?

I guess in the 1-key version it doesn't really matter what branch we follow as both have the same effect anyway.

@jsdw jsdw requested review from a team and ascjones February 18, 2022 21:14
Copy link
Contributor

@ascjones ascjones left a comment

Choose a reason for hiding this comment

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

LGTM!

Ok(())
}

// This fails until the fix in https://github.com/paritytech/subxt/pull/458 is introduced.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can remove this self referential comment now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wanted it there more as a record for why the slightly random looking test exists for future onlookers :)

Copy link
Contributor

@maciejhirsz maciejhirsz left a comment

Choose a reason for hiding this comment

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

Minor nits

// something shaped like a StorageNMap, and each field should be hashed separately
// according to the corresponding hasher.
let keys = (0..tuple.fields().len())
.into_iter()
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't think you need this, range should be an iterator already

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I always forget this (because they probably shouldn't be :D)

// If the number of hashers matches the number of fields, we're dealing with
// something shaped like a StorageNMap, and each field should be hashed separately
// according to the corresponding hasher.
let keys = (0..tuple.fields().len())
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd have probably gone with hashers.into_iter().enumerate().take(fields.len()), but that's just how my brain works with iterators, this is fine.

I'd change tuple.fields().len() to fields.len() though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point; I think I'd have gone for the same sort of thing if I was approaching it fresh :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes that's much more intuitive, I am responsible for this hacky approach 🙈

// StorageMap.
let hasher = hashers.get(0).expect("checked for 1 hasher");
let items =
(0..tuple.fields().len()).into_iter().map(|field_idx| {
Copy link
Contributor

Choose a reason for hiding this comment

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

Ditto re fields.len() and into_iterator().

Copy link
Contributor

@dvdplm dvdplm left a comment

Choose a reason for hiding this comment

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

lgtm

Ok(())
}

// This fails until the fix in https://github.com/paritytech/subxt/pull/458 is introduced.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// This fails until the fix in https://github.com/paritytech/subxt/pull/458 is introduced.
// See https://github.com/paritytech/subxt/pull/458 for the motivation.

…maybe?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm easy either way :)

let ctx = test_context().await;

// Boilerplate; we create a new asset class with ID 99, and then
// we "approveTransfer" of some of this asset class. This Gives us an
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// we "approveTransfer" of some of this asset class. This Gives us an
// we "approveTransfer" of some of this asset class. This gives us an

@jsdw jsdw merged commit 70d83fe into master Feb 21, 2022
@jsdw jsdw deleted the jsdw-storage-map-bug branch February 21, 2022 14:56
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.

Issue querying storage with tuple key
4 participants