Skip to content

Commit

Permalink
increase signal-to-noise ratio in debug.log by adjusting log level wh…
Browse files Browse the repository at this point in the history
…en logging failed non-manual connect():s

Before this patch:

```
$ src/bitcoind -printtoconsole
…
2018-02-28 18:42:51 UpdateTip: new best=0000000000005448b10a219683d34b770a28044e1cc421032dea1a79ff548948 height=1286903 version=0x20000000 log2_work=69.791313 tx=17408546 date='2018-02-28 18:42:46' progress=1.000000 cache=0.0MiB(173txo)
2018-02-28T18:37:52Z connect() 10.11.21.34:18333 failed after select(): Connection refused (111)
2018-02-28 18:43:22 connect() to 10.11.43.14:18333 failed after select(): Network is unreachable (101)
2018-02-28 18:44:49 UpdateTip: new best=000000000000029a521ff2803e1441b09413b876accff5084a4cccf7747d798b height=1286904 version=0x20000000 log2_work=69.791345 tx=17408559 date='2018-02-28 18:44:51' progress=1.000000 cache=0.1MiB(502txo)
2018-02-28 18:46:54 connect() to [2001:0:9d38:78ff:1234:1234:1234:1234]:18333 failed: Network is unreachable (101)
2018-02-28 18:48:56 connect() to [2001:0:9d38:6aff:1234:1234:1234:1234]:18333 failed: Network is unreachable (101)
2018-02-28 18:49:11 UpdateTip: new best=000000000000000206b79eb235e5dd907b6369de0e5d764330bf40ec0d460311 height=1286905 version=0x20000000 log2_work=69.791377 tx=17408577 date='2018-02-28 18:49:12' progress=1.000000 cache=1.0MiB(5245txo)
```

After this patch:

```
$ src/bitcoind -printtoconsole
…
2018-02-28 18:42:51 UpdateTip: new best=0000000000005448b10a219683d34b770a28044e1cc421032dea1a79ff548948 height=1286903 version=0x20000000 log2_work=69.791313 tx=17408546 date='2018-02-28 18:42:46' progress=1.000000 cache=0.0MiB(173txo)
2018-02-28 18:44:49 UpdateTip: new best=000000000000029a521ff2803e1441b09413b876accff5084a4cccf7747d798b height=1286904 version=0x20000000 log2_work=69.791345 tx=17408559 date='2018-02-28 18:44:51' progress=1.000000 cache=0.1MiB(502txo)
2018-02-28 18:49:11 UpdateTip: new best=000000000000000206b79eb235e5dd907b6369de0e5d764330bf40ec0d460311 height=1286905 version=0x20000000 log2_work=69.791377 tx=17408577 date='2018-02-28 18:49:12' progress=1.000000 cache=1.0MiB(5245txo)
```

Please note that "manual connect():s" (invoked via `-connect`, `-proxy` or `addnode`) are still reported as usual:

```
$ src/bitcoind -printtoconsole -connect=10.11.12.13
…
2018-02-28 18:33:13 connect() to 10.11.12.13:18333 failed after select(): Connection refused (111)
$ src/bitcoind -printtoconsole -proxy=10.11.12.13
…
2018-02-28 18:32:32 connect() to 10.11.12.13:9050 failed after select(): Connection refused (111)
$ src/bitcoind -printtoconsole &
$ src/bitcoin-cli addnode "10.11.12.13" onetry
…
2018-02-28 18:34:40 connect() to 10.11.12.13:18333 failed after select(): Connection refused (111)
```
  • Loading branch information
practicalswift authored and furszy committed Feb 10, 2022
1 parent 889ce4a commit 6f4e17c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/activemasternode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ void CActiveDeterministicMasternodeManager::Init(const CBlockIndex* pindexTip)
LogPrintf("%s -- ERROR: %s\n", __func__, strError);
return;
}
bool fConnected = ConnectSocketDirectly(info.service, hSocket, nConnectTimeout) && IsSelectableSocket(hSocket);
bool fConnected = ConnectSocketDirectly(info.service, hSocket, nConnectTimeout, true) && IsSelectableSocket(hSocket);
CloseSocket(hSocket);

if (!fConnected) {
Expand Down
10 changes: 5 additions & 5 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ bool CConnman::CheckIncomingNonce(uint64_t nonce)
return true;
}

CNode* CConnman::ConnectNode(CAddress addrConnect, const char* pszDest, bool fCountFailure)
CNode* CConnman::ConnectNode(CAddress addrConnect, const char* pszDest, bool fCountFailure, bool manual_connection)
{
if (pszDest == nullptr) {
if (IsLocal(addrConnect)) {
Expand Down Expand Up @@ -396,7 +396,7 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char* pszDest, bool fCo
if (hSocket == INVALID_SOCKET) {
return nullptr;
}
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout);
connected = ConnectSocketDirectly(addrConnect, hSocket, nConnectTimeout, manual_connection);
}
if (!proxyConnectionFailed) {
// If a connection to the node was attempted, and failure (if any) is not caused by a problem connecting to
Expand Down Expand Up @@ -1916,7 +1916,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
} else if (FindNode(pszDest))
return;

CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure, fAddnode);

if (!pnode)
return;
Expand Down Expand Up @@ -2769,9 +2769,9 @@ bool CConnman::IsNodeConnected(const CAddress& addr)
return FindNode(addr.ToStringIPPort());
}

CNode* CConnman::ConnectNode(CAddress addrConnect)
CNode* CConnman::ConnectNode(const CAddress& addrConnect)
{
return ConnectNode(addrConnect, nullptr, true);
return ConnectNode(addrConnect, nullptr, true, true);
}

// valid, reachable and routable address (except for RegTest)
Expand Down
4 changes: 2 additions & 2 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ class CConnman
void RelayInv(CInv& inv);
bool IsNodeConnected(const CAddress& addr);
// Retrieves a connected peer (if connection success). Used only to check peer address availability for now.
CNode* ConnectNode(CAddress addrConnect);
CNode* ConnectNode(const CAddress& addrConnect);

// Addrman functions
void SetServices(const CService &addr, ServiceFlags nServices);
Expand Down Expand Up @@ -386,7 +386,7 @@ class CConnman
CNode* FindNode(const CService& addr);

bool AttemptToEvictConnection(bool fPreferNewConnection);
CNode* ConnectNode(CAddress addrConnect, const char* pszDest, bool fCountFailure);
CNode* ConnectNode(CAddress addrConnect, const char* pszDest, bool fCountFailure, bool manual_connection);
bool IsWhitelistedRange(const CNetAddr &addr);

void DeleteNode(CNode* pnode);
Expand Down
19 changes: 15 additions & 4 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "sync.h"
#include "random.h"
#include "tinyformat.h"
#include "util/string.h"
#include "util/system.h"
#include "utilstrencodings.h"
Expand Down Expand Up @@ -519,7 +520,17 @@ SOCKET CreateSocket(const CService& addrConnect)
return hSocket;
}

bool ConnectSocketDirectly(const CService& addrConnect, const SOCKET& hSocket, int nTimeout)
template<typename... Args>
static void LogConnectFailure(bool manual_connection, const char* fmt, const Args&... args) {
std::string error_message = tfm::format(fmt, args...);
if (manual_connection) {
LogPrintf("%s\n", error_message);
} else {
LogPrint(BCLog::NET, "%s\n", error_message);
}
}

bool ConnectSocketDirectly(const CService& addrConnect, const SOCKET& hSocket, int nTimeout, bool manual_connection)
{
struct sockaddr_storage sockaddr;
socklen_t len = sizeof(sockaddr);
Expand Down Expand Up @@ -562,7 +573,7 @@ bool ConnectSocketDirectly(const CService& addrConnect, const SOCKET& hSocket, i
return false;
}
if (nRet != 0) {
LogPrintf("connect() to %s failed after select(): %s\n", addrConnect.ToString(), NetworkErrorString(nRet));
LogConnectFailure(manual_connection, "connect() to %s failed after select(): %s", addrConnect.ToString(), NetworkErrorString(nRet));
return false;
}
}
Expand All @@ -572,7 +583,7 @@ bool ConnectSocketDirectly(const CService& addrConnect, const SOCKET& hSocket, i
else
#endif
{
LogPrintf("connect() to %s failed: %s\n", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
LogConnectFailure(manual_connection, "connect() to %s failed: %s", addrConnect.ToString(), NetworkErrorString(WSAGetLastError()));
return false;
}
}
Expand Down Expand Up @@ -636,7 +647,7 @@ bool IsProxy(const CNetAddr& addr)
bool ConnectThroughProxy(const proxyType &proxy, const std::string& strDest, int port, const SOCKET& hSocket, int nTimeout, bool *outProxyConnectionFailed)
{
// first connect to proxy server
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout)) {
if (!ConnectSocketDirectly(proxy.proxy, hSocket, nTimeout, true)) {
if (outProxyConnectionFailed)
*outProxyConnectionFailed = true;
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool Lookup(const std::string& name, std::vector<CService>& vAddr, int portDefau
CService LookupNumeric(const std::string& name, int portDefault = 0);
bool LookupSubNet(const std::string& name, CSubNet& subnet);
SOCKET CreateSocket(const CService &addrConnect);
bool ConnectSocketDirectly(const CService& addrConnect, const SOCKET& hSocketRet, int nTimeout);
bool ConnectSocketDirectly(const CService& addrConnect, const SOCKET& hSocketRet, int nTimeout, bool manual_connection);
bool ConnectThroughProxy(const proxyType& proxy, const std::string& strDest, int port, const SOCKET& hSocketRet, int nTimeout, bool* outProxyConnectionFailed);
/** Return readable error string for a network error code */
std::string NetworkErrorString(int err);
Expand Down

0 comments on commit 6f4e17c

Please sign in to comment.