Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evm gas estimation #3299

Merged
merged 66 commits into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
29c8863
initial gas used in evm interpreter
ailisp Sep 9, 2020
6b02f23
see what case in result
ailisp Sep 9, 2020
7917573
atempt of return different gas left
ailisp Sep 11, 2020
981e7c1
gas_left
ailisp Sep 11, 2020
c90e816
all gas metering works
ailisp Sep 11, 2020
31fc610
undo all debug prints
ailisp Sep 11, 2020
f5a7002
revert is not considered as error until top level to calc revert gas …
ailisp Sep 11, 2020
ecc207d
resolve all gas from revert
ailisp Sep 12, 2020
f00a8ae
cost hook for precompile functions
ailisp Sep 12, 2020
8508167
basic evm gas counter
ailisp Sep 12, 2020
b9adf54
got evm gas used in all tests
ailisp Sep 12, 2020
bb50e66
count deploy evm code cost
ailisp Sep 15, 2020
ca0b69e
compiles
ailisp Sep 15, 2020
740d012
wip measure functions
ailisp Sep 15, 2020
c1f6210
fix deploy cost
ailisp Sep 15, 2020
299ae12
measure funcall cost, start adding contract function to know precompi…
ailisp Sep 16, 2020
15445f4
all cost countings except bn256 precompiled functions
ailisp Sep 17, 2020
8752e6c
calc and deduct near gas from number given by evm gas
ailisp Sep 17, 2020
97f7947
address illia comments
ailisp Sep 17, 2020
14e55ef
thread in prepaid function cost in evm
ailisp Sep 17, 2020
fdb49b9
fix random fee
ailisp Sep 17, 2020
c8e8449
convert prepaid near gas to evm gas so evm gas convert to near wont o…
ailisp Sep 18, 2020
c03b077
made mistake of reverse base cost and per_evm_gas cost, correct it
ailisp Sep 18, 2020
05df01a
wip measure evm include block cost, stuck in get account code hash sa…
ailisp Sep 23, 2020
521f87a
deploy contract total cost calc works
ailisp Sep 24, 2020
30811dd
merge from evm-precompile
ailisp Sep 24, 2020
4a39ee8
fix small errors after resolve conflict
ailisp Sep 24, 2020
71917fb
deploy cost works, function cost is 0, debug
ailisp Sep 25, 2020
2c49248
evm cost of function call
ailisp Sep 25, 2020
f4dcfa9
comment
ailisp Sep 25, 2020
dcf1c70
deduct action fee and todo on get correct evm addr to function call
ailisp Sep 26, 2020
7b21ee7
fix deploy evm addr
ailisp Sep 26, 2020
53c39f8
correct deduct action receipt cost
ailisp Sep 26, 2020
d7dd8c5
saturating sub
ailisp Sep 28, 2020
f831b07
adding warmup
ailisp Sep 29, 2020
6ca3489
remove some debug prints
ailisp Sep 29, 2020
0e1bb66
costs counting only on recet sevm gas counter
ailisp Sep 30, 2020
de25d56
merge and resolve conflict
ailisp Sep 30, 2020
f4e9b76
some fixes
ailisp Sep 30, 2020
901e341
fix errors in semantic conflicts happens in merge
ailisp Sep 30, 2020
f8ebf3c
delete
ailisp Oct 5, 2020
79fea3c
nit
ailisp Oct 5, 2020
4c6721e
nit
ailisp Oct 5, 2020
759e89f
nit
ailisp Oct 5, 2020
329754c
nit
ailisp Oct 5, 2020
8d39d07
refactor use test-utils.testlib to get evm contract addr
ailisp Oct 7, 2020
1896a50
evm deploy cost based on both evm gas and contract size
ailisp Oct 9, 2020
0423b63
lsq compiles
ailisp Oct 13, 2020
3ac79ac
lsq compiles
ailisp Oct 13, 2020
940904f
my rust impl of 2d lsq, end result stable but lsq is incorrect
ailisp Oct 14, 2020
6e151d4
simplify
ailisp Oct 14, 2020
36b0602
accurate evm deploy base cost, per evm gas and per byte
ailisp Oct 14, 2020
681bd9a
fix deploy evm contract to all accounts nonce issue
ailisp Oct 16, 2020
6139e99
balancer contract binary suite
ailisp Oct 16, 2020
63d9937
evm precompile function per byte evm gas, but they are too small
ailisp Oct 16, 2020
4cc2940
wire in evm gas per byte and update numbers
ailisp Oct 16, 2020
133a4ff
merge from evm-precompile and resolve conflict
ailisp Oct 19, 2020
d03d30a
avoid pay evm bootstrap fee when evm is not started
ailisp Oct 19, 2020
bcb2358
do not measure action receipt fee for evm as they are deduct separately
ailisp Oct 21, 2020
af97944
new numbers
ailisp Oct 21, 2020
9291037
merge evm-precompile fix confliction and test
ailisp Oct 23, 2020
675d5ca
resolve conflict, compiles
ailisp Oct 30, 2020
702b6d1
features for runtime-params-estimator
ailisp Oct 30, 2020
29fa4df
evm features works in runtime-param-estimator
ailisp Nov 2, 2020
b52d37a
undo debug print
ailisp Nov 2, 2020
838acfd
undo disable lto
ailisp Nov 2, 2020
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
10 changes: 9 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion runtime/near-evm-runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "near-evm-runner"
version = "0.1.0"
version = "2.2.0"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
38 changes: 35 additions & 3 deletions runtime/near-evm-runner/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{

use byteorder::{BigEndian, ByteOrder, LittleEndian, ReadBytesExt};
use ethereum_types::{Address, H256, U256};
use near_runtime_fees::EvmCostConfig;
use num_bigint::BigUint;
use num_traits::{FromPrimitive, One, ToPrimitive, Zero};
use parity_bytes::BytesRef;
Expand Down Expand Up @@ -45,13 +46,23 @@ pub fn precompile(id: u64) -> Result<Box<dyn Impl>, String> {
})
}

pub fn process_precompile(addr: &Address, input: &[u8]) -> MessageCallResult {
pub fn process_precompile(
addr: &Address,
input: &[u8],
gas: &U256,
evm_gas_config: &EvmCostConfig,
) -> MessageCallResult {
let f = match precompile(addr.to_low_u64_be()) {
Ok(f) => f,
Err(_) => return MessageCallResult::Failed,
};
let mut bytes = vec![];
let mut output = parity_bytes::BytesRef::Flexible(&mut bytes);
let cost = f.gas(input, evm_gas_config);

if cost > *gas {
return MessageCallResult::Failed;
}

// mutates bytes
match f.execute(input, &mut output) {
Expand All @@ -60,8 +71,7 @@ pub fn process_precompile(addr: &Address, input: &[u8]) -> MessageCallResult {
};
let size = bytes.len();

// TODO: add gas usage here.
MessageCallResult::Success(1_000_000_000.into(), ReturnData::new(bytes, 0, size))
MessageCallResult::Success(*gas - cost, ReturnData::new(bytes, 0, size))
}

/** the following is copied from ethcore/src/builtin.rs **/
Expand Down Expand Up @@ -128,13 +138,19 @@ pub struct Blake2FImpl;
pub trait Impl: Send + Sync {
/// execute this built-in on the given input, writing to the given output.
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error>;
fn gas(&self, _input: &[u8], _evm_gas_config: &EvmCostConfig) -> U256 {
0.into()
}
}

impl Impl for Identity {
fn execute(&self, input: &[u8], output: &mut BytesRef) -> Result<(), Error> {
output.write(0, input);
Ok(())
}
fn gas(&self, _input: &[u8], evm_gas_config: &EvmCostConfig) -> U256 {
evm_gas_config.identity_cost.into()
}
}

impl Impl for EcRecover {
Expand Down Expand Up @@ -174,6 +190,10 @@ impl Impl for EcRecover {

Ok(())
}

fn gas(&self, _input: &[u8], evm_gas_config: &EvmCostConfig) -> U256 {
evm_gas_config.ecrecover_cost.into()
}
}

impl Impl for Sha256 {
Expand All @@ -183,6 +203,10 @@ impl Impl for Sha256 {
output.write(0, &*d);
Ok(())
}

fn gas(&self, _input: &[u8], evm_gas_config: &EvmCostConfig) -> U256 {
evm_gas_config.sha256_cost.into()
}
}

impl Impl for Ripemd160 {
Expand All @@ -192,6 +216,10 @@ impl Impl for Ripemd160 {
output.write(12, &hash);
Ok(())
}

fn gas(&self, _input: &[u8], evm_gas_config: &EvmCostConfig) -> U256 {
evm_gas_config.ripemd160_cost.into()
}
}

// calculate modexp: left-to-right binary exponentiation to keep multiplicands lower
Expand Down Expand Up @@ -300,6 +328,10 @@ impl Impl for ModexpImpl {

Ok(())
}

fn gas(&self, _input: &[u8], evm_gas_config: &EvmCostConfig) -> U256 {
evm_gas_config.modexp_cost.into()
}
}

fn read_fr(reader: &mut io::Chain<&[u8], io::Repeat>) -> Result<::bn::Fr, Error> {
Expand Down
24 changes: 24 additions & 0 deletions runtime/near-evm-runner/src/evm_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,30 @@ impl EvmState for SubState<'_> {
}
}

pub struct EvmGasCounter {
pub used_gas: U256,
pub max_gas: U256,
}

impl EvmGasCounter {
pub fn new(used_gas: U256, max_gas: U256) -> EvmGasCounter {
Self { used_gas, max_gas }
}

pub fn pay_gas(&mut self, amount: U256) {
// TODO: return error if gas not sufficient
self.used_gas += amount;
}

pub fn set_gas_left(&mut self, left: U256) {
self.used_gas = self.max_gas - left;
ailisp marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn gas_left(&self) -> U256 {
self.max_gas - self.used_gas
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading