Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add translate function for storage prefixed map. #4555

Merged
merged 4 commits into from
Jan 13, 2020

Conversation

gui1117
Copy link
Contributor

@gui1117 gui1117 commented Jan 7, 2020

Add a function to migrate values from one type to one another.

In case we fail to migrate one element we return an error with the key that has failed to decoded.
We could also remove values that fail to decode and finish the migration and at the end returning an error saying there is some value that has been removed.

@gui1117 gui1117 added the A3-in_progress Pull request is in progress. No review needed at this stage. label Jan 7, 2020
.filter(|n| n.starts_with(&prefix[..]))
{
let value: OldValue = unhashed::get(&next_key)
// We failed to read the value. Stop the translation and return an error.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

in case the value can't be decoded then we return an error, but what should we do then.
In the case of linkedmap if an element fails to decode during the translation then we end the linkedmap at the last correctly decoded element and we return an error.

What about here should we remove this value and continue to decode following values or should we stop at the first failure ?

Copy link
Member

@shawntabrizi shawntabrizi Jan 7, 2020

Choose a reason for hiding this comment

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

What about here should we remove this value and continue to decode following values

This makes sense. Stopping at the first failure means that half of the storage will be one type and the other half another type. This would also prevent us from ever being able to call translate_value again. Removing the value seems like the best thing we can do without breaking the module using this storage.

@gui1117 gui1117 added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Jan 7, 2020
@gui1117
Copy link
Contributor Author

gui1117 commented Jan 7, 2020

related comment in regard to which implementation in case of error: #4052 (comment)

@gui1117 gui1117 added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Jan 8, 2020
@gui1117
Copy link
Contributor Author

gui1117 commented Jan 9, 2020

now key that couldn't be decoded are removed and an error is returned with the number of key that failed to decode.

EDIT: actually maybe better not to remove them and just let them untranslated in the storage, anyway this will only happen if the storage is corrupted or the translate function expect wrong type. in both case I don't think we can solve anything from our side.

@gui1117 gui1117 added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Jan 9, 2020
Copy link
Member

@bkchr bkchr left a comment

Choose a reason for hiding this comment

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

Some test nitpick, otherwise it looks good.

assert_eq!(MyStorage::iter().collect::<Vec<_>>(), vec![]);

// test migration
unhashed::put(&[&k[..], &vec![1][..]].concat(), &1u128);
Copy link
Member

Choose a reason for hiding this comment

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

You inserting a u128 and use u32 in the translate function. You should actually store a u32.

And then call iter() to make sure that an empty vec is returned.

Than you should to the translation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yes I updated tests

@shawntabrizi
Copy link
Member

EDIT: actually maybe better not to remove them and just let them untranslated in the storage, anyway this will only happen if the storage is corrupted or the translate function expect wrong type. in both case I don't think we can solve anything from our side.

If you leave them untranslated, runtime code could really get messed up. Better to remove data then to have bad data where the runtime assumes it is good. imo

@gavofyork gavofyork requested a review from bkchr January 13, 2020 08:13
@gavofyork gavofyork added A8-looksgood and removed A0-please_review Pull request needs code review. labels Jan 13, 2020
@gavofyork gavofyork merged commit 857f733 into master Jan 13, 2020
@gavofyork gavofyork deleted the gui-translate-prefixed-storage branch January 13, 2020 11:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants