Skip to content

Commit f1f2418

Browse files
committed
Merge #20253: net: use std::chrono throughout maxOutbound logic
0475c8b net: use std::chrono throughout maxOutbound logic (fanquake) f805933 init: set nMaxOutboundLimit connection option directly (fanquake) 173d0d3 net: remove nMaxOutboundTimeframe from connection options (fanquake) b117eb1 net: remove SetMaxOutboundTimeframe (fanquake) 2f3f1ae 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 0475c8b MarcoFalke: review ACK 0475c8b 🎭 Tree-SHA512: 5a6d5b61e0d4c08a235cfc0257dae65d09a5df019d8d230b1a58a3e2483ddf4a31efdefc885c4a02e4715e4180b0ed92ebc0a1c08b2bf476a391945114593514
2 parents 33d6337 + 0475c8b commit f1f2418

File tree

5 files changed

+28
-67
lines changed

5 files changed

+28
-67
lines changed

src/init.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,12 +1513,6 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
15131513
RegisterValidationInterface(g_zmq_notification_interface);
15141514
}
15151515
#endif
1516-
uint64_t nMaxOutboundLimit = 0; //unlimited unless -maxuploadtarget is set
1517-
uint64_t nMaxOutboundTimeframe = MAX_UPLOAD_TIMEFRAME;
1518-
1519-
if (args.IsArgSet("-maxuploadtarget")) {
1520-
nMaxOutboundLimit = args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET) * 1024 * 1024;
1521-
}
15221516

15231517
// ********************************************************* Step 7: load block chain
15241518

@@ -1921,8 +1915,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
19211915
connOptions.nReceiveFloodSize = 1000 * args.GetArg("-maxreceivebuffer", DEFAULT_MAXRECEIVEBUFFER);
19221916
connOptions.m_added_nodes = args.GetArgs("-addnode");
19231917

1924-
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
1925-
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
1918+
connOptions.nMaxOutboundLimit = 1024 * 1024 * args.GetArg("-maxuploadtarget", DEFAULT_MAX_UPLOAD_TARGET);
19261919
connOptions.m_peer_connect_timeout = peer_connect_timeout;
19271920

19281921
for (const std::string& bind_arg : args.GetArgs("-bind")) {

src/net.cpp

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ static constexpr std::chrono::seconds DNSSEEDS_DELAY_FEW_PEERS{11};
7272
static constexpr std::chrono::minutes DNSSEEDS_DELAY_MANY_PEERS{5};
7373
static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" peers
7474

75+
/** The default timeframe for -maxuploadtarget. 1 day. */
76+
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24};
77+
7578
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
7679
#define FEELER_SLEEP_WINDOW 1
7780

@@ -2847,8 +2850,8 @@ void CConnman::RecordBytesSent(uint64_t bytes)
28472850
LOCK(cs_totalBytesSent);
28482851
nTotalBytesSent += bytes;
28492852

2850-
uint64_t now = GetTime();
2851-
if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2853+
const auto now = GetTime<std::chrono::seconds>();
2854+
if (nMaxOutboundCycleStartTime + MAX_UPLOAD_TIMEFRAME < now)
28522855
{
28532856
// timeframe expired, reset cycle
28542857
nMaxOutboundCycleStartTime = now;
@@ -2859,48 +2862,29 @@ void CConnman::RecordBytesSent(uint64_t bytes)
28592862
nMaxOutboundTotalBytesSentInCycle += bytes;
28602863
}
28612864

2862-
void CConnman::SetMaxOutboundTarget(uint64_t limit)
2863-
{
2864-
LOCK(cs_totalBytesSent);
2865-
nMaxOutboundLimit = limit;
2866-
}
2867-
28682865
uint64_t CConnman::GetMaxOutboundTarget()
28692866
{
28702867
LOCK(cs_totalBytesSent);
28712868
return nMaxOutboundLimit;
28722869
}
28732870

2874-
uint64_t CConnman::GetMaxOutboundTimeframe()
2871+
std::chrono::seconds CConnman::GetMaxOutboundTimeframe()
28752872
{
2876-
LOCK(cs_totalBytesSent);
2877-
return nMaxOutboundTimeframe;
2873+
return MAX_UPLOAD_TIMEFRAME;
28782874
}
28792875

2880-
uint64_t CConnman::GetMaxOutboundTimeLeftInCycle()
2876+
std::chrono::seconds CConnman::GetMaxOutboundTimeLeftInCycle()
28812877
{
28822878
LOCK(cs_totalBytesSent);
28832879
if (nMaxOutboundLimit == 0)
2884-
return 0;
2880+
return 0s;
28852881

2886-
if (nMaxOutboundCycleStartTime == 0)
2887-
return nMaxOutboundTimeframe;
2882+
if (nMaxOutboundCycleStartTime.count() == 0)
2883+
return MAX_UPLOAD_TIMEFRAME;
28882884

2889-
uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2890-
uint64_t now = GetTime();
2891-
return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2892-
}
2893-
2894-
void CConnman::SetMaxOutboundTimeframe(uint64_t timeframe)
2895-
{
2896-
LOCK(cs_totalBytesSent);
2897-
if (nMaxOutboundTimeframe != timeframe)
2898-
{
2899-
// reset measure-cycle in case of changing
2900-
// the timeframe
2901-
nMaxOutboundCycleStartTime = GetTime();
2902-
}
2903-
nMaxOutboundTimeframe = timeframe;
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;
29042888
}
29052889

29062890
bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
@@ -2912,8 +2896,8 @@ bool CConnman::OutboundTargetReached(bool historicalBlockServingLimit)
29122896
if (historicalBlockServingLimit)
29132897
{
29142898
// keep a large enough buffer to at least relay each block once
2915-
uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2916-
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;
29172901
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
29182902
return true;
29192903
}

src/net.h

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ static const bool DEFAULT_UPNP = false;
7575
/** The maximum number of peer connections to maintain. */
7676
static const unsigned int DEFAULT_MAX_PEER_CONNECTIONS = 125;
7777
/** The default for -maxuploadtarget. 0 = Unlimited */
78-
static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
79-
/** The default timeframe for -maxuploadtarget. 1 day. */
80-
static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
78+
static constexpr uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
8179
/** Default for blocks only*/
8280
static const bool DEFAULT_BLOCKSONLY = false;
8381
/** -peertimeout default */
@@ -207,7 +205,6 @@ class CConnman
207205
BanMan* m_banman = nullptr;
208206
unsigned int nSendBufferMaxSize = 0;
209207
unsigned int nReceiveFloodSize = 0;
210-
uint64_t nMaxOutboundTimeframe = 0;
211208
uint64_t nMaxOutboundLimit = 0;
212209
int64_t m_peer_connect_timeout = DEFAULT_PEER_CONNECT_TIMEOUT;
213210
std::vector<std::string> vSeedNodes;
@@ -239,7 +236,6 @@ class CConnman
239236
m_peer_connect_timeout = connOptions.m_peer_connect_timeout;
240237
{
241238
LOCK(cs_totalBytesSent);
242-
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
243239
nMaxOutboundLimit = connOptions.nMaxOutboundLimit;
244240
}
245241
vWhitelistedRange = connOptions.vWhitelistedRange;
@@ -366,13 +362,8 @@ class CConnman
366362
//! that peer during `net_processing.cpp:PushNodeVersion()`.
367363
ServiceFlags GetLocalServices() const;
368364

369-
//!set the max outbound target in bytes
370-
void SetMaxOutboundTarget(uint64_t limit);
371365
uint64_t GetMaxOutboundTarget();
372-
373-
//!set the timeframe for the max outbound target
374-
void SetMaxOutboundTimeframe(uint64_t timeframe);
375-
uint64_t GetMaxOutboundTimeframe();
366+
std::chrono::seconds GetMaxOutboundTimeframe();
376367

377368
//! check if the outbound target is reached
378369
//! if param historicalBlockServingLimit is set true, the function will
@@ -383,9 +374,9 @@ class CConnman
383374
//! in case of no limit, it will always response 0
384375
uint64_t GetOutboundTargetBytesLeft();
385376

386-
//! response the time in second left in the current max outbound cycle
387-
//! in case of no limit, it will always response 0
388-
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();
389380

390381
uint64_t GetTotalBytesRecv();
391382
uint64_t GetTotalBytesSent();
@@ -484,9 +475,8 @@ class CConnman
484475

485476
// outbound limit & stats
486477
uint64_t nMaxOutboundTotalBytesSentInCycle GUARDED_BY(cs_totalBytesSent) {0};
487-
uint64_t nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};
478+
std::chrono::seconds nMaxOutboundCycleStartTime GUARDED_BY(cs_totalBytesSent) {0};
488479
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
489-
uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent);
490480

491481
// P2P timeout in seconds
492482
int64_t m_peer_connect_timeout;

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

src/test/fuzz/connman.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void test_one_input(const std::vector<uint8_t>& buffer)
3131
CSubNet random_subnet;
3232
std::string random_string;
3333
while (fuzzed_data_provider.ConsumeBool()) {
34-
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 30)) {
34+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 28)) {
3535
case 0:
3636
random_address = ConsumeAddress(fuzzed_data_provider);
3737
break;
@@ -127,18 +127,12 @@ void test_one_input(const std::vector<uint8_t>& buffer)
127127
connman.SetBestHeight(fuzzed_data_provider.ConsumeIntegral<int>());
128128
break;
129129
case 26:
130-
connman.SetMaxOutboundTarget(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
131-
break;
132-
case 27:
133-
connman.SetMaxOutboundTimeframe(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
134-
break;
135-
case 28:
136130
connman.SetNetworkActive(fuzzed_data_provider.ConsumeBool());
137131
break;
138-
case 29:
132+
case 27:
139133
connman.SetServices(random_service, static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()));
140134
break;
141-
case 30:
135+
case 28:
142136
connman.SetTryNewOutboundPeer(fuzzed_data_provider.ConsumeBool());
143137
break;
144138
}

0 commit comments

Comments
 (0)