Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
taokayan committed Feb 22, 2024
1 parent f5be162 commit d39abf6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 22 deletions.
1 change: 1 addition & 0 deletions include/evm_runtime/evm_contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class [[eosio::contract]] evm_contract : public contract
[[eosio::action]] void configchange(consensus_parameter_data_type consensus_parameter_data) {
eosio::check(get_sender() == get_self(), "forbidden to call");
};
using configchange_action = eosio::action_wrapper<"configchange"_n, &evm_contract::configchange>;

#ifdef WITH_ADMIN_ACTIONS
[[eosio::action]] void rmgcstore(uint64_t id);
Expand Down
9 changes: 9 additions & 0 deletions include/evm_runtime/tables.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,15 @@ struct consensus_parameter_type {
return current_block_num > pending_block_num;
}

// Reference invalidated by get_consensus_param_and_maybe_promote and update_consensus_param.
const consensus_parameter_data_type& get_consensus_param(
time_point_sec genesis_time, time_point current_time) const {
if (is_pending_active(genesis_time, current_time)) {
return pending->data;
}
return current;
}

std::pair<const consensus_parameter_data_type &, bool> get_consensus_param_and_maybe_promote(
time_point_sec genesis_time, time_point current_time) {
if (is_pending_active(genesis_time, current_time)) {
Expand Down
7 changes: 7 additions & 0 deletions include/evm_runtime/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ namespace evm_runtime {
constexpr intx::uint256 minimum_natively_representable = intx::exp(10_u256, intx::uint256(evm_precision - token_symbol.precision()));
static_assert(evm_precision - token_symbol.precision() <= 14, "dust math may overflow a uint64_t");

constexpr double pow10_const(int v) {
double r = 1.0;
while (v-- > 0) r *= 10.0;
return r;
}
constexpr double minimum_natively_representable_f = pow10_const(evm_precision - token_symbol.precision());

typedef intx::uint<256> uint256;
typedef intx::uint<512> uint512;
typedef std::vector<char> bytes;
Expand Down
4 changes: 2 additions & 2 deletions src/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ void evm_contract::process_tx(const runtime_config& rc, eosio::name miner, const
ep.state().write_to_db(ep.evm().block().header.number);

if (gas_param_pair.second) {
action(std::vector<permission_level>{}, get_self(),
"configchange"_n, gas_param_pair.first).send();
configchange_action act{get_self(), std::vector<eosio::permission_level>()};
act.send(gas_param_pair.first);
}

if (current_version >= 1) {
Expand Down
36 changes: 16 additions & 20 deletions src/config_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ config_wrapper::config_wrapper(eosio::name self) : _self(self), _config(self, se
if(_exists) {
_cached_config = _config.get();
}
if (!_cached_config.consensus_parameter.has_value()){
if (!_cached_config.consensus_parameter.has_value()) {
_cached_config.consensus_parameter = consensus_parameter_type();
}
std::visit([&](auto &v) {
if (v.minimum_gas_price == 0) {
std::visit([&](auto &v) {
v.minimum_gas_price = _cached_config.gas_price;
// don't set dirty, as trxs can be read-only
}
}, _cached_config.consensus_parameter->current);
}, _cached_config.consensus_parameter->current);
// Don't set dirty because action can be read-only.
}
}

config_wrapper::~config_wrapper() {
Expand Down Expand Up @@ -74,14 +72,9 @@ void config_wrapper::set_ingress_bridge_fee(const eosio::asset& ingress_bridge_f
}

uint64_t config_wrapper::get_gas_price()const {
uint64_t gas_price = _cached_config.gas_price;
if (_cached_config.consensus_parameter.has_value() &&
_cached_config.consensus_parameter->is_pending_active(_cached_config.genesis_time, get_current_time())) {
std::visit([&](const auto &v) {
gas_price = v.minimum_gas_price;
}, _cached_config.consensus_parameter->pending->data);
}
return gas_price;
return std::visit([&](const auto& v) {
return v.minimum_gas_price;
}, _cached_config.consensus_parameter->get_consensus_param(_cached_config.genesis_time, get_current_time()));
}

uint32_t config_wrapper::get_miner_cut()const {
Expand Down Expand Up @@ -133,7 +126,7 @@ void config_wrapper::set_fee_parameters(const fee_parameters& fee_params,
bool allow_any_to_be_unspecified)
{
if (fee_params.gas_price.has_value()) {
eosio::check(*fee_params.gas_price >= 1000000000ull, "gas_price must >= 1Gwei");
eosio::check(*fee_params.gas_price >= one_gwei, "gas_price must >= 1Gwei");
if (_cached_config.evm_version.has_value() && _cached_config.evm_version->cached_version >= 1) {
// activate in the next evm block
this->update_gas_params2(std::optional<uint64_t>(), /* gas_txnewaccount */
Expand All @@ -145,6 +138,10 @@ void config_wrapper::set_fee_parameters(const fee_parameters& fee_params,
);
} else {
_cached_config.gas_price = *fee_params.gas_price;
std::visit([&](auto &v) {
v.minimum_gas_price = _cached_config.gas_price;
},
_cached_config.consensus_parameter->current);
}
} else {
eosio::check(allow_any_to_be_unspecified, "All required fee parameters not specified: missing gas_price");
Expand All @@ -170,10 +167,9 @@ void config_wrapper::set_fee_parameters(const fee_parameters& fee_params,
void config_wrapper::update_gas_params(eosio::asset ram_price_mb, uint64_t minimum_gas_price) {

eosio::check(ram_price_mb.symbol == token_symbol, "invalid price symbol");
eosio::check(minimum_gas_price >= 1000000000ull, "gas_price must >= 1Gwei");
eosio::check(minimum_gas_price >= one_gwei, "gas_price must >= 1Gwei");

double gas_per_byte_f = (ram_price_mb.amount / 10000.0 * 1e18 / (1024.0 * 1024.0)) /
(minimum_gas_price * (double)(100000 - _cached_config.miner_cut) / 100000.0);
double gas_per_byte_f = (ram_price_mb.amount / (1024.0 * 1024.0) * minimum_natively_representable_f) / (minimum_gas_price * static_cast<double>(hundred_percent - _cached_config.miner_cut) / hundred_percent);

constexpr uint64_t account_bytes = 347;
constexpr uint64_t contract_fixed_bytes = 606;
Expand Down Expand Up @@ -203,7 +199,7 @@ void config_wrapper::update_gas_params2(std::optional<uint64_t> gas_txnewaccount
eosio::check(_cached_config.consensus_parameter.has_value(), "consensus_parameter not exist");

if (minimum_gas_price.has_value()) {
eosio::check(*minimum_gas_price >= 1000000000ull, "gas_price must >= 1Gwei");
eosio::check(*minimum_gas_price >= one_gwei, "gas_price must >= 1Gwei");
}

_cached_config.consensus_parameter->update_consensus_param([&](auto & v) {
Expand Down

0 comments on commit d39abf6

Please sign in to comment.