From 71de91808726d1a10910ac615e1187b3d4ab1190 Mon Sep 17 00:00:00 2001 From: "lukasz.rozmej" Date: Fri, 8 Sep 2023 15:36:54 +0200 Subject: [PATCH] Remove EvmTransactionSignedEcRecovered type to make code less complicated Rename RawEvmTransaction to RlpEvmTransaction --- full-node/sov-ethereum/src/lib.rs | 6 +- .../sov-evm/src/call.rs | 13 ++-- .../sov-evm/src/evm/conversions.rs | 67 +++++++++---------- .../sov-evm/src/evm/executor.rs | 8 ++- .../sov-evm/src/evm/mod.rs | 2 +- .../sov-evm/src/evm/transaction.rs | 45 +------------ .../sov-evm/src/tests/dev_signer.rs | 6 +- .../module-schemas/schemas/sov-evm.json | 4 +- 8 files changed, 53 insertions(+), 98 deletions(-) diff --git a/full-node/sov-ethereum/src/lib.rs b/full-node/sov-ethereum/src/lib.rs index 722c5ea10..35cc95d24 100644 --- a/full-node/sov-ethereum/src/lib.rs +++ b/full-node/sov-ethereum/src/lib.rs @@ -17,7 +17,7 @@ pub mod experimental { use reth_primitives::{Address, TransactionSignedNoHash as RethTransactionSignedNoHash}; use reth_rpc::eth::error::EthApiError; use sov_evm::call::CallMessage; - use sov_evm::evm::RawEvmTransaction; + use sov_evm::evm::RlpEvmTransaction; use sov_modules_api::transaction::Transaction; use sov_modules_api::utils::to_jsonrpsee_error_object; use sov_modules_api::EncodeCall; @@ -73,7 +73,7 @@ pub mod experimental { impl Ethereum { fn make_raw_tx( &self, - raw_tx: RawEvmTransaction, + raw_tx: RlpEvmTransaction, ) -> Result<(H256, Vec), jsonrpsee::core::Error> { let signed_transaction: RethTransactionSignedNoHash = raw_tx.clone().try_into().map_err(EthApiError::from)?; @@ -144,7 +144,7 @@ pub mod experimental { |parameters, ethereum| async move { let data: Bytes = parameters.one().unwrap(); - let raw_evm_tx = RawEvmTransaction { rlp: data.to_vec() }; + let raw_evm_tx = RlpEvmTransaction { rlp: data.to_vec() }; let (tx_hash, raw_tx) = ethereum .make_raw_tx(raw_evm_tx) diff --git a/module-system/module-implementations/sov-evm/src/call.rs b/module-system/module-implementations/sov-evm/src/call.rs index 4e2d11bc4..3b7b42e00 100644 --- a/module-system/module-implementations/sov-evm/src/call.rs +++ b/module-system/module-implementations/sov-evm/src/call.rs @@ -1,12 +1,13 @@ use anyhow::Result; +use reth_primitives::TransactionSignedEcRecovered; use revm::primitives::{CfgEnv, SpecId}; use sov_modules_api::CallResponse; use sov_state::WorkingSet; use crate::evm::db::EvmDb; use crate::evm::executor::{self}; -use crate::evm::transaction::{BlockEnv, EvmTransactionSignedEcRecovered}; -use crate::evm::{contract_address, EvmChainConfig, RawEvmTransaction}; +use crate::evm::transaction::BlockEnv; +use crate::evm::{contract_address, EvmChainConfig, RlpEvmTransaction}; use crate::Evm; #[cfg_attr( @@ -17,17 +18,17 @@ use crate::Evm; )] #[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Clone)] pub struct CallMessage { - pub tx: RawEvmTransaction, + pub tx: RlpEvmTransaction, } impl Evm { pub(crate) fn execute_call( &self, - tx: RawEvmTransaction, + tx: RlpEvmTransaction, _context: &C, working_set: &mut WorkingSet, ) -> Result { - let evm_tx_recovered: EvmTransactionSignedEcRecovered = tx.try_into()?; + let evm_tx_recovered: TransactionSignedEcRecovered = tx.try_into()?; let block_env = self.pending_block.get(working_set).unwrap_or_default(); let cfg = self.cfg.get(working_set).unwrap_or_default(); @@ -42,7 +43,7 @@ impl Evm { let from = evm_tx_recovered.signer(); let to = evm_tx_recovered.to(); - let transaction = reth_rpc_types::Transaction::from_recovered(evm_tx_recovered.tx); + let transaction = reth_rpc_types::Transaction::from_recovered(evm_tx_recovered); self.pending_transactions .push(&transaction, &mut working_set.accessory_state()); diff --git a/module-system/module-implementations/sov-evm/src/evm/conversions.rs b/module-system/module-implementations/sov-evm/src/evm/conversions.rs index 50469b217..542fa1b05 100644 --- a/module-system/module-implementations/sov-evm/src/evm/conversions.rs +++ b/module-system/module-implementations/sov-evm/src/evm/conversions.rs @@ -2,9 +2,7 @@ use bytes::Bytes; use ethereum_types::U64; use ethers_core::types::{Bytes as EthBytes, OtherFields, Transaction}; use reth_primitives::{ - Bytes as RethBytes, TransactionSigned as RethTransactionSigned, - TransactionSignedEcRecovered as RethTransactionSignedEcRecovered, - TransactionSignedNoHash as RethTransactionSignedNoHash, + Bytes as RethBytes, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, }; use reth_rpc::eth::error::EthApiError; use reth_rpc_types::CallRequest; @@ -14,7 +12,7 @@ use revm::primitives::{ }; use thiserror::Error; -use super::transaction::{BlockEnv, EvmTransactionSignedEcRecovered, RawEvmTransaction}; +use super::transaction::{BlockEnv, RlpEvmTransaction}; use super::AccountInfo; impl From for ReVmAccountInfo { @@ -54,36 +52,31 @@ impl From for ReVmBlockEnv { } } -impl From<&EvmTransactionSignedEcRecovered> for TxEnv { - fn from(tx: &EvmTransactionSignedEcRecovered) -> Self { - let tx: &RethTransactionSignedEcRecovered = tx.as_ref(); +pub(crate) fn create_tx_env(tx: &TransactionSignedEcRecovered) -> TxEnv { + let to = match tx.to() { + Some(addr) => TransactTo::Call(addr), + None => TransactTo::Create(CreateScheme::Create), + }; - let to = match tx.to() { - Some(addr) => TransactTo::Call(addr), - None => TransactTo::Create(CreateScheme::Create), - }; - - Self { - caller: tx.signer(), - gas_limit: tx.gas_limit(), - gas_price: U256::from(tx.effective_gas_price(None)), - gas_priority_fee: tx.max_priority_fee_per_gas().map(U256::from), - transact_to: to, - value: U256::from(tx.value()), - data: Bytes::from(tx.input().to_vec()), - chain_id: tx.chain_id(), - nonce: Some(tx.nonce()), - // TODO handle access list - access_list: vec![], - } + TxEnv { + caller: tx.signer(), + gas_limit: tx.gas_limit(), + gas_price: U256::from(tx.effective_gas_price(None)), + gas_priority_fee: tx.max_priority_fee_per_gas().map(U256::from), + transact_to: to, + value: U256::from(tx.value()), + data: Bytes::from(tx.input().to_vec()), + chain_id: tx.chain_id(), + nonce: Some(tx.nonce()), + // TODO handle access list + access_list: vec![], } } -impl TryFrom for Transaction { +impl TryFrom for Transaction { type Error = RawEvmTxConversionError; - fn try_from(evm_tx: RawEvmTransaction) -> Result { - let tx: EvmTransactionSignedEcRecovered = evm_tx.try_into()?; - let tx: &RethTransactionSignedEcRecovered = tx.as_ref(); + fn try_from(evm_tx: RlpEvmTransaction) -> Result { + let tx: TransactionSignedEcRecovered = evm_tx.try_into()?; Ok(Self { hash: tx.hash().into(), @@ -139,33 +132,33 @@ impl From for EthApiError { } } -impl TryFrom for RethTransactionSignedNoHash { +impl TryFrom for TransactionSignedNoHash { type Error = RawEvmTxConversionError; - fn try_from(data: RawEvmTransaction) -> Result { + fn try_from(data: RlpEvmTransaction) -> Result { let data = RethBytes::from(data.rlp); if data.is_empty() { return Err(RawEvmTxConversionError::EmptyRawTransactionData); } - let transaction = RethTransactionSigned::decode_enveloped(data) + let transaction = TransactionSigned::decode_enveloped(data) .map_err(|_| RawEvmTxConversionError::FailedToDecodeSignedTransaction)?; Ok(transaction.into()) } } -impl TryFrom for EvmTransactionSignedEcRecovered { +impl TryFrom for TransactionSignedEcRecovered { type Error = RawEvmTxConversionError; - fn try_from(evm_tx: RawEvmTransaction) -> Result { - let tx = RethTransactionSignedNoHash::try_from(evm_tx)?; - let tx: RethTransactionSigned = tx.into(); + fn try_from(evm_tx: RlpEvmTransaction) -> Result { + let tx = TransactionSignedNoHash::try_from(evm_tx)?; + let tx: TransactionSigned = tx.into(); let tx = tx .into_ecrecovered() .ok_or(RawEvmTxConversionError::FailedToDecodeSignedTransaction)?; - Ok(EvmTransactionSignedEcRecovered::new(tx)) + Ok(tx) } } diff --git a/module-system/module-implementations/sov-evm/src/evm/executor.rs b/module-system/module-implementations/sov-evm/src/evm/executor.rs index d7e56d96c..1cb8ae987 100644 --- a/module-system/module-implementations/sov-evm/src/evm/executor.rs +++ b/module-system/module-implementations/sov-evm/src/evm/executor.rs @@ -1,15 +1,17 @@ use std::convert::Infallible; +use reth_primitives::TransactionSignedEcRecovered; use reth_revm::tracing::{TracingInspector, TracingInspectorConfig}; use revm::primitives::{CfgEnv, EVMError, Env, ExecutionResult, ResultAndState, TxEnv}; use revm::{self, Database, DatabaseCommit}; -use super::transaction::{BlockEnv, EvmTransactionSignedEcRecovered}; +use super::conversions::create_tx_env; +use super::transaction::BlockEnv; pub(crate) fn execute_tx + DatabaseCommit>( db: DB, block_env: BlockEnv, - tx: &EvmTransactionSignedEcRecovered, + tx: &TransactionSignedEcRecovered, config_env: CfgEnv, ) -> Result> { let mut evm = revm::new(); @@ -17,7 +19,7 @@ pub(crate) fn execute_tx + DatabaseCommit>( let env = Env { block: block_env.into(), cfg: config_env, - tx: tx.into(), + tx: create_tx_env(tx), }; evm.env = env; diff --git a/module-system/module-implementations/sov-evm/src/evm/mod.rs b/module-system/module-implementations/sov-evm/src/evm/mod.rs index 8482f2d0c..553d3ef9f 100644 --- a/module-system/module-implementations/sov-evm/src/evm/mod.rs +++ b/module-system/module-implementations/sov-evm/src/evm/mod.rs @@ -15,7 +15,7 @@ pub(crate) mod transaction; pub use conversions::prepare_call_env; use sov_state::codec::BcsCodec; -pub use transaction::RawEvmTransaction; +pub use transaction::RlpEvmTransaction; // Stores information about an EVM account #[derive(Deserialize, Serialize, Debug, PartialEq, Clone, Default)] diff --git a/module-system/module-implementations/sov-evm/src/evm/transaction.rs b/module-system/module-implementations/sov-evm/src/evm/transaction.rs index bf92911fc..3c0b6925e 100644 --- a/module-system/module-implementations/sov-evm/src/evm/transaction.rs +++ b/module-system/module-implementations/sov-evm/src/evm/transaction.rs @@ -1,6 +1,4 @@ -use reth_primitives::{ - Address, TransactionSignedEcRecovered as RethTransactionSignedEcRecovered, H160, H256, U256, -}; +use reth_primitives::{Address, H256, U256}; #[derive(serde::Deserialize, serde::Serialize, Debug, PartialEq, Clone)] pub(crate) struct BlockEnv { @@ -35,46 +33,7 @@ impl Default for BlockEnv { derive(schemars::JsonSchema) )] #[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Clone)] -pub struct RawEvmTransaction { +pub struct RlpEvmTransaction { /// Rlp data. pub rlp: Vec, } - -/// EC recovered evm transaction. -pub struct EvmTransactionSignedEcRecovered { - pub(crate) tx: RethTransactionSignedEcRecovered, -} - -impl EvmTransactionSignedEcRecovered { - /// Creates a new EvmTransactionSignedEcRecovered. - pub fn new(tx: RethTransactionSignedEcRecovered) -> Self { - Self { tx } - } - - /// Transaction hash. Used to identify transaction. - pub fn hash(&self) -> H256 { - self.tx.hash() - } - - /// Signer of transaction recovered from signature. - pub fn signer(&self) -> H160 { - self.tx.signer() - } - - /// Receiver of the transaction. - pub fn to(&self) -> Option
{ - self.tx.to() - } -} - -impl AsRef for EvmTransactionSignedEcRecovered { - fn as_ref(&self) -> &RethTransactionSignedEcRecovered { - &self.tx - } -} - -impl From for RethTransactionSignedEcRecovered { - fn from(tx: EvmTransactionSignedEcRecovered) -> Self { - tx.tx - } -} diff --git a/module-system/module-implementations/sov-evm/src/tests/dev_signer.rs b/module-system/module-implementations/sov-evm/src/tests/dev_signer.rs index db30338fb..52206b0f0 100644 --- a/module-system/module-implementations/sov-evm/src/tests/dev_signer.rs +++ b/module-system/module-implementations/sov-evm/src/tests/dev_signer.rs @@ -8,7 +8,7 @@ use reth_primitives::{ use reth_rpc::eth::error::SignError; use secp256k1::{PublicKey, SecretKey}; -use crate::evm::RawEvmTransaction; +use crate::evm::RlpEvmTransaction; /// ETH transactions signer used in tests. pub(crate) struct DevSigner { @@ -61,7 +61,7 @@ impl DevSigner { to: TransactionKind, data: Vec, nonce: u64, - ) -> Result { + ) -> Result { let reth_tx = RethTxEip1559 { to, input: RethBytes::from(data), @@ -73,7 +73,7 @@ impl DevSigner { let signed = self.sign_transaction(reth_tx)?; - Ok(RawEvmTransaction { + Ok(RlpEvmTransaction { rlp: signed.envelope_encoded().to_vec(), }) } diff --git a/module-system/module-schemas/schemas/sov-evm.json b/module-system/module-schemas/schemas/sov-evm.json index 00b39d865..88f25a0f8 100644 --- a/module-system/module-schemas/schemas/sov-evm.json +++ b/module-system/module-schemas/schemas/sov-evm.json @@ -7,11 +7,11 @@ ], "properties": { "tx": { - "$ref": "#/definitions/RawEvmTransaction" + "$ref": "#/definitions/RlpEvmTransaction" } }, "definitions": { - "RawEvmTransaction": { + "RlpEvmTransaction": { "description": "Rlp encoded evm transaction.", "type": "object", "required": [