Skip to content

Commit

Permalink
feat: (WIP) Implement update_engine
Browse files Browse the repository at this point in the history
TODO: Remove commented test.
  • Loading branch information
TAMARA LIPOWSKI committed Oct 28, 2024
1 parent d0baefc commit 4c18c89
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/evm/simulation_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
87 changes: 75 additions & 12 deletions src/protocol/vm/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RwLock<PreCachedDB>> =
Expand Down Expand Up @@ -68,19 +73,41 @@ where
engine
}

pub fn update_engine<D: DatabaseRef + Clone>(
db: Arc<RwLock<D>>,
pub async fn update_engine(
db: Arc<RwLock<PreCachedDB>>,
block: BlockHeader,
vm_storage: Option<HashMap<Bytes, ResponseAccount>>,
account_updates: HashMap<Bytes, AccountUpdate>,
vm_storage: Option<HashMap<Address, ResponseAccount>>,
account_updates: HashMap<Address, AccountUpdate>,
) -> Vec<AccountUpdate> {
// 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<AccountUpdate> = 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)]
Expand Down Expand Up @@ -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<U256, U256> = HashMap::new();
// let mut account_updates: HashMap<Address, AccountUpdate> = HashMap::new();
// let mut vm_storage: HashMap<Address, ResponseAccount> = 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!()
}
}

0 comments on commit 4c18c89

Please sign in to comment.