Skip to content

Commit

Permalink
Implemente eos-evm gas-fee (G_txnewaccount), update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
elmato committed Feb 27, 2024
1 parent b6967de commit c8cdb63
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
17 changes: 11 additions & 6 deletions silkworm/core/execution/evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,18 @@ evmc::Result EVM::call(const evmc_message& message) noexcept {
tracer.get().on_execution_end(res.raw(),state_);
}
} else {
const ByteView code{state_.get_code(message.code_address)};
if (code.empty() && tracers_.empty()) { // Do not skip execution if there are any tracers
return res;
}
if(message.depth == 0 && state_.is_dead(message.recipient)) {
if ((res.gas_left -= static_cast<int64_t>(gas_params_.G_txnewaccount)) < 0)
res.status_code = EVMC_OUT_OF_GAS;
} else {
const ByteView code{state_.get_code(message.code_address)};
if (code.empty() && tracers_.empty()) { // Do not skip execution if there are any tracers
return res;
}

const evmc::bytes32 code_hash{state_.get_code_hash(message.code_address)};
res = evmc::Result{execute(message, code, &code_hash)};
const evmc::bytes32 code_hash{state_.get_code_hash(message.code_address)};
res = evmc::Result{execute(message, code, &code_hash)};
}
}

if (res.status_code != EVMC_SUCCESS) {
Expand Down
4 changes: 4 additions & 0 deletions silkworm/core/execution/evm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class EVM {
message_filter_ = message_filter;
}

void update_gas_params(const evmone::gas_parameters& gas_params) {
gas_params_ = gas_params;
}

private:
friend class EvmHost;

Expand Down
36 changes: 36 additions & 0 deletions silkworm/core/execution/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,42 @@ TEST_CASE("EOS EVM codedeposit test") {
CHECK(gas-res.gas_left == 123 + 500*371); //G_codedeposit=500, codelen=371
}

TEST_CASE("EOS EVM G_txnewaccount") {
Block block{};
block.header.number = 1;
block.header.nonce = eosevm::version_to_nonce(1);

evmc::address sender{0x0a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address};
evmc::address receiver{0x1a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address};

evmone::gas_parameters gas_params;
gas_params.G_txnewaccount = 0;

InMemoryState db;
IntraBlockState state{db};
state.set_balance(sender, intx::uint256{1e18});
EVM evm{block, state, test::kIstanbulTrustConfig, gas_params};

Transaction txn{};
txn.from = sender;
txn.to = receiver;
txn.value = intx::uint256{0};

CallResult res = evm.execute(txn, 0);
CHECK(res.status == EVMC_SUCCESS);
CHECK(res.gas_left == 0);
CHECK(res.gas_refund == 0);

gas_params.G_txnewaccount = 1;
evm.update_gas_params(gas_params);

res = evm.execute(txn, 0);
CHECK(res.status == EVMC_OUT_OF_GAS);
CHECK(res.gas_refund == 0);
CHECK(res.gas_left == 0);

}



} // namespace silkworm
2 changes: 1 addition & 1 deletion third_party/evmone

0 comments on commit c8cdb63

Please sign in to comment.