From 69a44843e2ad4c9aa209c7705be7ae0ff88a7b48 Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Tue, 3 Dec 2024 13:54:02 +0100 Subject: [PATCH] fix: propose gas limit validation --- .../config/include/config/config_utils.hpp | 4 +--- libraries/config/src/config.cpp | 20 ++----------------- libraries/config/src/config_utils.cpp | 13 +++--------- libraries/config/src/network.cpp | 7 +++---- .../consensus/src/dag/dag_block_proposer.cpp | 6 ++++-- 5 files changed, 13 insertions(+), 37 deletions(-) diff --git a/libraries/config/include/config/config_utils.hpp b/libraries/config/include/config/config_utils.hpp index 333c21bbdd..c82ee77987 100644 --- a/libraries/config/include/config/config_utils.hpp +++ b/libraries/config/include/config/config_utils.hpp @@ -16,11 +16,9 @@ Json::Value getConfigData(Json::Value root, const std::vector &path std::string getConfigDataAsString(const Json::Value &root, const std::vector &path, bool optional = false, const std::string &value = {}); -uint32_t getConfigDataAsUInt(const Json::Value &root, const std::vector &path, bool optional = false, +uint64_t getConfigDataAsUInt(const Json::Value &root, const std::vector &path, bool optional = false, uint32_t value = 0); -uint64_t getConfigDataAsUInt64(const Json::Value &root, const std::vector &path); - bool getConfigDataAsBoolean(const Json::Value &root, const std::vector &path, bool optional = false, bool value = false); diff --git a/libraries/config/src/config.cpp b/libraries/config/src/config.cpp index 8a2b9e9c71..f91b75f979 100644 --- a/libraries/config/src/config.cpp +++ b/libraries/config/src/config.cpp @@ -54,8 +54,8 @@ std::vector FullNodeConfig::loadLoggingConfigs(const Json::Value output.target = log_path; output.file_name = (log_path / getConfigDataAsString(o, {"file_name"})).string(); output.format = getConfigDataAsString(o, {"format"}); - output.max_size = getConfigDataAsUInt64(o, {"max_size"}); - output.rotation_size = getConfigDataAsUInt64(o, {"rotation_size"}); + output.max_size = getConfigDataAsUInt(o, {"max_size"}); + output.rotation_size = getConfigDataAsUInt(o, {"rotation_size"}); output.time_based_rotation = getConfigDataAsString(o, {"time_based_rotation"}); } logging.outputs.push_back(output); @@ -202,22 +202,6 @@ void FullNodeConfig::validate() const { throw ConfigException("transactions_pool_size cannot be smaller than " + std::to_string(kMinTransactionPoolSize)); } - if (genesis.pbft.gas_limit < propose_pbft_gas_limit || - (genesis.state.hardforks.cornus_hf.block_num != uint64_t(-1) && - genesis.state.hardforks.cornus_hf.pbft_gas_limit < propose_pbft_gas_limit)) { - throw ConfigException("Propose pbft gas limit:" + std::to_string(propose_pbft_gas_limit) + - " greater than max allowed pbft gas limit:" + std::to_string(genesis.pbft.gas_limit) + ":" + - std::to_string(genesis.state.hardforks.cornus_hf.pbft_gas_limit)); - } - - if (genesis.dag.gas_limit < propose_dag_gas_limit || - (genesis.state.hardforks.cornus_hf.block_num != uint64_t(-1) && - genesis.state.hardforks.cornus_hf.dag_gas_limit < propose_dag_gas_limit)) { - throw ConfigException("Propose dag gas limit:" + std::to_string(propose_pbft_gas_limit) + - " greater than max allowed pbft gas limit:" + std::to_string(genesis.pbft.gas_limit) + ":" + - std::to_string(genesis.state.hardforks.cornus_hf.pbft_gas_limit)); - } - // TODO: add validation of other config values } diff --git a/libraries/config/src/config_utils.cpp b/libraries/config/src/config_utils.cpp index f7dc9dc475..a3946d6b4f 100644 --- a/libraries/config/src/config_utils.cpp +++ b/libraries/config/src/config_utils.cpp @@ -7,6 +7,7 @@ #include #include "common/config_exception.hpp" +#include "libdevcore/CommonJS.h" namespace taraxa { @@ -44,14 +45,14 @@ std::string getConfigDataAsString(const Json::Value &root, const std::vector &path, bool optional, +uint64_t getConfigDataAsUInt(const Json::Value &root, const std::vector &path, bool optional, uint32_t value) { try { Json::Value ret = getConfigData(root, path, optional); if (ret.isNull()) { return value; } else { - return ret.asUInt(); + return dev::getUInt(ret); } } catch (Json::Exception &e) { if (optional) { @@ -61,14 +62,6 @@ uint32_t getConfigDataAsUInt(const Json::Value &root, const std::vector &path) { - try { - return getConfigData(root, path).asUInt64(); - } catch (Json::Exception &e) { - throw ConfigException(getConfigErr(path) + e.what()); - } -} - bool getConfigDataAsBoolean(const Json::Value &root, const std::vector &path, bool optional, bool value) { try { Json::Value ret = getConfigData(root, path, optional); diff --git a/libraries/config/src/network.cpp b/libraries/config/src/network.cpp index 7e6b24cce8..59701f0d69 100644 --- a/libraries/config/src/network.cpp +++ b/libraries/config/src/network.cpp @@ -75,10 +75,9 @@ DdosProtectionConfig dec_ddos_protection_config_json(const Json::Value &json) { ddos_protection.packets_stats_time_period_ms = std::chrono::milliseconds{getConfigDataAsUInt(json, {"packets_stats_time_period_ms"})}; ddos_protection.peer_max_packets_processing_time_us = - std::chrono::microseconds{getConfigDataAsUInt64(json, {"peer_max_packets_processing_time_us"})}; - ddos_protection.peer_max_packets_queue_size_limit = - getConfigDataAsUInt64(json, {"peer_max_packets_queue_size_limit"}); - ddos_protection.max_packets_queue_size = getConfigDataAsUInt64(json, {"max_packets_queue_size"}); + std::chrono::microseconds{getConfigDataAsUInt(json, {"peer_max_packets_processing_time_us"})}; + ddos_protection.peer_max_packets_queue_size_limit = getConfigDataAsUInt(json, {"peer_max_packets_queue_size_limit"}); + ddos_protection.max_packets_queue_size = getConfigDataAsUInt(json, {"max_packets_queue_size"}); return ddos_protection; } diff --git a/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp b/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp index 6fdeae81c9..9e42063db9 100644 --- a/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp @@ -26,8 +26,10 @@ DagBlockProposer::DagBlockProposer(const FullNodeConfig& config, std::shared_ptr node_sk_(config.node_secret), vrf_sk_(config.vrf_secret), vrf_pk_(vrf_wrapper::getVrfPublicKey(vrf_sk_)), - kPbftGasLimit(config.propose_pbft_gas_limit), - kDagGasLimit(config.propose_dag_gas_limit), + kPbftGasLimit( + std::min(config.propose_pbft_gas_limit, config.genesis.getGasLimits(final_chain_->lastBlockNumber()).second)), + kDagGasLimit( + std::min(config.propose_dag_gas_limit, config.genesis.getGasLimits(final_chain_->lastBlockNumber()).first)), kHardforks(config.genesis.state.hardforks), kValidatorMaxVote(config.genesis.state.dpos.validator_maximum_stake / config.genesis.state.dpos.vote_eligibility_balance_step) {