diff --git a/crates/evm/src/evm/db_commit.rs b/crates/evm/src/evm/db_commit.rs index b0aac5dce..4223c78b0 100644 --- a/crates/evm/src/evm/db_commit.rs +++ b/crates/evm/src/evm/db_commit.rs @@ -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); + } } } diff --git a/crates/evm/src/evm/handler.rs b/crates/evm/src/evm/handler.rs index 6e47144f1..1d8906c0b 100644 --- a/crates/evm/src/evm/handler.rs +++ b/crates/evm/src/evm/handler.rs @@ -433,6 +433,7 @@ fn calc_diff_size( let InnerEvmContext { journaled_state, env, + db, .. } = &mut context.evm.inner; @@ -556,9 +557,13 @@ fn calc_diff_size( 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: {}",