Skip to content

Commit 8042bbf

Browse files
Zain Iqbal Allarakhiazallarak
authored andcommitted
p2p: allow p2ptimeout to be configurable, speed up slow test
1 parent 384967f commit 8042bbf

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), false, 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
@@ -848,6 +849,7 @@ int nMaxConnections;
848849
int nUserMaxConnections;
849850
int nFD;
850851
ServiceFlags nLocalServices = ServiceFlags(NODE_NETWORK | NODE_NETWORK_LIMITED);
852+
int64_t peer_connect_timeout;
851853

852854
} // namespace
853855

@@ -1046,8 +1048,14 @@ bool AppInitParameterInteraction()
10461048
}
10471049

10481050
nConnectTimeout = gArgs.GetArg("-timeout", DEFAULT_CONNECT_TIMEOUT);
1049-
if (nConnectTimeout <= 0)
1051+
if (nConnectTimeout <= 0) {
10501052
nConnectTimeout = DEFAULT_CONNECT_TIMEOUT;
1053+
}
1054+
1055+
peer_connect_timeout = gArgs.GetArg("-peertimeout", DEFAULT_PEER_CONNECT_TIMEOUT);
1056+
if (peer_connect_timeout <= 0) {
1057+
return InitError(_("peertimeout cannot be configured with a negative value."));
1058+
}
10511059

10521060
if (gArgs.IsArgSet("-minrelaytxfee")) {
10531061
CAmount n = 0;
@@ -1685,6 +1693,7 @@ bool AppInitMain(InitInterfaces& interfaces)
16851693

16861694
connOptions.nMaxOutboundTimeframe = nMaxOutboundTimeframe;
16871695
connOptions.nMaxOutboundLimit = nMaxOutboundLimit;
1696+
connOptions.m_peer_connect_timeout = peer_connect_timeout;
16881697

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

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,11 +1232,11 @@ void CConnman::NotifyNumConnectionsChanged()
12321232
void CConnman::InactivityCheck(CNode *pnode)
12331233
{
12341234
int64_t nTime = GetSystemTimeInSeconds();
1235-
if (nTime - pnode->nTimeConnected > 60)
1235+
if (nTime - pnode->nTimeConnected > m_peer_connect_timeout)
12361236
{
12371237
if (pnode->nLastRecv == 0 || pnode->nLastSend == 0)
12381238
{
1239-
LogPrint(BCLog::NET, "socket no message in first 60 seconds, %d %d from %d\n", pnode->nLastRecv != 0, pnode->nLastSend != 0, pnode->GetId());
1239+
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());
12401240
pnode->fDisconnect = true;
12411241
}
12421242
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)