feat: integrate OpPrimitives (#13556)

This commit is contained in:
Arsenii Kulikov
2024-12-27 18:11:11 +03:00
committed by GitHub
parent c35fe4ac54
commit 4994cdf0b0
44 changed files with 524 additions and 506 deletions

View File

@ -2,10 +2,10 @@
use crate::{l1::ensure_create2_deployer, OpBlockExecutionError, OpEvmConfig};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use alloy_consensus::{Header, Transaction as _};
use alloy_consensus::{Eip658Value, Header, Receipt, Transaction as _};
use alloy_eips::eip7685::Requests;
use core::fmt::Display;
use op_alloy_consensus::DepositTransaction;
use op_alloy_consensus::{OpDepositReceipt, OpTxType};
use reth_chainspec::EthereumHardforks;
use reth_consensus::ConsensusError;
use reth_evm::{
@ -22,8 +22,9 @@ use reth_evm::{
use reth_optimism_chainspec::OpChainSpec;
use reth_optimism_consensus::validate_block_post_execution;
use reth_optimism_forks::OpHardfork;
use reth_optimism_primitives::OpPrimitives;
use reth_primitives::{BlockWithSenders, Receipt, TransactionSigned, TxType};
use reth_optimism_primitives::{OpBlock, OpPrimitives, OpReceipt, OpTransactionSigned};
use reth_primitives::BlockWithSenders;
use reth_primitives_traits::SignedTransaction;
use reth_revm::{Database, State};
use revm_primitives::{db::DatabaseCommit, EnvWithHandlerCfg, ResultAndState};
use tracing::trace;
@ -58,7 +59,7 @@ where
+ Sync
+ Send
+ 'static
+ ConfigureEvm<Header = alloy_consensus::Header, Transaction = TransactionSigned>,
+ ConfigureEvm<Header = alloy_consensus::Header, Transaction = OpTransactionSigned>,
{
type Primitives = OpPrimitives;
type Strategy<DB: Database<Error: Into<ProviderError> + Display>> =
@ -121,7 +122,7 @@ where
impl<DB, EvmConfig> BlockExecutionStrategy for OpExecutionStrategy<DB, EvmConfig>
where
DB: Database<Error: Into<ProviderError> + Display>,
EvmConfig: ConfigureEvm<Header = alloy_consensus::Header, Transaction = TransactionSigned>,
EvmConfig: ConfigureEvm<Header = alloy_consensus::Header, Transaction = OpTransactionSigned>,
{
type DB = DB;
type Primitives = OpPrimitives;
@ -131,7 +132,10 @@ where
self.tx_env_overrides = Some(tx_env_overrides);
}
fn apply_pre_execution_changes(&mut self, block: &BlockWithSenders) -> Result<(), Self::Error> {
fn apply_pre_execution_changes(
&mut self,
block: &BlockWithSenders<OpBlock>,
) -> Result<(), Self::Error> {
// Set state clear flag if the block is after the Spurious Dragon hardfork.
let state_clear_flag =
(*self.chain_spec).is_spurious_dragon_active_at_block(block.header.number);
@ -159,8 +163,8 @@ where
fn execute_transactions(
&mut self,
block: &BlockWithSenders,
) -> Result<ExecuteOutput<Receipt>, Self::Error> {
block: &BlockWithSenders<OpBlock>,
) -> Result<ExecuteOutput<OpReceipt>, Self::Error> {
let env = self.evm_env_for_block(&block.header);
let mut evm = self.evm_config.evm_with_env(&mut self.state, env);
@ -174,7 +178,7 @@ where
// must be no greater than the blocks gasLimit.
let block_available_gas = block.header.gas_limit - cumulative_gas_used;
if transaction.gas_limit() > block_available_gas &&
(is_regolith || !transaction.is_system_transaction())
(is_regolith || !transaction.is_deposit())
{
return Err(BlockValidationError::TransactionGasLimitMoreThanAvailableBlockGas {
transaction_gas_limit: transaction.gas_limit(),
@ -183,11 +187,6 @@ where
.into())
}
// An optimism block should never contain blob transactions.
if matches!(transaction.tx_type(), TxType::Eip4844) {
return Err(OpBlockExecutionError::BlobTransactionRejected.into())
}
// Cache the depositor account prior to the state transition for the deposit nonce.
//
// Note that this *only* needs to be done post-regolith hardfork, as deposit nonces
@ -230,22 +229,32 @@ where
// append gas used
cumulative_gas_used += result.gas_used();
// Push transaction changeset and calculate header bloom filter for receipt.
receipts.push(Receipt {
tx_type: transaction.tx_type(),
let receipt = Receipt {
// Success flag was added in `EIP-658: Embedding transaction status code in
// receipts`.
success: result.is_success(),
cumulative_gas_used,
status: Eip658Value::Eip658(result.is_success()),
cumulative_gas_used: cumulative_gas_used as u128,
logs: result.into_logs(),
deposit_nonce: depositor.map(|account| account.nonce),
// The deposit receipt version was introduced in Canyon to indicate an update to how
// receipt hashes should be computed when set. The state transition process ensures
// this is only set for post-Canyon deposit transactions.
deposit_receipt_version: (transaction.is_deposit() &&
self.chain_spec
.is_fork_active_at_timestamp(OpHardfork::Canyon, block.timestamp))
.then_some(1),
};
// Push transaction changeset and calculate header bloom filter for receipt.
receipts.push(match transaction.tx_type() {
OpTxType::Legacy => OpReceipt::Legacy(receipt),
OpTxType::Eip2930 => OpReceipt::Eip2930(receipt),
OpTxType::Eip1559 => OpReceipt::Eip1559(receipt),
OpTxType::Eip7702 => OpReceipt::Eip7702(receipt),
OpTxType::Deposit => OpReceipt::Deposit(OpDepositReceipt {
inner: receipt,
deposit_nonce: depositor.map(|account| account.nonce),
// The deposit receipt version was introduced in Canyon to indicate an update to
// how receipt hashes should be computed when set. The state
// transition process ensures this is only set for
// post-Canyon deposit transactions.
deposit_receipt_version: (transaction.is_deposit() &&
self.chain_spec
.is_fork_active_at_timestamp(OpHardfork::Canyon, block.timestamp))
.then_some(1),
}),
});
}
@ -254,8 +263,8 @@ where
fn apply_post_execution_changes(
&mut self,
block: &BlockWithSenders,
_receipts: &[Receipt],
block: &BlockWithSenders<OpBlock>,
_receipts: &[OpReceipt],
) -> Result<Requests, Self::Error> {
let balance_increments =
post_block_balance_increments(&self.chain_spec.clone(), &block.block);
@ -284,8 +293,8 @@ where
fn validate_block_post_execution(
&self,
block: &BlockWithSenders,
receipts: &[Receipt],
block: &BlockWithSenders<OpBlock>,
receipts: &[OpReceipt],
_requests: &Requests,
) -> Result<(), ConsensusError> {
validate_block_post_execution(block, &self.chain_spec.clone(), receipts)
@ -313,11 +322,11 @@ mod tests {
use alloy_primitives::{
b256, Address, PrimitiveSignature as Signature, StorageKey, StorageValue, U256,
};
use op_alloy_consensus::TxDeposit;
use op_alloy_consensus::{OpTypedTransaction, TxDeposit};
use reth_chainspec::MIN_TRANSACTION_GAS;
use reth_evm::execute::{BasicBlockExecutorProvider, BatchExecutor, BlockExecutorProvider};
use reth_optimism_chainspec::OpChainSpecBuilder;
use reth_primitives::{Account, Block, BlockBody, Transaction, TransactionSigned};
use reth_primitives::{Account, Block, BlockBody};
use reth_revm::{
database::StateProviderDatabase, test_utils::StateProviderTest, L1_BLOCK_CONTRACT,
};
@ -380,8 +389,8 @@ mod tests {
let chain_spec = Arc::new(OpChainSpecBuilder::base_mainnet().regolith_activated().build());
let tx = TransactionSigned::new_unhashed(
Transaction::Eip1559(TxEip1559 {
let tx = OpTransactionSigned::new_unhashed(
OpTypedTransaction::Eip1559(TxEip1559 {
chain_id: chain_spec.chain.id(),
nonce: 0,
gas_limit: MIN_TRANSACTION_GAS,
@ -391,8 +400,8 @@ mod tests {
Signature::test_signature(),
);
let tx_deposit = TransactionSigned::new_unhashed(
Transaction::Deposit(op_alloy_consensus::TxDeposit {
let tx_deposit = OpTransactionSigned::new_unhashed(
OpTypedTransaction::Deposit(op_alloy_consensus::TxDeposit {
from: addr,
to: addr.into(),
gas_limit: MIN_TRANSACTION_GAS,
@ -424,13 +433,14 @@ mod tests {
let tx_receipt = receipts[0][0].as_ref().unwrap();
let deposit_receipt = receipts[0][1].as_ref().unwrap();
assert!(!matches!(tx_receipt, OpReceipt::Deposit(_)));
// deposit_nonce is present only in deposit transactions
let OpReceipt::Deposit(deposit_receipt) = deposit_receipt else {
panic!("expected deposit")
};
assert!(deposit_receipt.deposit_nonce.is_some());
// deposit_receipt_version is not present in pre canyon transactions
assert!(deposit_receipt.deposit_receipt_version.is_none());
assert!(tx_receipt.deposit_receipt_version.is_none());
// deposit_nonce is present only in deposit transactions
assert!(deposit_receipt.deposit_nonce.is_some());
assert!(tx_receipt.deposit_nonce.is_none());
}
#[test]
@ -455,8 +465,8 @@ mod tests {
let chain_spec = Arc::new(OpChainSpecBuilder::base_mainnet().canyon_activated().build());
let tx = TransactionSigned::new_unhashed(
Transaction::Eip1559(TxEip1559 {
let tx = OpTransactionSigned::new_unhashed(
OpTypedTransaction::Eip1559(TxEip1559 {
chain_id: chain_spec.chain.id(),
nonce: 0,
gas_limit: MIN_TRANSACTION_GAS,
@ -466,8 +476,8 @@ mod tests {
Signature::test_signature(),
);
let tx_deposit = TransactionSigned::new_unhashed(
Transaction::Deposit(op_alloy_consensus::TxDeposit {
let tx_deposit = OpTransactionSigned::new_unhashed(
OpTypedTransaction::Deposit(op_alloy_consensus::TxDeposit {
from: addr,
to: addr.into(),
gas_limit: MIN_TRANSACTION_GAS,
@ -500,11 +510,13 @@ mod tests {
let deposit_receipt = receipts[0][1].as_ref().unwrap();
// deposit_receipt_version is set to 1 for post canyon deposit transactions
assert!(!matches!(tx_receipt, OpReceipt::Deposit(_)));
let OpReceipt::Deposit(deposit_receipt) = deposit_receipt else {
panic!("expected deposit")
};
assert_eq!(deposit_receipt.deposit_receipt_version, Some(1));
assert!(tx_receipt.deposit_receipt_version.is_none());
// deposit_nonce is present only in deposit transactions
assert!(deposit_receipt.deposit_nonce.is_some());
assert!(tx_receipt.deposit_nonce.is_none());
}
}

View File

@ -18,7 +18,8 @@ use alloy_primitives::{Address, U256};
use op_alloy_consensus::EIP1559ParamError;
use reth_evm::{env::EvmEnv, ConfigureEvm, ConfigureEvmEnv, NextBlockEnvAttributes};
use reth_optimism_chainspec::OpChainSpec;
use reth_primitives::{transaction::FillTxEnv, TransactionSigned};
use reth_optimism_primitives::OpTransactionSigned;
use reth_primitives_traits::FillTxEnv;
use reth_revm::{
inspector_handle_register,
primitives::{AnalysisKind, CfgEnvWithHandlerCfg, TxEnv},
@ -58,10 +59,10 @@ impl OpEvmConfig {
impl ConfigureEvmEnv for OpEvmConfig {
type Header = Header;
type Transaction = TransactionSigned;
type Transaction = OpTransactionSigned;
type Error = EIP1559ParamError;
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &TransactionSigned, sender: Address) {
fn fill_tx_env(&self, tx_env: &mut TxEnv, transaction: &OpTransactionSigned, sender: Address) {
transaction.fill_tx_env(tx_env, sender);
}
@ -189,7 +190,7 @@ impl ConfigureEvm for OpEvmConfig {
#[cfg(test)]
mod tests {
use super::*;
use alloy_consensus::{constants::KECCAK_EMPTY, Header};
use alloy_consensus::{constants::KECCAK_EMPTY, Header, Receipt};
use alloy_eips::eip7685::Requests;
use alloy_genesis::Genesis;
use alloy_primitives::{bytes, Address, LogData, B256, U256};
@ -199,8 +200,8 @@ mod tests {
AccountRevertInit, BundleStateInit, Chain, ExecutionOutcome, RevertsInit,
};
use reth_optimism_chainspec::BASE_MAINNET;
use reth_optimism_primitives::OpPrimitives;
use reth_primitives::{Account, Log, Receipt, Receipts, SealedBlockWithSenders, TxType};
use reth_optimism_primitives::{OpBlock, OpPrimitives, OpReceipt};
use reth_primitives::{Account, Log, Receipts, SealedBlockWithSenders};
use reth_revm::{
db::{BundleState, CacheDB, EmptyDBTyped},
inspectors::NoOpInspector,
@ -530,7 +531,7 @@ mod tests {
#[test]
fn receipts_by_block_hash() {
// Create a default SealedBlockWithSenders object
let block: SealedBlockWithSenders = Default::default();
let block: SealedBlockWithSenders<OpBlock> = Default::default();
// Define block hashes for block1 and block2
let block1_hash = B256::new([0x01; 32]);
@ -548,24 +549,18 @@ mod tests {
block2.block.header.set_hash(block2_hash);
// Create a random receipt object, receipt1
let receipt1 = Receipt {
tx_type: TxType::Legacy,
let receipt1 = OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
};
status: true.into(),
});
// Create another random receipt object, receipt2
let receipt2 = Receipt {
tx_type: TxType::Legacy,
let receipt2 = OpReceipt::Legacy(Receipt {
cumulative_gas_used: 1325345,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
};
status: true.into(),
});
// Create a Receipts object with a vector of receipt vectors
let receipts =
@ -573,7 +568,7 @@ mod tests {
// Create an ExecutionOutcome object with the created bundle, receipts, an empty requests
// vector, and first_block set to 10
let execution_outcome = ExecutionOutcome {
let execution_outcome = ExecutionOutcome::<OpReceipt> {
bundle: Default::default(),
receipts,
requests: vec![],
@ -614,14 +609,11 @@ mod tests {
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
receipt_vec: vec![vec![Some(Receipt {
tx_type: TxType::Legacy,
receipt_vec: vec![vec![Some(OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
})]],
status: true.into(),
}))]],
};
// Create a Requests object with a vector of requests
@ -677,14 +669,11 @@ mod tests {
fn test_block_number_to_index() {
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
receipt_vec: vec![vec![Some(Receipt {
tx_type: TxType::Legacy,
receipt_vec: vec![vec![Some(OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
})]],
status: true.into(),
}))]],
};
// Define the first block number
@ -713,14 +702,11 @@ mod tests {
fn test_get_logs() {
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
receipt_vec: vec![vec![Some(Receipt {
tx_type: TxType::Legacy,
receipt_vec: vec![vec![Some(OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![Log::<LogData>::default()],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
})]],
status: true.into(),
}))]],
};
// Define the first block number
@ -746,14 +732,11 @@ mod tests {
fn test_receipts_by_block() {
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
receipt_vec: vec![vec![Some(Receipt {
tx_type: TxType::Legacy,
receipt_vec: vec![vec![Some(OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![Log::<LogData>::default()],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
})]],
status: true.into(),
}))]],
};
// Define the first block number
@ -774,14 +757,11 @@ mod tests {
// Assert that the receipts for block number 123 match the expected receipts
assert_eq!(
receipts_by_block,
vec![&Some(Receipt {
tx_type: TxType::Legacy,
vec![&Some(OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![Log::<LogData>::default()],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
})]
status: true.into(),
}))]
);
}
@ -789,14 +769,11 @@ mod tests {
fn test_receipts_len() {
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
receipt_vec: vec![vec![Some(Receipt {
tx_type: TxType::Legacy,
receipt_vec: vec![vec![Some(OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![Log::<LogData>::default()],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
})]],
status: true.into(),
}))]],
};
// Create an empty Receipts object
@ -838,14 +815,11 @@ mod tests {
#[test]
fn test_revert_to() {
// Create a random receipt object
let receipt = Receipt {
tx_type: TxType::Legacy,
let receipt = OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
};
status: true.into(),
});
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {
@ -888,14 +862,11 @@ mod tests {
#[test]
fn test_extend_execution_outcome() {
// Create a Receipt object with specific attributes.
let receipt = Receipt {
tx_type: TxType::Legacy,
let receipt = OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
};
status: true.into(),
});
// Create a Receipts object containing the receipt.
let receipts = Receipts { receipt_vec: vec![vec![Some(receipt.clone())]] };
@ -933,14 +904,11 @@ mod tests {
#[test]
fn test_split_at_execution_outcome() {
// Create a random receipt object
let receipt = Receipt {
tx_type: TxType::Legacy,
let receipt = OpReceipt::Legacy(Receipt {
cumulative_gas_used: 46913,
logs: vec![],
success: true,
deposit_nonce: Some(18),
deposit_receipt_version: Some(34),
};
status: true.into(),
});
// Create a Receipts object with a vector of receipt vectors
let receipts = Receipts {