Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,23 @@ where
>,
) -> (ExitReason, R),
{
// EIP-3607: https://eips.ethereum.org/EIPS/eip-3607
// Do not allow transactions for which `tx.sender` has any code deployed.
//
// We extend the principle of this EIP to also prevent `tx.sender` to be the address
// of a precompile. While mainnet Ethereum currently only has stateless precompiles,
// projects using Frontier can have stateful precompiles that can manage funds or
// which calls other contracts that expects this precompile address to be trustworthy.
if !<AccountCodes<T>>::get(source).is_empty() || precompiles.is_precompile(source) {
return Err(RunnerError {
error: Error::<T>::TransactionMustComeFromEOA,
weight,
});
// Only check the restrictions of EIP-3607 if the source of the EVM operation is from an external transaction.
// If the source of this EVM operation is from an internal call, like from `eth_call` or `eth_estimateGas` RPC,
// we will skip the checks for the EIP-3607.
if is_transactional {
// EIP-3607: https://eips.ethereum.org/EIPS/eip-3607
// Do not allow transactions for which `tx.sender` has any code deployed.
//
// We extend the principle of this EIP to also prevent `tx.sender` to be the address
// of a precompile. While mainnet Ethereum currently only has stateless precompiles,
// projects using Frontier can have stateful precompiles that can manage funds or
// which calls other contracts that expects this precompile address to be trustworthy.
if !<AccountCodes<T>>::get(source).is_empty() || precompiles.is_precompile(source) {
return Err(RunnerError {
error: Error::<T>::TransactionMustComeFromEOA,
weight,
});
}
}

let (total_fee_per_gas, _actual_priority_fee_per_gas) =
Expand Down
54 changes: 50 additions & 4 deletions frame/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ fn eip3607_transaction_from_contract_should_fail() {
None,
None,
Vec::new(),
false, // non-transactional
true, // must be validated
true, // transactional
false, // may not be validated
&<Test as Config>::config().clone(),
) {
Err(RunnerError {
Expand All @@ -585,6 +585,29 @@ fn eip3607_transaction_from_contract_should_fail() {
});
}

#[test]
fn eip3607_non_transaction_from_contract_should_succeed() {
new_test_ext().execute_with(|| {
let r = <Test as Config>::Runner::call(
// Contract address.
H160::from_str("1000000000000000000000000000000000000001").unwrap(),
H160::from_str("1000000000000000000000000000000000000001").unwrap(),
Vec::new(),
U256::from(1u32),
1000000,
None,
None,
None,
Vec::new(),
false, // non-transactional
true, // must be validated
&<Test as Config>::config().clone(),
);

assert!(r.is_ok());
});
}

#[test]
fn eip3607_transaction_from_precompile_should_fail() {
new_test_ext().execute_with(|| {
Expand All @@ -599,8 +622,8 @@ fn eip3607_transaction_from_precompile_should_fail() {
None,
None,
Vec::new(),
false, // non-transactional
true, // must be validated
true, // transactional
false, // may be validated
&<Test as Config>::config().clone(),
) {
Err(RunnerError {
Expand All @@ -611,3 +634,26 @@ fn eip3607_transaction_from_precompile_should_fail() {
}
});
}

#[test]
fn eip3607_non_transaction_from_precompile_should_succeed() {
new_test_ext().execute_with(|| {
let r = <Test as Config>::Runner::call(
// Precompile address.
H160::from_str("0000000000000000000000000000000000000001").unwrap(),
H160::from_str("1000000000000000000000000000000000000001").unwrap(),
Vec::new(),
U256::from(1u32),
1000000,
None,
None,
None,
Vec::new(),
false, // non-transactional
true, // must be validated
&<Test as Config>::config().clone(),
);

assert!(r.is_ok());
});
}
77 changes: 77 additions & 0 deletions ts-tests/tests/test-eth-call.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { expect, use as chaiUse } from "chai";
import chaiAsPromised from "chai-as-promised";
import Web3 from "web3";
import { Contract } from "web3-eth-contract";
import { AbiItem } from "web3-utils";

import Test from "../build/contracts/Test.json";
import { GENESIS_ACCOUNT, GENESIS_ACCOUNT_PRIVATE_KEY } from "./config";
import { createAndFinalizeBlock, customRequest, describeWithFrontier } from "./util";

chaiUse(chaiAsPromised);

async function deployContract(
web3: Web3,
bytecode: string,
abi: AbiItem[]
): Promise<{
contract: Contract;
contractAddress: string;
}> {
const contract = new web3.eth.Contract(abi);
const data = contract.deploy({ data: bytecode }).encodeABI();
const tx = await web3.eth.accounts.signTransaction(
{
from: GENESIS_ACCOUNT,
data,
value: "0x00",
gas: "0x100000",
},
GENESIS_ACCOUNT_PRIVATE_KEY
);
const { result } = await customRequest(web3, "eth_sendRawTransaction", [tx.rawTransaction]);
await createAndFinalizeBlock(web3);
const receipt = await web3.eth.getTransactionReceipt(result);
const contractAddress = receipt.contractAddress;

return { contract, contractAddress };
}

describeWithFrontier("Frontier RPC (EthCall)", (context) => {
let contract: Contract;
let contractAddress: string;
let contractAddress2: string;

before("deploy contract to two addresses", async function () {
this.timeout(15000);
const firstDeploy = await deployContract(context.web3, Test.bytecode, Test.abi as AbiItem[]);
const secondDeploy = await deployContract(context.web3, Test.bytecode, Test.abi as AbiItem[]);
contract = firstDeploy.contract;
contractAddress = firstDeploy.contractAddress;
contractAddress2 = secondDeploy.contractAddress;
});

it("should be able to call eth_call from address with no code", async function () {
const r = await customRequest(context.web3, "eth_call", [
{
from: GENESIS_ACCOUNT,
to: contractAddress,
data: await contract.methods.multiply(5).encodeABI(),
},
]);
expect(r.error?.message).to.be.undefined;
expect(Web3.utils.hexToNumberString(r.result)).to.equal("35");
});

it("should be able to call eth_call from address with code", async function () {
const r = await customRequest(context.web3, "eth_call", [
{
from: contractAddress2,
to: contractAddress,
data: await contract.methods.multiply(5).encodeABI(),
},
]);
expect(r.error?.message).to.be.undefined;
expect(Web3.utils.hexToNumberString(r.result)).to.equal("35");
});
});