-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from eosnetworkfoundation/yarkin/consensus_pa…
…rameters_storage Consensus parameters storage
- Loading branch information
Showing
15 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
find_package(Microsoft.GSL REQUIRED) | ||
find_package(nlohmann_json REQUIRED) | ||
find_package(tl-expected REQUIRED) | ||
|
||
if(MSVC) | ||
add_compile_options(/EHsc) | ||
else() | ||
add_compile_options(-fno-exceptions) | ||
endif() | ||
|
||
file( | ||
GLOB_RECURSE | ||
EOS_EVM_SRC | ||
CONFIGURE_DEPENDS | ||
"*.cpp" | ||
"*.hpp" | ||
"*.c" | ||
"*.h" | ||
) | ||
list(FILTER EOS_EVM_SRC EXCLUDE REGEX "_test\\.cpp$") | ||
list(FILTER EOS_EVM_SRC EXCLUDE REGEX "_benchmark\\.cpp$") | ||
|
||
add_library(eos_evm ${EOS_EVM_SRC}) | ||
target_include_directories(eos_evm PUBLIC ${SILKWORM_MAIN_DIR}) | ||
|
||
set(EOS_EVM_PUBLIC_LIBS | ||
intx::intx | ||
evmc | ||
tl::expected | ||
nlohmann_json::nlohmann_json | ||
) | ||
|
||
target_link_libraries( | ||
eos_evm | ||
PUBLIC ${EOS_EVM_PUBLIC_LIBS} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include "consensus_parameters.hpp" | ||
|
||
#if not defined(ANTELOPE) | ||
#include <silkworm/core/common/assert.hpp> | ||
#include <silkworm/core/common/endian.hpp> | ||
#endif | ||
|
||
namespace eosevm { | ||
bool operator==(const eosevm::GasFeeParameters& a, const eosevm::GasFeeParameters& b) { | ||
return a.gas_codedeposit == b.gas_codedeposit && a.gas_newaccount == b.gas_newaccount && | ||
a.gas_sset == b.gas_sset && a.gas_txcreate == b.gas_txcreate && a.gas_txnewaccount == b.gas_txnewaccount; | ||
} | ||
|
||
bool operator==(const eosevm::ConsensusParameters& a, const eosevm::ConsensusParameters& b) { | ||
return a.min_gas_price == b.min_gas_price && a.gas_fee_parameters == b.gas_fee_parameters; } | ||
|
||
|
||
#if not defined(ANTELOPE) | ||
[[nodiscard]] silkworm::Bytes GasFeeParameters::encode() const noexcept { | ||
silkworm::Bytes ret(40, '\0'); | ||
silkworm::endian::store_big_u64(&ret[0], gas_txnewaccount); | ||
silkworm::endian::store_big_u64(&ret[8], gas_newaccount); | ||
silkworm::endian::store_big_u64(&ret[16], gas_txcreate); | ||
silkworm::endian::store_big_u64(&ret[24], gas_codedeposit); | ||
silkworm::endian::store_big_u64(&ret[32], gas_sset); | ||
|
||
return ret; | ||
} | ||
|
||
std::optional<GasFeeParameters> GasFeeParameters::decode(silkworm::ByteView encoded) noexcept { | ||
SILKWORM_ASSERT(encoded.length() >= 40); | ||
GasFeeParameters feeParams; | ||
feeParams.gas_txnewaccount = silkworm::endian::load_big_u64(&encoded[0]); | ||
feeParams.gas_newaccount = silkworm::endian::load_big_u64(&encoded[8]); | ||
feeParams.gas_txcreate = silkworm::endian::load_big_u64(&encoded[16]); | ||
feeParams.gas_codedeposit = silkworm::endian::load_big_u64(&encoded[24]); | ||
feeParams.gas_sset = silkworm::endian::load_big_u64(&encoded[32]); | ||
|
||
return feeParams; | ||
} | ||
#endif | ||
|
||
#if not defined(ANTELOPE) | ||
[[nodiscard]] silkworm::Bytes ConsensusParameters::encode() const noexcept { | ||
SILKWORM_ASSERT(min_gas_price.has_value()); | ||
SILKWORM_ASSERT(gas_fee_parameters.has_value()); | ||
constexpr size_t size_before_fee_param = 2 * sizeof(uint64_t); | ||
auto value = gas_fee_parameters->encode(); | ||
silkworm::Bytes ret(value.length() + size_before_fee_param, '\0'); | ||
// Always store as latest supported version: currently 0. | ||
silkworm::endian::store_big_u64(&ret[0], 0); | ||
silkworm::endian::store_big_u64(&ret[sizeof(uint64_t)], *min_gas_price); | ||
std::memcpy(&ret[size_before_fee_param], &value[0], value.length()); | ||
return ret; | ||
}; | ||
|
||
std::optional<ConsensusParameters> ConsensusParameters::decode(silkworm::ByteView encoded) noexcept { | ||
SILKWORM_ASSERT(encoded.length() > sizeof(uint64_t)); | ||
ConsensusParameters config{}; | ||
const auto version = silkworm::endian::load_big_u64(&encoded[0]); | ||
|
||
// Parse according to version. For now, only 0. | ||
switch (version) { | ||
case 0: { | ||
constexpr size_t size_before_fee_param = 2 * sizeof(uint64_t); | ||
SILKWORM_ASSERT(encoded.length() > size_before_fee_param); | ||
config.min_gas_price = silkworm::endian::load_big_u64(&encoded[sizeof(uint64_t)]); | ||
config.gas_fee_parameters = GasFeeParameters::decode(silkworm::ByteView{&encoded[size_before_fee_param], encoded.length() - size_before_fee_param}); | ||
break; | ||
} | ||
default: SILKWORM_ASSERT(version <= 0); | ||
} | ||
|
||
return config; | ||
} | ||
#endif | ||
|
||
} // namespace eosevm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
#include <optional> | ||
#include <string> | ||
|
||
#include <intx/intx.hpp> | ||
|
||
|
||
#if not defined(ANTELOPE) | ||
#include <silkworm/core/common/base.hpp> | ||
#endif | ||
|
||
|
||
namespace eosevm { | ||
|
||
// Note: GasFeeParameters struct is NOT versioned, version will be handled by ConsensusParameters. | ||
// If we want to change this struct, create GasFeeParametersV2 and let ConsensusParameters use it. | ||
struct GasFeeParameters { | ||
// gas_txnewaccount = account_bytes * gas_per_byte | ||
uint64_t gas_txnewaccount; | ||
// gas_newaccount = account_bytes * gas_per_byte | ||
uint64_t gas_newaccount; | ||
// gas_txcreate = gas_create = contract_fixed_bytes * gas_per_byte | ||
uint64_t gas_txcreate; | ||
// gas_codedeposit = gas_per_byte | ||
uint64_t gas_codedeposit; | ||
// gas_sset = 100 + storage_slot_bytes * gas_per_byte | ||
uint64_t gas_sset; | ||
|
||
#if not defined(ANTELOPE) | ||
// Encode for storage in db. | ||
[[nodiscard]] silkworm::Bytes encode() const noexcept; | ||
|
||
// Decode from storage in db. | ||
static std::optional<GasFeeParameters> decode(silkworm::ByteView encoded) noexcept; | ||
#endif | ||
|
||
friend bool operator==(const GasFeeParameters&, const GasFeeParameters&); | ||
}; | ||
|
||
struct ConsensusParameters { | ||
std::optional<uint64_t> min_gas_price; | ||
std::optional<GasFeeParameters> gas_fee_parameters; | ||
|
||
#if not defined(ANTELOPE) | ||
// Encode for storage in db. | ||
[[nodiscard]] silkworm::Bytes encode() const noexcept; | ||
|
||
// Decode from storage in db. | ||
static std::optional<ConsensusParameters> decode(silkworm::ByteView encoded) noexcept; | ||
#endif | ||
|
||
friend bool operator==(const ConsensusParameters&, const ConsensusParameters&); | ||
}; | ||
|
||
} // namespace eosevm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.