Skip to content

Commit

Permalink
add new function to ExecutionDB
Browse files Browse the repository at this point in the history
  • Loading branch information
fborello-lambda committed Nov 4, 2024
1 parent f29ec4b commit b2e429d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/l2/proposer/prover_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl ProverServer {

let block = Block::new(header, body);

let (db, _) = ExecutionDB::from_exec(&block, &self.store).map_err(|err| err.to_string())?;
let db = ExecutionDB::from_exec(&block, &self.store).map_err(|err| err.to_string())?;

let parent_header = self
.store
Expand Down
4 changes: 2 additions & 2 deletions crates/l2/utils/save_prover_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ mod tests {

// Write all
for block in &blocks {
let (_, account_updates) =
ExecutionDB::from_exec(blocks.last().unwrap(), &store).unwrap();
let account_updates =
ExecutionDB::get_account_updates(blocks.last().unwrap(), &store).unwrap();

account_updates_vec.push(account_updates.clone());

Expand Down
38 changes: 28 additions & 10 deletions crates/vm/execution_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,29 @@ pub struct ExecutionDB {
}

impl ExecutionDB {
/// Creates a database and returns the ExecutionDB and account_updates by executing a block,
/// Creates a database and returns the ExecutionDB by executing a block,
/// without performing any validation.
pub fn from_exec(
pub fn from_exec(block: &Block, store: &Store) -> Result<Self, ExecutionDBError> {
// TODO: perform validation to exit early
let account_updates = Self::get_account_updates(block, store)?;
Self::from_account_updates(account_updates, block, store)
}

/// Creates a database and returns the ExecutionDB from a Vec<[AccountUpdate]>,
/// without performing any validation.
pub fn from_account_updates(
account_updates: Vec<AccountUpdate>,
block: &Block,
store: &Store,
) -> Result<(Self, Vec<AccountUpdate>), ExecutionDBError> {
) -> Result<Self, ExecutionDBError> {
// TODO: perform validation to exit early

let mut state = evm_state(store.clone(), block.header.parent_hash);
let mut store_wrapper = StoreWrapper {
store: store.clone(),
block_hash: block.header.parent_hash,
};

let chain_config = store.get_chain_config()?;

execute_block(block, &mut state).map_err(Box::new)?;

let account_updates = get_state_transitions(&mut state);

let mut accounts = HashMap::new();
let code = HashMap::new(); // TODO: `code` remains empty for now
let mut storage = HashMap::new();
Expand Down Expand Up @@ -90,7 +93,22 @@ impl ExecutionDB {
chain_config,
};

Ok((execution_db, account_updates))
Ok(execution_db)
}

/// Gets the Vec<[AccountUpdate]>/StateTransitions obtained after executing a block.
pub fn get_account_updates(
block: &Block,
store: &Store,
) -> Result<Vec<AccountUpdate>, ExecutionDBError> {
// TODO: perform validation to exit early

let mut state = evm_state(store.clone(), block.header.parent_hash);

execute_block(block, &mut state).map_err(Box::new)?;

let account_updates = get_state_transitions(&mut state);
Ok(account_updates)
}

pub fn get_chain_config(&self) -> ChainConfig {
Expand Down

0 comments on commit b2e429d

Please sign in to comment.