From 8ac4c35bf7e2edfbc889b4f2af2f90378f3fa23e Mon Sep 17 00:00:00 2001 From: Matias Romeo Date: Mon, 12 Aug 2024 16:54:53 -0300 Subject: [PATCH] Return constant reference in value_promoter --- include/evm_runtime/config_wrapper.hpp | 4 ++-- include/evm_runtime/value_promoter.hpp | 20 +++++++++----------- src/config_wrapper.cpp | 4 ++-- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/include/evm_runtime/config_wrapper.hpp b/include/evm_runtime/config_wrapper.hpp index 931cc019..b893dd70 100644 --- a/include/evm_runtime/config_wrapper.hpp +++ b/include/evm_runtime/config_wrapper.hpp @@ -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 gas_txnewaccount, std::optional gas_newaccount, std::optional gas_txcreate, std::optional gas_codedeposit, std::optional gas_sset); - consensus_parameter_data_type get_consensus_param(); - std::pair get_consensus_param_and_maybe_promote(); + const consensus_parameter_data_type& get_consensus_param(); + std::pair get_consensus_param_and_maybe_promote(); void set_token_contract(eosio::name token_contract); // only set during init eosio::name get_token_contract() const; diff --git a/include/evm_runtime/value_promoter.hpp b/include/evm_runtime/value_promoter.hpp index e7d58f13..635a1479 100644 --- a/include/evm_runtime/value_promoter.hpp +++ b/include/evm_runtime/value_promoter.hpp @@ -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 get_value_and_maybe_promote(time_point_sec genesis_time, time_point current_time) {\ - T current_value = cached_value;\ + std::pair 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(current_value, promoted);\ + return std::pair(cached_value, promoted);\ }\ template \ 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\ });\ }\ diff --git a/src/config_wrapper.cpp b/src/config_wrapper.cpp index bb308bab..4724df8a 100644 --- a/src/config_wrapper.cpp +++ b/src/config_wrapper.cpp @@ -324,13 +324,13 @@ void config_wrapper::update_consensus_parameters2(std::optional 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 config_wrapper::get_consensus_param_and_maybe_promote() { +std::pair config_wrapper::get_consensus_param_and_maybe_promote() { // should not happen eosio::check(_cached_config.consensus_parameter.has_value(), "consensus_parameter not exist");