aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-11-03 15:32:02 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2017-11-03 15:42:50 -0700
commitf518d9ae6aa525c4cd360a16f07a9272cbef558d (patch)
tree335ee53ff19753f10dfd40b42570e0419701e183 /src/net.cpp
parentf224cbc3d8f4478d30e121126658c2ab22c6fb90 (diff)
parent8195cb0d7fc4f8699b35aff7a43ed2fb3013608c (diff)
downloadbitcoin-f518d9ae6aa525c4cd360a16f07a9272cbef558d.tar.xz
Merge #11592: 0.15: Backportsv0.15.1rc1
8195cb0d7 rpc: further constrain the libevent workaround (Cory Fields) 34153a7e4 rpc: work-around an upstream libevent bug (Cory Fields) fc308a6cd Add unit test for stale tip checking (Suhas Daftuar) 2ed0647ac Add CConnmanTest to mutate g_connman in tests (João Barbosa) a607a95d8 Connect to an extra outbound peer if our tip is stale (Suhas Daftuar) 459f2db42 Track tip update time and last new block announcement from each peer (Suhas Daftuar) 49bf09018 net: Allow connecting to extra outbound peers (Suhas Daftuar) bb83fe190 Add release notes describing blockmaxweight deprecation (Matt Corallo) 4c82cea99 Use a sensible default for blockmaxweight (Matt Corallo) 7871a7d3b Deprecate confusing blockmaxsize, fix getmininginfo output (Matt Corallo) 6baa317b5 Fix minchainwork test for 0.15 backport (Suhas Daftuar) 55b7abfa8 Make p2p-acceptablock not an extended test (Matt Corallo) 5bec7744d [qa] test that invalid blocks on an invalid chain get a disconnect (Matt Corallo) 92d6105c4 Reject headers building on invalid chains by tracking invalidity (Matt Corallo) 51001d684 Accept unrequested blocks with work equal to our tip (Matt Corallo) c6e4d0ce8 Stop always storing blocks from whitelisted peers (Matt Corallo) e976c36dd Rewrite p2p-acceptblock in preparation for slight behavior changes (Matt Corallo) ec8dedff4 net: Add missing lock in ProcessHeadersMessage(...) (practicalswift) 59b210d9a Disconnect outbound peers relaying invalid headers (Suhas Daftuar) fc966bbd2 moveonly: factor out headers processing into separate function (Suhas Daftuar) e3272242e Add unit test for outbound peer eviction (Suhas Daftuar) 9961abf9e Permit disconnection of outbound peers on bad/slow chains (Suhas Daftuar) bf191a718 Disconnecting from bad outbound peers in IBD (Suhas Daftuar) d570aa429 Fix uninitialized g_connman crash in Shutdown() (MeshCollider) 0a5477c7e net: stop both net/net_processing before destroying them (Cory Fields) b4136f21c net: drop unused connman param (Cory Fields) dc897e53d net: use an interface class rather than signals for message processing (Cory Fields) 8aee55af3 net: pass CConnman via pointer rather than reference (Cory Fields) 6f279652b Rename fAddnode to a more-descriptive "manual_connection" (Matt Corallo) ffb6ea4e5 Add comment explaining forced processing of compact blocks (Suhas Daftuar) 2df65eeb9 qa: add test for minchainwork use in acceptblock (Suhas Daftuar) 3acec3878 Don't process unrequested, low-work blocks (Suhas Daftuar) 0e9d04bf0 [qa] Test nMinimumChainWork (Suhas Daftuar) da4908c3a Allow setting nMinimumChainWork on command line (Suhas Daftuar) 41088795d qa: Remove never used return value of sync_with_ping (MarcoFalke) f3457d0e8 qa: Make tmpdir option an absolute path (MarcoFalke) 9c8006dc3 Avoid opening copied wallet databases simultaneously (Russell Yanofsky) de7053f11 [wallet] Fix leak in CDB constructor (João Barbosa) fd79ed6b2 Ensure backupwallet fails when attempting to backup to source file (Tomas van der Wansem) d94fc336c scripted-diff: rename assert_raises_jsonrpc to assert_raises_rpc error (John Newbery) 623de0acb [tests] do not allow assert_raises_message to be called with JSONRPCException (John Newbery) 5b728c8e9 [tests] remove direct testing on JSONRPCException from individual test cases (John Newbery) Pull request description: Tree-SHA512: 9fdb5c47844a899271023d8d445f7fc728e3ad71916490cd9783464684967594b07cda05dd644b722bfcea9fade74d06cfc501e1a68abf118d6d03fbbf7d7707
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp67
1 files changed, 49 insertions, 18 deletions
diff --git a/src/net.cpp b/src/net.cpp
index 599a6128fa..b3bc8292fc 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -89,10 +89,6 @@ std::string strSubVersion;
limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
-// Signals for message handling
-static CNodeSignals g_signals;
-CNodeSignals& GetNodeSignals() { return g_signals; }
-
void CConnman::AddOneShot(const std::string& strDest)
{
LOCK(cs_vOneShots);
@@ -665,7 +661,7 @@ void CNode::copyStats(CNodeStats &stats)
X(cleanSubVer);
}
X(fInbound);
- X(fAddnode);
+ X(m_manual_connection);
X(nStartingHeight);
{
LOCK(cs_vSend);
@@ -1114,7 +1110,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
CNode* pnode = new CNode(id, nLocalServices, GetBestHeight(), hSocket, addr, CalculateKeyedNetGroup(addr), nonce, addr_bind, "", true);
pnode->AddRef();
pnode->fWhitelisted = whitelisted;
- GetNodeSignals().InitializeNode(pnode, *this);
+ m_msgproc->InitializeNode(pnode);
LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
@@ -1674,6 +1670,37 @@ void CConnman::ProcessOneShot()
}
}
+bool CConnman::GetTryNewOutboundPeer()
+{
+ return m_try_another_outbound_peer;
+}
+
+void CConnman::SetTryNewOutboundPeer(bool flag)
+{
+ m_try_another_outbound_peer = flag;
+ LogPrint(BCLog::NET, "net: setting try another outbound peer=%s\n", flag ? "true" : "false");
+}
+
+// Return the number of peers we have over our outbound connection limit
+// Exclude peers that are marked for disconnect, or are going to be
+// disconnected soon (eg one-shots and feelers)
+// Also exclude peers that haven't finished initial connection handshake yet
+// (so that we don't decide we're over our desired connection limit, and then
+// evict some peer that has finished the handshake)
+int CConnman::GetExtraOutboundCount()
+{
+ int nOutbound = 0;
+ {
+ LOCK(cs_vNodes);
+ for (CNode* pnode : vNodes) {
+ if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected) {
+ ++nOutbound;
+ }
+ }
+ }
+ return std::max(nOutbound - nMaxOutbound, 0);
+}
+
void CConnman::ThreadOpenConnections()
{
// Connect to specific addresses
@@ -1738,7 +1765,7 @@ void CConnman::ThreadOpenConnections()
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
- if (!pnode->fInbound && !pnode->fAddnode) {
+ if (!pnode->fInbound && !pnode->m_manual_connection) {
// Count the peers that have all relevant services
if (pnode->fSuccessfullyConnected && !pnode->fFeeler && ((pnode->nServices & nRelevantServices) == nRelevantServices)) {
@@ -1768,7 +1795,8 @@ void CConnman::ThreadOpenConnections()
// * Only make a feeler connection once every few minutes.
//
bool fFeeler = false;
- if (nOutbound >= nMaxOutbound) {
+
+ if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer()) {
int64_t nTime = GetTimeMicros(); // The current time right now (in microseconds).
if (nTime > nNextFeeler) {
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
@@ -1934,7 +1962,7 @@ void CConnman::ThreadOpenAddedConnections()
}
// if successful, this moves the passed grant to the constructed node
-bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool fAddnode)
+bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot, bool fFeeler, bool manual_connection)
{
//
// Initiate outbound network connection
@@ -1963,10 +1991,10 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
pnode->fOneShot = true;
if (fFeeler)
pnode->fFeeler = true;
- if (fAddnode)
- pnode->fAddnode = true;
+ if (manual_connection)
+ pnode->m_manual_connection = true;
- GetNodeSignals().InitializeNode(pnode, *this);
+ m_msgproc->InitializeNode(pnode);
{
LOCK(cs_vNodes);
vNodes.push_back(pnode);
@@ -1996,16 +2024,16 @@ void CConnman::ThreadMessageHandler()
continue;
// Receive messages
- bool fMoreNodeWork = GetNodeSignals().ProcessMessages(pnode, *this, flagInterruptMsgProc);
+ bool fMoreNodeWork = m_msgproc->ProcessMessages(pnode, flagInterruptMsgProc);
fMoreWork |= (fMoreNodeWork && !pnode->fPauseSend);
if (flagInterruptMsgProc)
return;
-
// Send messages
{
LOCK(pnode->cs_sendProcessing);
- GetNodeSignals().SendMessages(pnode, *this, flagInterruptMsgProc);
+ m_msgproc->SendMessages(pnode, flagInterruptMsgProc);
}
+
if (flagInterruptMsgProc)
return;
}
@@ -2211,6 +2239,7 @@ CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSe
semOutbound = nullptr;
semAddnode = nullptr;
flagInterruptMsgProc = false;
+ SetTryNewOutboundPeer(false);
Options connOptions;
Init(connOptions);
@@ -2324,6 +2353,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
//
// Start threads
//
+ assert(m_msgproc);
InterruptSocks5(false);
interruptNet.reset();
flagInterruptMsgProc = false;
@@ -2443,9 +2473,10 @@ void CConnman::DeleteNode(CNode* pnode)
{
assert(pnode);
bool fUpdateConnectionTime = false;
- GetNodeSignals().FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
- if(fUpdateConnectionTime)
+ m_msgproc->FinalizeNode(pnode->GetId(), fUpdateConnectionTime);
+ if(fUpdateConnectionTime) {
addrman.Connected(pnode->addr);
+ }
delete pnode;
}
@@ -2704,7 +2735,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
strSubVer = "";
fWhitelisted = false;
fOneShot = false;
- fAddnode = false;
+ m_manual_connection = false;
fClient = false; // set by version message
fFeeler = false;
fSuccessfullyConnected = false;