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

contracts: Refactor trait Ext::*_storage_transparent functions #13600

Merged
merged 7 commits into from
Mar 17, 2023

Conversation

pgherveou
Copy link
Contributor

@pgherveou pgherveou commented Mar 14, 2023

fix #13594

@pgherveou pgherveou requested a review from athei as a code owner March 14, 2023 15:19
@pgherveou pgherveou changed the title merge _transparent methods fix https://github.com/paritytech/substrate/issues/13594 merge _transparent methods Mar 14, 2023
@pgherveou pgherveou marked this pull request as draft March 14, 2023 15:19
@pgherveou pgherveou changed the title merge _transparent methods contracts: Refactor trait Ext::*_storage_transparent functions Mar 14, 2023
@pgherveou pgherveou marked this pull request as ready for review March 14, 2023 19:52
@pgherveou
Copy link
Contributor Author

/ptal @athei

@pgherveou
Copy link
Contributor Author

pgherveou commented Mar 14, 2023

The obvious solution is to refactor the tests to no longer use Ext as an object. Maybe there are other solutions for removing the code duplication.

Went for something a bit easier, to keep the trait object safe.
I moved both the static and variable size key into a new combined enum type.

Generics only fails because the test is using Ext as an object as you mentioned, I can look into doing what you suggested as well, if you think this is a better solution

Copy link
Member

@athei athei left a comment

Choose a reason for hiding this comment

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

I agree. Refactoring the tests is probably not worth it.

frame/contracts/src/exec.rs Show resolved Hide resolved
frame/contracts/src/exec.rs Outdated Show resolved Hide resolved
frame/contracts/src/exec.rs Outdated Show resolved Hide resolved
frame/contracts/src/wasm/runtime.rs Outdated Show resolved Hide resolved
@athei athei requested a review from agryaznov March 15, 2023 14:25
@athei athei added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Mar 15, 2023
@pgherveou pgherveou force-pushed the pg/merge-_transparent-methods branch from 7ed9c97 to 2141598 Compare March 16, 2023 08:57
@pgherveou
Copy link
Contributor Author

@athei replied to your comments, and applied some of the suggestions.

@pgherveou
Copy link
Contributor Author

pgherveou commented Mar 16, 2023

pushed another commit to make these methods take the key by value, since it looks like they are consumed by these methods and don't need to be passed around to other calls.
We can change these signatures and use impl Into<Key<Self::T>> and get rid of all the into(), later on if we refactor the test to not be objectsafe

fn get_storage(&mut self, key: Key<Self::T>) -> Option<Vec<u8>>;
/// Returns `Some(len)` (in bytes) if a storage item exists at `key`.
///
/// Returns `None` if the `key` wasn't previously set by `set_storage` or
/// was deleted.
fn get_storage_size(&mut self, key: Key<Self::T>) -> Option<u32>;

@athei
Copy link
Member

athei commented Mar 16, 2023

I pushed another commit to make these methods take the key by value, since it looks like they are consumed by these methods and don't need to be passed around to other calls.

Is this enough reason to make it pass by value? Usually, when both is viable I go for reference: Passing by value makes the compiler copy the value. I know that logically it is moved but the optimizer is not smart enough to demote this to a pass by reference. Since we are passing an array the whole thing will be copied. For VarSizedKey it is less severe since it is a Vec. But since Key is an enum you even copy the array in this case.

I am just saying: It is not really consumed. It just happens to be not being used after being passed into those functions. I think if you can pass something by reference without having to call clone somewhere you should go for this.

impl Into<Key<Self::T>> would be impl AsRef<Key<Self::T>> in this case.

@pgherveou
Copy link
Contributor Author

I pushed another commit to make these methods take the key by value, since it looks like they are consumed by these methods and don't need to be passed around to other calls.

Is this enough reason to make it pass by value? Usually, when both is viable I go for reference: Passing by value makes the compiler copy the value. I know that logically it is moved but the optimizer is not smart enough to demote this to a pass by reference. Since we are passing an array the whole thing will be copied. For VarSizedKey it is less severe since it is a Vec. But since Key is an enum you even copy the array in this case.

I am just saying: It is not really consumed. It just happens to be not being used after being passed into those functions. I think if you can pass something by reference without having to call clone somewhere you should go for this.

impl Into<Key<Self::T>> would be impl AsRef<Key<Self::T>> in this case.

I pushed another commit to make these methods take the key by value, since it looks like they are consumed by these methods and don't need to be passed around to other calls.

Is this enough reason to make it pass by value? Usually, when both is viable I go for reference: Passing by value makes the compiler copy the value. I know that logically it is moved but the optimizer is not smart enough to demote this to a pass by reference. Since we are passing an array the whole thing will be copied. For VarSizedKey it is less severe since it is a Vec. But since Key is an enum you even copy the array in this case.

I am just saying: It is not really consumed. It just happens to be not being used after being passed into those functions. I think if you can pass something by reference without having to call clone somewhere you should go for this.

impl Into<Key<Self::T>> would be impl AsRef<Key<Self::T>> in this case.

Did not know the perf implication, will try to remember that.
So you only use value when you want to design an API that change ownership, else you default to reference if you have the choice

@athei
Copy link
Member

athei commented Mar 16, 2023

So you only use value when you want to design an API that change ownership, else you default to reference if you have the choice

Exactly

@pgherveou pgherveou force-pushed the pg/merge-_transparent-methods branch from 4d2faa6 to 14372fd Compare March 16, 2023 17:38
@pgherveou
Copy link
Contributor Author

@athei should be good to go, rolled back the commit that used value instead of references and remove the type aliases(kept VarSizedKey but made it private)

Copy link
Member

@athei athei left a comment

Choose a reason for hiding this comment

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

Looking good. Just some nits.

frame/contracts/src/exec.rs Outdated Show resolved Hide resolved
frame/contracts/src/exec.rs Outdated Show resolved Hide resolved
frame/contracts/src/lib.rs Outdated Show resolved Hide resolved
frame/contracts/src/wasm/runtime.rs Outdated Show resolved Hide resolved
frame/contracts/src/wasm/runtime.rs Outdated Show resolved Hide resolved
@pgherveou pgherveou force-pushed the pg/merge-_transparent-methods branch from 00615ce to e8de276 Compare March 17, 2023 11:27
Copy link
Contributor

@agryaznov agryaznov left a comment

Choose a reason for hiding this comment

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

Nice refactoring! LGTM

frame/contracts/src/exec.rs Outdated Show resolved Hide resolved
frame/contracts/src/wasm/runtime.rs Outdated Show resolved Hide resolved
@pgherveou
Copy link
Contributor Author

pgherveou commented Mar 17, 2023

mmm
need to look into this failing job

[2023-03-17 12:49:02]     Checking cumulus-client-consensus-aura v0.1.0 (/builds/parity/mirrors/substrate/cumulus/client/consensus/aura)
[2023-03-17 12:49:02] error[E0432]: unresolved import `sp_keystore::SyncCryptoStorePtr`
[2023-03-17 12:49:02]   --> client/consensus/aura/src/lib.rs:43:5
[2023-03-17 12:49:02]    |
[2023-03-17 12:49:02] 43 | use sp_keystore::SyncCryptoStorePtr;
[2023-03-17 12:49:02]    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `SyncCryptoStorePtr` in the root
[2023-03-17 12:49:02] 
[2023-03-17 12:49:02] For more information about this error, try `rustc --explain E0432`.
[2023-03-17 12:49:02] error: could not compile `cumulus-client-consensus-aura` due to previous error
[2023-03-17 12:49:02] warning: build failed, waiting for other jobs to finish...
[2023-03-17 12:49:02] error: could not compile `cumulus-client-consensus-aura` due to previous error
[2023-03-17 12:51:19] 
Running after_script
00:02
Running after script...
$ env RUSTY_CACHIER_SUPRESS_OUTPUT=true rusty-cachier snapshot destroy
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

looks unrelated to this PR to me 🤔
rebasing...

rewrote commits, stashed the typo changes to remove some diff noise
fixed my unverified email commit
@pgherveou pgherveou force-pushed the pg/merge-_transparent-methods branch from 72cfe6f to 90b5ca5 Compare March 17, 2023 16:04
Copy link
Member

@athei athei left a comment

Choose a reason for hiding this comment

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

Nice

@athei athei merged commit d574cb7 into master Mar 17, 2023
@athei athei deleted the pg/merge-_transparent-methods branch March 17, 2023 22:17
breathx pushed a commit to gear-tech/substrate that referenced this pull request Apr 22, 2023
…itytech#13600)

* Refactor _transparent methods

rewrote commits, stashed the typo changes to remove some diff noise
fixed my unverified email commit

* remove type alias

* Get rid of From<Fix/VarSizedKey> impl blocks

* Get rid of KeyType impl block

* remove unnecessary Key export

* Update frame/contracts/src/exec.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* PR review comment

---------

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
nathanwhit pushed a commit to nathanwhit/substrate that referenced this pull request Jul 19, 2023
…itytech#13600)

* Refactor _transparent methods

rewrote commits, stashed the typo changes to remove some diff noise
fixed my unverified email commit

* remove type alias

* Get rid of From<Fix/VarSizedKey> impl blocks

* Get rid of KeyType impl block

* remove unnecessary Key export

* Update frame/contracts/src/exec.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* PR review comment

---------

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

contracts: Refactor trait Ext::*_storage_transparent functions
3 participants