From 153ae462038caf3fd8d538e532acfd75b24eb2ce Mon Sep 17 00:00:00 2001 From: mfrankovi Date: Mon, 12 Aug 2024 09:12:32 +0200 Subject: [PATCH] chore: dag block proposal limit --- .../cli/config_jsons/default/default_config.json | 2 +- .../cli/config_jsons/devnet/devnet_config.json | 2 +- .../cli/config_jsons/mainnet/mainnet_config.json | 2 +- .../cli/config_jsons/testnet/testnet_config.json | 2 +- libraries/common/include/common/constants.hpp | 1 + .../consensus/src/dag/dag_block_proposer.cpp | 13 ++++++++++--- .../core_libs/network/include/network/network.hpp | 7 +++++++ libraries/core_libs/network/src/network.cpp | 6 ++++++ 8 files changed, 28 insertions(+), 7 deletions(-) diff --git a/libraries/cli/include/cli/config_jsons/default/default_config.json b/libraries/cli/include/cli/config_jsons/default/default_config.json index 72e9409c3f..0f72c0bc9b 100644 --- a/libraries/cli/include/cli/config_jsons/default/default_config.json +++ b/libraries/cli/include/cli/config_jsons/default/default_config.json @@ -24,7 +24,7 @@ "packets_stats_time_period_ms": 60000, "peer_max_packets_processing_time_us": 10000000, "peer_max_packets_queue_size_limit": 100000, - "max_packets_queue_size": 200000 + "max_packets_queue_size": 100 }, "listen_ip": "0.0.0.0", "listen_port": 10002, diff --git a/libraries/cli/include/cli/config_jsons/devnet/devnet_config.json b/libraries/cli/include/cli/config_jsons/devnet/devnet_config.json index 47d0713725..2f812df6f4 100644 --- a/libraries/cli/include/cli/config_jsons/devnet/devnet_config.json +++ b/libraries/cli/include/cli/config_jsons/devnet/devnet_config.json @@ -24,7 +24,7 @@ "packets_stats_time_period_ms": 60000, "peer_max_packets_processing_time_us": 10000000, "peer_max_packets_queue_size_limit": 100000, - "max_packets_queue_size": 200000 + "max_packets_queue_size": 100 }, "listen_ip": "0.0.0.0", "listen_port": 10002, diff --git a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_config.json b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_config.json index 66cb1b4499..f82d06c4f2 100644 --- a/libraries/cli/include/cli/config_jsons/mainnet/mainnet_config.json +++ b/libraries/cli/include/cli/config_jsons/mainnet/mainnet_config.json @@ -24,7 +24,7 @@ "packets_stats_time_period_ms": 60000, "peer_max_packets_processing_time_us": 0, "peer_max_packets_queue_size_limit": 0, - "max_packets_queue_size": 200000 + "max_packets_queue_size": 100 }, "listen_ip": "0.0.0.0", "listen_port": 10002, diff --git a/libraries/cli/include/cli/config_jsons/testnet/testnet_config.json b/libraries/cli/include/cli/config_jsons/testnet/testnet_config.json index d444117b3c..676a6428c8 100644 --- a/libraries/cli/include/cli/config_jsons/testnet/testnet_config.json +++ b/libraries/cli/include/cli/config_jsons/testnet/testnet_config.json @@ -24,7 +24,7 @@ "packets_stats_time_period_ms": 60000, "peer_max_packets_processing_time_us": 0, "peer_max_packets_queue_size_limit": 0, - "max_packets_queue_size": 200000 + "max_packets_queue_size": 100 }, "listen_ip": "0.0.0.0", "listen_port": 10002, diff --git a/libraries/common/include/common/constants.hpp b/libraries/common/include/common/constants.hpp index 5d7e4540e1..9122c34c96 100644 --- a/libraries/common/include/common/constants.hpp +++ b/libraries/common/include/common/constants.hpp @@ -29,6 +29,7 @@ const uint64_t kMinTxGas{21000}; constexpr uint32_t kMinTransactionPoolSize{30000}; constexpr uint32_t kDefaultTransactionPoolSize{200000}; +constexpr uint32_t kMaxNonFinalizedTransactions{1000000}; const size_t kV2NetworkVersion = 2; 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 cad7733b8c..9587b11daf 100644 --- a/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp +++ b/libraries/core_libs/consensus/src/dag/dag_block_proposer.cpp @@ -51,6 +51,11 @@ bool DagBlockProposer::proposeDagBlock() { return false; } + // Do not propose dag blocks if number of non finalized transactions is over the limit + if (trx_mgr_->getNonfinalizedTrxSize() > kMaxNonFinalizedTransactions) { + return false; + } + auto frontier = dag_mgr_->getDagFrontier(); LOG(log_dg_) << "Get frontier with pivot: " << frontier.pivot << " tips: " << frontier.tips; assert(!frontier.pivot.isZero()); @@ -183,12 +188,14 @@ void DagBlockProposer::start() { while (!stopped_) { // Blocks are not proposed if we are behind the network and still syncing auto syncing = false; + auto packets_over_the_limit = false; if (auto net = network_.lock()) { syncing = net->pbft_syncing(); + packets_over_the_limit = net->packetQueueOverLimit(); } - // Only sleep if block was not proposed or if we are syncing, if block is proposed try to propose another block - // immediately - if (syncing || !proposeDagBlock()) { + // Only sleep if block was not proposed or if we are syncing or if packets queue is over the limit, if block is + // proposed try to propose another block immediately + if (syncing || packets_over_the_limit || !proposeDagBlock()) { thisThreadSleepForMilliSeconds(min_proposal_delay); } } diff --git a/libraries/core_libs/network/include/network/network.hpp b/libraries/core_libs/network/include/network/network.hpp index 8c654eaaa7..30fd397944 100644 --- a/libraries/core_libs/network/include/network/network.hpp +++ b/libraries/core_libs/network/include/network/network.hpp @@ -77,6 +77,13 @@ class Network { */ void requestPillarBlockVotesBundle(PbftPeriod period, const blk_hash_t &pillar_block_hash); + /** + * @brief Get packets queue status + * + * @return true if packets queue is over the limit + */ + bool packetQueueOverLimit() const; + // METHODS USED IN TESTS ONLY template std::shared_ptr getSpecificHandler() const; diff --git a/libraries/core_libs/network/src/network.cpp b/libraries/core_libs/network/src/network.cpp index 9091c68571..248f773951 100644 --- a/libraries/core_libs/network/src/network.cpp +++ b/libraries/core_libs/network/src/network.cpp @@ -132,6 +132,12 @@ void Network::start() { bool Network::isStarted() { return tp_.is_running(); } +bool Network::packetQueueOverLimit() const { + auto [hp_queue_size, mp_queue_size, lp_queue_size] = packets_tp_->getQueueSize(); + auto total_size = hp_queue_size + mp_queue_size + lp_queue_size; + return total_size > kConf.network.ddos_protection.max_packets_queue_size; +} + std::list Network::getAllNodes() const { return host_->getNodes(); } size_t Network::getPeerCount() { return host_->peer_count(); }