Skip to content

Commit

Permalink
Return constant reference in value_promoter
Browse files Browse the repository at this point in the history
  • Loading branch information
elmato committed Aug 12, 2024
1 parent 3c6ff18 commit 8ac4c35
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
4 changes: 2 additions & 2 deletions include/evm_runtime/config_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ struct config_wrapper {
void update_consensus_parameters(eosio::asset ram_price_mb, uint64_t gas_price);
void update_consensus_parameters2(std::optional<uint64_t> gas_txnewaccount, std::optional<uint64_t> gas_newaccount, std::optional<uint64_t> gas_txcreate, std::optional<uint64_t> gas_codedeposit, std::optional<uint64_t> gas_sset);

consensus_parameter_data_type get_consensus_param();
std::pair<consensus_parameter_data_type, bool> get_consensus_param_and_maybe_promote();
const consensus_parameter_data_type& get_consensus_param();
std::pair<const consensus_parameter_data_type&, bool> get_consensus_param_and_maybe_promote();

void set_token_contract(eosio::name token_contract); // only set during init
eosio::name get_token_contract() const;
Expand Down
20 changes: 9 additions & 11 deletions include/evm_runtime/value_promoter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,27 @@
};

#define VALUE_PROMOTER_IMPL(T)\
T get_value(time_point_sec genesis_time, time_point current_time)const {\
T current_value = cached_value;\
const T& get_value(time_point_sec genesis_time, time_point current_time)const {\
if(pending_value.has_value() && pending_value->is_active(genesis_time, current_time)) {\
current_value = pending_value->value;\
return pending_value->value;\
}\
return current_value;\
return cached_value;\
}\
std::pair<T, bool> get_value_and_maybe_promote(time_point_sec genesis_time, time_point current_time) {\
T current_value = cached_value;\
std::pair<const T&, bool> get_value_and_maybe_promote(time_point_sec genesis_time, time_point current_time) {\
bool promoted = false;\
if(pending_value.has_value() && pending_value->is_active(genesis_time, current_time)) {\
current_value = pending_value->value;\
promote_pending();\
promoted = true;\
}\
return std::pair<T, bool>(current_value, promoted);\
return std::pair<const T&, bool>(cached_value, promoted);\
}\
template <typename Visitor>\
void update(Visitor&& visitor_fn, time_point_sec genesis_time, time_point current_time) {\
auto value = get_value_and_maybe_promote(genesis_time, current_time);\
visitor_fn(value.first);\
auto tmp = get_value_and_maybe_promote(genesis_time, current_time);\
T value = tmp.first;\
visitor_fn(value);\
pending_value.emplace(T##_pending{\
.value = value.first,\
.value = value,\
.time = current_time\
});\
}\
Expand Down
4 changes: 2 additions & 2 deletions src/config_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ void config_wrapper::update_consensus_parameters2(std::optional<uint64_t> gas_tx
set_dirty();
}

consensus_parameter_data_type config_wrapper::get_consensus_param() {
const consensus_parameter_data_type& config_wrapper::get_consensus_param() {
// should not happen
eosio::check(_cached_config.consensus_parameter.has_value(), "consensus_parameter not exist");
return _cached_config.consensus_parameter->get_value(_cached_config.genesis_time, get_current_time());
}

std::pair<consensus_parameter_data_type, bool> config_wrapper::get_consensus_param_and_maybe_promote() {
std::pair<const consensus_parameter_data_type&, bool> config_wrapper::get_consensus_param_and_maybe_promote() {

// should not happen
eosio::check(_cached_config.consensus_parameter.has_value(), "consensus_parameter not exist");
Expand Down

0 comments on commit 8ac4c35

Please sign in to comment.