<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=An0nymous</id>
	<title>Bitcoin Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://en.bitcoin.it/w/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=An0nymous"/>
	<link rel="alternate" type="text/html" href="https://en.bitcoin.it/wiki/Special:Contributions/An0nymous"/>
	<updated>2026-04-09T21:23:09Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Talk:Avalon&amp;diff=36537</id>
		<title>Talk:Avalon</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Talk:Avalon&amp;diff=36537"/>
		<updated>2013-03-31T14:16:40Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: Start discussion&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;There should be start some information about chips, where can we buy it, and how to make your own asic. Is n&#039;t it?&lt;br /&gt;
[[User:An0nymous|An0nymous]] ([[User talk:An0nymous|talk]]) 14:16, 31 March 2013 (GMT)&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Block_Exchange&amp;diff=35274</id>
		<title>Satoshi Client Block Exchange</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Block_Exchange&amp;diff=35274"/>
		<updated>2013-01-18T06:42:36Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Inventory Messages */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
This article describes how blocks are exchanged between nodes.&lt;br /&gt;
See [[Protocol rules]] for more information on how blocks are [[Invalid block|validated]].&lt;br /&gt;
&lt;br /&gt;
Upon initial connection, if the connection was not inbound[1],&lt;br /&gt;
or in other words, if the connection was initiated by the local node,&lt;br /&gt;
the version message is queued for sending immediately. When the remote node&lt;br /&gt;
receives the version message it replies with its own version message.[2]&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;version&amp;quot; message, it may send a &amp;quot;getblocks&amp;quot; request&lt;br /&gt;
to the remote node if EITHER:&lt;br /&gt;
&lt;br /&gt;
# The node has never sent an initial getblocks request to any node yet.&lt;br /&gt;
# Or, this is the only active node connection. Presumably the node had zero connections prior to this connection, so maybe it was disconnected for a long time. So, it will ask for blocks to catch up.&lt;br /&gt;
&lt;br /&gt;
The getblocks message contains multiple block hashes that the requesting&lt;br /&gt;
node already possesses, in order to help the remote note find the latest&lt;br /&gt;
common block between the nodes.  The list of hashes starts with the latest&lt;br /&gt;
block and goes back ten and then doubles in an exponential progression&lt;br /&gt;
until the genesis block is reached.[3]  Since both nodes are hard coded&lt;br /&gt;
with the genesis block, they are guaranteed to a least start there.&lt;br /&gt;
If that block does not match for some reason, no blocks are exchanged.&lt;br /&gt;
&lt;br /&gt;
== Inventory Messages ==&lt;br /&gt;
&lt;br /&gt;
Note that the node receiving the getblocks request does not actually send&lt;br /&gt;
full blocks in response. The node sends an &amp;quot;inv&amp;quot; message containing just&lt;br /&gt;
the hashes of the series of blocks that fit the request, which verifies&lt;br /&gt;
that the node does indeed have blocks to send that the remote node does&lt;br /&gt;
not have (but does not presume the remote node wants the full blocks yet).&lt;br /&gt;
&lt;br /&gt;
When the local node receives the &amp;quot;inv&amp;quot; message, it will request the actual&lt;br /&gt;
blocks with a &amp;quot;getdata&amp;quot; message. See Below.&lt;br /&gt;
&lt;br /&gt;
But first, here is more detail how the remote node sends the &amp;quot;inv&amp;quot; message&lt;br /&gt;
in response to the &amp;quot;getblocks&amp;quot; request sent by the local node. The remote&lt;br /&gt;
node calls pFrom-&amp;gt;PushInventory which is a method on the CNode instance that&lt;br /&gt;
represents the node that requested the blocks (the local node in this&lt;br /&gt;
walkthrough), and PushInventory adds the block hash to the vInventoryToSend&lt;br /&gt;
variable of the CNode. The SendMessages function in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp] will take the&lt;br /&gt;
inv items out of vInventoryToSend and add it to a vInv variable which&lt;br /&gt;
means they are really ready for sending.[4]&lt;br /&gt;
The reason for the seperate variable is that some inventory items&lt;br /&gt;
(transactions only right now) may be &amp;quot;trickled&amp;quot; to the remote node,&lt;br /&gt;
which means they may kept from from being sent right away.&lt;br /&gt;
When the vInv variable fills up with 1000 entries, a message is queued&lt;br /&gt;
with those 1000 entries and the loop continues.  At the end, any&lt;br /&gt;
remaining entries are sent in a final &amp;quot;inv&amp;quot; message.&lt;br /&gt;
&lt;br /&gt;
When the local node receives the &amp;quot;inv&amp;quot; message, it will request the actual&lt;br /&gt;
block with a &amp;quot;getdata&amp;quot; message. To be precise, the node calls pfrom-&amp;gt;AskFor&lt;br /&gt;
to request the block, and that method queues the request for the blocks in&lt;br /&gt;
mapAskFor, and the multipurpose SendMessage() sends the &amp;quot;getdata&amp;quot; requests&lt;br /&gt;
in batches of 1000 entries from the map.[5]&lt;br /&gt;
&lt;br /&gt;
The code attempts to limit redundant requests to every 2 minutes for the&lt;br /&gt;
same block by using a map called mapAlreadyAskedFor to delay the message&lt;br /&gt;
if necessary.[6]&lt;br /&gt;
&lt;br /&gt;
== Block Batching ==&lt;br /&gt;
&lt;br /&gt;
The responding node to a &amp;quot;getblocks&amp;quot; request attempts to limit the response&lt;br /&gt;
to the requestor to 500 blocks.[7]&lt;br /&gt;
&lt;br /&gt;
However, in a peculiar twist, if the requestor appears to have diverged&lt;br /&gt;
from the main branch, the node will send as many blocks as necessary to&lt;br /&gt;
replace the entire bad chain of the requestor, from the lastest common block&lt;br /&gt;
between the nodes, up to the last block the requestor has (on their bad main&lt;br /&gt;
branch). That is in addition to the 500 &amp;quot;catch up&amp;quot; blocks for main branch&lt;br /&gt;
updates that will also be sent.[8]&lt;br /&gt;
&lt;br /&gt;
Note that in addition to a flat limit on the number of blocks queued for&lt;br /&gt;
sending, bitcoind also limits the total size of the blocks that are being&lt;br /&gt;
queued. This is currently limited to half the send buffer size[9], which is&lt;br /&gt;
10MB, for a limit of 5MB of queued blocks for send.[10]&lt;br /&gt;
&lt;br /&gt;
== Batch Continue Mechanism ==&lt;br /&gt;
&lt;br /&gt;
When a node is finished sending a batch of block inventory, it records the&lt;br /&gt;
hash of the last block in the batch.[11]  When the node receives a request&lt;br /&gt;
for that full block, it realizes the remove node is done with the current&lt;br /&gt;
batch and directly queues a special &amp;quot;inv&amp;quot; message (bypassing the normal&lt;br /&gt;
SendMessage mechanism) with one block hash entry containing the&lt;br /&gt;
latest block hash.[12] When the remote node receives that &amp;quot;inv&amp;quot; message,&lt;br /&gt;
it will see that it does not have that block and it it will ask for that&lt;br /&gt;
block as described above. However, this time when it receives the block&lt;br /&gt;
and processes it, it will notice that it does not have the previous block,&lt;br /&gt;
so it will record the latest block as an &amp;quot;orphan&amp;quot; block, and will request a&lt;br /&gt;
block update starting with the latest block it has up to the block before&lt;br /&gt;
the orphan [13], in order to fill in the gap. That goes out as a &amp;quot;getblocks&amp;quot;&lt;br /&gt;
request and the whole batch process repeats itself.&lt;br /&gt;
&lt;br /&gt;
However, there is a twist. When the next batch finishes, the remote node&lt;br /&gt;
sending the blocks will send the &amp;quot;inv&amp;quot; with latest block hash as usual, and&lt;br /&gt;
the local node will notice it already has this block in the orphan block&lt;br /&gt;
map this time and so it will skip requesting the block and directly ask&lt;br /&gt;
for a block update.[14] This process will continue until the last&lt;br /&gt;
block prior to the latest block is received. At the end of processing &lt;br /&gt;
that block, it will notice there is an orphan that pointed to this block&lt;br /&gt;
and will process the orphan block, (and any other orphans, recursively)&lt;br /&gt;
thus completing the entire process.[15]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Stall Recovery ==&lt;br /&gt;
&lt;br /&gt;
If the batching processes is interrupted for some reason, such as the&lt;br /&gt;
remote node failing to honor the &amp;quot;Batch Continue Mechanism&amp;quot; or if a &lt;br /&gt;
disconnection occurs, there is a way for the process to restart. When&lt;br /&gt;
a new block is solved and advertised around[16], any nodes that are&lt;br /&gt;
behind will notice the new block in the &amp;quot;inv&amp;quot; and that will trigger it&lt;br /&gt;
to request a &amp;quot;getblocks&amp;quot; update from the node that sent it the message.&lt;br /&gt;
That will cause blocks to be sent starting from wherever in the block&lt;br /&gt;
chain that the node that is behind is currently at.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Long Orphan Chains ==&lt;br /&gt;
&lt;br /&gt;
In various tests, it has proven relatively common (say more than one&lt;br /&gt;
in ten) to discover nodes that are significantly behind on the block&lt;br /&gt;
chain, probably because they are in the process of catching up as well.&lt;br /&gt;
Since a well connected node will have at least 8 and up to dozens of&lt;br /&gt;
connections, it is fairly likely that a new node will connect to&lt;br /&gt;
another node that is also catching up.&lt;br /&gt;
&lt;br /&gt;
Nodes that are catching up will advertise the blocks they are processing,&lt;br /&gt;
as they accept blocks into their main chain, to every other node.[16]&lt;br /&gt;
While there is code to prevent advertising old blocks before a certain&lt;br /&gt;
checkpoint, that code also has a clause that does advertise blocks to&lt;br /&gt;
remote nodes if the block height is over the remote node&#039;s current best&lt;br /&gt;
height minus 2000 blocks.[17] This appears to allow nodes to &amp;quot;help&amp;quot; other&lt;br /&gt;
nodes catch up, even if they are both processing old blocks.&lt;br /&gt;
&lt;br /&gt;
These advertisements cause the local node to request those blocks&lt;br /&gt;
from the remote node, which could be blocks well into the future compared&lt;br /&gt;
to what has been processed locally. Due to the way blocks are requested,&lt;br /&gt;
the remote node will send a large batch of blocks in response and will&lt;br /&gt;
continue sending blocks to the local node until it reaches the end.&lt;br /&gt;
Note that this is likely to occur at the same time the local node is&lt;br /&gt;
downloading earlier blocks on the main chain from another node. That&lt;br /&gt;
process may eventually catch up with the orphan chain and produce a &lt;br /&gt;
very, very long operation to revalidate and connect up all the orphan&lt;br /&gt;
blocks. Orphan chains over ten thousand blocks long, taking over an hour&lt;br /&gt;
to process are possible.&lt;br /&gt;
&lt;br /&gt;
Therefore, two nodes talking to each other that are both catching up can&lt;br /&gt;
lead to suboptimal interactions, especially when one both are far behind&lt;br /&gt;
and one is far ahead of the other.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Flood Limit Effects ==&lt;br /&gt;
&lt;br /&gt;
Even with the batching mechanism described above, there are scenarios&lt;br /&gt;
that occur that result in the remote node overflowing the local receive&lt;br /&gt;
buffer while blocks are being exchanged.&lt;br /&gt;
&lt;br /&gt;
For example, if a remote node is &amp;quot;catching up&amp;quot;, it will advertise each block&lt;br /&gt;
it processes to the local node in certain circumstances (see above [17]).&lt;br /&gt;
The local node will request each of those blocks right away. There is no&lt;br /&gt;
protection against the local node requesting too many of these blocks.&lt;br /&gt;
The remote node will send all blocks requested. There is no protection&lt;br /&gt;
against the remote node sending too many blocks before the local node has&lt;br /&gt;
time to process them, in this circumstance.&lt;br /&gt;
&lt;br /&gt;
The local receive buffer can fill up. When the local node notices a receive&lt;br /&gt;
buffer is full, it disconnects that node connection.[18]&lt;br /&gt;
If sets the fDisconnect flag, and once the buffers are empty[19], the&lt;br /&gt;
socket is closed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Performance ==&lt;br /&gt;
&lt;br /&gt;
As of September 1, 2011, on a server class computer circa 2005 running&lt;br /&gt;
Ubuntu with a Comcast cable internet connection takes over 10 hours &lt;br /&gt;
to download and process the block chain. While it is debatable what&lt;br /&gt;
the bottleneck is early in the download process, it is clear from&lt;br /&gt;
the processing of recent blocks that the network is not the bottleneck&lt;br /&gt;
for all but the slowest internet connections.&lt;br /&gt;
&lt;br /&gt;
Blocks are taking over a second, on average, to process once downloaded.[20]&lt;br /&gt;
However, the average size of a block is only around 24 kilobytes&lt;br /&gt;
in August 2011. It certainly does not take 1 second to download 24K.&lt;br /&gt;
Also, testing reveals very large queues of blocks being processed per&lt;br /&gt;
message loop, which is not what you would expect if the thread was&lt;br /&gt;
pulling them out of the queue as they arrive on the sockets. &lt;br /&gt;
&lt;br /&gt;
There are a number of &amp;quot;false signals&amp;quot; that lead one to believe the problem&lt;br /&gt;
is with network performance. The first false signal is that, as of&lt;br /&gt;
August 2011, nearly all of the first 60 or 70% of  blocks downloaded are&lt;br /&gt;
very small. Recent average block sizes are around one hundred times bigger!&lt;br /&gt;
So, almost all of a sudden, the block rate goes from very fast to very slow.&lt;br /&gt;
It looks like something went wrong. In reality, if you measure the rate&lt;br /&gt;
of block processing by kilobyte, the rate remains relatively constant.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Another false signal is related to the fact that message queues are&lt;br /&gt;
processed to completion, one at a time per node. This can result in big&lt;br /&gt;
backups of messages from other nodes. So, a long period of increasing&lt;br /&gt;
blocks may freeze for long periods as other nodes are serviced. Consider&lt;br /&gt;
that block downloads typically come from just one remote node (at&lt;br /&gt;
least until a miner or other relaying or downloading node advertises&lt;br /&gt;
a late block and disrupts the process) and so all the work might&lt;br /&gt;
be on one node. Things go fast processing the blocks from a node,&lt;br /&gt;
and then that looks like it stops as &amp;quot;addr&amp;quot; messages are processed from&lt;br /&gt;
other nodes and other work is done. But it looks like something is wrong.&lt;br /&gt;
&lt;br /&gt;
Also, the orphaning effects described above can lead to excessive block&lt;br /&gt;
processing with nothing to show for it until the orphan chain is connected.&lt;br /&gt;
Also, you do ocassionally run into a node that is slow to respond, perhaps&lt;br /&gt;
because they are also processing blocks or because they have a slow machine&lt;br /&gt;
or connection.&lt;br /&gt;
&lt;br /&gt;
All of the above contributes to heavy &amp;quot;jitter&amp;quot; in the block download process,&lt;br /&gt;
and that is a more frustrating user experience than a constant download rate.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Footnotes==&lt;br /&gt;
1. See pfrom-&amp;gt;fInbound where pfrom is a CNode.&lt;br /&gt;
&lt;br /&gt;
2. See ProcessMessage() in main.cpp where strCommand == &amp;quot;version&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. See CBlockLocator in main.h.&lt;br /&gt;
&lt;br /&gt;
4. See Message: inventory in SendMessage in main.cpp.&lt;br /&gt;
&lt;br /&gt;
5. See Message: getdata at the end of SendMessage in main.cpp.&lt;br /&gt;
&lt;br /&gt;
6. See CNode::AskFor in net.h.&lt;br /&gt;
&lt;br /&gt;
7. See ProcessMessage() in main.cpp where strCommand ==&amp;quot;getblocks&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
8. See&lt;br /&gt;
 int nLimit = 500 + locator.GetDistanceBack();&lt;br /&gt;
in ProcessMessage in main.cpp where strCommand ==&amp;quot;getblocks&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
9. See&lt;br /&gt;
 if (--nLimit &amp;lt;= 0 || nBytes &amp;gt;= SendBufferSize()/2)&lt;br /&gt;
in ProcessMessage() in main.cpp where strCommand ==&amp;quot;getblocks&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
10. See&lt;br /&gt;
 inline unsigned int SendBufferSize() {&lt;br /&gt;
        return 1000*GetArg(&amp;quot;-maxsendbuffer&amp;quot;, 10*1000); }&lt;br /&gt;
in net.h.&lt;br /&gt;
&lt;br /&gt;
11. See pfrom-&amp;gt;hashContinue = pindex-&amp;gt;GetBlockHash();&lt;br /&gt;
    in ProcessMessage() in main.cpp where strCommand ==&amp;quot;getblocks&amp;quot;.&lt;br /&gt;
12. See: if (inv.hash == pfrom-&amp;gt;hashContinue)&lt;br /&gt;
    in ProcessMessage() in main.cpp where strCommand ==&amp;quot;getdata&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
13. See:&lt;br /&gt;
        // Ask this guy to fill in what we&#039;re missing&lt;br /&gt;
        if (pfrom)&lt;br /&gt;
            pfrom-&amp;gt;PushGetBlocks(pindexBest, GetOrphanRoot(pblock2));&lt;br /&gt;
in ProcessBlock() in main.cpp.&lt;br /&gt;
&lt;br /&gt;
14. See:&lt;br /&gt;
        else if (inv.type == MSG_BLOCK &amp;amp;&amp;amp; mapOrphanBlocks.count(inv.hash))&lt;br /&gt;
            pfrom-&amp;gt;PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));&lt;br /&gt;
in ProcessMessage() in main.cpp where strCommand ==&amp;quot;inv&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
15. See:&lt;br /&gt;
    // Recursively process any orphan blocks that depended on this one&lt;br /&gt;
in ProcessBlock() in main.cpp.&lt;br /&gt;
&lt;br /&gt;
16. See the last block of code in AcceptBlock in main.cpp.&lt;br /&gt;
&lt;br /&gt;
17. See:&lt;br /&gt;
        if (nBestHeight &amp;gt; (pnode-&amp;gt;nStartingHeight != -1 ? pnode-&amp;gt;nStartingHeight - 2000 : 134444))&lt;br /&gt;
    in AcceptBlock() in main.cpp.&lt;br /&gt;
&lt;br /&gt;
18. See:&lt;br /&gt;
 if (nPos &amp;gt; ReceiveBufferSize()) {&lt;br /&gt;
in ThreadSocketHandler2() in net.cpp.&lt;br /&gt;
&lt;br /&gt;
19. See:&lt;br /&gt;
        if (pnode-&amp;gt;fDisconnect ||&lt;br /&gt;
            (pnode-&amp;gt;GetRefCount() &amp;lt;= 0 &amp;amp;&amp;amp; pnode-&amp;gt;vRecv.empty() &amp;amp;&amp;amp; pnode-&amp;gt;vSend.empty()))&lt;br /&gt;
in ThreadSocketHandler2() in net.cpp.&lt;br /&gt;
&lt;br /&gt;
20. This is from the authors experience and also&lt;br /&gt;
    see https://bitcointalk.org/index.php?topic=31376.0.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Sockets_and_Messages&amp;diff=35273</id>
		<title>Satoshi Client Sockets and Messages</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Sockets_and_Messages&amp;diff=35273"/>
		<updated>2013-01-18T05:05:33Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Message Thread */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
The original bitcoin client uses a multithreaded approach to socket&lt;br /&gt;
handling and messages processing. There is one thread that handles&lt;br /&gt;
socket communication (ThreadSocketHandler) and one (ThreadMessageHandler)&lt;br /&gt;
which handles pulling messages off sockets and calling the&lt;br /&gt;
processing routines. Both of these threads are in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp].&lt;br /&gt;
The message processing routines are in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], however.&lt;br /&gt;
&lt;br /&gt;
==Socket Thread==&lt;br /&gt;
The socket thread reads the sockets and places data into a CDataStream&lt;br /&gt;
associated with each node called vRecv. The Satoshi client uses C++&lt;br /&gt;
serialization operators &amp;gt;&amp;gt; and &amp;lt;&amp;lt; to read and write to a CDataStream&lt;br /&gt;
and then it uses generic routines to move the data between the streams&lt;br /&gt;
and sockets.&lt;br /&gt;
&lt;br /&gt;
==Message Thread==&lt;br /&gt;
The message thread reads and processes all messages from each node in&lt;br /&gt;
sequence, and then it sends messages to each node that should be sent&lt;br /&gt;
messages. That is all it does.&lt;br /&gt;
&lt;br /&gt;
Specifically, ThreadMessageHandler2 calls ProcessMessages(), which is&lt;br /&gt;
located in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], to pull messages off each node&#039;s socket and&lt;br /&gt;
process them. Then it calls SendMessages(), which is also located&lt;br /&gt;
in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp] to create and send any messages appropriate for each node.&lt;br /&gt;
&lt;br /&gt;
ProcessMessages() attempts to find a message start signature in the&lt;br /&gt;
vRecv stream. If it finds a message start, it deletes everything&lt;br /&gt;
prior to the start. Then it reads the header, extracts the message&lt;br /&gt;
type, and calls ProcessMessage on the message.&lt;br /&gt;
&lt;br /&gt;
SendMessages() actually creates and sends messages; it does not&lt;br /&gt;
just send preexisting queued messages. It goes through various&lt;br /&gt;
maps looking for work to do and produces a message and calls&lt;br /&gt;
PushMessage method of CNode to send the message. PushMessage&lt;br /&gt;
queues outbound data in the vSend data stream. (See PushMessage() in&lt;br /&gt;
[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp]). The socket thread handler then pulls data off the&lt;br /&gt;
vSend data stream and calls send on the socket to send the data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Sockets_and_Messages&amp;diff=35272</id>
		<title>Satoshi Client Sockets and Messages</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Sockets_and_Messages&amp;diff=35272"/>
		<updated>2013-01-18T05:04:10Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
The original bitcoin client uses a multithreaded approach to socket&lt;br /&gt;
handling and messages processing. There is one thread that handles&lt;br /&gt;
socket communication (ThreadSocketHandler) and one (ThreadMessageHandler)&lt;br /&gt;
which handles pulling messages off sockets and calling the&lt;br /&gt;
processing routines. Both of these threads are in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp].&lt;br /&gt;
The message processing routines are in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], however.&lt;br /&gt;
&lt;br /&gt;
==Socket Thread==&lt;br /&gt;
The socket thread reads the sockets and places data into a CDataStream&lt;br /&gt;
associated with each node called vRecv. The Satoshi client uses C++&lt;br /&gt;
serialization operators &amp;gt;&amp;gt; and &amp;lt;&amp;lt; to read and write to a CDataStream&lt;br /&gt;
and then it uses generic routines to move the data between the streams&lt;br /&gt;
and sockets.&lt;br /&gt;
&lt;br /&gt;
==Message Thread==&lt;br /&gt;
The message thread reads and processes all messages from each node in&lt;br /&gt;
sequence, and then it sends messages to each node that should be sent&lt;br /&gt;
messages. That is all it does.&lt;br /&gt;
&lt;br /&gt;
Specifically, ThreadMessageHandler2 calls ProcessMessages(), which is&lt;br /&gt;
located in main.cpp, to pull messages off each node&#039;s socket and&lt;br /&gt;
process them. Then it calls SendMessages(), which is also located&lt;br /&gt;
in main.cpp to create and send any messages appropriate for each node.&lt;br /&gt;
&lt;br /&gt;
ProcessMessages() attempts to find a message start signature in the&lt;br /&gt;
vRecv stream. If it finds a message start, it deletes everything&lt;br /&gt;
prior to the start. Then it reads the header, extracts the message&lt;br /&gt;
type, and calls ProcessMessage on the message.&lt;br /&gt;
&lt;br /&gt;
SendMessages() actually creates and sends messages; it does not&lt;br /&gt;
just send preexisting queued messages. It goes through various&lt;br /&gt;
maps looking for work to do and produces a message and calls&lt;br /&gt;
PushMessage method of CNode to send the message. PushMessage&lt;br /&gt;
queues outbound data in the vSend data stream. (See PushMessage() in&lt;br /&gt;
net.cpp). The socket thread handler then pulls data off the&lt;br /&gt;
vSend data stream and calls send on the socket to send the data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=35271</id>
		<title>Satoshi Client Node Connectivity</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=35271"/>
		<updated>2013-01-18T05:03:16Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Inbound Accepting and Disconnecting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==&amp;lt;br/&amp;gt;Overview==&lt;br /&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;
&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;
&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;
&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;
&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;
&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;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=35270</id>
		<title>Satoshi Client Node Connectivity</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=35270"/>
		<updated>2013-01-18T05:02:02Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Inbound Accepting and Disconnecting */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==&amp;lt;br/&amp;gt;Overview==&lt;br /&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;
&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;
&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;
&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;
&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;
&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 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 net.cpp)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=35269</id>
		<title>Satoshi Client Node Connectivity</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Connectivity&amp;diff=35269"/>
		<updated>2013-01-18T04:50:17Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==&amp;lt;br/&amp;gt;Overview==&lt;br /&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;
&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;
&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;
&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;
&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;
&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 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 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 net.cpp)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35268</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35268"/>
		<updated>2013-01-18T04:41:10Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* IRC Addresses */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in [https://github.com/bitcoin/bitcoin/blob/master/src/irc.cpp irc.cpp]), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp])&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See [https://github.com/bitcoin/bitcoin/blob/master/src/irc.cpp irc.cpp].&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp].&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in [https://github.com/bitcoin/bitcoin/blob/master/src/db.cpp db.cpp].&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35267</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35267"/>
		<updated>2013-01-18T04:40:11Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Local Client&amp;#039;s External Address */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in [https://github.com/bitcoin/bitcoin/blob/master/src/irc.cpp irc.cpp]), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp])&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp].&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in [https://github.com/bitcoin/bitcoin/blob/master/src/db.cpp db.cpp].&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35266</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35266"/>
		<updated>2013-01-18T04:39:08Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in irc.cpp), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in net.cpp)&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp].&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in [https://github.com/bitcoin/bitcoin/blob/master/src/db.cpp db.cpp].&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35265</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35265"/>
		<updated>2013-01-18T04:34:30Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Addresses stored in the Database */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in net.cpp handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in irc.cpp), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in net.cpp)&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp].&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in [https://github.com/bitcoin/bitcoin/blob/master/src/db.cpp db.cpp].&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35264</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35264"/>
		<updated>2013-01-18T04:33:08Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Old Address Cleanup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in net.cpp handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in irc.cpp), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in net.cpp)&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp].&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp], there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in db.cpp.&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35263</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35263"/>
		<updated>2013-01-18T04:32:36Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Self broadcast */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in net.cpp handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in irc.cpp), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in net.cpp)&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in [https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp main.cpp].&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in main.cpp, there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in db.cpp.&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35262</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35262"/>
		<updated>2013-01-18T04:30:16Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* Ongoing &amp;quot;addr&amp;quot; advertisements */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in net.cpp handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in irc.cpp), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in net.cpp)&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in [https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp net.cpp] will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in main.cpp.&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in main.cpp, there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in db.cpp.&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35261</id>
		<title>Satoshi Client Node Discovery</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Satoshi_Client_Node_Discovery&amp;diff=35261"/>
		<updated>2013-01-18T04:27:08Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: /* DNS Addresses */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Overview==&lt;br /&gt;
&lt;br /&gt;
The Satoshi client discovers the IP address and port of nodes in several different ways.&lt;br /&gt;
&lt;br /&gt;
# Nodes discover their own external address by various methods.&lt;br /&gt;
# Nodes receive the callback address of remote nodes that connect to them.&lt;br /&gt;
# Nodes connect to IRC to receive addresses.&lt;br /&gt;
# Nodes makes DNS request to receive IP addresses.&lt;br /&gt;
# Nodes can use addresses hard coded into the software.&lt;br /&gt;
# Nodes exchange addresses with other nodes.&lt;br /&gt;
# Nodes store addresses in a database and read that database on startup.&lt;br /&gt;
# Nodes can be provided addresses as command line arguments&lt;br /&gt;
# Nodes read addresses from a user provided text file on startup&lt;br /&gt;
&lt;br /&gt;
A timestamp is kept for each address to keep track of when the node&lt;br /&gt;
address was last seen. The AddressCurrentlyConnected in net.cpp handles&lt;br /&gt;
updating the timestamp whenever a message is received from a node.&lt;br /&gt;
Timestamps are only updated on an address and saved to the database&lt;br /&gt;
when the timestamp is over 20 minutes old.&lt;br /&gt;
&lt;br /&gt;
See the Node Connectivity article for information on which type of&lt;br /&gt;
addresses take precedence when actually connecting to nodes.&lt;br /&gt;
&lt;br /&gt;
In the first section we will cover how a node handles a request for&lt;br /&gt;
addresses via the &amp;quot;getaddr&amp;quot; message. By understanding the role of&lt;br /&gt;
timestamps, it will become more clear why timestamps are kept the way&lt;br /&gt;
they are for each of the different ways an address is discovered.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Handling Message &amp;quot;getaddr&amp;quot;==&lt;br /&gt;
&lt;br /&gt;
When a node receives a &amp;quot;getaddr&amp;quot; request, it first figures out how many&lt;br /&gt;
addresses it has that have a timestamp in the last 3 hours.&lt;br /&gt;
Then it sends those addresses, but if there are more than 2500 addresses&lt;br /&gt;
seen in the last 3 hours, it selects around 2500 out of the available&lt;br /&gt;
recent addresses by using random selection.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Now lets look at the ways a node finds out about node addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Discovery Methods==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Local Client&#039;s External Address===&lt;br /&gt;
The client uses two methods to determine its own external, routable IP address: it uses IRC, preferably, and if that does not succeed, it uses public web services which return the information.&lt;br /&gt;
&lt;br /&gt;
From a thread created for this work (called ThreadIRCSeed in irc.cpp), the client makes an IRC connection to 92.243.23.21 or irc.lfnet.org, if the direct IP connection fails. The port is 6667.&lt;br /&gt;
&lt;br /&gt;
If the connection succeeds, the client issues a USERHOST command to the IRC server, in order to get their own IP address.&lt;br /&gt;
&lt;br /&gt;
The client also runs a thread called ThreadGetMyExternalIP (in net.cpp)&lt;br /&gt;
which attempts to determine the client&#039;s IP address as seen from the outside&lt;br /&gt;
world.  It gives the IRC thread a chance to discover the IP address&lt;br /&gt;
first, sleeping and checking periodically for 2 minutes, and then it&lt;br /&gt;
proceeds if the IRC method did not succeed.&lt;br /&gt;
&lt;br /&gt;
First, it attempts to connect to 91.198.22.70 port 80, which should be&lt;br /&gt;
the checkip.dyndns.org server. If connection fails, a DNS request is made&lt;br /&gt;
for checkip.dyndns.org and a connection is attempted to that address.&lt;br /&gt;
Next, it attemps to connect to 74.208.43.192 port 80, which should be&lt;br /&gt;
the www.showmyip.com server. If connection fails, a DNS request is made&lt;br /&gt;
for www.showmyip.com and a connection is attempted to that address.&lt;br /&gt;
&lt;br /&gt;
For each address attempted above, the client attempts to connect,&lt;br /&gt;
send a HTTP request, read the appropriate response line, and parse the&lt;br /&gt;
IP address from it.&lt;br /&gt;
If this succeeds, the IP is returned, it is advertised to any connected&lt;br /&gt;
nodes, and then the thread finishes (without proceeding to the next&lt;br /&gt;
address).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Connect Callback Address===&lt;br /&gt;
When a node receives an initial &amp;quot;version&amp;quot; message, and that node initiated the connection, then the node advertises its address to the remote so that it can connect back to the local node if it wants to.&lt;br /&gt;
&lt;br /&gt;
After sending its own address, it sends a &amp;quot;getaddr&amp;quot; request message to the remote node to learn about more addresses, if the remote node version is recent or if the local node does not yet have 1000 addresses.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===IRC Addresses===&lt;br /&gt;
&lt;br /&gt;
As of version 0.6.x the Bitcoin client no longer uses IRC bootstrapping by default.  This documentation below is accurate for most prior versions.&lt;br /&gt;
&lt;br /&gt;
In addition to learning and sharing its own address, the node learns about other node addresses via an IRC channel. See irc.cpp.&lt;br /&gt;
&lt;br /&gt;
After learning its own address, a node encodes its own address into a string to be used as a nickname. Then, it randomly joins an IRC channel named between #bitcoin00 and #bitcoin99. Then it issues a WHO command. The thread reads the lines as they appear in the channel and decodes the IP addresses of other nodes in the channel. It does this in a loop, forever, until the node is shutdown.&lt;br /&gt;
&lt;br /&gt;
When the client discovers an address from IRC, it sets the timestamp on the address to the current time, but it uses a &amp;quot;penalty&amp;quot; of 51 minutes, which means it looks like it was actually seen almost an hour ago.&lt;br /&gt;
&lt;br /&gt;
===DNS Addresses===&lt;br /&gt;
Upon startup, if peer node discovery is needed, the client then issues DNS requests to learn about the addresses of other peer nodes.  The client includes a list of host names for DNS services that are seeded.  As-of May 17, 2012 the list (from net.cpp&amp;lt;ref&amp;gt;[https://github.com/bitcoin/bitcoin/blob/master/src/net.cpp bitcoin/src/net.cpp at master]&amp;lt;/ref&amp;gt;) includes:&lt;br /&gt;
&lt;br /&gt;
* bitseed.xf2.org&lt;br /&gt;
* dnsseed.bluematt.me&lt;br /&gt;
* seed.bitcoin.sipa.be&lt;br /&gt;
* dnsseed.bitcoin.dashjr.org&lt;br /&gt;
&lt;br /&gt;
A DNS reply can contain multiple IP addresses for a requested name.&lt;br /&gt;
&lt;br /&gt;
Addresses discovered via DNS are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Hard Coded &amp;quot;Seed&amp;quot; Addresses===&lt;br /&gt;
The client contains hard coded IP addresses that represent bitcoin nodes.&lt;br /&gt;
&lt;br /&gt;
These addresses are only used as a last resort, if no other method has produced any addresses at all. When the loop in the connection handling thread ThreadOpenConnections2() sees an empty address map, it uses the &amp;quot;seed&amp;quot; IP addresses as backup.&lt;br /&gt;
&lt;br /&gt;
There is code is move away from seed nodes when possible. The presumption is that this is to avoid overloading those nodes. Once the local node has enough addresses (presumably learned from the seed nodes), the connection thread will close seed node connections.&lt;br /&gt;
&lt;br /&gt;
Seed Addresses are initially given a zero timestamp,&lt;br /&gt;
therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
===Ongoing &amp;quot;addr&amp;quot; advertisements===&lt;br /&gt;
Nodes may receive addresses in an &amp;quot;addr&amp;quot; message after having sent a &amp;quot;getaddr&amp;quot; request, or &amp;quot;addr&amp;quot; messages may arrive  unsolicited, because nodes advertise addresses gratuitously when they relay addresses (see below), when they advertise their own address periodically, and when a connection is made.&lt;br /&gt;
&lt;br /&gt;
If the address is from a really old version, it is ignored; if from a not-so-old version, it is ignored if we have 1000 addresses already.&lt;br /&gt;
&lt;br /&gt;
If the sender sent over 1000 addresses, they are all ignored.&lt;br /&gt;
&lt;br /&gt;
Addresses received from an &amp;quot;addr&amp;quot; message have a timestamp, but the timestamp is not necessarily honored directly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For every address in the message:&lt;br /&gt;
# If the timestamp is too low or too high, it is set to 5 days ago.&lt;br /&gt;
# We subtract 2 hours from the timestamp and add the address.&lt;br /&gt;
&lt;br /&gt;
Note that when any address is added, for any reason, the code that calls AddAddress() does not check to see if it already exists. The AddAddresss() function in net.cpp will do that, and if the address already exists, further processing is done to update the address record. If the advertised services of the address have changed, that is updated and stored.&lt;br /&gt;
&lt;br /&gt;
If the address has been seen in the last 24 hours and the timestamp is currently over 60 minutes old, then it is updated to 60 minutes ago.&lt;br /&gt;
&lt;br /&gt;
If the address has NOT been seen in the last 24 hours, and the timestamp is currently over 24 hours old, then it is updated to 24 hours ago.&lt;br /&gt;
&lt;br /&gt;
====Address Relay====&lt;br /&gt;
&lt;br /&gt;
Once addresses are added from an &amp;quot;addr&amp;quot; message (see above), they then may be relayed to the other nodes. First, the following criteria must be set [9]:&lt;br /&gt;
&lt;br /&gt;
#The address timestamp, after processing, is within 60 minutes of the current time&lt;br /&gt;
#The &amp;quot;addr&amp;quot; message contains 10 addresses or less&lt;br /&gt;
#And fGetAddr is not set on the node. fGetAddr starts false, is set to true when we request addresses from a node, and it is cleared when we receive less than 1000 addresses from a node.&lt;br /&gt;
#The address must be routable.&lt;br /&gt;
&lt;br /&gt;
When they meet the above criteria, the node hashes all the eligible node IP addresses, as well as the current day in the form of an integer, and the two nodes with the lowest hash value are chosen to have the address relayed to them.&lt;br /&gt;
&lt;br /&gt;
====Self broadcast====&lt;br /&gt;
&lt;br /&gt;
Every 24 hours, the node advertises its own address to all connected nodes.&lt;br /&gt;
&lt;br /&gt;
It also clears the list of the addresses we think the remote node has, which will trigger a refresh of sends to nodes. This code is in SendMessages() in main.cpp.&lt;br /&gt;
&lt;br /&gt;
====Old Address Cleanup====&lt;br /&gt;
&lt;br /&gt;
In SendMessages() in main.cpp, there is code to remove old addresses.&lt;br /&gt;
&lt;br /&gt;
This is done every ten minutes, as long as there are 3 active connections.&lt;br /&gt;
&lt;br /&gt;
The node erases messages that have not been used in 14 days as long as there are at least 1000 addresses in the map, and as long as the erasing process has not taken more than 20 seconds.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Addresses stored in the Database===&lt;br /&gt;
Addresses are stored in the database when AddAddress() is called.&lt;br /&gt;
&lt;br /&gt;
Addresses are read on startup when AppInit2() calls LoadAddresses(), which is located in db.cpp.&lt;br /&gt;
&lt;br /&gt;
Currently, it appears all addresses are stored all at once whenever any address is stored or updated&amp;lt;ref&amp;gt;[http://bitcointalk.org/index.php?topic=26436.0 Lots of disk activity on Bitcoin startup: easy fix?]&amp;lt;/ref&amp;gt;. Indeed, AddAddress is seen to take over .01 seconds in various testing and is typically called tens of thousands of times in the initial 12 hours of running the client.&lt;br /&gt;
&lt;br /&gt;
===Command Line Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The user can specify nodes to connect to with the&lt;br /&gt;
 -addnode &amp;lt;ip&amp;gt; &lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
Addresses provided on the command line are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
The user can also specify an address to connect to with the -connect &amp;lt;ip&amp;gt;&lt;br /&gt;
command line argument. Multiple nodes may be specified.&lt;br /&gt;
&lt;br /&gt;
The -connect argument differs from -addnode in that -connect addresses are not added to the address database and when -connect is specified, only those addresses are used.&lt;br /&gt;
&lt;br /&gt;
===Text File Provided Addresses===&lt;br /&gt;
&lt;br /&gt;
The client will automatically read a file named &amp;quot;addr.txt&amp;quot; in the bitcoin data directory and will add any addresses it finds in there as node addresses. These nodes are given no special preference over other addresses. They are just added to the pool.&lt;br /&gt;
&lt;br /&gt;
Addresses loaded from the text file are initially given a zero timestamp, therefore they are not advertised in response to a &amp;quot;getaddr&amp;quot; request.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
* [[Network#Bootstrapping|Network - Boostrapping]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Category:Developer]]&lt;br /&gt;
[[Category:Technical]]&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
	<entry>
		<id>https://en.bitcoin.it/w/index.php?title=Talk:Work_For_Bitcoin&amp;diff=35165</id>
		<title>Talk:Work For Bitcoin</title>
		<link rel="alternate" type="text/html" href="https://en.bitcoin.it/w/index.php?title=Talk:Work_For_Bitcoin&amp;diff=35165"/>
		<updated>2013-01-16T23:26:57Z</updated>

		<summary type="html">&lt;p&gt;An0nymous: Created page with &amp;quot;Looks like a great intelligence collapse&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Looks like a great intelligence collapse&lt;/div&gt;</summary>
		<author><name>An0nymous</name></author>
	</entry>
</feed>