Skip to content

Commit 8844588

Browse files
committed
Merge #14733: P2P: Make peer timeout configurable, speed up very slow test and ensure correct code path tested.
48b37db make peertimeout a debug argument, remove error message translation (Zain Iqbal Allarakhia) 8042bbf p2p: allow p2ptimeout to be configurable, speed up slow test (Zain Iqbal Allarakhia) Pull request description: **Summary:** 1. _Primary_: Adds a `debug_only=true` flag for peertimeout, defaults to 60 sec., the current hard-coded setting. 2. _Secondary_: Drastically speeds up `p2p_timeout.py` test. 3. _Secondary_: Tests that the correct code path is being tested by adding log assertions to the test. **Rationale:** - P2P timeout was hard-coded: make it explicitly specified and configurable, instead of a magic number. - Addresses #13518; `p2p_timeout.py` takes 4 sec. to run instead of 61 sec. - Makes `p2p_timeout.py` more explicit. Previously, we relied on a comment to inform us of the timeout amount being tested. Now it is specified directly in the test via passing in the new arg; `-peertimeout=3`. - Opens us up to testing more P2P scenarios; oftentimes slow tests are the reason we don't test. **Locally verified changes:** _With Proposed Change (4.7 sec.):_ ``` $ time ./test/functional/p2p_timeouts.py 2018-11-19T00:04:19.077000Z TestFramework (INFO): Initializing test directory /tmp/testhja7g2n7 2018-11-19T00:04:23.479000Z TestFramework (INFO): Stopping nodes 2018-11-19T00:04:23.683000Z TestFramework (INFO): Cleaning up /tmp/testhja7g2n7 on exit 2018-11-19T00:04:23.683000Z TestFramework (INFO): Tests successful real 0m4.743s ``` _Currently on master (62.8 sec.):_ ``` $ time ./test/functional/p2p_timeouts.py 2018-11-19T00:06:10.948000Z TestFramework (INFO): Initializing test directory /tmp/test6mo6k21h 2018-11-19T00:07:13.376000Z TestFramework (INFO): Stopping nodes 2018-11-19T00:07:13.631000Z TestFramework (INFO): Cleaning up /tmp/test6mo6k21h on exit 2018-11-19T00:07:13.631000Z TestFramework (INFO): Tests successful real 1m2.836s ``` _Error message demonstrated for new argument `-peertimeout`:_ ``` $ ./bitcoind -peertimeout=-5 ... Error: peertimeout cannot be configured with a negative value. ``` Tree-SHA512: ff7a244ebea54c4059407bf4fb86465714e6a79cef5d2bcaa22cfe831a81761aaf597ba4d5172fc2ec12266f54712216fc41b5d24849e5d9dab39ba6f09e3a2a
2 parents 4265357 + 48b37db commit 8844588

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

src/init.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ void SetupServerArgs()
403403
gArgs.AddArg("-proxyrandomize", strprintf("Randomize credentials for every proxy connection. This enables Tor stream isolation (default: %u)", DEFAULT_PROXYRANDOMIZE), false, OptionsCategory::CONNECTION);
404404
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);
405405
gArgs.AddArg("-timeout=<n>", strprintf("Specify connection timeout in milliseconds (minimum: 1, default: %d)", DEFAULT_CONNECT_TIMEOUT), false, OptionsCategory::CONNECTION);
406+
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), true, OptionsCategory::CONNECTION);
406407
gArgs.AddArg("-torcontrol=<ip>:<port>", strprintf("Tor control port to use if onion listening enabled (default: %s)", DEFAULT_TOR_CONTROL), false, OptionsCategory::CONNECTION);
407408
gArgs.AddArg("-torpassword=<pass>", "Tor control port password (default: empty)", false, OptionsCategory::CONNECTION);
408409
#ifdef USE_UPNP
@@ -856,6 +857,7 @@ int nMaxConnections;
856857
int nUserMaxConnections;
857858
int nFD;
858859
ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK | NODE_NETWORK_LIMITED);
860+
int64_t peer_connect_timeout;
859861

860862
} // namespace
861863

@@ -1054,8 +1056,14 @@ bool AppInitParameterInteraction()
10541056
}
10551057

10561058
nConnectTimeout = gArgs.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
1057-
if (nConnectTimeout <= 0)
1059+
if (nConnectTimeout <= 0) {
10581060
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
1061+
}
1062+
1063+
peer_connect_timeout = gArgs.GetArg("-peertimeout", DEFAULT_PEER_CONNECT_TIMEOUT);
1064+
if (peer_connect_timeout <= 0) {
1065+
return InitError("peertimeout cannot be configured with a negative value.");
1066+
}
10591067

10601068
if (gArgs.IsArgSet("-minrelaytxfee")) {
10611069
CAmount n = 0;
@@ -1693,6 +1701,7 @@ bool AppInitMain(InitInterfaces& interfaces)
16931701

16941702
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
16951703
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
1704+
connOptions.m_peer_connect_timeout = peer_connect_timeout;
16961705

16971706
for (const std::string& strBind : gArgs.GetArgs("-bind")) {
16981707
CService addrBind;

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,11 +1227,11 @@ void CConnman::NotifyNumConnectionsChanged()
12271227
void CConnman::InactivityCheck(CNode *pnode)
12281228
{
12291229
int64_t nTime = GetSystemTimeInSeconds();
1230-
if (nTime - pnode->nTimeConnected > 60)
1230+
if (nTime - pnode->nTimeConnected > m_peer_connect_timeout)
12311231
{
12321232
if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
12331233
{
1234-
LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1234+
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());
12351235
pnode->fDisconnect = true;
12361236
}
12371237
else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL)

src/net.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ static const uint64_t DEFAULT_MAX_UPLOAD_TARGET = 0;
7878
static const uint64_t MAX_UPLOAD_TIMEFRAME = 60 * 60 * 24;
7979
/** Default for blocks only*/
8080
static const bool DEFAULT_BLOCKSONLY = false;
81+
/** -peertimeout default */
82+
static const int64_t DEFAULT_PEER_CONNECT_TIMEOUT = 60;
8183

8284
static const bool DEFAULT_FORCEDNSSEED = false;
8385
static const size_t DEFAULT_MAXRECEIVEBUFFER = 5 * 1000;
@@ -138,6 +140,7 @@ class CConnman
138140
unsigned int nReceiveFloodSize = 0;
139141
uint64_t nMaxOutboundTimeframe = 0;
140142
uint64_t nMaxOutboundLimit = 0;
143+
int64_t m_peer_connect_timeout = DEFAULT_PEER_CONNECT_TIMEOUT;
141144
std::vector<std::string> vSeedNodes;
142145
std::vector<CSubNet> vWhitelistedRange;
143146
std::vector<CService> vBinds, vWhiteBinds;
@@ -158,6 +161,7 @@ class CConnman
158161
m_msgproc = connOptions.m_msgproc;
159162
nSendBufferMaxSize = connOptions.nSendBufferMaxSize;
160163
nReceiveFloodSize = connOptions.nReceiveFloodSize;
164+
m_peer_connect_timeout = connOptions.m_peer_connect_timeout;
161165
{
162166
LOCK(cs_totalBytesSent);
163167
nMaxOutboundTimeframe = connOptions.nMaxOutboundTimeframe;
@@ -391,6 +395,9 @@ class CConnman
391395
uint64_t nMaxOutboundLimit GUARDED_BY(cs_totalBytesSent);
392396
uint64_t nMaxOutboundTimeframe GUARDED_BY(cs_totalBytesSent);
393397

398+
// P2P timeout in seconds
399+
int64_t m_peer_connect_timeout;
400+
394401
// Whitelisted ranges. Any node connecting from these is automatically
395402
// whitelisted (as well as those connecting to whitelisted binds).
396403
std::vector<CSubNet> vWhitelistedRange;

test/functional/p2p_timeouts.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
- Wait 1 second
1515
- Assert that we're connected
1616
- Send a ping to no_verack_node and no_version_node
17-
- Wait 30 seconds
17+
- Wait 1 second
1818
- Assert that we're still connected
1919
- Send a ping to no_verack_node and no_version_node
20-
- Wait 31 seconds
21-
- Assert that we're no longer connected (timeout to receive version/verack is 60 seconds)
20+
- Wait 2 seconds
21+
- Assert that we're no longer connected (timeout to receive version/verack is 3 seconds)
2222
"""
2323

2424
from time import sleep
@@ -36,6 +36,8 @@ class TimeoutsTest(BitcoinTestFramework):
3636
def set_test_params(self):
3737
self.setup_clean_chain = True
3838
self.num_nodes = 1
39+
# set timeout to receive version/verack to 3 seconds
40+
self.extra_args = [["-peertimeout=3"]]
3941

4042
def run_test(self):
4143
# Setup the p2p connections
@@ -52,7 +54,7 @@ def run_test(self):
5254
no_verack_node.send_message(msg_ping())
5355
no_version_node.send_message(msg_ping())
5456

55-
sleep(30)
57+
sleep(1)
5658

5759
assert "version" in no_verack_node.last_message
5860

@@ -63,11 +65,17 @@ def run_test(self):
6365
no_verack_node.send_message(msg_ping())
6466
no_version_node.send_message(msg_ping())
6567

66-
sleep(31)
67-
68-
assert not no_verack_node.is_connected
69-
assert not no_version_node.is_connected
70-
assert not no_send_node.is_connected
68+
expected_timeout_logs = [
69+
"version handshake timeout from 0",
70+
"socket no message in first 3 seconds, 1 0 from 1",
71+
"socket no message in first 3 seconds, 0 0 from 2",
72+
]
73+
74+
with self.nodes[0].assert_debug_log(expected_msgs=expected_timeout_logs):
75+
sleep(2)
76+
assert not no_verack_node.is_connected
77+
assert not no_version_node.is_connected
78+
assert not no_send_node.is_connected
7179

7280
if __name__ == '__main__':
7381
TimeoutsTest().main()

0 commit comments

Comments
 (0)