From d0baefcfe14d0c5b39ae8f1a9ab924b7add71ac1 Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Mon, 28 Oct 2024 11:08:09 -0400 Subject: [PATCH 1/3] WIP --- src/protocol/vm/engine.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/protocol/vm/engine.rs b/src/protocol/vm/engine.rs index bff17758..c9be0e53 100644 --- a/src/protocol/vm/engine.rs +++ b/src/protocol/vm/engine.rs @@ -68,6 +68,21 @@ where engine } +pub fn update_engine( + db: Arc>, + block: BlockHeader, + vm_storage: Option>, + account_updates: HashMap, +) -> Vec { + // from protosim_py.python.protosim_py.evm.decoders.handle_vm_updates + // Acquire a read lock for the database instance + // let db_write = db.write().unwrap(); + // + // process messages and call db_write.update + + todo!() +} + #[cfg(test)] mod tests { use super::*; From 4c18c89fb6de66e4b6c8579d7b97e93c9f90ec74 Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Mon, 28 Oct 2024 11:08:09 -0400 Subject: [PATCH 2/3] feat: (WIP) Implement update_engine TODO: Remove commented test. --- src/evm/simulation_db.rs | 2 +- src/protocol/vm/engine.rs | 87 +++++++++++++++++++++++++++++++++------ 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/evm/simulation_db.rs b/src/evm/simulation_db.rs index 4d3e3f65..676b51a8 100644 --- a/src/evm/simulation_db.rs +++ b/src/evm/simulation_db.rs @@ -74,7 +74,7 @@ impl<'a, DB: DatabaseRef> DatabaseRef for OverriddenSimulationDB<'a, DB> { } } -#[derive(Debug, Clone, Copy, PartialEq, Default)] +#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Default)] pub struct BlockHeader { pub number: u64, pub hash: H256, diff --git a/src/protocol/vm/engine.rs b/src/protocol/vm/engine.rs index c9be0e53..014a432e 100644 --- a/src/protocol/vm/engine.rs +++ b/src/protocol/vm/engine.rs @@ -9,10 +9,15 @@ use revm::{ primitives::{AccountInfo, Address}, DatabaseRef, }; -use std::{fmt::Debug, sync::Arc}; +use std::{collections::HashMap, fmt::Debug, sync::Arc}; use tokio::sync::RwLock; -use crate::evm::{simulation::SimulationEngine, tycho_db::PreCachedDB}; +use crate::evm::{ + simulation::SimulationEngine, + simulation_db::BlockHeader, + tycho_db::PreCachedDB, + tycho_models::{AccountUpdate, ChangeType, ResponseAccount}, +}; lazy_static! { pub static ref SHARED_TYCHO_DB: Arc> = @@ -68,19 +73,41 @@ where engine } -pub fn update_engine( - db: Arc>, +pub async fn update_engine( + db: Arc>, block: BlockHeader, - vm_storage: Option>, - account_updates: HashMap, + vm_storage: Option>, + account_updates: HashMap, ) -> Vec { - // from protosim_py.python.protosim_py.evm.decoders.handle_vm_updates - // Acquire a read lock for the database instance - // let db_write = db.write().unwrap(); - // - // process messages and call db_write.update + let db_write = db.write().await; + + let mut vm_updates: Vec = Vec::new(); - todo!() + for (_address, account_update) in account_updates.iter() { + vm_updates.push(account_update.clone()); + } + + if let Some(vm_storage_values) = vm_storage { + for (_address, vm_storage_values) in vm_storage_values.iter() { + // ResponseAccount objects to AccountUpdate objects as required by the update method + vm_updates.push(AccountUpdate { + address: vm_storage_values.address, + chain: vm_storage_values.chain, + slots: vm_storage_values.slots.clone(), + balance: Some(vm_storage_values.balance), + code: Some(vm_storage_values.code.clone()), + change: ChangeType::Creation, + }); + } + } + + if !vm_updates.is_empty() { + db_write + .update(vm_updates.clone(), Some(block)) + .await; + } + + vm_updates } #[cfg(test)] @@ -197,4 +224,40 @@ mod tests { // Verify trace flag is set assert!(engine.trace); } + + #[tokio::test] + async fn test_update_engine() { + // TODO Does not accept a MockDatabase - only PreCachedDB + // It's not possible to factor out the `update` method into a Trait since this is an async + // fn and clippy disapproves with `auto trait bounds cannot be specified`. + // Any workaround I tried results in lifetime errors, or requiring me to change the + // PreCachedDB to include mock attributes. It's bad. + // So, unfortunately this can't be tested, but maybe we can take what I wrote in this + // test and use it as a reference for some integration test if we need? + + // let db = create_shared_db_ref(PreCachedDB::new().expect("Failed to create PreCachedDB")); + // let address_a = Address::parse_checksummed( + // String::from("0xA2C5C98A892fD6656a7F39A2f63228C0Bc846270"), + // None, + // ) + // .expect("Invalid checksum"); + // let mut address_a_slots: HashMap = HashMap::new(); + // let mut account_updates: HashMap = HashMap::new(); + // let mut vm_storage: HashMap = HashMap::new(); + // + // address_a_slots.insert(U256::from(0), U256::from(1)); + // account_updates.insert( + // address_a, + // AccountUpdate { + // address: address_a, + // chain: Chain::Ethereum, + // slots: address_a_slots.clone(), + // balance: Some(U256::ZERO), + // code: None, + // change: ChangeType::Update, + // }, + // ); + // update_engine(db, BlockHeader::default(), Some(vm_storage), account_updates).await; + todo!() + } } From 16fad4b187a2ca6eaca886e09f67069db5d48307 Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Tue, 29 Oct 2024 09:12:42 -0400 Subject: [PATCH 3/3] chore: Remove test - We will test this at a later phase. The mocking of this test is too difficult to make work and perhaps not worth the effort. --- src/protocol/vm/engine.rs | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/protocol/vm/engine.rs b/src/protocol/vm/engine.rs index 014a432e..790e19ee 100644 --- a/src/protocol/vm/engine.rs +++ b/src/protocol/vm/engine.rs @@ -224,40 +224,4 @@ mod tests { // Verify trace flag is set assert!(engine.trace); } - - #[tokio::test] - async fn test_update_engine() { - // TODO Does not accept a MockDatabase - only PreCachedDB - // It's not possible to factor out the `update` method into a Trait since this is an async - // fn and clippy disapproves with `auto trait bounds cannot be specified`. - // Any workaround I tried results in lifetime errors, or requiring me to change the - // PreCachedDB to include mock attributes. It's bad. - // So, unfortunately this can't be tested, but maybe we can take what I wrote in this - // test and use it as a reference for some integration test if we need? - - // let db = create_shared_db_ref(PreCachedDB::new().expect("Failed to create PreCachedDB")); - // let address_a = Address::parse_checksummed( - // String::from("0xA2C5C98A892fD6656a7F39A2f63228C0Bc846270"), - // None, - // ) - // .expect("Invalid checksum"); - // let mut address_a_slots: HashMap = HashMap::new(); - // let mut account_updates: HashMap = HashMap::new(); - // let mut vm_storage: HashMap = HashMap::new(); - // - // address_a_slots.insert(U256::from(0), U256::from(1)); - // account_updates.insert( - // address_a, - // AccountUpdate { - // address: address_a, - // chain: Chain::Ethereum, - // slots: address_a_slots.clone(), - // balance: Some(U256::ZERO), - // code: None, - // change: ChangeType::Update, - // }, - // ); - // update_engine(db, BlockHeader::default(), Some(vm_storage), account_updates).await; - todo!() - } }