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

Save overhead/storage price in block extra data #278

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 41 additions & 3 deletions src/block_conversion_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <eosevm/block_mapping.hpp>
#include <eosevm/consensus_parameters.hpp>
#include <eosevm/version.hpp>
#include <eosevm/gas_prices.hpp>

#include <fstream>

Expand Down Expand Up @@ -128,11 +129,19 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi

new_block.header.parent_hash = last_evm_block.header.hash();
new_block.header.transactions_root = silkworm::kEmptyRoot;

// Note: can be null
auto cpi = last_evm_block.get_consensus_parameter_index();
if(cpi.has_value()) {
new_block.set_consensus_parameter_index(cpi);
}

// Note: can be null
auto gpi = last_evm_block.get_gas_prices_index();
if(gpi.has_value()) {
new_block.set_gas_prices_index(gpi);
}

return new_block;
}

Expand Down Expand Up @@ -305,6 +314,35 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
auto dtx = deserialize_tx(act);
auto& rlpx_ref = std::visit([](auto&& arg) -> auto& { return arg.rlpx; }, dtx);

if(block_version >= 3) {
SILKWORM_ASSERT(std::holds_alternative<evmtx_v3>(dtx));
const auto& dtx_v3 = std::get<evmtx_v3>(dtx);
auto tx_gas_prices = eosevm::gas_prices{
.overhead_price = dtx_v3.overhead_price,
.storage_price = dtx_v3.storage_price
};
auto tx_gas_prices_index = tx_gas_prices.hash();
auto curr_block_prices_index = curr.get_gas_prices_index();

auto set_new_gas_prices = [&](){
curr.set_gas_prices_index(tx_gas_prices_index);
silkworm::db::update_gas_prices(appbase::app().get_plugin<blockchain_plugin>().get_tx(), tx_gas_prices_index, tx_gas_prices);
};

if(!curr_block_prices_index) {
set_new_gas_prices();
} else if(*curr_block_prices_index != tx_gas_prices_index) {
if(curr.transactions.empty()) {
set_new_gas_prices();
} else {
SILK_CRIT << "curr_block_prices_index != tx_gas_prices_index";
throw std::runtime_error("curr_block_prices_index != tx_gas_prices_index");
}
}

curr.header.base_fee_per_gas = eosevm::calculate_base_fee_per_gas(tx_gas_prices.overhead_price, tx_gas_prices.storage_price);
}

silkworm::ByteView bv = {(const uint8_t*)rlpx_ref.data(), rlpx_ref.size()};
silkworm::Transaction evm_tx;
if (!silkworm::rlp::decode_transaction(bv, evm_tx, silkworm::rlp::Eip2718Wrapping::kBoth, silkworm::rlp::Leftover::kProhibit)) {
Expand All @@ -326,8 +364,8 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
}
}

if(block_version >= 1) {
auto tx_base_fee = std::visit([](auto&& arg) -> auto { return arg.base_fee_per_gas; }, dtx);
if(block_version >= 1 && block_version < 3) {
auto tx_base_fee = std::get<evmtx_v1>(dtx).base_fee_per_gas;
if(!curr.header.base_fee_per_gas.has_value()) {
curr.header.base_fee_per_gas = tx_base_fee;
} else if (curr.header.base_fee_per_gas.value() != tx_base_fee) {
Expand Down Expand Up @@ -380,7 +418,7 @@ class block_conversion_plugin_impl : std::enable_shared_from_this<block_conversi
if( na.name == pushtx_n ) {
pushtx tx;
eosio::convert_from_bin(tx, na.data);
return evmtx_type{evmtx_v0{0, std::move(tx.rlpx), 0}};
return evmtx_type{evmtx_v1{0, std::move(tx.rlpx), 0}};
}
evmtx_type evmtx;
eosio::convert_from_bin(evmtx, na.data);
Expand Down
18 changes: 15 additions & 3 deletions src/block_conversion_plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ struct pushtx {
};
EOSIO_REFLECT(pushtx, miner, rlpx)

struct evmtx_v0 {
struct evmtx_base {
uint64_t eos_evm_version;
std::vector<uint8_t> rlpx;
};

struct evmtx_v1 : evmtx_base {
uint64_t base_fee_per_gas;
};
using evmtx_type = std::variant<evmtx_v0>;
EOSIO_REFLECT(evmtx_v0, eos_evm_version, rlpx, base_fee_per_gas)

struct evmtx_v3 : evmtx_base {
uint64_t overhead_price;
uint64_t storage_price;
};

using evmtx_type = std::variant<evmtx_v1, evmtx_v3>;

EOSIO_REFLECT(evmtx_base, eos_evm_version, rlpx);
EOSIO_REFLECT(evmtx_v1, base evmtx_base, base_fee_per_gas);
EOSIO_REFLECT(evmtx_v3, base evmtx_base, overhead_price, storage_price);

struct gas_parameter_type {
uint64_t gas_txnewaccount = 0;
Expand Down
Loading