0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

net: use std::chrono throughout maxOutbound logic

This commit is contained in:
fanquake 2020-10-24 19:13:42 +08:00
parent f805933e70
commit 0475c8ba4d
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
3 changed files with 18 additions and 18 deletions

View file

@ -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 static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers
/** The default timeframe for -maxuploadtarget. 1 day. */ /** 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. // 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
@ -2850,7 +2850,7 @@ 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 + MAX_UPLOAD_TIMEFRAME < now) if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now)
{ {
// timeframe expired, reset cycle // timeframe expired, reset cycle
@ -2868,23 +2868,23 @@ uint64_t CConnman::GetMaxOutboundTarget()
return nMaxOutboundLimit; return nMaxOutboundLimit;
} }
uint64_t CConnman::GetMaxOutboundTimeframe() std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
{ {
return MAX_UPLOAD_TIMEFRAME; return MAX_UPLOAD_TIMEFRAME;
} }
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 MAX_UPLOAD_TIMEFRAME; return MAX_UPLOAD_TIMEFRAME;
uint64_t cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME; 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;
} }
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit) bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
@ -2896,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;
} }

View file

@ -363,7 +363,7 @@ public:
ServiceFlags GetLocalServices() const; ServiceFlags GetLocalServices() const;
uint64_t GetMaxOutboundTarget(); uint64_t GetMaxOutboundTarget();
uint64_t GetMaxOutboundTimeframe(); std::chrono::seconds 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
@ -374,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();
@ -475,7 +475,7 @@ 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);
// P2P timeout in seconds // P2P timeout in seconds

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;
}, },