From 2f3f1aec1f8aadd4a6fb08ca5da7eeda31eb388f Mon Sep 17 00:00:00 2001 From: fanquake Date: Sat, 24 Oct 2020 16:24:07 +0800 Subject: net: remove SetMaxOutboundTarget This has been unused since f3552da81393a8e78ce3e2afed0b9c9d1ff5cee0. --- src/net.cpp | 6 ------ src/net.h | 2 -- src/test/fuzz/connman.cpp | 11 ++++------- 3 files changed, 4 insertions(+), 15 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 7cb91f1388..519717e46c 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2859,12 +2859,6 @@ void CConnman::RecordBytesSent(uint64_t bytes) nMaxOutboundTotalBytesSentInCycle += bytes; } -void CConnman::SetMaxOutboundTarget(uint64_t limit) -{ - LOCK(cs_totalBytesSent); - nMaxOutboundLimit = limit; -} - uint64_t CConnman::GetMaxOutboundTarget() { LOCK(cs_totalBytesSent); diff --git a/src/net.h b/src/net.h index 41f7fa93ff..dc841d1b11 100644 --- a/src/net.h +++ b/src/net.h @@ -366,8 +366,6 @@ public: //! that peer during `net_processing.cpp:PushNodeVersion()`. ServiceFlags GetLocalServices() const; - //!set the max outbound target in bytes - void SetMaxOutboundTarget(uint64_t limit); uint64_t GetMaxOutboundTarget(); //!set the timeframe for the max outbound target diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index bb97f58cf2..4afaf9def0 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -31,7 +31,7 @@ void test_one_input(const std::vector& buffer) CSubNet random_subnet; std::string random_string; while (fuzzed_data_provider.ConsumeBool()) { - switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 30)) { + switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 29)) { case 0: random_address = ConsumeAddress(fuzzed_data_provider); break; @@ -127,18 +127,15 @@ void test_one_input(const std::vector& buffer) connman.SetBestHeight(fuzzed_data_provider.ConsumeIntegral()); break; case 26: - connman.SetMaxOutboundTarget(fuzzed_data_provider.ConsumeIntegral()); - break; - case 27: connman.SetMaxOutboundTimeframe(fuzzed_data_provider.ConsumeIntegral()); break; - case 28: + case 27: connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool()); break; - case 29: + case 28: connman.SetServices(random_service, static_cast(fuzzed_data_provider.ConsumeIntegral())); break; - case 30: + case 29: connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool()); break; } -- cgit v1.2.3 From b117eb148678b0fc5be02346cef29d87d4f81af9 Mon Sep 17 00:00:00 2001 From: fanquake Date: Sat, 24 Oct 2020 16:25:56 +0800 Subject: net: remove SetMaxOutboundTimeframe This was introduced in 872fee3fccc8b33b9af0a401b5f85ac5504b57eb and it's unclear if it's ever been used. --- src/net.cpp | 12 ------------ src/net.h | 3 --- src/test/fuzz/connman.cpp | 9 +++------ 3 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 519717e46c..2250bc0530 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2885,18 +2885,6 @@ uint64_t CConnman::GetMaxOutboundTimeLeftInCycle() return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime(); } -void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe) -{ - LOCK(cs_totalBytesSent); - if (nMaxOutboundTimeframe != timeframe) - { - // reset measure-cycle in case of changing - // the timeframe - nMaxOutboundCycleStartTime = GetTime(); - } - nMaxOutboundTimeframe = timeframe; -} - bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) { LOCK(cs_totalBytesSent); diff --git a/src/net.h b/src/net.h index dc841d1b11..fd47b5db6a 100644 --- a/src/net.h +++ b/src/net.h @@ -367,9 +367,6 @@ public: ServiceFlags GetLocalServices() const; uint64_t GetMaxOutboundTarget(); - - //!set the timeframe for the max outbound target - void SetMaxOutboundTimeframe(uint64_t timeframe); uint64_t GetMaxOutboundTimeframe(); //! check if the outbound target is reached diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index 4afaf9def0..af85e7be4c 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -31,7 +31,7 @@ void test_one_input(const std::vector& buffer) CSubNet random_subnet; std::string random_string; while (fuzzed_data_provider.ConsumeBool()) { - switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 29)) { + switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 28)) { case 0: random_address = ConsumeAddress(fuzzed_data_provider); break; @@ -127,15 +127,12 @@ void test_one_input(const std::vector& buffer) connman.SetBestHeight(fuzzed_data_provider.ConsumeIntegral()); break; case 26: - connman.SetMaxOutboundTimeframe(fuzzed_data_provider.ConsumeIntegral()); - break; - case 27: connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool()); break; - case 28: + case 27: connman.SetServices(random_service, static_cast(fuzzed_data_provider.ConsumeIntegral())); break; - case 29: + case 28: connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool()); break; } -- cgit v1.2.3 From 173d0d35f1bb271175047c9eb7dd58cc46ed35bf Mon Sep 17 00:00:00 2001 From: fanquake Date: Sat, 24 Oct 2020 16:33:26 +0800 Subject: net: remove nMaxOutboundTimeframe from connection options It's not actually possible to change this value, so remove the indirection of it being a conn option. DEFAULT_MAX_UPLOAD_TIMEFRAME is a compile time constant. --- src/init.cpp | 2 -- src/net.cpp | 12 +++++++----- src/net.h | 5 ----- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 09be3d01fa..7823f96c90 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1514,7 +1514,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA } #endif uint64_t nMaxOutboundLimit = 0; //unlimited unless -maxuploadtarget is set - uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME; if (args.IsArgSet("-maxuploadtarget")) { nMaxOutboundLimit = args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET) * 1024 * 1024; @@ -1921,7 +1920,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA connOptions.nReceiveFloodSize = 1000 * args.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); connOptions.m_added_nodes = args.GetArgs("-addnode"); - connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe; connOptions.nMaxOutboundLimit = nMaxOutboundLimit; connOptions.m_peer_connect_timeout = peer_connect_timeout; diff --git a/src/net.cpp b/src/net.cpp index 2250bc0530..56d82cf7e7 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -72,6 +72,9 @@ static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11}; static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5}; static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers +/** The default timeframe for -maxuploadtarget. 1 day. */ +static constexpr uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24; + // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization. #define FEELER_SLEEP_WINDOW 1 @@ -2848,7 +2851,7 @@ void CConnman::RecordBytesSent(uint64_t bytes) nTotalBytesSent += bytes; uint64_t now = GetTime(); - if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now) + if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now) { // timeframe expired, reset cycle nMaxOutboundCycleStartTime = now; @@ -2867,8 +2870,7 @@ uint64_t CConnman::GetMaxOutboundTarget() uint64_t CConnman::GetMaxOutboundTimeframe() { - LOCK(cs_totalBytesSent); - return nMaxOutboundTimeframe; + return MAX_UPLOAD_TIMEFRAME; } uint64_t CConnman::GetMaxOutboundTimeLeftInCycle() @@ -2878,9 +2880,9 @@ uint64_t CConnman::GetMaxOutboundTimeLeftInCycle() return 0; if (nMaxOutboundCycleStartTime == 0) - return nMaxOutboundTimeframe; + return MAX_UPLOAD_TIMEFRAME; - uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe; + uint64_t cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME; uint64_t now = GetTime(); return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime(); } diff --git a/src/net.h b/src/net.h index fd47b5db6a..31141d96c2 100644 --- a/src/net.h +++ b/src/net.h @@ -76,8 +76,6 @@ static const bool DEFAULT_UPNP = false; static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125; /** The default for -maxuploadtarget. 0 = Unlimited */ static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0; -/** The default timeframe for -maxuploadtarget. 1 day. */ -static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24; /** Default for blocks only*/ static const bool DEFAULT_BLOCKSONLY = false; /** -peertimeout default */ @@ -207,7 +205,6 @@ public: BanMan* m_banman = nullptr; unsigned int nSendBufferMaxSize = 0; unsigned int nReceiveFloodSize = 0; - uint64_t nMaxOutboundTimeframe = 0; uint64_t nMaxOutboundLimit = 0; int64_t m_peer_connect_timeout = DEFAULT_PEER_CONNECT_TIMEOUT; std::vector vSeedNodes; @@ -239,7 +236,6 @@ public: m_peer_connect_timeout = connOptions.m_peer_connect_timeout; { LOCK(cs_totalBytesSent); - nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe; nMaxOutboundLimit = connOptions.nMaxOutboundLimit; } vWhitelistedRange = connOptions.vWhitelistedRange; @@ -481,7 +477,6 @@ private: uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0}; uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0}; uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent); - uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent); // P2P timeout in seconds int64_t m_peer_connect_timeout; -- cgit v1.2.3 From f805933e70d4320e62739f4cecfc08bdbad8afaa Mon Sep 17 00:00:00 2001 From: fanquake Date: Sat, 24 Oct 2020 16:38:56 +0800 Subject: init: set nMaxOutboundLimit connection option directly DEFAULT_MAX_UPLOAD_TARGET is a compile time constant. --- src/init.cpp | 7 +------ src/net.h | 2 +- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 7823f96c90..48711f9cd2 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1513,11 +1513,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA RegisterValidationInterface(g_zmq_notification_interface); } #endif - uint64_t nMaxOutboundLimit = 0; //unlimited unless -maxuploadtarget is set - - if (args.IsArgSet("-maxuploadtarget")) { - nMaxOutboundLimit = args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET) * 1024 * 1024; - } // ********************************************************* Step 7: load block chain @@ -1920,7 +1915,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA connOptions.nReceiveFloodSize = 1000 * args.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); connOptions.m_added_nodes = args.GetArgs("-addnode"); - connOptions.nMaxOutboundLimit = nMaxOutboundLimit; + connOptions.nMaxOutboundLimit = 1024 * 1024 * args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET); connOptions.m_peer_connect_timeout = peer_connect_timeout; for (const std::string& bind_arg : args.GetArgs("-bind")) { diff --git a/src/net.h b/src/net.h index 31141d96c2..1b9722fc27 100644 --- a/src/net.h +++ b/src/net.h @@ -75,7 +75,7 @@ static const bool DEFAULT_UPNP = false; /** The maximum number of peer connections to maintain. */ static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125; /** The default for -maxuploadtarget. 0 = Unlimited */ -static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0; +static constexpr uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0; /** Default for blocks only*/ static const bool DEFAULT_BLOCKSONLY = false; /** -peertimeout default */ -- cgit v1.2.3 From 0475c8ba4d10dce79092361bc4c23c11dadba39a Mon Sep 17 00:00:00 2001 From: fanquake Date: Sat, 24 Oct 2020 19:13:42 +0800 Subject: net: use std::chrono throughout maxOutbound logic --- src/net.cpp | 22 +++++++++++----------- src/net.h | 10 +++++----- src/rpc/net.cpp | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 56d82cf7e7..8588f7841d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -73,7 +73,7 @@ static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5}; static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers /** The default timeframe for -maxuploadtarget. 1 day. */ -static constexpr uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24; +static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24}; // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization. #define FEELER_SLEEP_WINDOW 1 @@ -2850,7 +2850,7 @@ void CConnman::RecordBytesSent(uint64_t bytes) LOCK(cs_totalBytesSent); nTotalBytesSent += bytes; - uint64_t now = GetTime(); + const auto now = GetTime(); if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now) { // timeframe expired, reset cycle @@ -2868,23 +2868,23 @@ uint64_t CConnman::GetMaxOutboundTarget() return nMaxOutboundLimit; } -uint64_t CConnman::GetMaxOutboundTimeframe() +std::chrono::seconds CConnman::GetMaxOutboundTimeframe() { return MAX_UPLOAD_TIMEFRAME; } -uint64_t CConnman::GetMaxOutboundTimeLeftInCycle() +std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle() { LOCK(cs_totalBytesSent); if (nMaxOutboundLimit == 0) - return 0; + return 0s; - if (nMaxOutboundCycleStartTime == 0) + if (nMaxOutboundCycleStartTime.count() == 0) return MAX_UPLOAD_TIMEFRAME; - uint64_t cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME; - uint64_t now = GetTime(); - return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime(); + const std::chrono::seconds cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME; + const auto now = GetTime(); + return (cycleEndTime < now) ? 0s : cycleEndTime - now; } bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) @@ -2896,8 +2896,8 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) if (historicalBlockServingLimit) { // keep a large enough buffer to at least relay each block once - uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle(); - uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE; + const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle(); + const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MAX_BLOCK_SERIALIZED_SIZE; if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer) return true; } diff --git a/src/net.h b/src/net.h index 1b9722fc27..51cf6f4579 100644 --- a/src/net.h +++ b/src/net.h @@ -363,7 +363,7 @@ public: ServiceFlags GetLocalServices() const; uint64_t GetMaxOutboundTarget(); - uint64_t GetMaxOutboundTimeframe(); + std::chrono::seconds GetMaxOutboundTimeframe(); //! check if the outbound target is reached //! if param historicalBlockServingLimit is set true, the function will @@ -374,9 +374,9 @@ public: //! in case of no limit, it will always response 0 uint64_t GetOutboundTargetBytesLeft(); - //! response the time in second left in the current max outbound cycle - //! in case of no limit, it will always response 0 - uint64_t GetMaxOutboundTimeLeftInCycle(); + //! returns the time left in the current max outbound cycle + //! in case of no limit, it will always return 0 + std::chrono::seconds GetMaxOutboundTimeLeftInCycle(); uint64_t GetTotalBytesRecv(); uint64_t GetTotalBytesSent(); @@ -475,7 +475,7 @@ private: // outbound limit & stats uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0}; - uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0}; + std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0}; uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent); // P2P timeout in seconds diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 973d730218..6a2d1ea77f 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -487,12 +487,12 @@ static RPCHelpMan getnettotals() obj.pushKV("timemillis", GetTimeMillis()); UniValue outboundLimit(UniValue::VOBJ); - outboundLimit.pushKV("timeframe", node.connman->GetMaxOutboundTimeframe()); + outboundLimit.pushKV("timeframe", count_seconds(node.connman->GetMaxOutboundTimeframe())); outboundLimit.pushKV("target", node.connman->GetMaxOutboundTarget()); outboundLimit.pushKV("target_reached", node.connman->OutboundTargetReached(false)); outboundLimit.pushKV("serve_historical_blocks", !node.connman->OutboundTargetReached(true)); outboundLimit.pushKV("bytes_left_in_cycle", node.connman->GetOutboundTargetBytesLeft()); - outboundLimit.pushKV("time_left_in_cycle", node.connman->GetMaxOutboundTimeLeftInCycle()); + outboundLimit.pushKV("time_left_in_cycle", count_seconds(node.connman->GetMaxOutboundTimeLeftInCycle())); obj.pushKV("uploadtarget", outboundLimit); return obj; }, -- cgit v1.2.3