Skip to content

Commit

Permalink
Merge pull request #783 from eosnetworkfoundation/elmato/fix-gas-pric…
Browse files Browse the repository at this point in the history
…e-in-call

Fix gas price used in evm_contract::call_
  • Loading branch information
elmato authored Feb 6, 2025
2 parents ff85155 + 4f3db22 commit 4b3ea85
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
2 changes: 2 additions & 0 deletions include/evm_runtime/evm_contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ class [[eosio::contract]] evm_contract : public contract

void process_tx(const runtime_config& rc, eosio::name miner, const transaction& tx, std::optional<uint64_t> min_inclusion_price);
void dispatch_tx(const runtime_config& rc, const transaction& tx);

uint64_t get_gas_price(uint64_t version);
};

} // namespace evm_runtime
Expand Down
43 changes: 24 additions & 19 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,8 @@ void evm_contract::exec(const exec_input& input, const std::optional<exec_callba
auto evm_version = _config->get_evm_version();

std::optional<uint64_t> base_fee_per_gas;
auto gas_prices = _config->get_gas_prices();
if (evm_version >= 1) {
if( evm_version >= 3) {
base_fee_per_gas = gas_prices.get_base_price();
} else {
base_fee_per_gas = _config->get_gas_price();
}
base_fee_per_gas = get_gas_price(evm_version);
}

eosevm::prepare_block_header(block.header, bm, get_self().value,
Expand Down Expand Up @@ -481,13 +476,8 @@ void evm_contract::process_tx(const runtime_config& rc, eosio::name miner, const
Block block;

std::optional<uint64_t> base_fee_per_gas;
auto gas_prices = _config->get_gas_prices();
if (current_version >= 1) {
if( current_version >= 3) {
base_fee_per_gas = gas_prices.get_base_price();
} else {
base_fee_per_gas = _config->get_gas_price();
}
base_fee_per_gas = get_gas_price(current_version);
}

eosevm::prepare_block_header(block.header, bm, get_self().value,
Expand All @@ -512,9 +502,10 @@ void evm_contract::process_tx(const runtime_config& rc, eosio::name miner, const
eosio::check(inclusion_price >= (min_inclusion_price.has_value() ? *min_inclusion_price : 0), "inclusion price must >= min_inclusion_price");
} else { // old behavior
check(tx.max_priority_fee_per_gas == tx.max_fee_per_gas, "max_priority_fee_per_gas must be equal to max_fee_per_gas");
check(tx.max_fee_per_gas >= _config->get_gas_price(), "gas price is too low");
check(tx.max_fee_per_gas >= get_gas_price(current_version), "gas price is too low");
}

auto gas_prices = _config->get_gas_prices();
auto gp = silkworm::gas_prices_t{gas_prices.overhead_price, gas_prices.storage_price};
silkworm::ExecutionProcessor ep{block, engine, state, *found_chain_config->second, gp};

Expand Down Expand Up @@ -659,7 +650,8 @@ void evm_contract::handle_account_transfer(const eosio::asset& quantity, const s
}

void evm_contract::handle_evm_transfer(eosio::asset quantity, const std::string& memo) {
if(_config->get_evm_version() >= 1) _config->process_price_queue();
auto current_version = _config->get_evm_version();
if(current_version >= 1) _config->process_price_queue();
//move all incoming quantity in to the contract's balance. the evm bridge trx will "pull" from this balance
balances balance_table(get_self(), get_self().value);
balance_table.modify(balance_table.get(get_self().value), eosio::same_payer, [&](balance& b){
Expand Down Expand Up @@ -690,11 +682,13 @@ void evm_contract::handle_evm_transfer(eosio::asset quantity, const std::string&
return gas_limit;
};

auto txn_price = get_gas_price(current_version);

Transaction txn;
txn.type = TransactionType::kLegacy;
txn.nonce = get_and_increment_nonce(get_self());
txn.max_priority_fee_per_gas = _config->get_gas_price();
txn.max_fee_per_gas = _config->get_gas_price();
txn.max_priority_fee_per_gas = txn_price;
txn.max_fee_per_gas = txn_price;
txn.to = to_evmc_address(*address_bytes);
txn.gas_limit = calculate_gas_limit(*txn.to);
txn.value = value;
Expand Down Expand Up @@ -752,13 +746,16 @@ bool evm_contract::gc(uint32_t max) {
}

void evm_contract::call_(const runtime_config& rc, intx::uint256 s, const bytes& to, intx::uint256 value, const bytes& data, uint64_t gas_limit, uint64_t nonce) {
if(_config->get_evm_version() >= 1) _config->process_price_queue();
auto current_version = _config->get_evm_version();
if(current_version >= 1) _config->process_price_queue();

auto txn_price = get_gas_price(current_version);

Transaction txn;
txn.type = TransactionType::kLegacy;
txn.nonce = nonce;
txn.max_priority_fee_per_gas = _config->get_gas_price();
txn.max_fee_per_gas = _config->get_gas_price();
txn.max_priority_fee_per_gas = txn_price;
txn.max_fee_per_gas = txn_price;
txn.gas_limit = gas_limit;
txn.value = value;
txn.data = Bytes{(const uint8_t*)data.data(), data.size()};
Expand Down Expand Up @@ -930,4 +927,12 @@ void evm_contract::setgaslimit(uint64_t ingress_gas_limit) {
_config->set_ingress_gas_limit(ingress_gas_limit);
}

uint64_t evm_contract::get_gas_price(uint64_t evm_version) {
if( evm_version >= 3) {
auto gas_prices = _config->get_gas_prices();
return gas_prices.get_base_price();
}
return _config->get_gas_price();
}

} //evm_runtime
7 changes: 7 additions & 0 deletions tests/gas_param_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ BOOST_FIXTURE_TEST_CASE(gas_param_traces, gas_param_evm_tester) try {

init();

// We need to have some gas prices active before switching to v3
silkworm::gas_prices_t gas_prices1{
.overhead_price = 80*silkworm::kGiga,
.storage_price = 70*silkworm::kGiga
};
setgasprices({.overhead_price=gas_prices1.overhead_price, .storage_price=gas_prices1.storage_price});

setversion(3, evm_account_name);
produce_blocks(2);

Expand Down
12 changes: 8 additions & 4 deletions tests/gas_prices_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ BOOST_FIXTURE_TEST_CASE(gas_param_scale, gas_prices_evm_tester) try {
/////////////////////////////////////
/// change EOS EVM VERSION => 3 ///
/////////////////////////////////////

// We need to have some gas prices active before switching to v3
silkworm::gas_prices_t gas_prices1{
.overhead_price = 80*kGiga,
.storage_price = 70*kGiga
};
setgasprices({.overhead_price=gas_prices1.overhead_price, .storage_price=gas_prices1.storage_price});

setversion(3, evm_account_name);
fund_evm_faucet();
produce_block();
Expand Down Expand Up @@ -223,10 +231,6 @@ BOOST_FIXTURE_TEST_CASE(gas_param_scale, gas_prices_evm_tester) try {
validate_final_fee(res, 31250, std::get<4>(res).G_newaccount, gas_prices);
};

silkworm::gas_prices_t gas_prices1{
.overhead_price = 80*kGiga,
.storage_price = 70*kGiga
};
run_gasparams_scale_test(gas_prices1);

silkworm::gas_prices_t gas_prices2{
Expand Down

0 comments on commit 4b3ea85

Please sign in to comment.