Skip to content
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
11 changes: 6 additions & 5 deletions eosevm/block_extra_data.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include <silkworm/core/rlp/encode.hpp>
#include <silkworm/core/rlp/decode.hpp>

#include "block_extra_data.hpp"
#include <eosevm/block_extra_data.hpp>

using namespace silkworm;

namespace eosevm {
bool operator==(const eosevm::block_extra_data& a, const eosevm::block_extra_data& b) {
return a.consensus_parameter_index == b.consensus_parameter_index && a.gas_prices_index == b.gas_prices_index;
return a.consensus_parameter_index == b.consensus_parameter_index && a.gasprices == b.gasprices;
}
}

Expand Down Expand Up @@ -40,15 +40,16 @@ namespace silkworm { namespace rlp {
silkworm::Bytes encode(const eosevm::block_extra_data& out) {
silkworm::Bytes to;
encode(to, out.consensus_parameter_index);
encode(to, out.gas_prices_index);
encode(to, out.gasprices);
return to;
}

DecodingResult decode(silkworm::ByteView& from, eosevm::block_extra_data& to) noexcept{
to.consensus_parameter_index.reset();
decode(from, to.consensus_parameter_index);
to.gas_prices_index.reset();
if(from.length() > 0) {
decode(from, to.gas_prices_index);
to.gasprices.reset();
decode(from, to.gasprices);
}
return {};
}
Expand Down
4 changes: 2 additions & 2 deletions eosevm/block_extra_data.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#pragma once

#include <optional>

#include <eosevm/gas_prices.hpp>
namespace eosevm {

struct block_extra_data {
std::optional<evmc::bytes32> consensus_parameter_index;
std::optional<evmc::bytes32> gas_prices_index;
std::optional<gas_prices> gasprices;
};

} // namespace eosevm
Expand Down
42 changes: 16 additions & 26 deletions eosevm/gas_prices.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
#include "gas_prices.hpp"

#if not defined(ANTELOPE)
#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/core/common/util.hpp>
#endif

namespace eosevm {

bool operator==(const eosevm::gas_prices& a, const eosevm::gas_prices& b) {
return a.overhead_price == b.overhead_price && a.storage_price == b.storage_price;
}

} // namespace eosevm

#if not defined(ANTELOPE)
[[nodiscard]] silkworm::Bytes gas_prices::encode() const noexcept {
silkworm::Bytes ret(16, '\0');
silkworm::endian::store_big_u64(&ret[0], overhead_price);
silkworm::endian::store_big_u64(&ret[8], storage_price);
return ret;
}
namespace silkworm { namespace rlp {

std::optional<gas_prices> gas_prices::decode(silkworm::ByteView encoded) noexcept {
SILKWORM_ASSERT(encoded.length() >= 16);
gas_prices prices;
prices.overhead_price= silkworm::endian::load_big_u64(&encoded[0]);
prices.storage_price = silkworm::endian::load_big_u64(&encoded[8]);
return prices;
}
silkworm::Bytes encode(silkworm::Bytes& to, const eosevm::gas_prices& out) {
encode(to, out.overhead_price);
encode(to, out.storage_price);
return to;
}

[[nodiscard]] evmc::bytes32 gas_prices::hash() const noexcept {
auto encoded = this->encode();
evmc::bytes32 header_hash = std::bit_cast<evmc_bytes32>(silkworm::keccak256(encoded));
return header_hash;
}
#endif
DecodingResult decode(silkworm::ByteView& from, eosevm::gas_prices& to, Leftover) noexcept{
decode(from, to.overhead_price);
decode(from, to.storage_price);
return {};
}

} // namespace eosevm
} } //namespace silkworm::rlp

#endif
25 changes: 13 additions & 12 deletions eosevm/gas_prices.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
#include <algorithm>

#if not defined(ANTELOPE)
#include <silkworm/core/common/base.hpp>
#include <silkworm/core/rlp/decode.hpp>
#include <silkworm/core/common/assert.hpp>
#include <silkworm/core/common/endian.hpp>
#include <silkworm/core/common/util.hpp>
#endif

namespace eosevm {

struct gas_prices {
uint64_t overhead_price;
uint64_t storage_price;

#if not defined(ANTELOPE)
// Encode for storage in db.
[[nodiscard]] silkworm::Bytes encode() const noexcept;

// Decode from storage in db.
static std::optional<gas_prices> decode(silkworm::ByteView encoded) noexcept;
evmc::bytes32 hash() const noexcept;
#endif
uint64_t overhead_price{0};
uint64_t storage_price{0};

friend bool operator==(const gas_prices&, const gas_prices&);
};
Expand All @@ -28,3 +22,10 @@ inline uint64_t calculate_base_fee_per_gas(uint64_t overhead_price, uint64_t sto
}

} // namespace eosevm

#if not defined(ANTELOPE)
namespace silkworm { namespace rlp {
silkworm::Bytes encode(silkworm::Bytes& to, const eosevm::gas_prices& out);
DecodingResult decode(silkworm::ByteView& from, eosevm::gas_prices& to, Leftover mode) noexcept;
}}
#endif
12 changes: 6 additions & 6 deletions silkworm/core/types/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,18 @@ struct BlockBody {
return eosevm_extra_data.value().consensus_parameter_index;
}

bool has_gas_prices_index() {
return eosevm_extra_data.has_value() && eosevm_extra_data->gas_prices_index.has_value();
bool has_gas_prices() {
return eosevm_extra_data.has_value() && eosevm_extra_data->gasprices.has_value();
}

void set_gas_prices_index(const std::optional<evmc::bytes32>& index) {
void set_gas_prices(const eosevm::gas_prices& gas_prices) {
if(!eosevm_extra_data.has_value()) eosevm_extra_data=eosevm::block_extra_data{};
eosevm_extra_data->gas_prices_index = index;
eosevm_extra_data->gasprices = gas_prices;
}

std::optional<evmc::bytes32> get_gas_prices_index() const {
std::optional<eosevm::gas_prices> get_gas_prices() const {
if(!eosevm_extra_data.has_value()) return {};
return eosevm_extra_data.value().gas_prices_index;
return eosevm_extra_data.value().gasprices;
}

friend bool operator==(const BlockBody&, const BlockBody&);
Expand Down
27 changes: 0 additions & 27 deletions silkworm/node/db/access_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1272,31 +1272,4 @@ void update_consensus_parameters(RWTxn& txn, const evmc::bytes32& index, const e
cursor->upsert(to_slice(key), mdbx::slice(config.encode()));
}

std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const evmc::bytes32& index) {
auto cursor = txn.ro_cursor(table::kGasPrices);
auto key{db::block_key(index.bytes)};
auto data{cursor->find(to_slice(key), /*throw_notfound=*/false)};
if (!data) {
return std::nullopt;
}
const auto encoded = from_slice(data.value);
return eosevm::gas_prices::decode(encoded);
}

std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const Block& block) {
std::optional<eosevm::gas_prices> prices;
auto gpi = block.get_gas_prices_index();
if(gpi.has_value()) {
prices = read_gas_prices(txn, gpi.value());
}
return prices;
}

void update_gas_prices(RWTxn& txn, const evmc::bytes32& index, const eosevm::gas_prices& prices) {
auto cursor = txn.rw_cursor(table::kGasPrices);
auto key{db::block_key(index.bytes)};

cursor->upsert(to_slice(key), mdbx::slice(prices.encode()));
}

} // namespace silkworm::db
6 changes: 0 additions & 6 deletions silkworm/node/db/access_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include <silkworm/node/snapshot/repository.hpp>

#include <eosevm/consensus_parameters.hpp>
#include <eosevm/gas_prices.hpp>

namespace silkworm::db {

Expand Down Expand Up @@ -263,11 +262,6 @@ std::optional<eosevm::ConsensusParameters> read_consensus_parameters(ROTxn& txn,
//! \brief Write ConsensusParameters indexed by blocknum that it is added. Can overwrite during forks.
void update_consensus_parameters(RWTxn& txn, const evmc::bytes32&, const eosevm::ConsensusParameters& config);

std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const evmc::bytes32& index);
std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const Block& block);
void update_gas_prices(RWTxn& txn, const evmc::bytes32&, const eosevm::gas_prices& prices);


class DataModel {
public:
static void set_snapshot_repository(snapshot::SnapshotRepository* repository);
Expand Down
50 changes: 6 additions & 44 deletions silkworm/node/db/access_layer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ static BlockBody sample_block_body(bool with_block_extra_data=true) {

if(with_block_extra_data) {
body.eosevm_extra_data = {
.consensus_parameter_index = 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32
.consensus_parameter_index = 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32,
.gasprices = eosevm::gas_prices{1, 2}
};
}

Expand Down Expand Up @@ -510,6 +511,8 @@ TEST_CASE("Headers and bodies") {
CHECK(block.eosevm_extra_data.has_value() == true);
CHECK(block.eosevm_extra_data.value().consensus_parameter_index.has_value() == true);
CHECK(block.eosevm_extra_data->consensus_parameter_index == 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32);
CHECK(block.eosevm_extra_data->gasprices->overhead_price == 1);
CHECK(block.eosevm_extra_data->gasprices->storage_price == 2);

CHECK(!block.transactions[0].from);
CHECK(!block.transactions[1].from);
Expand Down Expand Up @@ -563,6 +566,8 @@ TEST_CASE("Headers and bodies") {
CHECK(block.eosevm_extra_data.has_value() == true);
CHECK(block.eosevm_extra_data.value().consensus_parameter_index.has_value() == true);
CHECK(block.eosevm_extra_data->consensus_parameter_index == 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32);
CHECK(block.eosevm_extra_data->gasprices->overhead_price == 1);
CHECK(block.eosevm_extra_data->gasprices->storage_price == 2);
});
REQUIRE(processed == 1);
REQUIRE(processed == count);
Expand Down Expand Up @@ -987,47 +992,4 @@ TEST_CASE("ConsensusParameters") {
CHECK(read_consensus_parameters(txn, value2.hash()) == value2);
}

TEST_CASE("gas_prices") {
test::Context context;
auto& txn{context.rw_txn()};

constexpr eosevm::gas_prices value1{
.overhead_price = 1,
.storage_price = 1
};

auto tmp = value1.encode();

ByteView bv{tmp};
REQUIRE_NOTHROW(eosevm::gas_prices::decode(bv));

constexpr eosevm::gas_prices value2{
.overhead_price = 2,
.storage_price = 2
};

CHECK(read_gas_prices(txn, evmc::bytes32(0)) == std::nullopt);

update_gas_prices(txn, evmc::bytes32(0), value1 );
CHECK(read_gas_prices(txn, evmc::bytes32(0)) == value1);

update_gas_prices(txn, evmc::bytes32(0), value2 );
CHECK(read_gas_prices(txn, evmc::bytes32(0)) == value2);

CHECK(read_gas_prices(txn, evmc::bytes32(1)) == std::nullopt);

update_gas_prices(txn, evmc::bytes32(1), value2 );
CHECK(read_gas_prices(txn, evmc::bytes32(1)) == value2);

update_gas_prices(txn, evmc::bytes32(1), value1 );
CHECK(read_gas_prices(txn, evmc::bytes32(1)) == value1);

update_gas_prices(txn, value1.hash(), value1 );
CHECK(read_gas_prices(txn, value1.hash()) == value1);

update_gas_prices(txn, value2.hash(), value2 );
CHECK(read_gas_prices(txn, value2.hash()) == value2);
}


} // namespace silkworm::db
6 changes: 1 addition & 5 deletions silkworm/node/db/tables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,6 @@ inline constexpr db::MapConfig kRuntimeStates{kRuntimeStatesName};
inline constexpr const char* kConsensusParametersName{"ConsensusParameters"};
inline constexpr db::MapConfig kConsensusParameters{kConsensusParametersName};

inline constexpr const char* kGasPricesName{"GasPrices"};
inline constexpr db::MapConfig kGasPrices{kGasPricesName};

inline constexpr db::MapConfig kChainDataTables[]{
kAccountChangeSet,
kAccountHistory,
Expand Down Expand Up @@ -438,8 +435,7 @@ inline constexpr db::MapConfig kChainDataTables[]{
kTxLookup,
kRuntimeStates,
kConsensusParameters,
kExtraBlockData,
kGasPrices
kExtraBlockData
};

//! \brief Ensures all defined tables are present in db with consistent flags. Should a table not exist it gets created
Expand Down
17 changes: 5 additions & 12 deletions silkworm/node/stagedsync/stages/stage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,12 @@ const evmone::gas_parameters& Stage::get_gas_params(db::ROTxn& txn, const Block&
return last_gas_params;
}

const gas_prices_t& Stage::get_gas_prices(db::ROTxn& txn, const Block& block) {
auto curr_gas_prices_index = block.get_gas_prices_index();
if(curr_gas_prices_index != last_gas_prices_index) {
auto gas_prices = silkworm::db::read_gas_prices(txn, block);
if(gas_prices.has_value()) {
const auto& v = gas_prices.value();
last_gas_prices = gas_prices_t(v.overhead_price, v.storage_price);
} else {
last_gas_prices=gas_prices_t{};
}
last_gas_prices_index = curr_gas_prices_index;
gas_prices_t Stage::get_gas_prices(const Block& block) {
auto gas_price = block.get_gas_prices();
if(!gas_price.has_value()) {
return gas_prices_t{};
}
return last_gas_prices;
return gas_prices_t{gas_price->overhead_price, gas_price->storage_price};
}

StageError::StageError(Stage::Result err)
Expand Down
4 changes: 1 addition & 3 deletions silkworm/node/stagedsync/stages/stage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,7 @@ class Stage : public Stoppable {
std::optional<evmc::bytes32> last_consensus_parameter_index{std::nullopt};
const evmone::gas_parameters& get_gas_params(db::ROTxn& txn, const Block& block);

gas_prices_t last_gas_prices;
std::optional<evmc::bytes32> last_gas_prices_index{std::nullopt};
const gas_prices_t& get_gas_prices(db::ROTxn& txn, const Block& block);
gas_prices_t get_gas_prices(const Block& block);

SyncContext* sync_context_; // Shared context across stages
const char* stage_name_; // Human friendly identifier of the stage
Expand Down
2 changes: 1 addition & 1 deletion silkworm/node/stagedsync/stages/stage_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ Stage::Result Execution::execute_batch(db::RWTxn& txn, BlockNum max_block_num, A
log_time = now + 5s;
}

auto gas_prices = get_gas_prices(txn, block);
auto gas_prices = get_gas_prices(block);
ExecutionProcessor processor(block, *rule_set_, buffer, node_settings_->chain_config.value(), gas_prices);
processor.evm().analysis_cache = &analysis_cache;
processor.evm().state_pool = &state_pool;
Expand Down
5 changes: 3 additions & 2 deletions silkworm/silkrpc/core/estimate_gas_oracle_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,8 @@ TEST_CASE("estimate conservative gas") {
};

Call call;
const silkworm::Block block{.header=kBlockHeader};
silkworm::Block block{.header=kBlockHeader};
block.set_gas_prices({.overhead_price=1, .storage_price=1});
const silkworm::ChainConfig config{.protocol_rule_set = protocol::RuleSetType::kTrust};
RemoteDatabaseTest remote_db_test;
auto tx = std::make_unique<ethdb::kv::RemoteTransaction>(*remote_db_test.stub_, remote_db_test.grpc_context_);
Expand All @@ -463,7 +464,7 @@ TEST_CASE("estimate conservative gas") {
try {
call.gas = 0;
call.max_priority_fee_per_gas = 1;
call.max_fee_per_gas = 1;
call.max_fee_per_gas = 2;
auto result = boost::asio::co_spawn(pool, estimate_gas_oracle.estimate_gas(call, block), boost::asio::use_future);
result.get();
CHECK(false);
Expand Down
Loading