Merge #20253: net: use std::chrono throughout maxOutbound logic

0475c8ba4d net: use std::chrono throughout maxOutbound logic (fanquake)
f805933e70 init: set nMaxOutboundLimit connection option directly (fanquake)
173d0d35f1 net: remove nMaxOutboundTimeframe from connection options (fanquake)
b117eb1486 net: remove SetMaxOutboundTimeframe (fanquake)
2f3f1aec1f net: remove SetMaxOutboundTarget (fanquake)

Pull request description:

  Switch to using `std::chrono` types for the max outbound related logic.
  Removes some unnecessary code from init.

ACKs for top commit:
  jnewbery:
    utACK 0475c8ba4d
  MarcoFalke:
    review ACK 0475c8ba4d 🎭

Tree-SHA512: 5a6d5b61e0d4c08a235cfc0257dae65d09a5df019d8d230b1a58a3e2483ddf4a31efdefc885c4a02e4715e4180b0ed92ebc0a1c08b2bf476a391945114593514
This commit is contained in:
fanquake committed 2020-12-15 20:48:21 +08:00
commit f1f2418433
5 files changed
+28 -67

No files matched your search

+1 -8
View File
@@ -1513,12 +1513,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
RegisterValidationInterface(g_zmq_notification_interface); RegisterValidationInterface(g_zmq_notification_interface);
} }
#endif #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;
}
// ********************************************************* Step 7: load block chain // ********************************************************* Step 7: load block chain
@@ -1921,8 +1915,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
connOptions.nReceiveFloodSize = 1000 * args.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER); connOptions.nReceiveFloodSize = 1000 * args.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
connOptions.m_added_nodes = args.GetArgs("-addnode"); connOptions.m_added_nodes = args.GetArgs("-addnode");
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe; connOptions.nMaxOutboundLimit = 1024 * 1024 * args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET);
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
connOptions.m_peer_connect_timeout = peer_connect_timeout; connOptions.m_peer_connect_timeout = peer_connect_timeout;
for (const std::string& bind_arg : args.GetArgs("-bind")) { for (const std::string& bind_arg : args.GetArgs("-bind")) {
+16 -32
View File
@@ -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 std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers
/** The default timeframe for -maxuploadtarget. 1 day. */
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. // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
#define FEELER_SLEEP_WINDOW 1 #define FEELER_SLEEP_WINDOW 1
@@ -2847,8 +2850,8 @@ void CConnman::RecordBytesSent(uint64_t bytes)
LOCK(cs_totalBytesSent); LOCK(cs_totalBytesSent);
nTotalBytesSent += bytes; nTotalBytesSent += bytes;
uint64_t now = GetTime(); const auto now = GetTime<std::chrono::seconds>();
if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now) if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now)
{ {
// timeframe expired, reset cycle // timeframe expired, reset cycle
nMaxOutboundCycleStartTime = now; nMaxOutboundCycleStartTime = now;
@@ -2859,48 +2862,29 @@ void CConnman::RecordBytesSent(uint64_t bytes)
nMaxOutboundTotalBytesSentInCycle += bytes; nMaxOutboundTotalBytesSentInCycle += bytes;
} }
void CConnman::SetMaxOutboundTarget(uint64_t limit)
{
LOCK(cs_totalBytesSent);
nMaxOutboundLimit = limit;
}
uint64_t CConnman::GetMaxOutboundTarget() uint64_t CConnman::GetMaxOutboundTarget()
{ {
LOCK(cs_totalBytesSent); LOCK(cs_totalBytesSent);
return nMaxOutboundLimit; return nMaxOutboundLimit;
} }
uint64_t CConnman::GetMaxOutboundTimeframe() std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
{ {
LOCK(cs_totalBytesSent); return MAX_UPLOAD_TIMEFRAME;
return nMaxOutboundTimeframe;
} }
uint64_t CConnman::GetMaxOutboundTimeLeftInCycle() std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
{ {
LOCK(cs_totalBytesSent); LOCK(cs_totalBytesSent);
if (nMaxOutboundLimit == 0) if (nMaxOutboundLimit == 0)
return 0; return 0s;
if (nMaxOutboundCycleStartTime == 0) if (nMaxOutboundCycleStartTime.count() == 0)
return nMaxOutboundTimeframe; return MAX_UPLOAD_TIMEFRAME;
uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe; const std::chrono::seconds cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME;
uint64_t now = GetTime(); const auto now = GetTime<std::chrono::seconds>();
return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime(); return (cycleEndTime < now) ? 0s : cycleEndTime - now;
}
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) bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
@@ -2912,8 +2896,8 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
if (historicalBlockServingLimit) if (historicalBlockServingLimit)
{ {
// keep a large enough buffer to at least relay each block once // keep a large enough buffer to at least relay each block once
uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle(); const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE; const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MAX_BLOCK_SERIALIZED_SIZE;
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer) if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
return true; return true;
} }
+6 -16
View File
@@ -75,9 +75,7 @@ static const bool DEFAULT_UPNP = false;
/** The maximum number of peer connections to maintain. */ /** The maximum number of peer connections to maintain. */
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125; static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125;
/** The default for -maxuploadtarget. 0 = Unlimited */ /** The default for -maxuploadtarget. 0 = Unlimited */
static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0; static constexpr 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*/ /** Default for blocks only*/
static const bool DEFAULT_BLOCKSONLY = false; static const bool DEFAULT_BLOCKSONLY = false;
/** -peertimeout default */ /** -peertimeout default */
@@ -207,7 +205,6 @@ public:
BanMan* m_banman = nullptr; BanMan* m_banman = nullptr;
unsigned int nSendBufferMaxSize = 0; unsigned int nSendBufferMaxSize = 0;
unsigned int nReceiveFloodSize = 0; unsigned int nReceiveFloodSize = 0;
uint64_t nMaxOutboundTimeframe = 0;
uint64_t nMaxOutboundLimit = 0; uint64_t nMaxOutboundLimit = 0;
int64_t m_peer_connect_timeout = DEFAULT_PEER_CONNECT_TIMEOUT; int64_t m_peer_connect_timeout = DEFAULT_PEER_CONNECT_TIMEOUT;
std::vector<std::string> vSeedNodes; std::vector<std::string> vSeedNodes;
@@ -239,7 +236,6 @@ public:
m_peer_connect_timeout = connOptions.m_peer_connect_timeout; m_peer_connect_timeout = connOptions.m_peer_connect_timeout;
{ {
LOCK(cs_totalBytesSent); LOCK(cs_totalBytesSent);
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
nMaxOutboundLimit = connOptions.nMaxOutboundLimit; nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
} }
vWhitelistedRange = connOptions.vWhitelistedRange; vWhitelistedRange = connOptions.vWhitelistedRange;
@@ -366,13 +362,8 @@ public:
//! that peer during `net_processing.cpp:PushNodeVersion()`. //! that peer during `net_processing.cpp:PushNodeVersion()`.
ServiceFlags GetLocalServices() const; ServiceFlags GetLocalServices() const;
//!set the max outbound target in bytes
void SetMaxOutboundTarget(uint64_t limit);
uint64_t GetMaxOutboundTarget(); uint64_t GetMaxOutboundTarget();
std::chrono::seconds GetMaxOutboundTimeframe();
//!set the timeframe for the max outbound target
void SetMaxOutboundTimeframe(uint64_t timeframe);
uint64_t GetMaxOutboundTimeframe();
//! check if the outbound target is reached //! check if the outbound target is reached
//! if param historicalBlockServingLimit is set true, the function will //! if param historicalBlockServingLimit is set true, the function will
@@ -383,9 +374,9 @@ public:
//! in case of no limit, it will always response 0 //! in case of no limit, it will always response 0
uint64_t GetOutboundTargetBytesLeft(); uint64_t GetOutboundTargetBytesLeft();
//! response the time in second left in the current max outbound cycle //! returns the time left in the current max outbound cycle
//! in case of no limit, it will always response 0 //! in case of no limit, it will always return 0
uint64_t GetMaxOutboundTimeLeftInCycle(); std::chrono::seconds GetMaxOutboundTimeLeftInCycle();
uint64_t GetTotalBytesRecv(); uint64_t GetTotalBytesRecv();
uint64_t GetTotalBytesSent(); uint64_t GetTotalBytesSent();
@@ -484,9 +475,8 @@ private:
// outbound limit & stats // outbound limit & stats
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0}; 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); uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent);
// P2P timeout in seconds // P2P timeout in seconds
int64_t m_peer_connect_timeout; int64_t m_peer_connect_timeout;
+2 -2
View File
@@ -487,12 +487,12 @@ static RPCHelpMan getnettotals()
obj.pushKV("timemillis", GetTimeMillis()); obj.pushKV("timemillis", GetTimeMillis());
UniValue outboundLimit(UniValue::VOBJ); 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", node.connman->GetMaxOutboundTarget());
outboundLimit.pushKV("target_reached", node.connman->OutboundTargetReached(false)); outboundLimit.pushKV("target_reached", node.connman->OutboundTargetReached(false));
outboundLimit.pushKV("serve_historical_blocks", !node.connman->OutboundTargetReached(true)); outboundLimit.pushKV("serve_historical_blocks", !node.connman->OutboundTargetReached(true));
outboundLimit.pushKV("bytes_left_in_cycle", node.connman->GetOutboundTargetBytesLeft()); 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); obj.pushKV("uploadtarget", outboundLimit);
return obj; return obj;
}, },
+3 -9
View File
@@ -31,7 +31,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
CSubNet random_subnet; CSubNet random_subnet;
std::string random_string; std::string random_string;
while (fuzzed_data_provider.ConsumeBool()) { while (fuzzed_data_provider.ConsumeBool()) {
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 30)) { switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 28)) {
case 0: case 0:
random_address = ConsumeAddress(fuzzed_data_provider); random_address = ConsumeAddress(fuzzed_data_provider);
break; break;
@@ -127,18 +127,12 @@ void test_one_input(const std::vector<uint8_t>& buffer)
connman.SetBestHeight(fuzzed_data_provider.ConsumeIntegral<int>()); connman.SetBestHeight(fuzzed_data_provider.ConsumeIntegral<int>());
break; break;
case 26: case 26:
connman.SetMaxOutboundTarget(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
break;
case 27:
connman.SetMaxOutboundTimeframe(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
break;
case 28:
connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool()); connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
break; break;
case 29: case 27:
connman.SetServices(random_service, static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>())); connman.SetServices(random_service, static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
break; break;
case 30: case 28:
connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool()); connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
break; break;
} }