From 6fe821822f104d247c0177a58cd21a0812e0790c Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Date: Wed, 16 Oct 2024 15:04:48 -0300 Subject: [PATCH] Add code override based on original_size and separate override code test --- Cargo.lock | 1 + crates/telos/node/tests/state_bypass.rs | 51 +++++++++++++++++++--- crates/telos/rpc-engine-api/Cargo.toml | 1 + crates/telos/rpc-engine-api/src/compare.rs | 29 +++++++++--- 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3e9cd5b99bae..8cbd4d54d73c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10624,6 +10624,7 @@ dependencies = [ "revm", "revm-primitives", "serde", + "sha2 0.10.8", "tracing", ] diff --git a/crates/telos/node/tests/state_bypass.rs b/crates/telos/node/tests/state_bypass.rs index 87e860f822f2e..73e4debd033fb 100644 --- a/crates/telos/node/tests/state_bypass.rs +++ b/crates/telos/node/tests/state_bypass.rs @@ -211,9 +211,6 @@ fn test_db_both_sides_present_but_dif() { let init_nonce = 0; let custom_nonce = 69; - let custom_code = Bytes::from(&hex!("ffff")); - let custom_bytecode = RevmBytecode::LegacyRaw(custom_code.clone()); - let revm_acc_info = AccountInfo { balance: init_balance, nonce: init_nonce, @@ -221,7 +218,6 @@ fn test_db_both_sides_present_but_dif() { code: None, }; - let mut db = CacheDB::new(EmptyDBTyped::::new()); db.insert_account_info(test_addr, revm_acc_info); @@ -234,7 +230,7 @@ fn test_db_both_sides_present_but_dif() { address: test_addr, account: "eosio".to_string(), nonce: custom_nonce, - code: custom_code.clone(), + code: Default::default(), balance: custom_balance, }]; @@ -245,12 +241,55 @@ fn test_db_both_sides_present_but_dif() { vec![], vec![], vec![], - true + false ); let db_acc = evm.db_mut().basic(test_addr).unwrap().unwrap(); assert_eq!(db_acc.nonce, statediffs_account[0].nonce); assert_eq!(db_acc.balance, statediffs_account[0].balance); +} + +#[test] +fn test_db_both_sides_only_code() { + let test_addr = Address::from_str("00000000000000000000000000000000deadbeef").unwrap(); + + let custom_code = Bytes::from(&hex!("ffff")); + let custom_bytecode = RevmBytecode::LegacyRaw(custom_code.clone()); + + let revm_acc_info = AccountInfo { + balance: U256::from(0), + nonce: 0, + code_hash: Default::default(), + code: None, + }; + + let mut db = CacheDB::new(EmptyDBTyped::::new()); + db.insert_account_info(test_addr, revm_acc_info); + + let mut state = State::builder().with_database(db).build(); + + let mut evm = Evm::builder().with_db(&mut state).build(); + + let statediffs_account = vec![TelosAccountTableRow { + removed: false, + address: test_addr, + account: "eosio".to_string(), + nonce: 0, + code: custom_code.clone(), + balance: U256::from(0), + }]; + + compare_state_diffs( + &mut evm, + HashMap::default(), + statediffs_account.clone(), + vec![], + vec![], + vec![], + false + ); + + let db_acc = evm.db_mut().basic(test_addr).unwrap().unwrap(); assert_eq!(db_acc.code, Some(custom_bytecode)); } diff --git a/crates/telos/rpc-engine-api/Cargo.toml b/crates/telos/rpc-engine-api/Cargo.toml index 30e0d56832e94..417f4537c2683 100644 --- a/crates/telos/rpc-engine-api/Cargo.toml +++ b/crates/telos/rpc-engine-api/Cargo.toml @@ -16,6 +16,7 @@ reth-storage-errors.workspace = true revm.workspace = true revm-primitives.workspace = true tracing.workspace = true +sha2.workspace = true [lints] workspace = true diff --git a/crates/telos/rpc-engine-api/src/compare.rs b/crates/telos/rpc-engine-api/src/compare.rs index d6aa88e2b8703..089d9cd7c3025 100644 --- a/crates/telos/rpc-engine-api/src/compare.rs +++ b/crates/telos/rpc-engine-api/src/compare.rs @@ -1,10 +1,11 @@ use std::collections::HashSet; use std::fmt::Display; -use alloy_primitives::{Address, B256, U256}; +use alloy_primitives::{Address, B256, Bytes, U256}; use revm_primitives::{Account, AccountInfo, Bytecode, HashMap}; use revm::{Database, Evm, State, TransitionAccount, db::AccountStatus as DBAccountStatus}; use revm_primitives::db::DatabaseCommit; use revm_primitives::state::AccountStatus; +use sha2::{Digest, Sha256}; use tracing::{debug, warn}; use reth_storage_errors::provider::ProviderError; use crate::structs::{TelosAccountStateTableRow, TelosAccountTableRow}; @@ -51,8 +52,10 @@ impl StateOverride { acc.info.balance = telos_row.balance; acc.info.nonce = telos_row.nonce; if telos_row.code.len() > 0 { + acc.info.code_hash = B256::from_slice(Sha256::digest(telos_row.code.as_ref()).as_slice()); acc.info.code = Some(Bytecode::LegacyRaw(telos_row.code.clone())); } else { + acc.info.code_hash = Default::default(); acc.info.code = None; } } @@ -69,10 +72,19 @@ impl StateOverride { acc.info.nonce = nonce; } - pub fn override_code (&mut self, revm_db: &mut &mut State, address: Address, code: Option) { + pub fn override_code (&mut self, revm_db: &mut &mut State, address: Address, maybe_code: Option) { self.maybe_init_account(revm_db, address); let mut acc = self.accounts.get_mut(&address).unwrap(); - acc.info.code = code; + match maybe_code { + None => { + acc.info.code_hash = Default::default(); + acc.info.code = None; + } + Some(code) => { + acc.info.code_hash = B256::from_slice(Sha256::digest(code.as_ref()).as_slice()); + acc.info.code = Some(Bytecode::LegacyRaw(code)); + } + } } pub fn apply (&self, revm_db: &mut &mut State) { @@ -155,13 +167,18 @@ where state_override.override_nonce(revm_db, row.address, row.nonce); } // Check code size inequality - if unwrapped_revm_row.clone().code.is_none() && row.code.len() != 0 || unwrapped_revm_row.clone().code.is_some() && !unwrapped_revm_row.clone().code.unwrap().is_empty() && row.code.len() == 0 { + if (unwrapped_revm_row.clone().code.is_none() && row.code.len() != 0) || + (unwrapped_revm_row.clone().code.is_some() && !unwrapped_revm_row.clone().code.unwrap().original_bytes().len() != row.code.len()) { match revm_db.code_by_hash(unwrapped_revm_row.code_hash) { Ok(code_by_hash) => if (code_by_hash.is_empty() && row.code.len() != 0) || (!code_by_hash.is_empty() && row.code.len() == 0) { - maybe_panic!(panic_mode, "Difference in code existence, address: {:?} - revm: {:?} - tevm: {:?}",row.address,code_by_hash,row.code) + maybe_panic!(panic_mode, "Difference in code existence, address: {:?} - revm: {:?} - tevm: {:?}",row.address,code_by_hash,row.code); + state_override.override_code(revm_db, row.address, Some(row.code.clone())); }, - Err(_) => maybe_panic!(panic_mode, "Difference in code existence, address: {:?} - revm: {:?} - tevm: {:?}",row.address,unwrapped_revm_row.code,row.code), + Err(_) => { + maybe_panic!(panic_mode, "Difference in code existence (Err while searching by code_hash), address: {:?} - revm: {:?} - tevm: {:?}",row.address,unwrapped_revm_row.code,row.code); + state_override.override_code(revm_db, row.address, Some(row.code.clone())); + }, } } // // Check code content inequality