Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass in post state instead of extending #47

Merged
merged 1 commit into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,13 @@ where
//
// NOTE: we will be able to refactor to do rollbacks after the following is merged:
// https://github.com/paradigmxyz/reth/pull/3512
let mut tmp_db = db.clone();
let mut execution_db = db.clone();
let mut execution_post_state = post_state.clone();

let mut bundle = bundle.0;
let execution = execute(
&mut tmp_db,
&mut execution_db,
&mut execution_post_state,
&cfg_env,
&block_env,
cumulative_gas_used,
Expand All @@ -593,9 +595,9 @@ where
coinbase_payment += execution.coinbase_payment;
cumulative_gas_used = execution.cumulative_gas_used;
txs.append(&mut bundle);
post_state.extend(execution.post_state);

db = tmp_db;
db = execution_db;
post_state = execution_post_state;
}
Err(_) => continue,
}
Expand All @@ -619,6 +621,7 @@ where
// NOTE: we do not need to clone the DB here as we do for bundle execution
let execution = execute(
&mut db,
&mut post_state,
&cfg_env,
&block_env,
cumulative_gas_used,
Expand All @@ -629,7 +632,6 @@ where
coinbase_payment += execution.coinbase_payment;
cumulative_gas_used = execution.cumulative_gas_used;
txs.push(recovered_tx);
post_state.extend(execution.post_state);
}
// if we have any transaction error other than the nonce being too low, then we mark
// the transaction invalid
Expand Down Expand Up @@ -668,6 +670,7 @@ where
// if the payment transaction fails, then the entire payload build fails
let execution = execute(
&mut db,
&mut post_state,
&cfg_env,
&block_env,
cumulative_gas_used,
Expand All @@ -676,7 +679,6 @@ where
.map_err(PayloadBuilderError::EvmExecutionError)?;
cumulative_gas_used = execution.cumulative_gas_used;
txs.push(payment_tx);
post_state.extend(execution.post_state);
}

// NOTE: here we assume post-shanghai
Expand Down Expand Up @@ -710,13 +712,13 @@ where

#[derive(Clone, Debug)]
struct Execution {
post_state: PostState,
cumulative_gas_used: u64,
coinbase_payment: U256,
}

fn execute<S, I>(
db: &mut CacheDB<Arc<State<S>>>,
post_state: &mut PostState,
cfg_env: &CfgEnv,
block_env: &BlockEnv,
mut cumulative_gas_used: u64,
Expand All @@ -732,8 +734,6 @@ where
let coinbase_acct = db.basic(block_env.coinbase).map_err(EVMError::Database)?;
let initial_coinbase_balance = coinbase_acct.map_or(U256::ZERO, |acct| acct.balance);

let mut post_state = PostState::default();

for tx in txs {
// construct EVM
let tx_env = tx_env_with_recovered(&tx);
Expand All @@ -749,7 +749,7 @@ where
let ResultAndState { result, state } = evm.transact()?;

// commit changes to DB and post state
commit_state_changes(db, &mut post_state, block_num, state, true);
commit_state_changes(db, post_state, block_num, state, true);

cumulative_gas_used += result.gas_used();

Expand All @@ -769,7 +769,6 @@ where
compute_coinbase_payment(&block_env.coinbase, initial_coinbase_balance, &post_state);

Ok(Execution {
post_state,
cumulative_gas_used,
coinbase_payment,
})
Expand Down Expand Up @@ -950,6 +949,7 @@ mod tests {

let state = State::new(state);
let mut db = CacheDB::new(Arc::new(state));
let mut post_state = PostState::default();

// builder will be the coinbase (i.e. beneficiary)
let builder_wallet = LocalWallet::new(&mut rand::thread_rng());
Expand All @@ -973,10 +973,16 @@ mod tests {
);

// execute the transfer transaction
let execution = execute(&mut db, &cfg_env, &block_env, 0, Some(transfer_tx))
.expect("execution doesn't fail");
let execution = execute(
&mut db,
&mut post_state,
&cfg_env,
&block_env,
0,
Some(transfer_tx),
)
.expect("execution doesn't fail");
let Execution {
post_state,
cumulative_gas_used,
coinbase_payment,
} = execution;
Expand Down Expand Up @@ -1046,6 +1052,7 @@ mod tests {

let state = State::new(state);
let mut db = CacheDB::new(Arc::new(state));
let mut post_state = PostState::default();

// builder will be the coinbase (i.e. beneficiary)
let builder_wallet = LocalWallet::new(&mut rand::thread_rng());
Expand All @@ -1068,13 +1075,18 @@ mod tests {
sender_nonce,
);

let execution = execute(&mut db, &cfg_env, &block_env, 0, Some(call_tx))
.expect("execution doesn't fail");
let execution = execute(
&mut db,
&mut post_state,
&cfg_env,
&block_env,
0,
Some(call_tx),
)
.expect("execution doesn't fail");
let Execution {
post_state,
coinbase_payment,
cumulative_gas_used,
..
} = execution;

// check coinbase payment
Expand Down