<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Satoshi_Client_Node_Connectivity</id>
	<title>Satoshi Client Node Connectivity - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://en.zaoniao.it/index.php?action=history&amp;feed=atom&amp;title=Satoshi_Client_Node_Connectivity"/>
	<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Satoshi_Client_Node_Connectivity&amp;action=history"/>
	<updated>2026-05-16T08:11:30Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.32.0</generator>
	<entry>
		<id>http://en.zaoniao.it/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=6748&amp;oldid=prev</id>
		<title>Admin: Created page with &quot; The Satoshi bitcoin client creates a thread to manage making connections to other nodes. The code for that thread is in a function called ThreadOpenConnections2 in [https://g...&quot;</title>
		<link rel="alternate" type="text/html" href="http://en.zaoniao.it/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=6748&amp;oldid=prev"/>
		<updated>2019-07-10T06:13:12Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot; The Satoshi bitcoin client creates a thread to manage making connections to other nodes. The code for that thread is in a function called ThreadOpenConnections2 in [https://g...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&lt;br /&gt;
The Satoshi bitcoin client creates a thread to manage making&lt;br /&gt;
connections to other nodes. The code for that thread is in a&lt;br /&gt;
function called ThreadOpenConnections2 in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp].&lt;br /&gt;
&lt;br /&gt;
The client also handles accepting new inbound connections and &lt;br /&gt;
disconnecting nodes when appropriate in a a thread called&lt;br /&gt;
ThreadSocketHandler2, which is also in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp].&lt;br /&gt;
&lt;br /&gt;
The thread making connections does not discover the addresses of other&lt;br /&gt;
nodes. That information is gathered in various ways (See the article&lt;br /&gt;
on Node Discovery). The connection thread chooses among the available&lt;br /&gt;
addresses and makes connections and disconnects nodes when appropriate.&lt;br /&gt;
That is all it does.&lt;br /&gt;
&lt;br /&gt;
Node addresses are chosen based on the following set of rules.&lt;br /&gt;
&lt;br /&gt;
==Connection Rules==&lt;br /&gt;
&lt;br /&gt;
===Outbound Static Addresses===&lt;br /&gt;
&lt;br /&gt;
If the user specified addresses with -connect, the node uses &lt;br /&gt;
those addresses only. It tries to establish a connection to each node&lt;br /&gt;
and then sleeps for a half second, and then repeats that in a loop&lt;br /&gt;
until shut down. The code establishes a connection by calling&lt;br /&gt;
OpenNetworkConnection(addr). If the connection is already open,&lt;br /&gt;
OpenNetworkConnection just returns.&lt;br /&gt;
&lt;br /&gt;
If the user specified addresses with -node, then connections are&lt;br /&gt;
made to those nodes (with a half second delay between each) upon&lt;br /&gt;
startup. After those connections are attempted, the code proceeds&lt;br /&gt;
to the regular connection handling code.&lt;br /&gt;
&lt;br /&gt;
===Outbound Limiting===&lt;br /&gt;
&lt;br /&gt;
The connection handling code is one loop that performs various functions until shutdown. The first thing the loop does is count&lt;br /&gt;
the number of outbound connections, and if the maximum has been&lt;br /&gt;
reached (8 or -maxconnections), then it goes into a 2 second delay&lt;br /&gt;
loop until the count is below the max.&lt;br /&gt;
&lt;br /&gt;
Assuming the number of connections is below the limit, the node attempts&lt;br /&gt;
to connect to another node. See the next section.&lt;br /&gt;
&lt;br /&gt;
===Seed Nodes===&lt;br /&gt;
&lt;br /&gt;
If the node has not been able to learn about other addresses, presumably&lt;br /&gt;
because those methods have failed, the node will use an internal list&lt;br /&gt;
of 320 node addresses hard coded into the software to populate&lt;br /&gt;
the list of known node addresses.&lt;br /&gt;
&lt;br /&gt;
There is code to move away from seed nodes when possible. The presumption&lt;br /&gt;
is that this is to avoid overloading seed nodes. Once the local node has&lt;br /&gt;
enough addresses (presumably learned from the seed nodes), the&lt;br /&gt;
connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
===Outbound Random Selection===&lt;br /&gt;
&lt;br /&gt;
First the code puts the addresses into a.b.c.* buckets so only one&lt;br /&gt;
connection is made to each 24 bit netmasked network.&lt;br /&gt;
&lt;br /&gt;
Next, it loops through every address and determines whether it is &amp;quot;ready&amp;quot;,&lt;br /&gt;
and then, using a complex calculation, computes a score for every address.&lt;br /&gt;
The address with the highest score wins and OpenNetworkConnection is&lt;br /&gt;
called for it. Then the code completes the main loop of the thread and&lt;br /&gt;
continues.&lt;br /&gt;
&lt;br /&gt;
In order to determine readiness, the code hashes the IP and other entropy&lt;br /&gt;
into a deterministic random number between 1 and 3600. If the address&lt;br /&gt;
specifies a nonstandard port, a 2 hour (7200) penalty is added to the number.&lt;br /&gt;
This is an adjustment number to the retry interval.&lt;br /&gt;
&lt;br /&gt;
The main retry interval is basically the square root of the last time seen&lt;br /&gt;
plus the &amp;quot;random&amp;quot; adjustment from the previous paragraph. If the node&lt;br /&gt;
has been seen in the last hour, however, the retry interval is set to&lt;br /&gt;
ten minutes. The following table is in the code:&lt;br /&gt;
&lt;br /&gt;
 // Last seen Base retry frequency&lt;br /&gt;
 // &amp;lt;1 hour 10 min&lt;br /&gt;
 // 1 hour 1 hour&lt;br /&gt;
 // 4 hours 2 hours&lt;br /&gt;
 // 24 hours 5 hours&lt;br /&gt;
 // 48 hours 7 hours&lt;br /&gt;
 // 7 days 13 hours&lt;br /&gt;
 // 30 days 27 hours&lt;br /&gt;
 // 90 days 46 hours&lt;br /&gt;
 // 365 days 93 hours&lt;br /&gt;
&lt;br /&gt;
After computing the interval, if the address has already been contacted in&lt;br /&gt;
the interval, the address is skipped.&lt;br /&gt;
&lt;br /&gt;
If the address is over a day old, we may skip it. If we are successfully&lt;br /&gt;
getting IRC addresses, and have node connections, then we skip it with&lt;br /&gt;
the assumption that we will see the address advertisement if it is really&lt;br /&gt;
active.&lt;br /&gt;
&lt;br /&gt;
Finally, for all addresses that appear to be ready for a retry, the&lt;br /&gt;
address that has not been contacted the longest is chosen with a maximum&lt;br /&gt;
of 24 hours. However, there is a twist. The calculation for the score is this:&lt;br /&gt;
 int64 nScore = min(nSinceLastTry, (int64)24 * 60 * 60) - nSinceLastSeen - nRandomizer;&lt;br /&gt;
So, the address is penalized for every second since it is last seen (and&lt;br /&gt;
a random adjustment).&lt;br /&gt;
&lt;br /&gt;
===Inbound Accepting and Disconnecting===&lt;br /&gt;
&lt;br /&gt;
The client handles accepting new inbound connections and disconnecting&lt;br /&gt;
nodes when appropriate in a a thread called ThreadSocketHandler2,&lt;br /&gt;
which is in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp].&lt;br /&gt;
&lt;br /&gt;
The socket thread is simply a loop which disconnects sockets that&lt;br /&gt;
have the fDisconnect flag set on them (and have empty buffers),&lt;br /&gt;
prepares all sockets for &amp;quot;select&amp;quot; and calls &amp;quot;select&amp;quot;. &amp;quot;select&amp;quot; is &lt;br /&gt;
a system call which waits for activity on a set of sockets.&lt;br /&gt;
When that call returns, the node accepts any new connections,&lt;br /&gt;
receives and sends on any ready sockets, and marks any inactive sockets&lt;br /&gt;
for disconnect with the fDisconnect flag.&lt;br /&gt;
&lt;br /&gt;
Sockets are disconnected if they are 60 seconds old and have not sent&lt;br /&gt;
or received data.&lt;br /&gt;
&lt;br /&gt;
Sockets are disconnected if they have not sent or received data in&lt;br /&gt;
the last 90 minutes.&lt;br /&gt;
&lt;br /&gt;
Sockets are disconnected if the current inbound data exceeds a buffer limit.&lt;br /&gt;
(Search for: if (nPos &amp;gt; ReceiveBufferSize()) in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp])&lt;br /&gt;
&lt;br /&gt;
Sockets are disconnected if the current outbound data exceeds a buffer limit.&lt;br /&gt;
(Search for: if (vSend.size() &amp;gt; SendBufferSize()) in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp])&lt;br /&gt;
&lt;br /&gt;
==Source==&lt;br /&gt;
&lt;br /&gt;
[http://bitcoin.it/ http://bitcoin.it/]&lt;br /&gt;
&lt;br /&gt;
[[Category:Technology]]&lt;br /&gt;
[[Category:Manuals]]&lt;br /&gt;
==See Also on BitcoinWiki==&lt;br /&gt;
* [[MOBU]]&lt;br /&gt;
* [[MPCX]]&lt;br /&gt;
* [[CRYSTALS]]&lt;br /&gt;
* [[ZeroState]]&lt;br /&gt;
* [[Bitney]]&lt;/div&gt;</summary>
		<author><name>Admin</name></author>
		
	</entry>
</feed>