Skip to content

Commit

Permalink
Revert read_consensus_parameters to old behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
elmato committed Mar 14, 2024
1 parent 8738e98 commit 0eec15e
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 69 deletions.
6 changes: 4 additions & 2 deletions silkworm/core/execution/evm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -769,14 +770,15 @@ 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);
CHECK(res.status == EVMC_SUCCESS);
CHECK(res.gas_left == 0);
CHECK(res.gas_refund == 0);

txn.to = receiver2;
gas_params.G_txnewaccount = 1;
evm.update_gas_params(gas_params);

Expand Down
7 changes: 2 additions & 5 deletions silkworm/node/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,13 +1222,10 @@ void write_runtime_states_u64(RWTxn& txn, uint64_t num, RuntimeState runtime_sta
std::optional<eosevm::ConsensusParameters> 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<const unsigned char*>(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);
}
Expand Down
51 changes: 1 addition & 50 deletions silkworm/node/db/access_layer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
6 changes: 3 additions & 3 deletions silkworm/silkrpc/commands/eth_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ awaitable<void> 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<core::rawdb::DatabaseReader&>(cached_database) : static_cast<core::rawdb::DatabaseReader&>(tx_database);
Expand Down Expand Up @@ -1288,7 +1288,7 @@ awaitable<void> 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 =
Expand Down Expand Up @@ -1394,7 +1394,7 @@ awaitable<void> 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 =
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/core/estimate_gas_oracle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ boost::asio::awaitable<intx::uint256> 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<decltype(boost::asio::use_awaitable), void(ExecutionResult)>(
Expand Down
8 changes: 4 additions & 4 deletions silkworm/silkrpc/core/gas_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace silkworm {

awaitable<std::tuple<uint64_t, evmone::gas_parameters>> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::BlockHeader& header) {
awaitable<std::tuple<uint64_t, evmone::gas_parameters>> 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,
Expand Down
2 changes: 1 addition & 1 deletion silkworm/silkrpc/core/gas_parameters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ using boost::asio::awaitable;

namespace silkworm {

awaitable<std::tuple<uint64_t, evmone::gas_parameters>> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::BlockHeader& header);
awaitable<std::tuple<uint64_t, evmone::gas_parameters>> load_gas_parameters(TransactionDatabase& tx_database, const silkworm::ChainConfig* chain_config_ptr, const silkworm::Block& header);

}
6 changes: 3 additions & 3 deletions silkworm/silkrpc/core/rawdb/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,9 @@ boost::asio::awaitable<intx::uint256> read_cumulative_gas_used(const core::rawdb
co_return cumulative_gas_index;
}

boost::asio::awaitable<std::optional<eosevm::ConsensusParameters>> 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<std::optional<eosevm::ConsensusParameters>> 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;
}
Expand Down

0 comments on commit 0eec15e

Please sign in to comment.