p2p: allow p2ptimeout to be configurable, speed up slow test

This commit is contained in:
Zain Iqbal Allarakhia 2018-11-15 15:30:26 -08:00 committed by Zain Iqbal Allarakhia
parent 384967f311
commit 8042bbfbf0
4 changed files with 35 additions and 11 deletions

View file

@ -403,6 +403,7 @@ void SetupServerArgs()
gArgs.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), false, OptionsCategory::CONNECTION); gArgs.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), false, OptionsCategory::CONNECTION);
gArgs.AddArg("-seednode=<ip>", "Connect to a node to retrieve peer addresses, and disconnect. This option can be specified multiple times to connect to multiple nodes.", false, OptionsCategory::CONNECTION); gArgs.AddArg("-seednode=<ip>", "Connect to a node to retrieve peer addresses, and disconnect. This option can be specified multiple times to connect to multiple nodes.", false, OptionsCategory::CONNECTION);
gArgs.AddArg("-timeout=<n>", strprintf("Specify connection timeout in milliseconds (minimum: 1, default: %d)", DEFAULT_CONNECT_TIMEOUT), false, OptionsCategory::CONNECTION); gArgs.AddArg("-timeout=<n>", strprintf("Specify connection timeout in milliseconds (minimum: 1, default: %d)", DEFAULT_CONNECT_TIMEOUT), false, OptionsCategory::CONNECTION);
gArgs.AddArg("-peertimeout=<n>", strprintf("Specify p2p connection timeout in seconds. This option determines the amount of time a peer may be inactive before the connection to it is dropped. (minimum: 1, default: %d)", DEFAULT_PEER_CONNECT_TIMEOUT), false, OptionsCategory::CONNECTION);
gArgs.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control port to use if onion listening enabled (default: %s)", DEFAULT_TOR_CONTROL), false, OptionsCategory::CONNECTION); gArgs.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control port to use if onion listening enabled (default: %s)", DEFAULT_TOR_CONTROL), false, OptionsCategory::CONNECTION);
gArgs.AddArg("-torpassword=<pass>", "Tor control port password (default: empty)", false, OptionsCategory::CONNECTION); gArgs.AddArg("-torpassword=<pass>", "Tor control port password (default: empty)", false, OptionsCategory::CONNECTION);
#ifdef USE_UPNP #ifdef USE_UPNP
@ -848,6 +849,7 @@ int nMaxConnections;
int nUserMaxConnections; int nUserMaxConnections;
int nFD; int nFD;
ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK | NODE_NETWORK_LIMITED); ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK | NODE_NETWORK_LIMITED);
int64_t peer_connect_timeout;
} // namespace } // namespace
@ -1046,8 +1048,14 @@ bool AppInitParameterInteraction()
} }
nConnectTimeout = gArgs.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT); nConnectTimeout = gArgs.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
if (nConnectTimeout <= 0) if (nConnectTimeout <= 0) {
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT; nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
}
peer_connect_timeout = gArgs.GetArg("-peertimeout", DEFAULT_PEER_CONNECT_TIMEOUT);
if (peer_connect_timeout <= 0) {
return InitError(_("peertimeout cannot be configured with a negative value."));
}
if (gArgs.IsArgSet("-minrelaytxfee")) { if (gArgs.IsArgSet("-minrelaytxfee")) {
CAmount n = 0; CAmount n = 0;
@ -1685,6 +1693,7 @@ bool AppInitMain(InitInterfaces& interfaces)
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe; connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
connOptions.nMaxOutboundLimit = nMaxOutboundLimit; connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
connOptions.m_peer_connect_timeout = peer_connect_timeout;
for (const std::string& strBind : gArgs.GetArgs("-bind")) { for (const std::string& strBind : gArgs.GetArgs("-bind")) {
CService addrBind; CService addrBind;

View file

@ -1232,11 +1232,11 @@ void CConnman::NotifyNumConnectionsChanged()
void CConnman::InactivityCheck(CNode *pnode) void CConnman::InactivityCheck(CNode *pnode)
{ {
int64_t nTime = GetSystemTimeInSeconds(); int64_t nTime = GetSystemTimeInSeconds();
if (nTime - pnode->nTimeConnected > 60) if (nTime - pnode->nTimeConnected > m_peer_connect_timeout)
{ {
if (pnode->nLastRecv == 0 || pnode->nLastSend == 0) if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
{ {
LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId()); LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d from %d\n", m_peer_connect_timeout, pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
pnode->fDisconnect = true; pnode->fDisconnect = true;
} }
else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL) else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)

View file

@ -78,6 +78,8 @@ static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24; static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
/** Default for blocks only*/ /** Default for blocks only*/
static const bool DEFAULT_BLOCKSONLY = false; static const bool DEFAULT_BLOCKSONLY = false;
/** -peertimeout default */
static const int64_t DEFAULT_PEER_CONNECT_TIMEOUT = 60;
static const bool DEFAULT_FORCEDNSSEED = false; static const bool DEFAULT_FORCEDNSSEED = false;
static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000; static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
@ -138,6 +140,7 @@ public:
unsigned int nReceiveFloodSize = 0; unsigned int nReceiveFloodSize = 0;
uint64_t nMaxOutboundTimeframe = 0; uint64_t nMaxOutboundTimeframe = 0;
uint64_t nMaxOutboundLimit = 0; uint64_t nMaxOutboundLimit = 0;
int64_t m_peer_connect_timeout = DEFAULT_PEER_CONNECT_TIMEOUT;
std::vector<std::string> vSeedNodes; std::vector<std::string> vSeedNodes;
std::vector<CSubNet> vWhitelistedRange; std::vector<CSubNet> vWhitelistedRange;
std::vector<CService> vBinds, vWhiteBinds; std::vector<CService> vBinds, vWhiteBinds;
@ -158,6 +161,7 @@ public:
m_msgproc = connOptions.m_msgproc; m_msgproc = connOptions.m_msgproc;
nSendBufferMaxSize = connOptions.nSendBufferMaxSize; nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
nReceiveFloodSize = connOptions.nReceiveFloodSize; nReceiveFloodSize = connOptions.nReceiveFloodSize;
m_peer_connect_timeout = connOptions.m_peer_connect_timeout;
{ {
LOCK(cs_totalBytesSent); LOCK(cs_totalBytesSent);
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe; nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
@ -391,6 +395,9 @@ private:
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent); uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent); uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent);
// P2P timeout in seconds
int64_t m_peer_connect_timeout;
// Whitelisted ranges. Any node connecting from these is automatically // Whitelisted ranges. Any node connecting from these is automatically
// whitelisted (as well as those connecting to whitelisted binds). // whitelisted (as well as those connecting to whitelisted binds).
std::vector<CSubNet> vWhitelistedRange; std::vector<CSubNet> vWhitelistedRange;

View file

@ -14,11 +14,11 @@
- Wait 1 second - Wait 1 second
- Assert that we're connected - Assert that we're connected
- Send a ping to no_verack_node and no_version_node - Send a ping to no_verack_node and no_version_node
- Wait 30 seconds - Wait 1 second
- Assert that we're still connected - Assert that we're still connected
- Send a ping to no_verack_node and no_version_node - Send a ping to no_verack_node and no_version_node
- Wait 31 seconds - Wait 2 seconds
- Assert that we're no longer connected (timeout to receive version/verack is 60 seconds) - Assert that we're no longer connected (timeout to receive version/verack is 3 seconds)
""" """
from time import sleep from time import sleep
@ -36,6 +36,8 @@ class TimeoutsTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):
self.setup_clean_chain = True self.setup_clean_chain = True
self.num_nodes = 1 self.num_nodes = 1
# set timeout to receive version/verack to 3 seconds
self.extra_args = [["-peertimeout=3"]]
def run_test(self): def run_test(self):
# Setup the p2p connections # Setup the p2p connections
@ -52,7 +54,7 @@ class TimeoutsTest(BitcoinTestFramework):
no_verack_node.send_message(msg_ping()) no_verack_node.send_message(msg_ping())
no_version_node.send_message(msg_ping()) no_version_node.send_message(msg_ping())
sleep(30) sleep(1)
assert "version" in no_verack_node.last_message assert "version" in no_verack_node.last_message
@ -63,11 +65,17 @@ class TimeoutsTest(BitcoinTestFramework):
no_verack_node.send_message(msg_ping()) no_verack_node.send_message(msg_ping())
no_version_node.send_message(msg_ping()) no_version_node.send_message(msg_ping())
sleep(31) expected_timeout_logs = [
"version handshake timeout from 0",
"socket no message in first 3 seconds, 1 0 from 1",
"socket no message in first 3 seconds, 0 0 from 2",
]
assert not no_verack_node.is_connected with self.nodes[0].assert_debug_log(expected_msgs=expected_timeout_logs):
assert not no_version_node.is_connected sleep(2)
assert not no_send_node.is_connected assert not no_verack_node.is_connected
assert not no_version_node.is_connected
assert not no_send_node.is_connected
if __name__ == '__main__': if __name__ == '__main__':
TimeoutsTest().main() TimeoutsTest().main()