Skip to content

Commit

Permalink
Add code override based on original_size and separate override code test
Browse files Browse the repository at this point in the history
  • Loading branch information
guilledk committed Oct 16, 2024
1 parent 7b87e5a commit 6fe8218
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 12 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 45 additions & 6 deletions crates/telos/node/tests/state_bypass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,17 +211,13 @@ 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,
code_hash: Default::default(),
code: None,
};


let mut db = CacheDB::new(EmptyDBTyped::<MockDBError>::new());
db.insert_account_info(test_addr, revm_acc_info);

Expand All @@ -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,
}];

Expand All @@ -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::<MockDBError>::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));
}

Expand Down
1 change: 1 addition & 0 deletions crates/telos/rpc-engine-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 23 additions & 6 deletions crates/telos/rpc-engine-api/src/compare.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -69,10 +72,19 @@ impl StateOverride {
acc.info.nonce = nonce;
}

pub fn override_code<DB: Database> (&mut self, revm_db: &mut &mut State<DB>, address: Address, code: Option<Bytecode>) {
pub fn override_code<DB: Database> (&mut self, revm_db: &mut &mut State<DB>, address: Address, maybe_code: Option<Bytes>) {
self.maybe_init_account(revm_db, address);
let mut acc = self.accounts.get_mut(&address).unwrap();

Check warning on line 77 in crates/telos/rpc-engine-api/src/compare.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable
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<DB: Database> (&self, revm_db: &mut &mut State<DB>) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6fe8218

Please sign in to comment.