Skip to content

Commit

Permalink
Add logging and addr rate limiting statistics
Browse files Browse the repository at this point in the history
Includes logging improvements by Vasil Dimov and John Newbery.

Github-Pull: bitcoin#22387
Rebased-From: f424d60
  • Loading branch information
sipa committed Jul 29, 2021
1 parent b65129e commit 651216e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ struct CNodeState {
double m_addr_token_bucket{1.0};
/** When m_addr_token_bucket was last updated */
std::chrono::microseconds m_addr_token_timestamp{GetTime<std::chrono::microseconds>()};
/** Total number of addresses that were dropped due to rate limiting. */
std::atomic<uint64_t> m_addr_rate_limited{0};
/** Total number of addresses that were processed (excludes rate limited ones). */
std::atomic<uint64_t> m_addr_processed{0};

CNodeState(CAddress addrIn, bool is_inbound, bool is_manual)
: address(addrIn), m_is_inbound(is_inbound), m_is_manual_connection(is_manual)
Expand Down Expand Up @@ -914,6 +918,8 @@ bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
if (queue.pindex)
stats.vHeightInFlight.push_back(queue.pindex->nHeight);
}
stats.m_addr_processed = state->m_addr_processed.load();
stats.m_addr_rate_limited = state->m_addr_rate_limited.load();
}

PeerRef peer = GetPeerRef(nodeid);
Expand Down Expand Up @@ -2612,6 +2618,8 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
// Update/increment addr rate limiting bucket.
const auto current_time = GetTime<std::chrono::microseconds>();
size_t to_process;
uint64_t num_proc = 0;
uint64_t num_rate_limit = 0;
{
LOCK(cs_main);
CNodeState* state = State(pfrom.GetId());
Expand All @@ -2624,6 +2632,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
state->m_addr_token_timestamp = current_time;

to_process = std::min<size_t>(std::floor(state->m_addr_token_bucket), vAddr.size());
num_rate_limit = vAddr.size() - to_process;
state->m_addr_token_bucket -= to_process;
}

Expand Down Expand Up @@ -2652,6 +2661,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
// Do not process banned/discouraged addresses beyond remembering we received them
continue;
}
++num_proc;
bool fReachable = IsReachable(addr);
if (addr.nTime > nSince && !pfrom.fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
{
Expand All @@ -2662,6 +2672,20 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
if (fReachable)
vAddrOk.push_back(addr);
}
{
LOCK(cs_main);
CNodeState* state = State(pfrom.GetId());
state->m_addr_processed += num_proc;
state->m_addr_rate_limited += num_rate_limit;
}
LogPrint(BCLog::NET, "Received addr: %u addresses (%u processed, %u rate-limited) from peer=%d%s\n",
vAddr.size(),
num_proc,
num_rate_limit,
pfrom.GetId(),
fLogIPs ? ", peeraddr=" + pfrom.addr.ToString() : "");


m_connman.AddNewAddresses(vAddrOk, pfrom.addr, 2 * 60 * 60);
if (vAddr.size() < 1000)
pfrom.fGetAddr = false;
Expand Down
2 changes: 2 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ struct CNodeStateStats {
int nSyncHeight = -1;
int nCommonHeight = -1;
std::vector<int> vHeightInFlight;
uint64_t m_addr_processed = 0;
uint64_t m_addr_rate_limited = 0;
};

/** Get statistics from node state */
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ static RPCHelpMan getpeerinfo()
heights.push_back(height);
}
obj.pushKV("inflight", heights);
obj.pushKV("addr_processed", statestats.m_addr_processed);
obj.pushKV("addr_rate_limited", statestats.m_addr_rate_limited);
}
if (IsDeprecatedRPCEnabled("whitelisted")) {
// whitelisted is deprecated in v0.21 for removal in v0.22
Expand Down

0 comments on commit 651216e

Please sign in to comment.