From bcb7745e713624c33c588da3a9db1318fc7b250c Mon Sep 17 00:00:00 2001 From: Alan Sapede Date: Thu, 23 Jul 2020 11:18:47 -0400 Subject: [PATCH] Allows eth call to be used to create contract (#82) --- frame/ethereum/src/lib.rs | 2 +- rpc/primitives/src/lib.rs | 4 ++-- rpc/src/lib.rs | 9 ++++++--- template/runtime/src/lib.rs | 35 ++++++++++++++++++++++++----------- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index a113ba0dda9b4..a42c938bd71dd 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -37,7 +37,7 @@ use rlp; use sha3::{Digest, Keccak256}; pub use frontier_rpc_primitives::TransactionStatus; -pub use ethereum::{Transaction, Log, Block, Receipt}; +pub use ethereum::{Transaction, Log, Block, Receipt, TransactionAction}; #[cfg(all(feature = "std", test))] mod tests; diff --git a/rpc/primitives/src/lib.rs b/rpc/primitives/src/lib.rs index f9dd171f7a6d6..150609b60a33c 100644 --- a/rpc/primitives/src/lib.rs +++ b/rpc/primitives/src/lib.rs @@ -19,7 +19,7 @@ use sp_core::{H160, H256, U256}; use ethereum::{ Log, Block as EthereumBlock, Transaction as EthereumTransaction, - Receipt as EthereumReceipt + Receipt as EthereumReceipt, TransactionAction }; use ethereum_types::Bloom; use codec::{Encode, Decode}; @@ -68,12 +68,12 @@ sp_api::decl_runtime_apis! { /// Returns a pallet_evm::execute_call response. fn call( from: H160, - to: H160, data: Vec, value: U256, gas_limit: U256, gas_price: U256, nonce: Option, + action: TransactionAction ) -> Option<(Vec, U256)>; /// For a given block number, returns an ethereum::Block and all its TransactionStatus. fn block_by_number(number: u32) -> (Option, Vec>); diff --git a/rpc/src/lib.rs b/rpc/src/lib.rs index 3be37fca5af9f..25c0351d1210d 100644 --- a/rpc/src/lib.rs +++ b/rpc/src/lib.rs @@ -16,7 +16,7 @@ use std::{marker::PhantomData, sync::Arc}; use std::collections::BTreeMap; -use ethereum::{Block as EthereumBlock, Transaction as EthereumTransaction}; +use ethereum::{Block as EthereumBlock, Transaction as EthereumTransaction, TransactionAction}; use ethereum_types::{H160, H256, H64, U256, U64}; use jsonrpc_core::{BoxFuture, Result, ErrorCode, Error, futures::future::{self, Future}}; use futures::future::TryFutureExt; @@ -470,12 +470,12 @@ impl EthApiT for EthApi where .call( &BlockId::Hash(header.hash()), from, - to, data, value, gas_limit, gas_price, nonce, + ethereum::TransactionAction::Call(to) ) .map_err(|_| internal_err("executing call failed"))? .ok_or(internal_err("inner executing call failed"))?; @@ -501,12 +501,15 @@ impl EthApiT for EthApi where .call( &BlockId::Hash(header.hash()), from, - to, data, value, gas_limit, gas_price, nonce, + match request.to { + Some(to) => ethereum::TransactionAction::Call(to), + _ => ethereum::TransactionAction::Create, + } ) .map_err(|_| internal_err("executing call failed"))? .ok_or(internal_err("inner executing call failed"))?; diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index e63a4a6207cf3..74a2af5082478 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -496,23 +496,36 @@ impl_runtime_apis! { fn call( from: H160, - to: H160, data: Vec, value: U256, gas_limit: U256, gas_price: U256, nonce: Option, + action: ethereum::TransactionAction, ) -> Option<(Vec, U256)> { - evm::Module::::execute_call( - from, - to, - data, - value, - gas_limit.low_u32(), - gas_price, - nonce, - false, - ).ok().map(|(_, ret, gas)| (ret, gas)) + match action { + ethereum::TransactionAction::Call(to) => + evm::Module::::execute_call( + from, + to, + data, + value, + gas_limit.low_u32(), + gas_price, + nonce, + false, + ).ok().map(|(_, ret, gas)| (ret, gas)), + ethereum::TransactionAction::Create => + evm::Module::::execute_create( + from, + data, + value, + gas_limit.low_u32(), + gas_price, + nonce, + false, + ).ok().map(|(_, _, gas)| (vec![], gas)), + } } fn block_by_number(number: u32) -> (