Skip to content

Commit

Permalink
Merge pull request #23 from propeller-heads/tnl/ENG-3752-update-engine
Browse files Browse the repository at this point in the history
feat: Implement update_engine
  • Loading branch information
tamaralipows authored Oct 29, 2024
2 parents 6d3d437 + 16fad4b commit f2f3711
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 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
46 changes: 44 additions & 2 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,6 +73,43 @@ where
engine
}

pub async fn update_engine(
db: Arc<RwLock<PreCachedDB>>,
block: BlockHeader,
vm_storage: Option<HashMap<Address, ResponseAccount>>,
account_updates: HashMap<Address, AccountUpdate>,
) -> Vec<AccountUpdate> {
let db_write = db.write().await;

let mut vm_updates: Vec<AccountUpdate> = Vec::new();

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)]
mod tests {
use super::*;
Expand Down

0 comments on commit f2f3711

Please sign in to comment.