From 0eec15efdace6d138bde58c64cd32aa4cfc98e57 Mon Sep 17 00:00:00 2001 From: Matias Romeo Date: Thu, 14 Mar 2024 17:55:49 -0300 Subject: [PATCH] Revert read_consensus_parameters to old behavior --- silkworm/core/execution/evm_test.cpp | 6 ++- silkworm/node/db/access_layer.cpp | 7 +-- silkworm/node/db/access_layer_test.cpp | 51 +------------------ silkworm/silkrpc/commands/eth_api.cpp | 6 +-- silkworm/silkrpc/core/estimate_gas_oracle.cpp | 2 +- silkworm/silkrpc/core/gas_parameters.cpp | 8 +-- silkworm/silkrpc/core/gas_parameters.hpp | 2 +- silkworm/silkrpc/core/rawdb/chain.cpp | 6 +-- 8 files changed, 19 insertions(+), 69 deletions(-) diff --git a/silkworm/core/execution/evm_test.cpp b/silkworm/core/execution/evm_test.cpp index f9708667..ec80ca2e 100644 --- a/silkworm/core/execution/evm_test.cpp +++ b/silkworm/core/execution/evm_test.cpp @@ -757,7 +757,8 @@ TEST_CASE("EOS EVM G_txnewaccount") { block.header.nonce = eosevm::version_to_nonce(1); evmc::address sender{0x0a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address}; - evmc::address receiver{0x1a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address}; + evmc::address receiver1{0x1a6bb546b9208cfab9e8fa2b9b2c042b18df7030_address}; + evmc::address receiver2{0x1000000000000000000000000000000000000001_address}; evmone::gas_parameters gas_params; gas_params.G_txnewaccount = 0; @@ -769,7 +770,7 @@ TEST_CASE("EOS EVM G_txnewaccount") { Transaction txn{}; txn.from = sender; - txn.to = receiver; + txn.to = receiver1; txn.value = intx::uint256{1}; CallResult res = evm.execute(txn, 0); @@ -777,6 +778,7 @@ TEST_CASE("EOS EVM G_txnewaccount") { CHECK(res.gas_left == 0); CHECK(res.gas_refund == 0); + txn.to = receiver2; gas_params.G_txnewaccount = 1; evm.update_gas_params(gas_params); diff --git a/silkworm/node/db/access_layer.cpp b/silkworm/node/db/access_layer.cpp index 5d23fa94..937acb63 100644 --- a/silkworm/node/db/access_layer.cpp +++ b/silkworm/node/db/access_layer.cpp @@ -1222,13 +1222,10 @@ void write_runtime_states_u64(RWTxn& txn, uint64_t num, RuntimeState runtime_sta std::optional read_consensus_parameters(ROTxn& txn, BlockNum index) { auto cursor = txn.ro_cursor(table::kConsensusParameters); auto key{db::block_key(index)}; - auto data{cursor->lower_bound(to_slice(key), /*throw_notfound=*/false)}; + auto data{cursor->find(to_slice(key), /*throw_notfound=*/false)}; if (!data) { - data = cursor->to_last(/*throw_notfound=*/false); - } else if(endian::load_big_u64(static_cast(data.key.data())) > index) { - data = cursor->to_previous(/*throw_notfound=*/false); + return std::nullopt; } - if (!data) return std::nullopt; const auto encoded = from_slice(data.value); return eosevm::ConsensusParameters::decode(encoded); } diff --git a/silkworm/node/db/access_layer_test.cpp b/silkworm/node/db/access_layer_test.cpp index c8ec833d..c45675b2 100644 --- a/silkworm/node/db/access_layer_test.cpp +++ b/silkworm/node/db/access_layer_test.cpp @@ -948,7 +948,7 @@ TEST_CASE("ConsensusParameters") { update_consensus_parameters(txn, 0, value2 ); CHECK(read_consensus_parameters(txn, 0) == value2); - CHECK(read_consensus_parameters(txn, 1) == value2); + CHECK(read_consensus_parameters(txn, 1) == std::nullopt); update_consensus_parameters(txn, 1, value2 ); CHECK(read_consensus_parameters(txn, 1) == value2); @@ -957,53 +957,4 @@ TEST_CASE("ConsensusParameters") { CHECK(read_consensus_parameters(txn, 1) == value1); } -TEST_CASE("ConsensusParameters 2") { - test::Context context; - auto& txn{context.rw_txn()}; - - constexpr eosevm::ConsensusParameters value10{ - .min_gas_price = 10, - .gas_fee_parameters = eosevm::GasFeeParameters{ - .gas_txnewaccount = 10, - .gas_newaccount = 10, - .gas_txcreate = 10, - .gas_codedeposit = 10, - .gas_sset = 10 - } - }; - - constexpr eosevm::ConsensusParameters value20{ - .min_gas_price = 20, - .gas_fee_parameters = eosevm::GasFeeParameters{ - .gas_txnewaccount = 20, - .gas_newaccount = 20, - .gas_txcreate = 20, - .gas_codedeposit = 20, - .gas_sset = 20, - }, - }; - - CHECK(read_consensus_parameters(txn, 5) == std::nullopt); - CHECK(read_consensus_parameters(txn, 10) == std::nullopt); - CHECK(read_consensus_parameters(txn, 15) == std::nullopt); - CHECK(read_consensus_parameters(txn, 20) == std::nullopt); - CHECK(read_consensus_parameters(txn, 25) == std::nullopt); - - update_consensus_parameters(txn, 10, value10 ); - CHECK(read_consensus_parameters(txn, 5) == std::nullopt); - CHECK(read_consensus_parameters(txn, 10) == value10); - CHECK(read_consensus_parameters(txn, 15) == value10); - CHECK(read_consensus_parameters(txn, 20) == value10); - CHECK(read_consensus_parameters(txn, 25) == value10); - - update_consensus_parameters(txn, 20, value20 ); - CHECK(read_consensus_parameters(txn, 5) == std::nullopt); - CHECK(read_consensus_parameters(txn, 10) == value10); - CHECK(read_consensus_parameters(txn, 15) == value10); - CHECK(read_consensus_parameters(txn, 20) == value20); - CHECK(read_consensus_parameters(txn, 25) == value20); -} - - - } // namespace silkworm::db diff --git a/silkworm/silkrpc/commands/eth_api.cpp b/silkworm/silkrpc/commands/eth_api.cpp index 93df8271..eb65cc21 100644 --- a/silkworm/silkrpc/commands/eth_api.cpp +++ b/silkworm/silkrpc/commands/eth_api.cpp @@ -1139,7 +1139,7 @@ awaitable EthereumRpcApi::handle_eth_call(const nlohmann::json& request, s silkworm::Transaction txn{call.to_transaction()}; if(!txn.from.has_value()) txn.from = evmc::address{0}; - const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database, chain_config_ptr, block_with_hash->block.header); + const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database, chain_config_ptr, block_with_hash->block); const core::rawdb::DatabaseReader& db_reader = is_latest_block ? static_cast(cached_database) : static_cast(tx_database); @@ -1288,7 +1288,7 @@ awaitable EthereumRpcApi::handle_eth_create_access_list(const nlohmann::js const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); const auto chain_id = co_await core::rawdb::read_chain_id(tx_database); const auto chain_config_ptr = lookup_chain_config(chain_id); - const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database, chain_config_ptr, block_with_hash->block.header); + const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database, chain_config_ptr, block_with_hash->block); const bool is_latest_block = co_await core::get_latest_executed_block_number(tx_database) == block_with_hash->block.header.number; const core::rawdb::DatabaseReader& db_reader = @@ -1394,7 +1394,7 @@ awaitable EthereumRpcApi::handle_eth_call_bundle(const nlohmann::json& req const auto block_with_hash = co_await core::read_block_by_number_or_hash(*block_cache_, tx_database, block_number_or_hash); const auto chain_id = co_await core::rawdb::read_chain_id(tx_database); const auto chain_config_ptr = lookup_chain_config(chain_id); - const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database, chain_config_ptr, block_with_hash->block.header); + const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database, chain_config_ptr, block_with_hash->block); const bool is_latest_block = co_await core::get_latest_executed_block_number(tx_database) == block_with_hash->block.header.number; const core::rawdb::DatabaseReader& db_reader = diff --git a/silkworm/silkrpc/core/estimate_gas_oracle.cpp b/silkworm/silkrpc/core/estimate_gas_oracle.cpp index 18f1d15c..7e549117 100644 --- a/silkworm/silkrpc/core/estimate_gas_oracle.cpp +++ b/silkworm/silkrpc/core/estimate_gas_oracle.cpp @@ -78,7 +78,7 @@ boost::asio::awaitable EstimateGasOracle::estimate_gas(const Call SILK_DEBUG << "hi: " << hi << ", lo: " << lo << ", cap: " << cap; - const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database_, &config_, block.header); + const auto [eos_evm_version, gas_params] = co_await load_gas_parameters(tx_database_, &config_, block); auto this_executor = co_await boost::asio::this_coro::executor; auto exec_result = co_await boost::asio::async_compose( diff --git a/silkworm/silkrpc/core/gas_parameters.cpp b/silkworm/silkrpc/core/gas_parameters.cpp index df4e926a..53da2796 100644 --- a/silkworm/silkrpc/core/gas_parameters.cpp +++ b/silkworm/silkrpc/core/gas_parameters.cpp @@ -3,12 +3,12 @@ namespace silkworm { -awaitable> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::BlockHeader& header) { +awaitable> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::Block& block) { - auto eos_evm_version = chain_config_ptr->eos_evm_version(header); + auto eos_evm_version = chain_config_ptr->eos_evm_version(block.header); evmone::gas_parameters gas_params; - if(eos_evm_version > 0) { - auto consensus_params = co_await silkworm::rpc::core::rawdb::read_consensus_parameters(tx_database, header.number); + if(eos_evm_version > 0 && block.consensus_parameter_index.has_value()) { + auto consensus_params = co_await silkworm::rpc::core::rawdb::read_consensus_parameters(tx_database, block.consensus_parameter_index.value()); if(consensus_params.has_value() && consensus_params->gas_fee_parameters.has_value()) { gas_params=evmone::gas_parameters( consensus_params->gas_fee_parameters->gas_txnewaccount, diff --git a/silkworm/silkrpc/core/gas_parameters.hpp b/silkworm/silkrpc/core/gas_parameters.hpp index b2c08854..a6f7fbba 100644 --- a/silkworm/silkrpc/core/gas_parameters.hpp +++ b/silkworm/silkrpc/core/gas_parameters.hpp @@ -13,6 +13,6 @@ using boost::asio::awaitable; namespace silkworm { - awaitable> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::BlockHeader& header); + awaitable> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::Block& header); } diff --git a/silkworm/silkrpc/core/rawdb/chain.cpp b/silkworm/silkrpc/core/rawdb/chain.cpp index ce33e169..214155c7 100644 --- a/silkworm/silkrpc/core/rawdb/chain.cpp +++ b/silkworm/silkrpc/core/rawdb/chain.cpp @@ -435,9 +435,9 @@ boost::asio::awaitable read_cumulative_gas_used(const core::rawdb co_return cumulative_gas_index; } -boost::asio::awaitable> read_consensus_parameters(const core::rawdb::DatabaseReader& reader, uint64_t block_number) { - const auto block_key = silkworm::db::block_key(block_number); - const auto value = co_await reader.get_exact_or_previous(db::table::kConsensusParametersName, block_key); +boost::asio::awaitable> read_consensus_parameters(const core::rawdb::DatabaseReader& reader, uint64_t index) { + const auto block_key = silkworm::db::block_key(index); + const auto value = co_await reader.get_one(db::table::kConsensusParametersName, block_key); if (value.empty()) { co_return std::nullopt; }