Skip to content

Commit

Permalink
EVM: Recovering transaction signer in the EVM module. (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolad authored and preston-evans98 committed Sep 14, 2023
1 parent 1ccd652 commit 23622de
Show file tree
Hide file tree
Showing 16 changed files with 363 additions and 494 deletions.
169 changes: 42 additions & 127 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,16 @@ ethers-providers = "=2.0.8"
ethers-signers = { version = "=2.0.8", default-features = false }
ethers-middleware = "=2.0.8"

reth-primitives = { git = "https://github.com/paradigmxyz/reth", version = "0.1.0-alpha.4"}
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth", version = "0.1.0-alpha.4"}
reth-rpc = { git = "https://github.com/paradigmxyz/reth", version = "0.1.0-alpha.4"}
reth-revm = { git = "https://github.com/paradigmxyz/reth", version = "0.1.0-alpha.4"}
reth-primitives = { git = "https://github.com/paradigmxyz/reth", rev = "4ab924c5d361bbfdcdad9f997d16d67b4a1730b7"}
reth-rpc-types = { git = "https://github.com/paradigmxyz/reth", rev = "4ab924c5d361bbfdcdad9f997d16d67b4a1730b7"}
reth-rpc = { git = "https://github.com/paradigmxyz/reth", rev = "4ab924c5d361bbfdcdad9f997d16d67b4a1730b7"}
reth-revm = { git = "https://github.com/paradigmxyz/reth", rev = "4ab924c5d361bbfdcdad9f997d16d67b4a1730b7"}

revm = { git = "https://github.com/bluealloy/revm/", branch = "release/v25" }
revm-primitives = { git = "https://github.com/bluealloy/revm/", branch = "release/v25" }

secp256k1 = { version = "0.27.0", default-features = false, features = ["global-context", "rand-std", "recovery"] }

[patch.crates-io]
# See reth: https://github.com/paradigmxyz/reth/blob/main/Cargo.toml#L79
revm = { git = "https://github.com/bluealloy/revm/", branch = "release/v25" }
Expand Down
36 changes: 23 additions & 13 deletions full-node/sov-ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ pub mod experimental {
use jsonrpsee::types::ErrorObjectOwned;
use jsonrpsee::RpcModule;
use jupiter::da_service::DaServiceConfig;
use reth_primitives::Bytes as RethBytes;
use reth_primitives::TransactionSignedNoHash as RethTransactionSignedNoHash;
use reth_rpc::eth::error::EthApiError;
use sov_evm::call::CallMessage;
use sov_evm::evm::{EthAddress, EvmTransaction};
use sov_evm::evm::{EthAddress, RawEvmTransaction};
use sov_modules_api::transaction::Transaction;
use sov_modules_api::utils::to_jsonrpsee_error_object;
use sov_modules_api::EncodeCall;
Expand Down Expand Up @@ -47,24 +48,36 @@ pub mod experimental {
}

impl Ethereum {
fn make_raw_tx(&self, evm_tx: EvmTransaction) -> Result<Vec<u8>, std::io::Error> {
fn make_raw_tx(
&self,
raw_tx: RawEvmTransaction,
) -> Result<(H256, Vec<u8>), jsonrpsee::core::Error> {
let signed_transaction: RethTransactionSignedNoHash =
raw_tx.clone().try_into().map_err(EthApiError::from)?;

let tx_hash = signed_transaction.hash();
let sender = signed_transaction
.recover_signer()
.ok_or(EthApiError::InvalidTransactionSignature)?;

let mut nonces = self.nonces.lock().unwrap();
let nonce = *nonces
.entry(evm_tx.sender)
.entry(sender.into())
.and_modify(|n| *n += 1)
.or_insert(0);

let tx = CallMessage { tx: evm_tx };
let tx = CallMessage { tx: raw_tx };
let message =
<Runtime<DefaultContext> as EncodeCall<sov_evm::Evm<DefaultContext>>>::encode_call(
tx,
);

let tx = Transaction::<DefaultContext>::new_signed_tx(
&self.tx_signer_prov_key,
message,
nonce,
);
tx.try_to_vec()
Ok((H256::from(tx_hash), tx.try_to_vec()?))
}
}

Expand Down Expand Up @@ -111,21 +124,18 @@ pub mod experimental {
"eth_sendRawTransaction",
|parameters, ethereum| async move {
let data: Bytes = parameters.one().unwrap();
let data = RethBytes::from(data.as_ref());

let evm_transaction: EvmTransaction = data.try_into().expect("Not an evm tx");

let tx_hash = evm_transaction.hash;
let raw_tx = ethereum
.make_raw_tx(evm_transaction)
let raw_evm_tx = RawEvmTransaction { rlp: data.to_vec() };
let (tx_hash, raw_tx) = ethereum
.make_raw_tx(raw_evm_tx)
.map_err(|e| to_jsonrpsee_error_object(e, ETH_RPC_ERROR))?;

ethereum
.send_tx_to_da(raw_tx)
.await
.map_err(|e| to_jsonrpsee_error_object(e, ETH_RPC_ERROR))?;

Ok::<_, ErrorObjectOwned>(H256::from(tx_hash))
Ok::<_, ErrorObjectOwned>(tx_hash)
},
)?;

Expand Down
5 changes: 2 additions & 3 deletions module-system/module-implementations/sov-evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ ethers-contract = { workspace = true }
ethers-middleware = { workspace = true }
ethers-providers = { workspace = true }
ethers-signers = { workspace = true }



ethers = { workspace = true }

revm = { workspace = true }
Expand All @@ -48,12 +45,14 @@ reth-rpc-types = { workspace = true }
reth-rpc = { workspace = true }
reth-revm = { workspace = true }


[dev-dependencies]

primitive-types = "0.12.1"
tokio = { workspace = true }
tempfile = { workspace = true }
bytes = { workspace = true }
secp256k1 = { workspace = true }

sov-modules-api = { path = "../../sov-modules-api", version = "0.1", features = ["macros"] }

Expand Down
22 changes: 12 additions & 10 deletions module-system/module-implementations/sov-evm/src/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use sov_state::WorkingSet;

use crate::evm::db::EvmDb;
use crate::evm::executor::{self};
use crate::evm::transaction::{BlockEnv, EvmTransaction};
use crate::evm::{contract_address, EvmChainCfg};
use crate::evm::transaction::{BlockEnv, EvmTransactionSignedEcRecovered};
use crate::evm::{contract_address, EvmChainCfg, RawEvmTransaction};
use crate::experimental::SpecIdWrapper;
use crate::{Evm, TransactionReceipt};

Expand All @@ -18,38 +18,40 @@ use crate::{Evm, TransactionReceipt};
)]
#[derive(borsh::BorshDeserialize, borsh::BorshSerialize, Debug, PartialEq, Clone)]
pub struct CallMessage {
pub tx: EvmTransaction,
pub tx: RawEvmTransaction,
}

impl<C: sov_modules_api::Context> Evm<C> {
pub(crate) fn execute_call(
&self,
tx: EvmTransaction,
tx: RawEvmTransaction,
_context: &C,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<CallResponse> {
// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/515
let evm_tx_recovered: EvmTransactionSignedEcRecovered = tx.clone().try_into()?;

let block_env = self.block_env.get(working_set).unwrap_or_default();
let cfg = self.cfg.get(working_set).unwrap_or_default();
let cfg_env = get_cfg_env(&block_env, cfg, None);
self.transactions.set(&tx.hash, &tx, working_set);

let hash = evm_tx_recovered.hash();
self.transactions.set(&hash, &tx, working_set);

let evm_db: EvmDb<'_, C> = self.get_db(working_set);

// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/505
let result = executor::execute_tx(evm_db, block_env, tx.clone(), cfg_env).unwrap();
let result = executor::execute_tx(evm_db, block_env, &evm_tx_recovered, cfg_env).unwrap();

let receipt = TransactionReceipt {
transaction_hash: tx.hash,
transaction_hash: hash.into(),
// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/504
transaction_index: 0,
// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/504
block_hash: Default::default(),
// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/504
block_number: Some(0),
from: tx.sender,
to: tx.to,
from: evm_tx_recovered.signer().into(),
to: evm_tx_recovered.to(),
// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/504
cumulative_gas_used: Default::default(),
// TODO https://github.com/Sovereign-Labs/sovereign-sdk/issues/504
Expand Down
Loading

0 comments on commit 23622de

Please sign in to comment.