Skip to content

Commit

Permalink
Implement eip-7251 and enable more Prague EF tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Ventura committed Feb 14, 2025
1 parent c26cbe1 commit 71f7d06
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 5 deletions.
14 changes: 14 additions & 0 deletions cmd/ef_tests/blockchain/tests/prague.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@ datatest_stable::harness!(
parse_and_execute,
"vectors/prague/eip7002_el_triggerable_withdrawals",
r".*/.*\.json",
parse_and_execute,
"vectors/prague/eip7251_consolidations",
r".*/.*\.json",
// TODO: enable these tests once the evm is updated.
// parse_and_execute,
// "vectors/prague/eip2537_bls_12_381_precompiles",
// r".*/.*\.json",
// TODO: enable these tests once the evm is updated.
// parse_and_execute,
// "vectors/prague/eip7623_increase_calldata_cost",
// r".*/.*\.json",
parse_and_execute,
"vectors/prague/eip7685_general_purpose_el_requests",
r".*/.*\.json",
);
9 changes: 9 additions & 0 deletions crates/common/types/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub type Bytes32 = [u8; 32];
pub type Bytes96 = [u8; 96];
const DEPOSIT_TYPE: u8 = 0x00;
const WITHDRAWAL_TYPE: u8 = 0x01;
const CONSOLIDATION_TYPE: u8 = 0x02;

lazy_static::lazy_static! {
pub static ref DEPOSIT_CONTRACT_ADDRESS: Address = Address::from_slice(&hex::decode("00000000219ab540356cbb839cbe05303d7705fa").unwrap());
Expand All @@ -15,6 +16,7 @@ lazy_static::lazy_static! {
pub enum Requests {
Deposit(Vec<Deposit>),
Withdrawal(Vec<u8>),
Consolidation(Vec<u8>),
}

impl Requests {
Expand All @@ -27,6 +29,9 @@ impl Requests {
Requests::Withdrawal(data) => std::iter::once(WITHDRAWAL_TYPE)
.chain(data.iter().cloned())
.collect(),
Requests::Consolidation(data) => std::iter::once(CONSOLIDATION_TYPE)
.chain(data.iter().cloned())
.collect(),
}
}
pub fn from_deposit_receipts(receipts: &[Receipt]) -> Requests {
Expand All @@ -47,6 +52,10 @@ impl Requests {
pub fn from_withdrawals_data(data: Vec<u8>) -> Requests {
Requests::Withdrawal(data)
}

pub fn from_consolidation_data(data: Vec<u8>) -> Requests {
Requests::Consolidation(data)
}
}

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions crates/vm/backends/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pub const SYSTEM_ADDRESS_STR: &str = "fffffffffffffffffffffffffffffffffffffffe";
pub const BEACON_ROOTS_ADDRESS_STR: &str = "000F3df6D732807Ef1319fB7B8bB8522d0Beac02";
pub const HISTORY_STORAGE_ADDRESS_STR: &str = "0000F90827F1C53a10cb7A02335B175320002935";
pub const WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS: &str = "00000961Ef480Eb55e80D19ad83579A64c007002";
pub const CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS: &str =
"0000BBdDc7CE488642fb579F8B00f3a590007251";
72 changes: 70 additions & 2 deletions crates/vm/backends/levm.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::constants::{
BEACON_ROOTS_ADDRESS_STR, HISTORY_STORAGE_ADDRESS_STR, SYSTEM_ADDRESS_STR,
WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS,
WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS, CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS
};
use crate::db::StoreWrapper;
use crate::EvmError;
Expand Down Expand Up @@ -443,6 +443,59 @@ fn read_withdrawal_requests_levm(
}
}

fn dequeue_consolidation_requests(
store_wrapper: Arc<StoreWrapper>,
block_header: &BlockHeader,
config: EVMConfig,
) -> Option<ExecutionReport> {
lazy_static! {
static ref SYSTEM_ADDRESS: Address =
Address::from_slice(&hex::decode(SYSTEM_ADDRESS_STR).unwrap());
static ref CONTRACT_ADDRESS: Address =
Address::from_slice(&hex::decode(CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS).unwrap());
};

let env = Environment {
origin: *SYSTEM_ADDRESS,
gas_limit: 30_000_000,
block_number: block_header.number.into(),
coinbase: block_header.coinbase,
timestamp: block_header.timestamp.into(),
prev_randao: Some(block_header.prev_randao),
base_fee_per_gas: U256::zero(),
gas_price: U256::zero(),
block_excess_blob_gas: block_header.excess_blob_gas.map(U256::from),
block_blob_gas_used: block_header.blob_gas_used.map(U256::from),
block_gas_limit: 30_000_000,
transient_storage: HashMap::new(),
config,
..Default::default()
};

// Here execute with LEVM but just return transaction report. And I will handle it in the calling place.

let mut vm = VM::new(
TxKind::Call(*CONTRACT_ADDRESS),
env,
U256::zero(),
CoreBytes::new(),
store_wrapper,
CacheDB::new(),
vec![],
None,
)
.ok()?;

let mut report = vm.execute().ok()?;

report.new_state.remove(&*SYSTEM_ADDRESS);

match report.result {
TxResult::Success => Some(report),
_ => None,
}
}

pub fn extract_all_requests_levm(
receipts: &[Receipt],
state: &mut EvmState,
Expand Down Expand Up @@ -476,8 +529,23 @@ pub fn extract_all_requests_levm(
None => Default::default(),
};

let store_wrapper = Arc::new(StoreWrapper {
store: state.database().unwrap().clone(),
block_hash: header.parent_hash,
});

let consolidation_data: Vec<u8> =
match dequeue_consolidation_requests(store_wrapper, header, evm_config) {
Some(report) => {
cache.extend(report.new_state.clone());
report.output.into()
}
None => Default::default(),
};

let deposits = Requests::from_deposit_receipts(receipts);
let withdrawals = Requests::from_withdrawals_data(withdrawals_data);
let consolidation = Requests::from_consolidation_data(consolidation_data);

Ok(vec![deposits, withdrawals])
Ok(vec![deposits, withdrawals, consolidation])
}
67 changes: 64 additions & 3 deletions crates/vm/backends/revm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::constants::{BEACON_ROOTS_ADDRESS_STR, HISTORY_STORAGE_ADDRESS_STR, SYSTEM_ADDRESS_STR};
use crate::backends::constants::WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS;
use super::constants::{BEACON_ROOTS_ADDRESS_STR, HISTORY_STORAGE_ADDRESS_STR, SYSTEM_ADDRESS_STR, CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS, WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS};
use crate::spec_id;
use crate::EvmError;
use crate::EvmState;
Expand Down Expand Up @@ -626,6 +625,66 @@ fn read_withdrawal_requests(
}
}

fn dequeue_consolidation_requests(
state: &mut EvmState,
header: &BlockHeader,
spec_id: SpecId,
) -> Option<Vec<u8>> {
lazy_static! {
static ref SYSTEM_ADDRESS: RevmAddress =
RevmAddress::from_slice(&hex::decode(SYSTEM_ADDRESS_STR).unwrap());
static ref CONTRACT_ADDRESS: RevmAddress = RevmAddress::from_slice(
&hex::decode(CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS).unwrap(),
);
};
let tx_env = TxEnv {
caller: *SYSTEM_ADDRESS,
transact_to: RevmTxKind::Call(*CONTRACT_ADDRESS),
gas_limit: 30_000_000,
..Default::default()
};
let mut block_env = block_env(header);
block_env.basefee = RevmU256::ZERO;
block_env.gas_limit = RevmU256::from(30_000_000);

let tx_result = match state {
EvmState::Store(db) => {
let mut evm = Evm::builder()
.with_db(db)
.with_block_env(block_env)
.with_tx_env(tx_env)
.with_spec_id(spec_id)
.build();

let transaction_result = evm.transact().ok()?;
let mut result_state = transaction_result.state;
result_state.remove(&*SYSTEM_ADDRESS);
result_state.remove(&evm.block().coinbase);

evm.context.evm.db.commit(result_state);

transaction_result.result
}
EvmState::Execution(db) => {
let mut evm = Evm::builder()
.with_db(db)
.with_block_env(block_env)
.with_tx_env(tx_env)
.with_spec_id(spec_id)
.build();

// Not necessary to commit to DB
let transaction_result = evm.transact().ok()?;
transaction_result.result
}
};

match tx_result {
RevmExecutionResult::Success { output, .. } => Some(output.into_data().into()),
_ => None,
}
}

pub fn extract_all_requests(
receipts: &[Receipt],
state: &mut EvmState,
Expand All @@ -640,8 +699,10 @@ pub fn extract_all_requests(

let deposits = Requests::from_deposit_receipts(receipts);
let withdrawals_data = read_withdrawal_requests(state, header, spec_id);
let consolidation_data = dequeue_consolidation_requests(state, header, spec_id);

let withdrawals = Requests::from_withdrawals_data(withdrawals_data.unwrap_or_default());
let consolidation = Requests::from_consolidation_data(consolidation_data.unwrap_or_default());

Ok(vec![deposits, withdrawals])
Ok(vec![deposits, withdrawals, consolidation])
}

0 comments on commit 71f7d06

Please sign in to comment.