Skip to content

Commit 03de581

Browse files
authored
Merge pull request #254 from eosnetworkfoundation/elmato/prices-extra-data
Store gas_prices directly into extra_data table
2 parents 80c6636 + 4cae2cb commit 03de581

File tree

15 files changed

+64
-167
lines changed

15 files changed

+64
-167
lines changed

eosevm/block_extra_data.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
#include <silkworm/core/rlp/encode.hpp>
22
#include <silkworm/core/rlp/decode.hpp>
33

4-
#include "block_extra_data.hpp"
4+
#include <eosevm/block_extra_data.hpp>
55

66
using namespace silkworm;
77

88
namespace eosevm {
99
bool operator==(const eosevm::block_extra_data& a, const eosevm::block_extra_data& b) {
10-
return a.consensus_parameter_index == b.consensus_parameter_index && a.gas_prices_index == b.gas_prices_index;
10+
return a.consensus_parameter_index == b.consensus_parameter_index && a.gasprices == b.gasprices;
1111
}
1212
}
1313

@@ -40,15 +40,16 @@ namespace silkworm { namespace rlp {
4040
silkworm::Bytes encode(const eosevm::block_extra_data& out) {
4141
silkworm::Bytes to;
4242
encode(to, out.consensus_parameter_index);
43-
encode(to, out.gas_prices_index);
43+
encode(to, out.gasprices);
4444
return to;
4545
}
4646

4747
DecodingResult decode(silkworm::ByteView& from, eosevm::block_extra_data& to) noexcept{
48+
to.consensus_parameter_index.reset();
4849
decode(from, to.consensus_parameter_index);
49-
to.gas_prices_index.reset();
5050
if(from.length() > 0) {
51-
decode(from, to.gas_prices_index);
51+
to.gasprices.reset();
52+
decode(from, to.gasprices);
5253
}
5354
return {};
5455
}

eosevm/block_extra_data.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#pragma once
22

33
#include <optional>
4-
4+
#include <eosevm/gas_prices.hpp>
55
namespace eosevm {
66

77
struct block_extra_data {
88
std::optional<evmc::bytes32> consensus_parameter_index;
9-
std::optional<evmc::bytes32> gas_prices_index;
9+
std::optional<gas_prices> gasprices;
1010
};
1111

1212
} // namespace eosevm

eosevm/gas_prices.cpp

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
11
#include "gas_prices.hpp"
22

3-
#if not defined(ANTELOPE)
4-
#include <silkworm/core/common/assert.hpp>
5-
#include <silkworm/core/common/endian.hpp>
6-
#include <silkworm/core/common/util.hpp>
7-
#endif
8-
93
namespace eosevm {
104

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

9+
} // namespace eosevm
10+
1511
#if not defined(ANTELOPE)
16-
[[nodiscard]] silkworm::Bytes gas_prices::encode() const noexcept {
17-
silkworm::Bytes ret(16, '\0');
18-
silkworm::endian::store_big_u64(&ret[0], overhead_price);
19-
silkworm::endian::store_big_u64(&ret[8], storage_price);
20-
return ret;
21-
}
12+
namespace silkworm { namespace rlp {
2213

23-
std::optional<gas_prices> gas_prices::decode(silkworm::ByteView encoded) noexcept {
24-
SILKWORM_ASSERT(encoded.length() >= 16);
25-
gas_prices prices;
26-
prices.overhead_price= silkworm::endian::load_big_u64(&encoded[0]);
27-
prices.storage_price = silkworm::endian::load_big_u64(&encoded[8]);
28-
return prices;
29-
}
14+
silkworm::Bytes encode(silkworm::Bytes& to, const eosevm::gas_prices& out) {
15+
encode(to, out.overhead_price);
16+
encode(to, out.storage_price);
17+
return to;
18+
}
3019

31-
[[nodiscard]] evmc::bytes32 gas_prices::hash() const noexcept {
32-
auto encoded = this->encode();
33-
evmc::bytes32 header_hash = std::bit_cast<evmc_bytes32>(silkworm::keccak256(encoded));
34-
return header_hash;
35-
}
36-
#endif
20+
DecodingResult decode(silkworm::ByteView& from, eosevm::gas_prices& to, Leftover) noexcept{
21+
decode(from, to.overhead_price);
22+
decode(from, to.storage_price);
23+
return {};
24+
}
3725

38-
} // namespace eosevm
26+
} } //namespace silkworm::rlp
27+
28+
#endif

eosevm/gas_prices.hpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,17 @@
22
#include <algorithm>
33

44
#if not defined(ANTELOPE)
5-
#include <silkworm/core/common/base.hpp>
5+
#include <silkworm/core/rlp/decode.hpp>
6+
#include <silkworm/core/common/assert.hpp>
7+
#include <silkworm/core/common/endian.hpp>
8+
#include <silkworm/core/common/util.hpp>
69
#endif
710

811
namespace eosevm {
912

1013
struct gas_prices {
11-
uint64_t overhead_price;
12-
uint64_t storage_price;
13-
14-
#if not defined(ANTELOPE)
15-
// Encode for storage in db.
16-
[[nodiscard]] silkworm::Bytes encode() const noexcept;
17-
18-
// Decode from storage in db.
19-
static std::optional<gas_prices> decode(silkworm::ByteView encoded) noexcept;
20-
evmc::bytes32 hash() const noexcept;
21-
#endif
14+
uint64_t overhead_price{0};
15+
uint64_t storage_price{0};
2216

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

3024
} // namespace eosevm
25+
26+
#if not defined(ANTELOPE)
27+
namespace silkworm { namespace rlp {
28+
silkworm::Bytes encode(silkworm::Bytes& to, const eosevm::gas_prices& out);
29+
DecodingResult decode(silkworm::ByteView& from, eosevm::gas_prices& to, Leftover mode) noexcept;
30+
}}
31+
#endif

silkworm/core/types/block.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ struct BlockBody {
113113
return eosevm_extra_data.value().consensus_parameter_index;
114114
}
115115

116-
bool has_gas_prices_index() {
117-
return eosevm_extra_data.has_value() && eosevm_extra_data->gas_prices_index.has_value();
116+
bool has_gas_prices() {
117+
return eosevm_extra_data.has_value() && eosevm_extra_data->gasprices.has_value();
118118
}
119119

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

125-
std::optional<evmc::bytes32> get_gas_prices_index() const {
125+
std::optional<eosevm::gas_prices> get_gas_prices() const {
126126
if(!eosevm_extra_data.has_value()) return {};
127-
return eosevm_extra_data.value().gas_prices_index;
127+
return eosevm_extra_data.value().gasprices;
128128
}
129129

130130
friend bool operator==(const BlockBody&, const BlockBody&);

silkworm/node/db/access_layer.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,31 +1272,4 @@ void update_consensus_parameters(RWTxn& txn, const evmc::bytes32& index, const e
12721272
cursor->upsert(to_slice(key), mdbx::slice(config.encode()));
12731273
}
12741274

1275-
std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const evmc::bytes32& index) {
1276-
auto cursor = txn.ro_cursor(table::kGasPrices);
1277-
auto key{db::block_key(index.bytes)};
1278-
auto data{cursor->find(to_slice(key), /*throw_notfound=*/false)};
1279-
if (!data) {
1280-
return std::nullopt;
1281-
}
1282-
const auto encoded = from_slice(data.value);
1283-
return eosevm::gas_prices::decode(encoded);
1284-
}
1285-
1286-
std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const Block& block) {
1287-
std::optional<eosevm::gas_prices> prices;
1288-
auto gpi = block.get_gas_prices_index();
1289-
if(gpi.has_value()) {
1290-
prices = read_gas_prices(txn, gpi.value());
1291-
}
1292-
return prices;
1293-
}
1294-
1295-
void update_gas_prices(RWTxn& txn, const evmc::bytes32& index, const eosevm::gas_prices& prices) {
1296-
auto cursor = txn.rw_cursor(table::kGasPrices);
1297-
auto key{db::block_key(index.bytes)};
1298-
1299-
cursor->upsert(to_slice(key), mdbx::slice(prices.encode()));
1300-
}
1301-
13021275
} // namespace silkworm::db

silkworm/node/db/access_layer.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <silkworm/node/snapshot/repository.hpp>
3434

3535
#include <eosevm/consensus_parameters.hpp>
36-
#include <eosevm/gas_prices.hpp>
3736

3837
namespace silkworm::db {
3938

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

266-
std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const evmc::bytes32& index);
267-
std::optional<eosevm::gas_prices> read_gas_prices(ROTxn& txn, const Block& block);
268-
void update_gas_prices(RWTxn& txn, const evmc::bytes32&, const eosevm::gas_prices& prices);
269-
270-
271265
class DataModel {
272266
public:
273267
static void set_snapshot_repository(snapshot::SnapshotRepository* repository);

silkworm/node/db/access_layer_test.cpp

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ static BlockBody sample_block_body(bool with_block_extra_data=true) {
8383

8484
if(with_block_extra_data) {
8585
body.eosevm_extra_data = {
86-
.consensus_parameter_index = 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32
86+
.consensus_parameter_index = 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32,
87+
.gasprices = eosevm::gas_prices{1, 2}
8788
};
8889
}
8990

@@ -510,6 +511,8 @@ TEST_CASE("Headers and bodies") {
510511
CHECK(block.eosevm_extra_data.has_value() == true);
511512
CHECK(block.eosevm_extra_data.value().consensus_parameter_index.has_value() == true);
512513
CHECK(block.eosevm_extra_data->consensus_parameter_index == 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32);
514+
CHECK(block.eosevm_extra_data->gasprices->overhead_price == 1);
515+
CHECK(block.eosevm_extra_data->gasprices->storage_price == 2);
513516

514517
CHECK(!block.transactions[0].from);
515518
CHECK(!block.transactions[1].from);
@@ -563,6 +566,8 @@ TEST_CASE("Headers and bodies") {
563566
CHECK(block.eosevm_extra_data.has_value() == true);
564567
CHECK(block.eosevm_extra_data.value().consensus_parameter_index.has_value() == true);
565568
CHECK(block.eosevm_extra_data->consensus_parameter_index == 0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3_bytes32);
569+
CHECK(block.eosevm_extra_data->gasprices->overhead_price == 1);
570+
CHECK(block.eosevm_extra_data->gasprices->storage_price == 2);
566571
});
567572
REQUIRE(processed == 1);
568573
REQUIRE(processed == count);
@@ -987,47 +992,4 @@ TEST_CASE("ConsensusParameters") {
987992
CHECK(read_consensus_parameters(txn, value2.hash()) == value2);
988993
}
989994

990-
TEST_CASE("gas_prices") {
991-
test::Context context;
992-
auto& txn{context.rw_txn()};
993-
994-
constexpr eosevm::gas_prices value1{
995-
.overhead_price = 1,
996-
.storage_price = 1
997-
};
998-
999-
auto tmp = value1.encode();
1000-
1001-
ByteView bv{tmp};
1002-
REQUIRE_NOTHROW(eosevm::gas_prices::decode(bv));
1003-
1004-
constexpr eosevm::gas_prices value2{
1005-
.overhead_price = 2,
1006-
.storage_price = 2
1007-
};
1008-
1009-
CHECK(read_gas_prices(txn, evmc::bytes32(0)) == std::nullopt);
1010-
1011-
update_gas_prices(txn, evmc::bytes32(0), value1 );
1012-
CHECK(read_gas_prices(txn, evmc::bytes32(0)) == value1);
1013-
1014-
update_gas_prices(txn, evmc::bytes32(0), value2 );
1015-
CHECK(read_gas_prices(txn, evmc::bytes32(0)) == value2);
1016-
1017-
CHECK(read_gas_prices(txn, evmc::bytes32(1)) == std::nullopt);
1018-
1019-
update_gas_prices(txn, evmc::bytes32(1), value2 );
1020-
CHECK(read_gas_prices(txn, evmc::bytes32(1)) == value2);
1021-
1022-
update_gas_prices(txn, evmc::bytes32(1), value1 );
1023-
CHECK(read_gas_prices(txn, evmc::bytes32(1)) == value1);
1024-
1025-
update_gas_prices(txn, value1.hash(), value1 );
1026-
CHECK(read_gas_prices(txn, value1.hash()) == value1);
1027-
1028-
update_gas_prices(txn, value2.hash(), value2 );
1029-
CHECK(read_gas_prices(txn, value2.hash()) == value2);
1030-
}
1031-
1032-
1033995
} // namespace silkworm::db

silkworm/node/db/tables.hpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,6 @@ inline constexpr db::MapConfig kRuntimeStates{kRuntimeStatesName};
389389
inline constexpr const char* kConsensusParametersName{"ConsensusParameters"};
390390
inline constexpr db::MapConfig kConsensusParameters{kConsensusParametersName};
391391

392-
inline constexpr const char* kGasPricesName{"GasPrices"};
393-
inline constexpr db::MapConfig kGasPrices{kGasPricesName};
394-
395392
inline constexpr db::MapConfig kChainDataTables[]{
396393
kAccountChangeSet,
397394
kAccountHistory,
@@ -438,8 +435,7 @@ inline constexpr db::MapConfig kChainDataTables[]{
438435
kTxLookup,
439436
kRuntimeStates,
440437
kConsensusParameters,
441-
kExtraBlockData,
442-
kGasPrices
438+
kExtraBlockData
443439
};
444440

445441
//! \brief Ensures all defined tables are present in db with consistent flags. Should a table not exist it gets created

silkworm/node/stagedsync/stages/stage.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,12 @@ const evmone::gas_parameters& Stage::get_gas_params(db::ROTxn& txn, const Block&
6666
return last_gas_params;
6767
}
6868

69-
const gas_prices_t& Stage::get_gas_prices(db::ROTxn& txn, const Block& block) {
70-
auto curr_gas_prices_index = block.get_gas_prices_index();
71-
if(curr_gas_prices_index != last_gas_prices_index) {
72-
auto gas_prices = silkworm::db::read_gas_prices(txn, block);
73-
if(gas_prices.has_value()) {
74-
const auto& v = gas_prices.value();
75-
last_gas_prices = gas_prices_t(v.overhead_price, v.storage_price);
76-
} else {
77-
last_gas_prices=gas_prices_t{};
78-
}
79-
last_gas_prices_index = curr_gas_prices_index;
69+
gas_prices_t Stage::get_gas_prices(const Block& block) {
70+
auto gas_price = block.get_gas_prices();
71+
if(!gas_price.has_value()) {
72+
return gas_prices_t{};
8073
}
81-
return last_gas_prices;
74+
return gas_prices_t{gas_price->overhead_price, gas_price->storage_price};
8275
}
8376

8477
StageError::StageError(Stage::Result err)

0 commit comments

Comments
 (0)