Skip to content

Commit

Permalink
Support for evm "estimate" flag for RPC estimation (#230)
Browse files Browse the repository at this point in the history
* Update evm to 0.19

* Make evm config a variable in runner

* Finish estimate config support
  • Loading branch information
sorpaas authored Dec 7, 2020
1 parent a17720d commit 4c48cee
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 24 deletions.
18 changes: 9 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions client/rpc/src/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ impl<B, C, P, CT, BE, H: ExHashT> EthApiT for EthApi<B, C, P, CT, BE, H> where
gas_limit,
gas_price,
nonce,
false,
)
.map_err(|err| internal_err(format!("runtime error: {:?}", err)))?
.map_err(|err| internal_err(format!("execution fatal: {:?}", err)))?;
Expand All @@ -650,6 +651,7 @@ impl<B, C, P, CT, BE, H: ExHashT> EthApiT for EthApi<B, C, P, CT, BE, H> where
gas_limit,
gas_price,
nonce,
false,
)
.map_err(|err| internal_err(format!("runtime error: {:?}", err)))?
.map_err(|err| internal_err(format!("execution fatal: {:?}", err)))?;
Expand Down Expand Up @@ -689,6 +691,7 @@ impl<B, C, P, CT, BE, H: ExHashT> EthApiT for EthApi<B, C, P, CT, BE, H> where
gas_limit,
gas_price,
nonce,
true,
)
.map_err(|err| internal_err(format!("runtime error: {:?}", err)))?
.map_err(|err| internal_err(format!("execution fatal: {:?}", err)))?;
Expand All @@ -707,6 +710,7 @@ impl<B, C, P, CT, BE, H: ExHashT> EthApiT for EthApi<B, C, P, CT, BE, H> where
gas_limit,
gas_price,
nonce,
true,
)
.map_err(|err| internal_err(format!("runtime error: {:?}", err)))?
.map_err(|err| internal_err(format!("execution fatal: {:?}", err)))?;
Expand Down
2 changes: 1 addition & 1 deletion frame/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sp-runtime = { version = "2.0.0-dev", default-features = false, git = "https://g
sp-std = { version = "2.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
sp-io = { version = "2.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" }
fp-evm = { version = "0.8.0", default-features = false, path = "../../primitives/evm" }
evm = { version = "0.18.0", features = ["with-codec"], default-features = false }
evm = { version = "0.19.0", features = ["with-codec"], default-features = false }
ethereum = { version = "0.5", default-features = false, features = ["with-codec"] }
ethereum-types = { version = "0.9", default-features = false }
rlp = { version = "0.4", default-features = false }
Expand Down
6 changes: 5 additions & 1 deletion frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ decl_module! {
Some(transaction.gas_price),
Some(transaction.nonce),
transaction.action,
None,
)?;

let (reason, status, used_gas) = match info {
Expand Down Expand Up @@ -372,7 +373,7 @@ impl<T: Trait> Module<T> {
CurrentReceipts::get()
}

/// Execute an Ethereum transaction, ignoring transaction signatures.
/// Execute an Ethereum transaction.
pub fn execute(
from: H160,
input: Vec<u8>,
Expand All @@ -381,6 +382,7 @@ impl<T: Trait> Module<T> {
gas_price: Option<U256>,
nonce: Option<U256>,
action: TransactionAction,
config: Option<evm::Config>,
) -> Result<(Option<H160>, CallOrCreateInfo), DispatchError> {
match action {
ethereum::TransactionAction::Call(target) => {
Expand All @@ -392,6 +394,7 @@ impl<T: Trait> Module<T> {
gas_limit.low_u32(),
gas_price,
nonce,
config.as_ref().unwrap_or(T::config()),
).map_err(Into::into)?)))
},
ethereum::TransactionAction::Create => {
Expand All @@ -402,6 +405,7 @@ impl<T: Trait> Module<T> {
gas_limit.low_u32(),
gas_price,
nonce,
config.as_ref().unwrap_or(T::config()),
).map_err(Into::into)?)))
},
}
Expand Down
10 changes: 9 additions & 1 deletion frame/ethereum/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ fn transaction_should_increment_nonce() {
Some(t.gas_price),
Some(t.nonce),
t.action,
None,
));
assert_eq!(Evm::account_basic(&alice.address).nonce, U256::from(1));
});
Expand Down Expand Up @@ -115,6 +116,7 @@ fn transaction_with_invalid_nonce_should_not_work() {
Some(t.gas_price),
Some(t.nonce),
t.action,
None,
));

transaction.nonce = U256::from(0);
Expand Down Expand Up @@ -143,6 +145,7 @@ fn contract_constructor_should_get_executed() {
Some(t.gas_price),
Some(t.nonce),
t.action,
None,
));
assert_eq!(Evm::account_storages(
erc20_address, alice_storage_address
Expand Down Expand Up @@ -204,6 +207,7 @@ fn contract_should_be_created_at_given_address() {
Some(t.gas_price),
Some(t.nonce),
t.action,
None,
));
assert_ne!(Evm::account_codes(erc20_address).len(), 0);
});
Expand All @@ -226,6 +230,7 @@ fn transaction_should_generate_correct_gas_used() {
Some(t.gas_price),
Some(t.nonce),
t.action,
None,
).unwrap();

match info {
Expand Down Expand Up @@ -270,6 +275,7 @@ fn call_should_handle_errors() {
Some(t.gas_price),
Some(t.nonce),
t.action,
None,
));

let contract_address: Vec<u8> = FromHex::from_hex("32dcab0ef3fb2de2fce1d2e0799d36239671f04a").unwrap();
Expand All @@ -285,6 +291,7 @@ fn call_should_handle_errors() {
Some(U256::from(1)),
Some(U256::from(1)),
TransactionAction::Call(H160::from_slice(&contract_address)),
None,
).unwrap();

match info {
Expand All @@ -302,7 +309,8 @@ fn call_should_handle_errors() {
U256::from(1048576),
Some(U256::from(1)),
Some(U256::from(2)),
TransactionAction::Call(H160::from_slice(&contract_address))
TransactionAction::Call(H160::from_slice(&contract_address)),
None,
).ok().unwrap();
});
}
6 changes: 3 additions & 3 deletions frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ sp-io = { version = "2.0.0", default-features = false, git = "https://github.com
fp-evm = { version = "0.8.0", default-features = false, path = "../../primitives/evm" }
primitive-types = { version = "0.7.0", default-features = false, features = ["rlp", "byteorder"] }
rlp = { version = "0.4", default-features = false }
evm = { version = "0.18.5", default-features = false, features = ["with-codec"] }
evm-runtime = { version = "0.18", default-features = false }
evm-gasometer = { version = "0.18", default-features = false }
evm = { version = "0.19.0", default-features = false, features = ["with-codec"] }
evm-runtime = { version = "0.19.0", default-features = false }
evm-gasometer = { version = "0.19.0", default-features = false }
sha3 = { version = "0.8", default-features = false }
impl-trait-for-tuples = "0.1"
ripemd160 = { version = "0.9", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ decl_module! {
gas_limit,
Some(gas_price),
nonce,
T::config(),
)?;

match info.exit_reason {
Expand Down Expand Up @@ -429,6 +430,7 @@ decl_module! {
gas_limit,
Some(gas_price),
nonce,
T::config(),
)?;

match info {
Expand Down Expand Up @@ -476,6 +478,7 @@ decl_module! {
gas_limit,
Some(gas_price),
nonce,
T::config(),
)?;

match info {
Expand Down
9 changes: 3 additions & 6 deletions frame/evm/src/runner/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CallInfo, Self::Error> {
let gas_price = match gas_price {
Some(gas_price) => {
Expand All @@ -77,8 +78,6 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
origin: source,
};

let config = T::config();

let mut substate = Handler::<T>::new_with_precompile(
&vicinity,
gas_limit as usize,
Expand Down Expand Up @@ -123,6 +122,7 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error> {
let gas_price = match gas_price {
Some(gas_price) => {
Expand All @@ -145,8 +145,6 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
origin: source,
};

let config = T::config();

let mut substate = Handler::<T>::new_with_precompile(
&vicinity,
gas_limit as usize,
Expand Down Expand Up @@ -203,6 +201,7 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error> {
let gas_price = match gas_price {
Some(gas_price) => {
Expand All @@ -225,8 +224,6 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
origin: source,
};

let config = T::config();

let mut substate = Handler::<T>::new_with_precompile(
&vicinity,
gas_limit as usize,
Expand Down
3 changes: 3 additions & 0 deletions frame/evm/src/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub trait Runner<T: Trait> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CallInfo, Self::Error>;

fn create(
Expand All @@ -43,6 +44,7 @@ pub trait Runner<T: Trait> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error>;

fn create2(
Expand All @@ -53,5 +55,6 @@ pub trait Runner<T: Trait> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error>;
}
9 changes: 8 additions & 1 deletion frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ impl<T: Trait> Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
f: F,
) -> Result<ExecutionInfo<R>, Error<T>> where
F: FnOnce(&mut StackExecutor<Backend<T>>) -> (ExitReason, R),
Expand All @@ -66,7 +67,7 @@ impl<T: Trait> Runner<T> {
let mut executor = StackExecutor::new_with_precompile(
&backend,
gas_limit as usize,
T::config(),
config,
T::Precompiles::execute,
);

Expand Down Expand Up @@ -120,13 +121,15 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CallInfo, Self::Error> {
Self::execute(
source,
value,
gas_limit,
gas_price,
nonce,
config,
|executor| executor.transact_call(
source,
target,
Expand All @@ -144,13 +147,15 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error> {
Self::execute(
source,
value,
gas_limit,
gas_price,
nonce,
config,
|executor| {
let address = executor.create_address(
evm::CreateScheme::Legacy { caller: source },
Expand All @@ -173,6 +178,7 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit: u32,
gas_price: Option<U256>,
nonce: Option<U256>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error> {
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
Self::execute(
Expand All @@ -181,6 +187,7 @@ impl<T: Trait> RunnerT<T> for Runner<T> {
gas_limit,
gas_price,
nonce,
config,
|executor| {
let address = executor.create_address(
evm::CreateScheme::Create2 { caller: source, code_hash, salt },
Expand Down
2 changes: 1 addition & 1 deletion primitives/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ sp-core = { version = "2.0.0", git = "https://github.com/paritytech/substrate.gi
sp-std = { version = "2.0.0", git = "https://github.com/paritytech/substrate.git", branch = "frontier", default-features = false }
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false }
evm = { version = "0.18.5", default-features = false, features = ["with-codec"] }
evm = { version = "0.19.0", default-features = false, features = ["with-codec"] }

[features]
default = ["std"]
Expand Down
Loading

0 comments on commit 4c48cee

Please sign in to comment.