Skip to content

Commit

Permalink
Remove EvmTransactionSignedEcRecovered type to make code less complic…
Browse files Browse the repository at this point in the history
…ated

Rename RawEvmTransaction to RlpEvmTransaction
  • Loading branch information
LukaszRozmej committed Sep 8, 2023
1 parent b735eaf commit 71de918
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 98 deletions.
6 changes: 3 additions & 3 deletions full-node/sov-ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +73,7 @@ pub mod experimental {
impl<Da: DaService> Ethereum<Da> {
fn make_raw_tx(
&self,
raw_tx: RawEvmTransaction,
raw_tx: RlpEvmTransaction,
) -> Result<(H256, Vec<u8>), jsonrpsee::core::Error> {
let signed_transaction: RethTransactionSignedNoHash =
raw_tx.clone().try_into().map_err(EthApiError::from)?;
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions module-system/module-implementations/sov-evm/src/call.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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<C: sov_modules_api::Context> Evm<C> {
pub(crate) fn execute_call(
&self,
tx: RawEvmTransaction,
tx: RlpEvmTransaction,
_context: &C,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<CallResponse> {
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();
Expand All @@ -42,7 +43,7 @@ impl<C: sov_modules_api::Context> Evm<C> {

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());
Expand Down
67 changes: 30 additions & 37 deletions module-system/module-implementations/sov-evm/src/evm/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AccountInfo> for ReVmAccountInfo {
Expand Down Expand Up @@ -54,36 +52,31 @@ impl From<BlockEnv> 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<RawEvmTransaction> for Transaction {
impl TryFrom<RlpEvmTransaction> for Transaction {
type Error = RawEvmTxConversionError;
fn try_from(evm_tx: RawEvmTransaction) -> Result<Self, Self::Error> {
let tx: EvmTransactionSignedEcRecovered = evm_tx.try_into()?;
let tx: &RethTransactionSignedEcRecovered = tx.as_ref();
fn try_from(evm_tx: RlpEvmTransaction) -> Result<Self, Self::Error> {
let tx: TransactionSignedEcRecovered = evm_tx.try_into()?;

Ok(Self {
hash: tx.hash().into(),
Expand Down Expand Up @@ -139,33 +132,33 @@ impl From<RawEvmTxConversionError> for EthApiError {
}
}

impl TryFrom<RawEvmTransaction> for RethTransactionSignedNoHash {
impl TryFrom<RlpEvmTransaction> for TransactionSignedNoHash {
type Error = RawEvmTxConversionError;

fn try_from(data: RawEvmTransaction) -> Result<Self, Self::Error> {
fn try_from(data: RlpEvmTransaction) -> Result<Self, Self::Error> {
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<RawEvmTransaction> for EvmTransactionSignedEcRecovered {
impl TryFrom<RlpEvmTransaction> for TransactionSignedEcRecovered {
type Error = RawEvmTxConversionError;

fn try_from(evm_tx: RawEvmTransaction) -> Result<Self, Self::Error> {
let tx = RethTransactionSignedNoHash::try_from(evm_tx)?;
let tx: RethTransactionSigned = tx.into();
fn try_from(evm_tx: RlpEvmTransaction) -> Result<Self, Self::Error> {
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)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
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<DB: Database<Error = Infallible> + DatabaseCommit>(
db: DB,
block_env: BlockEnv,
tx: &EvmTransactionSignedEcRecovered,
tx: &TransactionSignedEcRecovered,
config_env: CfgEnv,
) -> Result<ExecutionResult, EVMError<Infallible>> {
let mut evm = revm::new();

let env = Env {
block: block_env.into(),
cfg: config_env,
tx: tx.into(),
tx: create_tx_env(tx),
};

evm.env = env;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<u8>,
}

/// 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<Address> {
self.tx.to()
}
}

impl AsRef<RethTransactionSignedEcRecovered> for EvmTransactionSignedEcRecovered {
fn as_ref(&self) -> &RethTransactionSignedEcRecovered {
&self.tx
}
}

impl From<EvmTransactionSignedEcRecovered> for RethTransactionSignedEcRecovered {
fn from(tx: EvmTransactionSignedEcRecovered) -> Self {
tx.tx
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -61,7 +61,7 @@ impl DevSigner {
to: TransactionKind,
data: Vec<u8>,
nonce: u64,
) -> Result<RawEvmTransaction, SignError> {
) -> Result<RlpEvmTransaction, SignError> {
let reth_tx = RethTxEip1559 {
to,
input: RethBytes::from(data),
Expand All @@ -73,7 +73,7 @@ impl DevSigner {

let signed = self.sign_transaction(reth_tx)?;

Ok(RawEvmTransaction {
Ok(RlpEvmTransaction {
rlp: signed.envelope_encoded().to_vec(),
})
}
Expand Down
4 changes: 2 additions & 2 deletions module-system/module-schemas/schemas/sov-evm.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
],
"properties": {
"tx": {
"$ref": "#/definitions/RawEvmTransaction"
"$ref": "#/definitions/RlpEvmTransaction"
}
},
"definitions": {
"RawEvmTransaction": {
"RlpEvmTransaction": {
"description": "Rlp encoded evm transaction.",
"type": "object",
"required": [
Expand Down

0 comments on commit 71de918

Please sign in to comment.