This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Implement EIP-1052 (EXTCODEHASH) and fix several issues in state account cache #9234
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
240c49b
Implement EIP-1052 and fix several issues related to account cache
sorpaas f1c9566
Fix jsontests
sorpaas e21423e
Merge two matches together
sorpaas 48e2c7c
Avoid making unnecessary Arc<Vec>
sorpaas 5c1cd0a
Address grumbles
sorpaas 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
This file contains 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 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 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 |
---|---|---|
|
@@ -230,8 +230,9 @@ impl<Cost: CostType> Interpreter<Cost> { | |
(instruction == instructions::STATICCALL && !schedule.have_static_call) || | ||
((instruction == instructions::RETURNDATACOPY || instruction == instructions::RETURNDATASIZE) && !schedule.have_return_data) || | ||
(instruction == instructions::REVERT && !schedule.have_revert) || | ||
((instruction == instructions::SHL || instruction == instructions::SHR || instruction == instructions::SAR) && !schedule.have_bitwise_shifting) { | ||
|
||
((instruction == instructions::SHL || instruction == instructions::SHR || instruction == instructions::SAR) && !schedule.have_bitwise_shifting) || | ||
(instruction == instructions::EXTCODEHASH && !schedule.have_extcodehash) | ||
{ | ||
return Err(vm::Error::BadInstruction { | ||
instruction: instruction as u8 | ||
}); | ||
|
@@ -568,9 +569,14 @@ impl<Cost: CostType> Interpreter<Cost> { | |
}, | ||
instructions::EXTCODESIZE => { | ||
let address = u256_to_address(&stack.pop_back()); | ||
let len = ext.extcodesize(&address)?; | ||
let len = ext.extcodesize(&address)?.unwrap_or(0); | ||
stack.push(U256::from(len)); | ||
}, | ||
instructions::EXTCODEHASH => { | ||
let address = u256_to_address(&stack.pop_back()); | ||
let hash = ext.extcodehash(&address)?.unwrap_or_else(H256::default); | ||
stack.push(U256::from(hash)); | ||
}, | ||
instructions::CALLDATACOPY => { | ||
Self::copy_data_to_memory(&mut self.mem, stack, params.data.as_ref().map_or_else(|| &[] as &[u8], |d| &*d as &[u8])); | ||
}, | ||
|
@@ -589,9 +595,15 @@ impl<Cost: CostType> Interpreter<Cost> { | |
Self::copy_data_to_memory(&mut self.mem, stack, params.code.as_ref().map_or_else(|| &[] as &[u8], |c| &**c as &[u8])); | ||
}, | ||
instructions::EXTCODECOPY => { | ||
use std::ops::Deref; | ||
|
||
let address = u256_to_address(&stack.pop_back()); | ||
let code = ext.extcode(&address)?; | ||
Self::copy_data_to_memory(&mut self.mem, stack, &code); | ||
Self::copy_data_to_memory( | ||
&mut self.mem, | ||
stack, | ||
code.as_ref().map(|c| &c.deref()[..]).unwrap_or(&[]) | ||
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 |
||
); | ||
}, | ||
instructions::GASPRICE => { | ||
stack.push(params.gas_price.clone()); | ||
|
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
|
@@ -608,9 +608,9 @@ impl<B: Backend> State<B> { | |
} | ||
|
||
/// Get an account's code hash. | ||
pub fn code_hash(&self, a: &Address) -> TrieResult<H256> { | ||
pub fn code_hash(&self, a: &Address) -> TrieResult<Option<H256>> { | ||
self.ensure_cached(a, RequireCache::None, true, | ||
|a| a.as_ref().map_or(KECCAK_EMPTY, |a| a.code_hash())) | ||
|a| a.as_ref().map(|a| a.code_hash())) | ||
} | ||
|
||
/// Get accounts' code size. | ||
|
@@ -911,31 +911,38 @@ impl<B: Backend> State<B> { | |
Ok(pod_state::diff_pod(&pod_state_pre, &pod_state_post)) | ||
} | ||
|
||
// load required account data from the databases. | ||
fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &HashDB<KeccakHasher>) { | ||
/// Load required account data from the databases. Returns whether the cache succeeds. | ||
#[must_use] | ||
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. 👍 We should probably use this more. |
||
fn update_account_cache(require: RequireCache, account: &mut Account, state_db: &B, db: &HashDB<KeccakHasher>) -> bool { | ||
if let RequireCache::None = require { | ||
return; | ||
return true; | ||
} | ||
|
||
if account.is_cached() { | ||
return; | ||
return true; | ||
} | ||
|
||
// if there's already code in the global cache, always cache it localy | ||
let hash = account.code_hash(); | ||
match state_db.get_cached_code(&hash) { | ||
Some(code) => account.cache_given_code(code), | ||
Some(code) => { | ||
account.cache_given_code(code); | ||
true | ||
}, | ||
None => match require { | ||
RequireCache::None => {}, | ||
RequireCache::None => true, | ||
RequireCache::Code => { | ||
if let Some(code) = account.cache_code(db) { | ||
// propagate code loaded from the database to | ||
// the global code cache. | ||
state_db.cache_code(hash, code) | ||
state_db.cache_code(hash, code); | ||
true | ||
} else { | ||
false | ||
} | ||
}, | ||
RequireCache::CodeSize => { | ||
account.cache_code_size(db); | ||
account.cache_code_size(db) | ||
} | ||
} | ||
} | ||
|
@@ -950,21 +957,26 @@ impl<B: Backend> State<B> { | |
if let Some(ref mut maybe_acc) = self.cache.borrow_mut().get_mut(a) { | ||
if let Some(ref mut account) = maybe_acc.account { | ||
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(a)); | ||
Self::update_account_cache(require, account, &self.db, accountdb.as_hashdb()); | ||
return Ok(f(Some(account))); | ||
if Self::update_account_cache(require, account, &self.db, accountdb.as_hashdb()) { | ||
return Ok(f(Some(account))); | ||
} else { | ||
return Err(Box::new(TrieError::IncompleteDatabase(H256::from(a)))); | ||
} | ||
} | ||
return Ok(f(None)); | ||
} | ||
// check global cache | ||
let result = self.db.get_cached(a, |mut acc| { | ||
if let Some(ref mut account) = acc { | ||
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(a)); | ||
Self::update_account_cache(require, account, &self.db, accountdb.as_hashdb()); | ||
if !Self::update_account_cache(require, account, &self.db, accountdb.as_hashdb()) { | ||
return Err(Box::new(TrieError::IncompleteDatabase(H256::from(a)))); | ||
} | ||
} | ||
f(acc.map(|a| &*a)) | ||
Ok(f(acc.map(|a| &*a))) | ||
}); | ||
match result { | ||
Some(r) => Ok(r), | ||
Some(r) => Ok(r?), | ||
None => { | ||
// first check if it is not in database for sure | ||
if check_null && self.db.is_known_null(a) { return Ok(f(None)); } | ||
|
@@ -975,7 +987,9 @@ impl<B: Backend> State<B> { | |
let mut maybe_acc = db.get_with(a, from_rlp)?; | ||
if let Some(ref mut account) = maybe_acc.as_mut() { | ||
let accountdb = self.factories.accountdb.readonly(self.db.as_hashdb(), account.address_hash(a)); | ||
Self::update_account_cache(require, account, &self.db, accountdb.as_hashdb()); | ||
if !Self::update_account_cache(require, account, &self.db, accountdb.as_hashdb()) { | ||
return Err(Box::new(TrieError::IncompleteDatabase(H256::from(a)))); | ||
} | ||
} | ||
let r = f(maybe_acc.as_ref()); | ||
self.insert_cache(a, AccountEntry::new_clean(maybe_acc)); | ||
|
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer
H256::zero
to make it more explicit?