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

Track storage usage #214

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
60 changes: 42 additions & 18 deletions silkworm/core/execution/evm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ CallResult EVM::execute(const Transaction& txn, uint64_t gas) noexcept {

const auto gas_left = static_cast<uint64_t>(res.gas_left);
const auto gas_refund = static_cast<uint64_t>(res.gas_refund);
return {res.status_code, gas_left, gas_refund, {res.output_data, res.output_size}};
const auto storage_gas_consumed = static_cast<uint64_t>(res.storage_gas_consumed);
const auto storage_gas_refund = static_cast<uint64_t>(res.storage_gas_refund);
const auto speculative_cpu_gas_consumed = static_cast<uint64_t>(res.speculative_cpu_gas_consumed);
return {res.status_code, gas_left, gas_refund, storage_gas_consumed, storage_gas_refund, speculative_cpu_gas_consumed, {res.output_data, res.output_size}};
}

evmc::Result EVM::create(const evmc_message& message) noexcept {
evmc::Result res{EVMC_SUCCESS, message.gas, 0};
evmc::Result res(EVMC_SUCCESS, message.gas, 0, 0, 0, 0, nullptr, 0);

auto value{intx::be::load<intx::uint256>(message.value)};
if (state_.get_balance(message.sender) < value) {
Expand Down Expand Up @@ -158,17 +161,30 @@ evmc::Result EVM::create(const evmc_message& message) noexcept {

auto evm_res{execute(deploy_message, ByteView{message.input_data, message.input_size}, /*code_hash=*/nullptr)};

auto has_sufficient_gas_for_deployment = [&](uint64_t code_deploy_gas) {
if(eos_evm_version_ >= 3) {
evmone::gas_state_t tmp_gas_state(eos_evm_version_, evm_res.gas_refund, evm_res.storage_gas_consumed, evm_res.storage_gas_refund, evm_res.speculative_cpu_gas_consumed);
code_deploy_gas = static_cast<uint64_t>(tmp_gas_state.apply_storage_gas_delta(static_cast<int64_t>(code_deploy_gas)));
}
return evm_res.gas_left >= 0 && static_cast<uint64_t>(evm_res.gas_left) >= code_deploy_gas;
};

if (evm_res.status_code == EVMC_SUCCESS) {
const size_t code_len{evm_res.output_size};
const uint64_t code_deploy_gas{code_len * (eos_evm_version_ > 0 ? gas_params_.G_codedeposit : protocol::fee::kGCodeDeposit)};

uint64_t code_deploy_gas{code_len * (eos_evm_version_ > 0 ? gas_params_.G_codedeposit : protocol::fee::kGCodeDeposit)};
if (rev >= EVMC_SPURIOUS_DRAGON && code_len > protocol::kMaxCodeSize) {
// EIP-170: Contract code size limit
evm_res.status_code = EVMC_OUT_OF_GAS;
} else if (rev >= EVMC_LONDON && code_len > 0 && evm_res.output_data[0] == 0xEF) {
// EIP-3541: Reject new contract code starting with the 0xEF byte
evm_res.status_code = EVMC_CONTRACT_VALIDATION_FAILURE;
} else if (evm_res.gas_left >= 0 && static_cast<uint64_t>(evm_res.gas_left) >= code_deploy_gas) {
} else if (has_sufficient_gas_for_deployment(code_deploy_gas)) {
if(eos_evm_version_ >= 3) {
evmone::gas_state_t tmp_gas_state(eos_evm_version_, evm_res.gas_refund, evm_res.storage_gas_consumed, evm_res.storage_gas_refund, evm_res.speculative_cpu_gas_consumed);
code_deploy_gas = static_cast<uint64_t>(tmp_gas_state.apply_storage_gas_delta(static_cast<int64_t>(code_deploy_gas)));
evm_res.storage_gas_consumed = tmp_gas_state.storage_gas_consumed();
evm_res.storage_gas_refund = tmp_gas_state.storage_gas_refund();
}
evm_res.gas_left -= static_cast<int64_t>(code_deploy_gas);
state_.set_code(contract_addr, {evm_res.output_data, evm_res.output_size});
} else if (rev >= EVMC_HOMESTEAD) {
Expand All @@ -181,6 +197,7 @@ evmc::Result EVM::create(const evmc_message& message) noexcept {
} else {
state_.revert_to_snapshot(snapshot);
evm_res.gas_refund = 0;
evm_res.storage_gas_refund = 0;
if (evm_res.status_code != EVMC_REVERT) {
evm_res.gas_left = 0;
}
Expand All @@ -195,7 +212,7 @@ evmc::Result EVM::create(const evmc_message& message) noexcept {
}

evmc::Result EVM::call(const evmc_message& message) noexcept {
evmc::Result res{EVMC_SUCCESS, message.gas};
evmc::Result res(EVMC_SUCCESS, message.gas, 0, 0, 0, 0, nullptr, 0);

auto at_exit = gsl::finally([&] {
if(res.status_code == EVMC_SUCCESS && message_filter_ && (*message_filter_)(message)) {
Expand Down Expand Up @@ -255,31 +272,39 @@ evmc::Result EVM::call(const evmc_message& message) noexcept {
tracer.get().on_execution_end(res.raw(),state_);
}
} else {

evmc_message revisted_message{message};
if(eos_evm_version_ > 0 && revisted_message.depth == 0 && !is_zero(evmc::bytes32{revisted_message.value}) && !recipient_exists) {
if ((res.gas_left -= static_cast<int64_t>(gas_params_.G_txnewaccount)) < 0) {
evmc_message revisited_message{message};
if(eos_evm_version_ > 0 && revisited_message.depth == 0 && !is_zero(evmc::bytes32{revisited_message.value}) && !recipient_exists) {
int64_t cost = static_cast<int64_t>(gas_params_.G_txnewaccount);
if ((res.gas_left -= cost) < 0) {
// If we run out of gas lets do everything here
state_.revert_to_snapshot(snapshot);
res.status_code = EVMC_OUT_OF_GAS;
res.gas_refund = 0;
if( eos_evm_version_ >= 3 ) {
res.storage_gas_consumed = cost + res.gas_left;
}
res.gas_left = 0;
for (auto tracer : tracers_) {
tracer.get().on_execution_start(rev, revisted_message, {});
tracer.get().on_execution_start(rev, revisited_message, {});
tracer.get().on_execution_end(res.raw(), state_);
}
return res;
}
revisted_message.gas = res.gas_left;
res.storage_gas_consumed = cost;
revisited_message.gas = res.gas_left;
}

const ByteView code{state_.get_code(revisted_message.code_address)};
const ByteView code{state_.get_code(revisited_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(revisted_message.code_address)};
res = evmc::Result{execute(revisted_message, code, &code_hash)};
const evmc::bytes32 code_hash{state_.get_code_hash(revisited_message.code_address)};
auto storage_gas_consumed = res.storage_gas_consumed;
res = evmc::Result{execute(revisited_message, code, &code_hash)};
if( eos_evm_version_ >= 3 ) {
res.storage_gas_consumed += storage_gas_consumed;
}
}

if (res.status_code != EVMC_SUCCESS) {
Expand Down Expand Up @@ -343,8 +368,7 @@ evmc_result EVM::execute_with_baseline_interpreter(evmc_revision rev, const evmc
EvmHost host{*this};
gsl::owner<evmone::ExecutionState*> state{acquire_state()};

state->reset(msg, rev, host.get_interface(), host.to_context(), code, gas_params_);
state->eos_evm_version = eos_evm_version_;
state->reset(msg, rev, host.get_interface(), host.to_context(), code, gas_params_, eos_evm_version_);

const auto vm{static_cast<evmone::VM*>(evm1_)};
evmc_result res{evmone::baseline::execute(*vm, msg.gas, *state, *analysis)};
Expand Down Expand Up @@ -497,7 +521,7 @@ evmc::Result EvmHost::call(const evmc_message& message) noexcept {
// geth returns CREATE output only in case of REVERT
return res;
} else {
evmc::Result res_with_no_output{res.status_code, res.gas_left, res.gas_refund};
evmc::Result res_with_no_output(res.status_code, res.gas_left, res.gas_refund, res.storage_gas_consumed, res.storage_gas_refund, res.speculative_cpu_gas_consumed, nullptr, 0);
res_with_no_output.create_address = res.create_address;
return res_with_no_output;
}
Expand Down
3 changes: 3 additions & 0 deletions silkworm/core/execution/evm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ struct CallResult {
evmc_status_code status{EVMC_SUCCESS};
uint64_t gas_left{0};
uint64_t gas_refund{0};
uint64_t storage_gas_consumed{0};
uint64_t storage_gas_refund{0};
uint64_t speculative_cpu_gas_consumed{0};
Bytes data;
};

Expand Down
67 changes: 66 additions & 1 deletion silkworm/core/execution/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,11 @@ class TestTracer : public EvmTracer {
execution_end_called_ = true;
const auto gas_left = static_cast<uint64_t>(res.gas_left);
const auto gas_refund = static_cast<uint64_t>(res.gas_refund);
result_ = {res.status_code, gas_left, gas_refund, {res.output_data, res.output_size}};
const auto storage_gas_consumed = static_cast<uint64_t>(res.storage_gas_consumed);
const auto storage_gas_refund = static_cast<uint64_t>(res.storage_gas_refund);
const auto speculative_cpu_gas_consumed = static_cast<uint64_t>(res.speculative_cpu_gas_consumed);

result_ = {res.status_code, gas_left, gas_refund, storage_gas_consumed, storage_gas_refund, speculative_cpu_gas_consumed, {res.output_data, res.output_size}};
if (contract_address_ && !pc_stack_.empty()) {
const auto pc = pc_stack_.back();
storage_stack_[pc] =
Expand Down Expand Up @@ -751,6 +755,35 @@ TEST_CASE("EOS EVM codedeposit test") {
CHECK(gas-res.gas_left == 123 + 500*371); //G_codedeposit=500, codelen=371
}

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

evmc::address caller{0x0a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address};
Bytes code{*from_hex("608060405234801561001057600080fd5b50610173806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c806313bdfacd14610030575b600080fd5b61003861004e565b604051610045919061011b565b60405180910390f35b60606040518060400160405280600c81526020017f48656c6c6f20576f726c64210000000000000000000000000000000000000000815250905090565b600081519050919050565b600082825260208201905092915050565b60005b838110156100c55780820151818401526020810190506100aa565b60008484015250505050565b6000601f19601f8301169050919050565b60006100ed8261008b565b6100f78185610096565b93506101078185602086016100a7565b610110816100d1565b840191505092915050565b6000602082019050818103600083015261013581846100e2565b90509291505056fea264697066735822122046344ce4c7e7c91dba98aef897cc7773af40fbff6b6da10885c36037b9d37a3764736f6c63430008110033")};

evmone::gas_parameters gas_params;
gas_params.G_codedeposit = 500;

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

Transaction txn{};
txn.from = caller;
txn.data = code;

uint64_t gas = 1'500'000;
CallResult res = evm.execute(txn, gas);
CHECK(res.status == EVMC_SUCCESS);
CHECK(gas-res.gas_left == 123 + 500*371); //G_codedeposit=500, codelen=371
CHECK(res.storage_gas_consumed == 500*371);
CHECK(res.storage_gas_refund == 0);
CHECK(res.speculative_cpu_gas_consumed == 0);
}

TEST_CASE("EOS EVM G_txnewaccount") {
Block block{};
block.header.number = 1;
Expand Down Expand Up @@ -789,6 +822,38 @@ TEST_CASE("EOS EVM G_txnewaccount") {

}

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

evmc::address sender{0x0a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address};
evmc::address receiver1{0x1a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address};
evmc::address receiver2{0x1000000000000000000000000000000000000001_address};

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

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 = receiver1;
txn.value = intx::uint256{1};

CallResult res = evm.execute(txn, 1000);
CHECK(res.status == EVMC_SUCCESS);
CHECK(res.gas_left == 0);
CHECK(res.gas_refund == 0);
CHECK(res.storage_gas_consumed == 1000);
CHECK(res.storage_gas_refund == 0);
CHECK(res.speculative_cpu_gas_consumed == 0);
}


TEST_CASE("EOS EVM send value to reserved address (tx)") {

auto send_tx_to_reserved_address = [&](uint64_t version, const evmone::gas_parameters& gas_params, uint64_t gas_limit) {
Expand Down
40 changes: 32 additions & 8 deletions silkworm/core/execution/processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,40 @@ void ExecutionProcessor::execute_transaction(const Transaction& txn, Receipt& re
const intx::uint256 data_gas_price{evm_.block().header.data_gas_price().value_or(0)};
state_.subtract_from_balance(*txn.from, txn.total_data_gas() * data_gas_price);

const intx::uint128 g0{protocol::intrinsic_gas(txn, rev, evm_.get_eos_evm_version(), evm_.get_gas_params())};
const auto eos_evm_version = evm_.get_eos_evm_version();
const auto& gas_params = evm_.get_gas_params();

const intx::uint128 g0{protocol::intrinsic_gas(txn, rev, eos_evm_version, gas_params)};
assert(g0 <= UINT64_MAX); // true due to the precondition (transaction must be valid)

const CallResult vm_res{evm_.execute(txn, txn.gas_limit - static_cast<uint64_t>(g0))};

const uint64_t gas_used{txn.gas_limit - refund_gas(txn, vm_res.gas_left, vm_res.gas_refund)};
uint64_t gas_used{0};
if(eos_evm_version < 3) {
gas_used = txn.gas_limit - refund_gas(txn, vm_res.gas_left, vm_res.gas_refund);

// award the fee recipient
const intx::uint256 price{evm_.config().protocol_rule_set == protocol::RuleSetType::kTrust ? effective_gas_price : txn.priority_fee_per_gas(base_fee_per_gas)};
state_.add_to_balance(evm_.beneficiary, price * gas_used);
// award the fee recipient
const intx::uint256 price{evm_.config().protocol_rule_set == protocol::RuleSetType::kTrust ? effective_gas_price : txn.priority_fee_per_gas(base_fee_per_gas)};
state_.add_to_balance(evm_.beneficiary, price * gas_used);
} else {
uint64_t storage_gas_consumed{vm_res.storage_gas_consumed};
const bool contract_creation{!txn.to};
if(contract_creation) {
storage_gas_consumed += gas_params.G_txcreate; //correct storage gas consumed to account for initial G_txcreate storage gas
yarkinwho marked this conversation as resolved.
Show resolved Hide resolved
}

evmone::gas_state_t vm_res_gas_state(eos_evm_version, static_cast<int64_t>(vm_res.gas_refund),
static_cast<int64_t>(storage_gas_consumed), static_cast<int64_t>(vm_res.storage_gas_refund), static_cast<int64_t>(vm_res.speculative_cpu_gas_consumed));
gas_used = txn.gas_limit - vm_res.gas_left;
gas_used -= static_cast<uint64_t>(vm_res_gas_state.collapse());
assert(vm_res_gas_state.cpu_gas_refund() == 0);
const auto total_storage_gas_consumed = vm_res_gas_state.storage_gas_consumed();
const auto total_cpu_gas_consumed = gas_used - static_cast<uint64_t>(total_storage_gas_consumed);

// award the fee recipient
const intx::uint256 price{evm_.config().protocol_rule_set == protocol::RuleSetType::kTrust ? effective_gas_price : txn.priority_fee_per_gas(base_fee_per_gas)};
state_.add_to_balance(evm_.beneficiary, price * total_cpu_gas_consumed);
}

state_.destruct_suicides();
if (rev >= EVMC_SPURIOUS_DRAGON) {
Expand All @@ -103,8 +127,9 @@ uint64_t ExecutionProcessor::available_gas() const noexcept {

uint64_t ExecutionProcessor::refund_gas(const Transaction& txn, uint64_t gas_left, uint64_t gas_refund) noexcept {
const evmc_revision rev{evm_.revision()};

if( evm_.get_eos_evm_version() < 2 ) {
const auto version = evm_.get_eos_evm_version();
assert(version < 3);
if( version < 2 ) {
const uint64_t max_refund_quotient{rev >= EVMC_LONDON ? protocol::kMaxRefundQuotientLondon
: protocol::kMaxRefundQuotientFrontier};
const uint64_t max_refund{(txn.gas_limit - gas_left) / max_refund_quotient};
Expand All @@ -117,7 +142,6 @@ uint64_t ExecutionProcessor::refund_gas(const Transaction& txn, uint64_t gas_lef
}
}


const intx::uint256 base_fee_per_gas{evm_.block().header.base_fee_per_gas.value_or(0)};
const intx::uint256 effective_gas_price{txn.effective_gas_price(base_fee_per_gas)};
state_.add_to_balance(*txn.from, gas_left * effective_gas_price);
Expand Down
Loading