use alloc::{boxed::Box, format, string::ToString, vec::Vec};
use core::fmt::Display;
use crate::ConfigureEvm;
use alloy_eips::eip7002::{WithdrawalRequest, WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS};
use reth_execution_errors::{BlockExecutionError, BlockValidationError};
use reth_primitives::{Buf, Header, Request};
use revm::{interpreter::Host, Database, DatabaseCommit, Evm};
use revm_primitives::{
Address, BlockEnv, Bytes, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, FixedBytes,
ResultAndState,
};
pub fn post_block_withdrawal_requests_contract_call<EvmConfig, DB>(
evm_config: &EvmConfig,
db: &mut DB,
initialized_cfg: &CfgEnvWithHandlerCfg,
initialized_block_env: &BlockEnv,
) -> Result<Vec<Request>, BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: Display,
EvmConfig: ConfigureEvm<Header = Header>,
{
let mut evm_post_block = Evm::builder()
.with_db(db)
.with_env_with_handler_cfg(EnvWithHandlerCfg::new_with_cfg_env(
initialized_cfg.clone(),
initialized_block_env.clone(),
Default::default(),
))
.build();
apply_withdrawal_requests_contract_call(evm_config, &mut evm_post_block)
}
#[inline]
pub fn apply_withdrawal_requests_contract_call<EvmConfig, EXT, DB>(
evm_config: &EvmConfig,
evm: &mut Evm<'_, EXT, DB>,
) -> Result<Vec<Request>, BlockExecutionError>
where
DB: Database + DatabaseCommit,
DB::Error: core::fmt::Display,
EvmConfig: ConfigureEvm<Header = Header>,
{
let previous_env = Box::new(evm.context.env().clone());
evm_config.fill_tx_env_system_contract_call(
&mut evm.context.evm.env,
alloy_eips::eip7002::SYSTEM_ADDRESS,
WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS,
Bytes::new(),
);
let ResultAndState { result, mut state } = match evm.transact() {
Ok(res) => res,
Err(e) => {
evm.context.evm.env = previous_env;
return Err(BlockValidationError::WithdrawalRequestsContractCall {
message: format!("execution failed: {e}"),
}
.into())
}
};
state.remove(&alloy_eips::eip7002::SYSTEM_ADDRESS);
state.remove(&evm.block().coinbase);
evm.context.evm.db.commit(state);
evm.context.evm.env = previous_env;
let mut data = match result {
ExecutionResult::Success { output, .. } => Ok(output.into_data()),
ExecutionResult::Revert { output, .. } => {
Err(BlockValidationError::WithdrawalRequestsContractCall {
message: format!("execution reverted: {output}"),
})
}
ExecutionResult::Halt { reason, .. } => {
Err(BlockValidationError::WithdrawalRequestsContractCall {
message: format!("execution halted: {reason:?}"),
})
}
}?;
const WITHDRAWAL_REQUEST_SIZE: usize = 20 + 48 + 8;
let mut withdrawal_requests = Vec::with_capacity(data.len() / WITHDRAWAL_REQUEST_SIZE);
while data.has_remaining() {
if data.remaining() < WITHDRAWAL_REQUEST_SIZE {
return Err(BlockValidationError::WithdrawalRequestsContractCall {
message: "invalid withdrawal request length".to_string(),
}
.into())
}
let mut source_address = Address::ZERO;
data.copy_to_slice(source_address.as_mut_slice());
let mut validator_pubkey = FixedBytes::<48>::ZERO;
data.copy_to_slice(validator_pubkey.as_mut_slice());
let amount = data.get_u64();
withdrawal_requests
.push(WithdrawalRequest { source_address, validator_pubkey, amount }.into());
}
Ok(withdrawal_requests)
}