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

Feat: refactore gas charge logic form EVM exit reason #935

Merged
merged 26 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0647fcd
Gas cost estimation fot aurora-evm usize checked casting
mrLSD Jun 26, 2024
8866fbe
Changes gas costs
mrLSD Jun 26, 2024
8f0d5bc
Fix clippy
mrLSD Jun 27, 2024
551b79d
Update SpuntikVM to v0.44.1-aurora and change gas costs for some tests
mrLSD Jul 3, 2024
f79d294
Refactore EVM ExitReason gas charge logic
mrLSD Jul 5, 2024
0dd2cb9
Merge branch 'develop' into feat/charge-gas-refactoring
mrLSD Aug 12, 2024
a993c64
Extended TransactionStatus erros and added From impls for EVM ExitError
mrLSD Aug 13, 2024
16f1551
Merge branch 'develop' of github.com:aurora-is-near/aurora-engine int…
mrLSD Aug 13, 2024
763823d
Refactore tests
mrLSD Aug 13, 2024
48999d3
Refactore tests
mrLSD Aug 13, 2024
74bc509
Renamed EvmErrorKind and added Docs
mrLSD Aug 13, 2024
fc1aeeb
Merge branch 'develop' of github.com:aurora-is-near/aurora-engine int…
mrLSD Aug 13, 2024
8e5a941
Test: test_contract_create_too_large - change expected error
mrLSD Aug 13, 2024
4bc53ae
Changed comment for test: test_contract_create_too_large
mrLSD Aug 13, 2024
4945df0
Update to evm-0.45.1-aurora - with ExitFatal refactoring
mrLSD Aug 13, 2024
9a74881
Added chenges from PR review
mrLSD Aug 13, 2024
0ef3c57
Return panic erorr message
mrLSD Aug 14, 2024
56d8b35
Merge branch 'develop' of github.com:aurora-is-near/aurora-engine int…
mrLSD Aug 14, 2024
0e8b034
Update gas cost
mrLSD Aug 14, 2024
a672169
Return empty line
mrLSD Aug 14, 2024
e92d8b6
Refactore is_empty_storage
mrLSD Aug 14, 2024
ad70951
Refactore is_empty_storage
mrLSD Aug 14, 2024
40d2eed
Returned TransactionStatus old attributes. Adde test: is_empty_storage
mrLSD Aug 20, 2024
b979225
Added transaction_status order test. Fixed is_fail func
mrLSD Aug 21, 2024
20bc926
Fix clippy
mrLSD Aug 21, 2024
fdde7a5
chore: use plain enum for the TransactionStatus (#949)
aleksuss Aug 27, 2024
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
16 changes: 8 additions & 8 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ byte-slice-cast = { version = "1", default-features = false }
criterion = "0.5"
digest = "0.10"
ethabi = { version = "18", default-features = false }
evm = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.42.0-aurora", default-features = false }
evm-core = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.42.0-aurora", default-features = false, features = ["std"] }
evm-gasometer = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.42.0-aurora", default-features = false, features = ["std", "tracing"] }
evm-runtime = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.42.0-aurora", default-features = false, features = ["std", "tracing"] }
evm = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.44.1-aurora", default-features = false }
evm-core = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.44.1-aurora", default-features = false, features = ["std"] }
evm-gasometer = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.44.1-aurora", default-features = false, features = ["std", "tracing"] }
evm-runtime = { git = "https://github.com/aurora-is-near/sputnikvm.git", tag = "v0.44.1-aurora", default-features = false, features = ["std", "tracing"] }
fixed-hash = { version = "0.8", default-features = false }
function_name = "0.3"
git2 = "0.18"
Expand Down
3 changes: 3 additions & 0 deletions engine-sdk/src/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ impl<K: Ord, V> FullCache<K, V> {
pub fn get_or_insert_with<F: FnOnce() -> V>(&mut self, k: K, f: F) -> &mut V {
self.inner.entry(k).or_insert_with(f)
}
pub fn contains_key(&self, k: &K) -> bool {
self.inner.contains_key(k)
}
}
6 changes: 3 additions & 3 deletions engine-tests/src/tests/one_inch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn test_1inch_liquidity_protocol() {

let (result, profile, pool_factory) = helper.create_pool_factory(&deployer_address);
assert!(result.gas_used >= 2_800_000); // more than 2.8M EVM gas used
assert_gas_bound(profile.all_gas(), 9); // less than 9 NEAR Tgas used
assert_gas_bound(profile.all_gas(), 10); // less than 9 NEAR Tgas used

// create some ERC-20 tokens to have a liquidity pool for
let signer_address = utils::address_from_secret_key(&helper.signer.secret_key);
Expand All @@ -38,7 +38,7 @@ fn test_1inch_liquidity_protocol() {
let (result, profile, pool) =
helper.create_pool(&pool_factory, token_a.0.address, token_b.0.address);
assert!(result.gas_used >= 4_500_000); // more than 4.5M EVM gas used
assert_gas_bound(profile.all_gas(), 18);
assert_gas_bound(profile.all_gas(), 20);

// Approve giving ERC-20 tokens to the pool
helper.approve_erc20_tokens(&token_a, pool.address());
Expand Down Expand Up @@ -100,7 +100,7 @@ fn test_1_inch_limit_order_deploy() {
// more than 3.5 million Ethereum gas used
assert!(result.gas_used > 3_500_000);
// less than 11 NEAR TGas used
assert_gas_bound(profile.all_gas(), 11);
assert_gas_bound(profile.all_gas(), 12);
// at least 45% of which is from wasm execution
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
Expand Down
4 changes: 2 additions & 2 deletions engine-tests/src/tests/repro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ fn repro_5bEgfRQ() {
block_timestamp: 1_651_073_772_931_594_646,
input_path: "src/tests/res/input_5bEgfRQ.hex",
evm_gas_used: 6_414_105,
near_gas_used: 655,
near_gas_used: 658,
});
}

Expand Down Expand Up @@ -125,7 +125,7 @@ fn repro_Emufid2() {
block_timestamp: 1_662_118_048_636_713_538,
input_path: "src/tests/res/input_Emufid2.hex",
evm_gas_used: 1_156_364,
near_gas_used: 296,
near_gas_used: 297,
});
}

Expand Down
2 changes: 1 addition & 1 deletion engine-tests/src/tests/uniswap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn test_uniswap_exact_output() {

let (_amount_in, profile) =
context.exact_output_single(&token_a, &token_b, OUTPUT_AMOUNT.into());
utils::assert_gas_bound(profile.all_gas(), 16);
utils::assert_gas_bound(profile.all_gas(), 17);
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
(40..=50).contains(&wasm_fraction),
Expand Down
33 changes: 29 additions & 4 deletions engine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,17 @@ trait ExitIntoResult {
}

impl ExitIntoResult for ExitReason {
/// We should be aligned to Ethereum's gas charging:
/// - Success| Revert
/// - ExitError - Execution errors should charge gas from users
/// - ExitFatal - shouldn't charge user gas
/// NOTE: Transactions validation errors should not charge user gas
fn into_result(self, data: Vec<u8>) -> Result<TransactionStatus, EngineErrorKind> {
match self {
Self::Succeed(_) => Ok(TransactionStatus::Succeed(data)),
Self::Revert(_) => Ok(TransactionStatus::Revert(data)),
Self::Error(ExitError::OutOfOffset) => Ok(TransactionStatus::OutOfOffset),
Self::Error(ExitError::OutOfFund) => Ok(TransactionStatus::OutOfFund),
Self::Error(ExitError::OutOfGas) => Ok(TransactionStatus::OutOfGas),
Self::Error(e) => Err(e.into()),
// To be compatible with Ethereum behaviour we should charge gas for Execution errors
Self::Error(err) => Ok(err.into()),
Self::Fatal(e) => Err(e.into()),
}
}
Expand Down Expand Up @@ -1544,6 +1547,11 @@ pub fn get_storage<I: IO>(io: &I, address: &Address, key: &H256, generation: u32
.unwrap_or_default()
}

pub fn is_empty_storage<I: IO>(io: &I, address: &Address, key: &H256, generation: u32) -> bool {
io.read_storage(storage_to_key(address, key, generation).as_ref())
.is_none()
}

pub fn is_account_empty<I: IO>(io: &I, address: &Address) -> bool {
get_balance(io, address).is_zero()
&& get_nonce(io, address).is_zero()
Expand Down Expand Up @@ -1915,6 +1923,23 @@ impl<'env, I: IO + Copy, E: Env, M: ModExpAlgorithm> Backend for Engine<'env, I,
result
}

fn is_empty_storage(&self, address: H160) -> bool {
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
let address = Address::new(address);
if self
.contract_storage_cache
.borrow()
.contains_key(&(address, H256::zero()))
{
return true;
mrLSD marked this conversation as resolved.
Show resolved Hide resolved
}
let generation = *self
.generation_cache
.borrow_mut()
.entry(address)
.or_insert_with(|| get_generation(&self.io, &address));
is_empty_storage(&self.io, &address, &H256::zero(), generation)
}

/// Get original storage value of address at index, if available.
///
/// Since `SputnikVM` collects storage changes in memory until the transaction is over,
Expand Down
Loading