-
Notifications
You must be signed in to change notification settings - Fork 547
Cache code size/hash in storage #893
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
Merged
sorpaas
merged 12 commits into
polkadot-evm:master
from
moonbeam-foundation:jeremy-account-code-metadata
May 31, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
59e36b4
cache code size/hash in storage
nanocryk ef356ce
Merge remote-tracking branch 'origin/master' into jeremy-account-code…
nanocryk 99ff55c
Merge remote-tracking branch 'origin/master' into jeremy-account-code…
nanocryk 0dfb2e0
use in-memory code
nanocryk 0b860ef
don't clone metadata
nanocryk 02b8485
bump evm
nanocryk 40f96da
don't cache empty code metadata + tests
nanocryk b6ea1c1
clippy
nanocryk 2c33399
remove deprecated getter attribute
nanocryk 23a1a8e
Merge remote-tracking branch 'origin/master' into jeremy-account-code…
nanocryk 6b3cb91
remove dep on sha3 crate
nanocryk ffb58e5
feedback
nanocryk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,7 +76,8 @@ use frame_support::{ | |
| }; | ||
| use frame_system::RawOrigin; | ||
| use impl_trait_for_tuples::impl_for_tuples; | ||
| use sp_core::{Hasher, H160, H256, U256}; | ||
| use scale_info::TypeInfo; | ||
| use sp_core::{Decode, Encode, Hasher, H160, H256, U256}; | ||
| use sp_runtime::{ | ||
| traits::{BadOrigin, Saturating, UniqueSaturatedInto, Zero}, | ||
| AccountId32, DispatchErrorWithPostInfo, | ||
|
|
@@ -512,6 +513,10 @@ pub mod pallet { | |
| #[pallet::storage] | ||
| pub type AccountCodes<T: Config> = StorageMap<_, Blake2_128Concat, H160, Vec<u8>, ValueQuery>; | ||
|
|
||
| #[pallet::storage] | ||
| pub type AccountCodesMetadata<T: Config> = | ||
| StorageMap<_, Blake2_128Concat, H160, CodeMetadata, OptionQuery>; | ||
|
|
||
| #[pallet::storage] | ||
| pub type AccountStorages<T: Config> = | ||
| StorageDoubleMap<_, Blake2_128Concat, H160, Blake2_128Concat, H256, H256, ValueQuery>; | ||
|
|
@@ -525,6 +530,21 @@ pub type BalanceOf<T> = | |
| type NegativeImbalanceOf<C, T> = | ||
| <C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance; | ||
|
|
||
| #[derive(Debug, Clone, Copy, Eq, PartialEq, Encode, Decode, TypeInfo)] | ||
| pub struct CodeMetadata { | ||
| pub size: u64, | ||
| pub hash: H256, | ||
| } | ||
|
|
||
| impl CodeMetadata { | ||
| fn from_code(code: &[u8]) -> Self { | ||
| let size = code.len() as u64; | ||
| let hash = H256::from(sp_io::hashing::keccak_256(code)); | ||
|
|
||
| Self { size, hash } | ||
| } | ||
| } | ||
|
|
||
| pub trait EnsureAddressOrigin<OuterOrigin> { | ||
| /// Success return type. | ||
| type Success; | ||
|
|
@@ -720,6 +740,7 @@ impl<T: Config> Pallet<T> { | |
| } | ||
|
|
||
| <AccountCodes<T>>::remove(address); | ||
| <AccountCodesMetadata<T>>::remove(address); | ||
| #[allow(deprecated)] | ||
| let _ = <AccountStorages<T>>::remove_prefix(address, None); | ||
| } | ||
|
|
@@ -735,9 +756,40 @@ impl<T: Config> Pallet<T> { | |
| let _ = frame_system::Pallet::<T>::inc_sufficients(&account_id); | ||
| } | ||
|
|
||
| // Update metadata. | ||
| let meta = CodeMetadata::from_code(&code); | ||
| <AccountCodesMetadata<T>>::insert(address, meta); | ||
|
|
||
| <AccountCodes<T>>::insert(address, code); | ||
| } | ||
|
|
||
| /// Get the account metadata (hash and size) from storage if it exists, | ||
| /// or compute it from code and store it if it doesn't exist. | ||
| pub fn account_code_metadata(address: H160) -> CodeMetadata { | ||
|
Member
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 think this function should return an option to express the case where the account not have any code. |
||
| if let Some(meta) = <AccountCodesMetadata<T>>::get(address) { | ||
| return meta; | ||
| } | ||
|
|
||
| let code = <AccountCodes<T>>::get(address); | ||
|
|
||
| // If code is empty we return precomputed hash for empty code. | ||
| // We don't store it as this address could get code deployed in the future. | ||
| if code.is_empty() { | ||
| const EMPTY_CODE_HASH: [u8; 32] = hex_literal::hex!( | ||
| "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470" | ||
| ); | ||
| return CodeMetadata { | ||
| size: 0, | ||
| hash: EMPTY_CODE_HASH.into(), | ||
| }; | ||
| } | ||
|
|
||
| let meta = CodeMetadata::from_code(&code); | ||
|
|
||
| <AccountCodesMetadata<T>>::insert(address, meta); | ||
| meta | ||
| } | ||
|
|
||
| /// Get the account basic in EVM format. | ||
| pub fn account_basic(address: &H160) -> (Account, frame_support::weights::Weight) { | ||
| let account_id = T::AddressMapping::into_account_id(*address); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.