Skip to content

Commit 872fee3

Browse files
committed
Introduce -maxuploadtarget
* -maxuploadtarget can be set in MiB * if <limit> - ( time-left-in-24h-cycle / 600 * MAX_BLOCK_SIZE ) has reach, stop serve blocks older than one week and filtered blocks * no action if limit has reached, no guarantee that the target will not be surpassed * add outbound limit informations to rpc getnettotals
1 parent 867d6c9 commit 872fee3

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

src/init.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ std::string HelpMessage(HelpMessageMode mode)
369369
strUsage += HelpMessageOpt("-whitebind=<addr>", _("Bind to given address and whitelist peers connecting to it. Use [host]:port notation for IPv6"));
370370
strUsage += HelpMessageOpt("-whitelist=<netmask>", _("Whitelist peers connecting from the given netmask or IP address. Can be specified multiple times.") +
371371
" " + _("Whitelisted peers cannot be DoS banned and their transactions are always relayed, even if they are already in the mempool, useful e.g. for a gateway"));
372+
strUsage += HelpMessageOpt("-maxuploadtarget=<n>", strprintf(_("Tries to keep outbound traffic under the given target (in MiB per 24h), 0 = no limit (default: %d)"), 0));
372373

373374
#ifdef ENABLE_WALLET
374375
strUsage += HelpMessageGroup(_("Wallet options:"));
@@ -1174,6 +1175,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11741175
RegisterValidationInterface(pzmqNotificationInterface);
11751176
}
11761177
#endif
1178+
if (mapArgs.count("-maxuploadtarget")) {
1179+
CNode::SetMaxOutboundTarget(GetArg("-maxuploadtarget", 0)*1024*1024);
1180+
}
11771181

11781182
// ********************************************************* Step 7: load block chain
11791183

src/main.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3805,6 +3805,16 @@ void static ProcessGetData(CNode* pfrom)
38053805
}
38063806
}
38073807
}
3808+
// disconnect node in case we have reached the outbound limit for serving historical blocks
3809+
static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
3810+
if (send && CNode::OutboundTargetReached(true) && ( ((pindexBestHeader != NULL) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) )
3811+
{
3812+
LogPrint("net", "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
3813+
3814+
//disconnect node
3815+
pfrom->fDisconnect = true;
3816+
send = false;
3817+
}
38083818
// Pruned nodes may have deleted the block, so check whether
38093819
// it's available before trying to send.
38103820
if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))

src/net.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "addrman.h"
1313
#include "chainparams.h"
1414
#include "clientversion.h"
15+
#include "consensus/consensus.h"
1516
#include "crypto/common.h"
1617
#include "hash.h"
1718
#include "primitives/transaction.h"
@@ -326,6 +327,11 @@ uint64_t CNode::nTotalBytesSent = 0;
326327
CCriticalSection CNode::cs_totalBytesRecv;
327328
CCriticalSection CNode::cs_totalBytesSent;
328329

330+
uint64_t CNode::nMaxOutboundLimit = 0;
331+
uint64_t CNode::nMaxOutboundTotalBytesSentInCycle = 0;
332+
uint64_t CNode::nMaxOutboundTimeframe = 60*60*24; //1 day
333+
uint64_t CNode::nMaxOutboundCycleStartTime = 0;
334+
329335
CNode* FindNode(const CNetAddr& ip)
330336
{
331337
LOCK(cs_vNodes);
@@ -2083,6 +2089,94 @@ void CNode::RecordBytesSent(uint64_t bytes)
20832089
{
20842090
LOCK(cs_totalBytesSent);
20852091
nTotalBytesSent += bytes;
2092+
2093+
uint64_t now = GetTime();
2094+
if (nMaxOutboundCycleStartTime + nMaxOutboundTimeframe < now)
2095+
{
2096+
// timeframe expired, reset cycle
2097+
nMaxOutboundCycleStartTime = now;
2098+
nMaxOutboundTotalBytesSentInCycle = 0;
2099+
}
2100+
2101+
// TODO, exclude whitebind peers
2102+
nMaxOutboundTotalBytesSentInCycle += bytes;
2103+
}
2104+
2105+
void CNode::SetMaxOutboundTarget(uint64_t limit)
2106+
{
2107+
LOCK(cs_totalBytesSent);
2108+
uint64_t recommendedMinimum = (nMaxOutboundTimeframe / 600) * MAX_BLOCK_SIZE;
2109+
nMaxOutboundLimit = limit;
2110+
2111+
if (limit < recommendedMinimum)
2112+
LogPrintf("Max outbound target is very small (%s) and will be overshot. Recommended minimum is %s\n.", nMaxOutboundLimit, recommendedMinimum);
2113+
}
2114+
2115+
uint64_t CNode::GetMaxOutboundTarget()
2116+
{
2117+
LOCK(cs_totalBytesSent);
2118+
return nMaxOutboundLimit;
2119+
}
2120+
2121+
uint64_t CNode::GetMaxOutboundTimeframe()
2122+
{
2123+
LOCK(cs_totalBytesSent);
2124+
return nMaxOutboundTimeframe;
2125+
}
2126+
2127+
uint64_t CNode::GetMaxOutboundTimeLeftInCycle()
2128+
{
2129+
LOCK(cs_totalBytesSent);
2130+
if (nMaxOutboundLimit == 0)
2131+
return 0;
2132+
2133+
if (nMaxOutboundCycleStartTime == 0)
2134+
return nMaxOutboundTimeframe;
2135+
2136+
uint64_t cycleEndTime = nMaxOutboundCycleStartTime + nMaxOutboundTimeframe;
2137+
uint64_t now = GetTime();
2138+
return (cycleEndTime < now) ? 0 : cycleEndTime - GetTime();
2139+
}
2140+
2141+
void CNode::SetMaxOutboundTimeframe(uint64_t timeframe)
2142+
{
2143+
LOCK(cs_totalBytesSent);
2144+
if (nMaxOutboundTimeframe != timeframe)
2145+
{
2146+
// reset measure-cycle in case of changing
2147+
// the timeframe
2148+
nMaxOutboundCycleStartTime = GetTime();
2149+
}
2150+
nMaxOutboundTimeframe = timeframe;
2151+
}
2152+
2153+
bool CNode::OutboundTargetReached(bool historicalBlockServingLimit)
2154+
{
2155+
LOCK(cs_totalBytesSent);
2156+
if (nMaxOutboundLimit == 0)
2157+
return false;
2158+
2159+
if (historicalBlockServingLimit)
2160+
{
2161+
// keep a large enought buffer to at least relay each block once
2162+
uint64_t timeLeftInCycle = GetMaxOutboundTimeLeftInCycle();
2163+
uint64_t buffer = timeLeftInCycle / 600 * MAX_BLOCK_SIZE;
2164+
if (buffer >= nMaxOutboundLimit || nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit - buffer)
2165+
return true;
2166+
}
2167+
else if (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit)
2168+
return true;
2169+
2170+
return false;
2171+
}
2172+
2173+
uint64_t CNode::GetOutboundTargetBytesLeft()
2174+
{
2175+
LOCK(cs_totalBytesSent);
2176+
if (nMaxOutboundLimit == 0)
2177+
return 0;
2178+
2179+
return (nMaxOutboundTotalBytesSentInCycle >= nMaxOutboundLimit) ? 0 : nMaxOutboundLimit - nMaxOutboundTotalBytesSentInCycle;
20862180
}
20872181

20882182
uint64_t CNode::GetTotalBytesRecv()

src/net.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,12 @@ class CNode
400400
static uint64_t nTotalBytesRecv;
401401
static uint64_t nTotalBytesSent;
402402

403+
// outbound limit & stats
404+
static uint64_t nMaxOutboundTotalBytesSentInCycle;
405+
static uint64_t nMaxOutboundCycleStartTime;
406+
static uint64_t nMaxOutboundLimit;
407+
static uint64_t nMaxOutboundTimeframe;
408+
403409
CNode(const CNode&);
404410
void operator=(const CNode&);
405411

@@ -701,6 +707,27 @@ class CNode
701707

702708
static uint64_t GetTotalBytesRecv();
703709
static uint64_t GetTotalBytesSent();
710+
711+
//!set the max outbound target in bytes
712+
static void SetMaxOutboundTarget(uint64_t limit);
713+
static uint64_t GetMaxOutboundTarget();
714+
715+
//!set the timeframe for the max outbound target
716+
static void SetMaxOutboundTimeframe(uint64_t timeframe);
717+
static uint64_t GetMaxOutboundTimeframe();
718+
719+
//!check if the outbound target is reached
720+
// if param historicalBlockServingLimit is set true, the function will
721+
// response true if the limit for serving historical blocks has been reached
722+
static bool OutboundTargetReached(bool historicalBlockServingLimit);
723+
724+
//!response the bytes left in the current max outbound cycle
725+
// in case of no limit, it will always response 0
726+
static uint64_t GetOutboundTargetBytesLeft();
727+
728+
//!response the time in second left in the current max outbound cycle
729+
// in case of no limit, it will always response 0
730+
static uint64_t GetMaxOutboundTimeLeftInCycle();
704731
};
705732

706733

src/rpcnet.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ UniValue getnettotals(const UniValue& params, bool fHelp)
379379
obj.push_back(Pair("totalbytesrecv", CNode::GetTotalBytesRecv()));
380380
obj.push_back(Pair("totalbytessent", CNode::GetTotalBytesSent()));
381381
obj.push_back(Pair("timemillis", GetTimeMillis()));
382+
383+
UniValue outboundLimit(UniValue::VOBJ);
384+
outboundLimit.push_back(Pair("timeframe", CNode::GetMaxOutboundTimeframe()));
385+
outboundLimit.push_back(Pair("target", CNode::GetMaxOutboundTarget()));
386+
outboundLimit.push_back(Pair("target_reached", CNode::OutboundTargetReached(false)));
387+
outboundLimit.push_back(Pair("serve_historical_blocks", !CNode::OutboundTargetReached(true)));
388+
outboundLimit.push_back(Pair("bytes_left_in_cycle", CNode::GetOutboundTargetBytesLeft()));
389+
outboundLimit.push_back(Pair("time_left_in_cycle", CNode::GetMaxOutboundTimeLeftInCycle()));
390+
obj.push_back(Pair("uploadtarget", outboundLimit));
382391
return obj;
383392
}
384393

0 commit comments

Comments
 (0)