Skip to content

Commit

Permalink
Don't insert duplicate account codes (#1180)
Browse files Browse the repository at this point in the history
* Don't insert duplicate account codes

* Don't charge l1 fee if code is already in Db

* Fix lints

* StorageInternalCache::exists -> contains_key

* Revert exists/contains_key

* Don't insert code on duplicates
  • Loading branch information
kpp authored Sep 13, 2024
1 parent aeffe80 commit af6584d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 8 additions & 3 deletions crates/evm/src/evm/db_commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ impl<'a, C: sov_modules_api::Context> DatabaseCommit for EvmDb<'a, C> {

if let Some(ref code) = account_info.code {
if !code.is_empty() {
// TODO: would be good to have a contains_key method on the StateMap that would be optimized, so we can check the hash before storing the code
self.code
.set(&account_info.code_hash, code, self.working_set);
let exists_in_db = self
.code
.get(&account_info.code_hash, self.working_set)
.is_some();
if !exists_in_db {
self.code
.set(&account_info.code_hash, code, self.working_set);
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions crates/evm/src/evm/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ fn calc_diff_size<EXT, DB: Database>(
let InnerEvmContext {
journaled_state,
env,
db,
..
} = &mut context.evm.inner;

Expand Down Expand Up @@ -556,9 +557,13 @@ fn calc_diff_size<EXT, DB: Database>(
let account = &state[addr];

if let Some(code) = account.info.code.as_ref() {
// if code is eoa code
diff_size += CODE_KEY_SIZE;
diff_size += code.len();
// Don't charge for account code if it is already in DB.
let db_code = db.code_by_hash(account.info.code_hash)?;
if db_code.is_empty() {
// if code is eoa code
diff_size += CODE_KEY_SIZE;
diff_size += code.len();
}
} else {
native_warn!(
"Code must exist for account when calculating diff: {}",
Expand Down

0 comments on commit af6584d

Please sign in to comment.