Skip to content

Commit 0475c8b

Browse files
committed
net: use std::chrono throughout maxOutbound logic
1 parent f805933 commit 0475c8b

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

src/net.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
7373
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers
7474

7575
/** The default timeframe for -maxuploadtarget. 1 day. */
76-
static constexpr uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
76+
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24};
7777

7878
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
7979
#define FEELER_SLEEP_WINDOW 1
@@ -2850,7 +2850,7 @@ void CConnman::RecordBytesSent(uint64_t bytes)
28502850
LOCK(cs_totalBytesSent);
28512851
nTotalBytesSent += bytes;
28522852

2853-
uint64_t now = GetTime();
2853+
const auto now = GetTime<std::chrono::seconds>();
28542854
if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now)
28552855
{
28562856
// timeframe expired, reset cycle
@@ -2868,23 +2868,23 @@ uint64_t CConnman::GetMaxOutboundTarget()
28682868
return nMaxOutboundLimit;
28692869
}
28702870

2871-
uint64_t CConnman::GetMaxOutboundTimeframe()
2871+
std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
28722872
{
28732873
return MAX_UPLOAD_TIMEFRAME;
28742874
}
28752875

2876-
uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2876+
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
28772877
{
28782878
LOCK(cs_totalBytesSent);
28792879
if (nMaxOutboundLimit == 0)
2880-
return 0;
2880+
return 0s;
28812881

2882-
if (nMaxOutboundCycleStartTime == 0)
2882+
if (nMaxOutboundCycleStartTime.count() == 0)
28832883
return MAX_UPLOAD_TIMEFRAME;
28842884

2885-
uint64_t cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME;
2886-
uint64_t now = GetTime();
2887-
return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2885+
const std::chrono::seconds cycleEndTime = nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME;
2886+
const auto now = GetTime<std::chrono::seconds>();
2887+
return (cycleEndTime < now) ? 0s : cycleEndTime - now;
28882888
}
28892889

28902890
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
@@ -2896,8 +2896,8 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
28962896
if (historicalBlockServingLimit)
28972897
{
28982898
// keep a large enough buffer to at least relay each block once
2899-
uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2900-
uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SERIALIZED_SIZE;
2899+
const std::chrono::seconds timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2900+
const uint64_t buffer = timeLeftInCycle / std::chrono::minutes{10} * MAX_BLOCK_SERIALIZED_SIZE;
29012901
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
29022902
return true;
29032903
}

src/net.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ class CConnman
363363
ServiceFlags GetLocalServices() const;
364364

365365
uint64_t GetMaxOutboundTarget();
366-
uint64_t GetMaxOutboundTimeframe();
366+
std::chrono::seconds GetMaxOutboundTimeframe();
367367

368368
//! check if the outbound target is reached
369369
//! if param historicalBlockServingLimit is set true, the function will
@@ -374,9 +374,9 @@ class CConnman
374374
//! in case of no limit, it will always response 0
375375
uint64_t GetOutboundTargetBytesLeft();
376376

377-
//! response the time in second left in the current max outbound cycle
378-
//! in case of no limit, it will always response 0
379-
uint64_t GetMaxOutboundTimeLeftInCycle();
377+
//! returns the time left in the current max outbound cycle
378+
//! in case of no limit, it will always return 0
379+
std::chrono::seconds GetMaxOutboundTimeLeftInCycle();
380380

381381
uint64_t GetTotalBytesRecv();
382382
uint64_t GetTotalBytesSent();
@@ -475,7 +475,7 @@ class CConnman
475475

476476
// outbound limit & stats
477477
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0};
478-
uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};
478+
std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};
479479
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
480480

481481
// P2P timeout in seconds

src/rpc/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,12 +487,12 @@ static RPCHelpMan getnettotals()
487487
obj.pushKV("timemillis", GetTimeMillis());
488488

489489
UniValue outboundLimit(UniValue::VOBJ);
490-
outboundLimit.pushKV("timeframe", node.connman->GetMaxOutboundTimeframe());
490+
outboundLimit.pushKV("timeframe", count_seconds(node.connman->GetMaxOutboundTimeframe()));
491491
outboundLimit.pushKV("target", node.connman->GetMaxOutboundTarget());
492492
outboundLimit.pushKV("target_reached", node.connman->OutboundTargetReached(false));
493493
outboundLimit.pushKV("serve_historical_blocks", !node.connman->OutboundTargetReached(true));
494494
outboundLimit.pushKV("bytes_left_in_cycle", node.connman->GetOutboundTargetBytesLeft());
495-
outboundLimit.pushKV("time_left_in_cycle", node.connman->GetMaxOutboundTimeLeftInCycle());
495+
outboundLimit.pushKV("time_left_in_cycle", count_seconds(node.connman->GetMaxOutboundTimeLeftInCycle()));
496496
obj.pushKV("uploadtarget", outboundLimit);
497497
return obj;
498498
},

0 commit comments

Comments
 (0)