<p>I don't know the answers to all questions but let me try to help (most of the above is not verified).</p>

<ol>
<li><p><a href="https://bitcoin.org/en/full-node#port-forwarding" rel="nofollow noreferrer">https://bitcoin.org/en/full-node#port-forwarding</a>
Bitcoin nodes use ports 8333 to listen to new transactions and blocks.
Unfortunately in the picture both TCP and UDP port forwarding is selected so I cannot reason which one is used. I assume it is not HTTP as port 8333 is used. EDIT: As per Pieter's comment TCP is used.
(If you have full node with wallet you do the above. If you have lightweight wallet you send your transaction to some full node that does the above process.)</p></li>
<li><p>EDIT per Pieter's comment. The miners (full nodes) listen on port 8333 for inbound connections. Other nodes can establish connection with that node on that port. Also the node itself can establish outbound connections to other nodes that listen for inbound connections on port 8333. It is not necessary to allow for inbound conections. When connection (oubound or inbound) is established between two nodes they exchange transactions and blocks when they a) make transaction, b) mine a block, or c) receive transaction or block from some other node.  Each full node holds several connections. What is the specific protocol, and if some common exist I don't know.</p></li>
<li><p>These websites are running servers that have full bitcoin node (more likely more they have more than one node). This node contains all information about the blockchain and pool of pending transactions (as every full node does). The servers crawl this information and store the data and statistics in their databases. The website is just the frontend that shows this information from these databases.</p></li>
<li><p>EDIT per Pieter's comment. The node (miner) broadcasts the new block to all nodes it has connection with. More commonly the miner mines in pools so it sends the new block to the pool server, which acts as a node and does the above.</p></li>
</ol>

CC BY-SA 3.0
2018-01-03T14:45:32.243Z
Ok, so if I understand this correctly electrum is a light node and only full nodes can broadcast transaction to the network so it needs an intermediary full node. Otherwise your answer is really insightful and I'm only leaving it open to have some more potential visitors who may can expand your answer with the protocol specifics (tcp, ud, http, etc) but I'll mark this as answer later today.
Balázs Sáros
CC BY-SA 3.0
2018-01-03T14:58:02.483Z
Electrum is not a node at all. UDP is not used. The protocol is bidirectional, so who connects to whom doesn't matter. Things are not just announced to outbound connections, but to all connections.
Pieter Wuille
CC BY-SA 3.0
2018-01-03T14:27:54.923Z
2018-01-03T19:10:58.857Z
2018-01-03T19:10:58.857Z
croraf
<p>Bitcoin works inline with the standard internet protocols we have. When you make a transaction from your wallet application, your wallet provider which hosts a bitcoin core application will submit the transaction on your behalf and add it to its version of the blockchain. Other miners will then begin to verify(mine) that transaction to make sure that the transaction is legitimate. After the Proof of Work is completed by a particular miner, the mined block is added to the longest most authoritative blockchain. This is then propagated across the network so that all the bitcoin core applications can keep themselves updated with the blockchain. The bitcoin core comes with a JSON RPC which can be used to interact with the blockchain to explore the blocks of transactions. You can look at the API documentation
<a href="https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)" rel="nofollow noreferrer">here</a> </p>

CC BY-SA 3.0
2018-01-03T14:37:11.007Z
As I stated in my question I'm very well aware of the conceptual working of bitcoin and only curious about the technical details which your answer almost entirely lacks. The only (seemingly) useful bit of information is JSON RPC but you didn't provide any details or info on it.
Balázs Sáros
CC BY-SA 3.0
2018-01-03T14:40:45.033Z
Here is a link to the API docs: https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)
stark49
CC BY-SA 3.0
2018-01-03T14:28:58.570Z
2018-01-03T14:40:28.400Z
2018-01-03T14:40:28.400Z
stark49
<p><strong>The Bitcoin P2P network</strong></p>

<p>The <a href="https://bitcoin.org/en/developer-reference#p2p-network" rel="nofollow noreferrer">Bitcoin P2P network</a> is a randomly-wired gossip network. This means that all nodes make arbitrary connections to other peers (using various ways to discover new addresses) using a custom TCP protocol, usually using port 8333. Typical nodes create 8 outgoing connections, and if publicly reachable, accept up to a few 100 incoming connections.</p>

<p>It being a gossip network means that nodes send <a href="https://bitcoin.org/en/developer-reference#inv" rel="nofollow noreferrer">messages</a> to each other of the form "do you have block with hash X" or "do you have transaction with hash X", after which the peer can respond by asking for the full transaction or block. Simplified, this means that eventually every node learns about every block. The protocol is bidirectional - it doesn't matter who connected to whom; both peers will announce if they have something they don't know the other side already has as well.</p>

<p>Not all network nodes are reachable. Some only make outgoing connections. Sometimes setups are used where someone runs a publicly reachable node, and then runs a second private node that only connects out to their own public node, thereby giving some extra privacy and reduced attack surface.</p>

<p>Not all network nodes validate the blocks and transactions they see and pass on. Nodes that do are called full nodes.</p>

<p>Miners (or at least mining pools) run nodes like this, picking up transactions their nodes learn about, <a href="https://bitcoin.org/en/developer-reference#getblocktemplate" rel="nofollow noreferrer">assembling them into candidate blocks</a>, hashing to find a nonce, and if they succeed, broadcast the resulting block out to their peers using the same P2P network. Miners in this way are not special - people don't directly submit things to miners.</p>

<p>Since recently, an extension to the protocol is used to relay blocks much more efficiently, called <a href="https://bitcoincore.org/en/2016/06/07/compact-blocks-faq/" rel="nofollow noreferrer">Compact Blocks</a>. It makes use of the fact that most nodes will already have most of the transactions that are added to a new block.</p>

<p><strong>Specialized relay networks</strong></p>

<p>Additional networks exist of groups of nodes that use specialized protocols to very quickly interconnect nodes. One such protocol is <a href="http://bitcoinfibre.org/" rel="nofollow noreferrer">FIBRE</a>, which is in fact UDP based and makes heavy use of error correcting codes to avoid roundtrips.</p>

<p><strong>Electrum</strong></p>

<p>Ironically, Electrum in your question is a bad example. Electrum is not a P2P client (not even a light one). Instead, it uses a client-server architecture, and connects to specialized servers (which are themselves Bitcoin P2P nodes however).</p>

CC BY-SA 3.0
2018-01-03T17:09:03.987Z
Thanks for the detailed explanation! What about the online block explorers? Can it be done on client side?
Balázs Sáros
CC BY-SA 3.0
2018-01-03T17:13:00.037Z
I don't understand the question.
Pieter Wuille
CC BY-SA 3.0
2018-01-03T17:17:27.400Z
Oh, nevermind. Block explorers do everything on the server side.
Pieter Wuille
CC BY-SA 3.0
2018-01-03T17:19:58.080Z
I see, thanks! Could you please include it in your answer because I addressed it in the question and someone may look for the info in your answer?
Balázs Sáros
CC BY-SA 3.0
2018-01-03T17:29:16.277Z
Perhaps it's better to move that to a different question; usually you shouldn't ask multiple questions in one thread (as it hurts searchability of answers). I think I could answer the other ones nicely together, but blockexplorers seem pretty unrelated.
Pieter Wuille
CC BY-SA 3.0
2018-01-03T17:32:09.523Z
Ok, then I'll remove it from the question. I included it because I thought there was a way to communicate with the blockchain from a webpage directly (which turned out the be not the case) and so it fitted with other questions.
Balázs Sáros
CC BY-SA 3.0
2018-01-03T23:52:34.727Z
So my full node makes the 8 outgoing connections but I've never seen more than ~20 incoming connections.  Certainly not a few 100.  Do I stink?  Or maybe it's because I'm on an rather slow connection - 75 down/15 up - and the other nodes detect that?  (The machine itself isn't slow or under load so I don't think the machine would be contributing to latency ... just the network connection itself?)
davidbak
CC BY-SA 3.0
2018-01-03T23:59:29.547Z
(Or maybe it's a config setting I forgot about ...)
davidbak
CC BY-SA 3.0
2018-01-04T06:41:38.423Z
@davidbak I said "allow up to". If every node makes 8 outgoing connections, on average each will only have 8 incoming connections too. Many nodes only make outgoing though, pushing the number of incoming connections somewhat higher on those that do support them. But in general, no, 20 incoming connections is very healthy. It's only nodes that are fast and reliable and up for very long periods of time (months or more) that gather 100s of connections. Note that more connections is not in any way better for you - once above a certain number, they just slow you down.
Pieter Wuille
CC BY-SA 4.0
2018-01-03T14:51:57.590Z
2019-09-18T00:03:00.227Z
2019-09-18T00:03:00.227Z
Pieter Wuille
<p>So I have several specific question connected to this topic and I'm aware of the conceptual answers and I'm only interested in the technical details (with programming terms and concepts as I'm a developer). </p>

<p><strong>My questions are:</strong></p>

<ol>
<li><p>If I'm sending a transaction from my wallet application (e.g. electrum), what does it do under the hood? Does it use the HTTP protocol or something else? Or not even use TCP but UDP instead?</p></li>
<li><p>How does the miners "listen" to transactions? Do they listening on some port? Is there any generic protocols for "listening" like this?</p></li>
<li><p>When a miner mines a block how does he/she broadcast it to the network? Is it the same as sending a transaction from my electrum wallet?</p></li>
</ol>

<protocol><network><transaction-verification>
How are bitcoin transactions and the blockchain transferred over the internet?
Balázs Sáros
# How are bitcoin transactions and the blockchain transferred over the internet?

(Asked by: username_0 on 2018-01-03)

So I have several specific question connected to this topic and I'm aware of the conceptual answers and I'm only interested in the technical details (with programming terms and concepts as I'm a developer).

**My questions are:**

1. If I'm sending a transaction from my wallet application (e.g. electrum), what does it do under the hood? Does it use the HTTP protocol or something else? Or not even use TCP but UDP instead?
2. How does the miners "listen" to transactions? Do they listening on some port? Is there any generic protocols for "listening" like this?
3. When a miner mines a block how does he/she broadcast it to the network? Is it the same as sending a transaction from my electrum wallet?

**Tags:** protocol, network, transaction-verification


---

## 3 answers

### Answer by username_2 on 2018-01-03. Score: 30 | Accepted answer

**The Bitcoin P2P network**

The [Bitcoin P2P network](https://bitcoin.org/en/developer-reference#p2p-network) is a randomly-wired gossip network. This means that all nodes make arbitrary connections to other peers (using various ways to discover new addresses) using a custom TCP protocol, usually using port 8333. Typical nodes create 8 outgoing connections, and if publicly reachable, accept up to a few 100 incoming connections.

It being a gossip network means that nodes send [messages](https://bitcoin.org/en/developer-reference#inv) to each other of the form "do you have block with hash X" or "do you have transaction with hash X", after which the peer can respond by asking for the full transaction or block. Simplified, this means that eventually every node learns about every block. The protocol is bidirectional - it doesn't matter who connected to whom; both peers will announce if they have something they don't know the other side already has as well.

Not all network nodes are reachable. Some only make outgoing connections. Sometimes setups are used where someone runs a publicly reachable node, and then runs a second private node that only connects out to their own public node, thereby giving some extra privacy and reduced attack surface.

Not all network nodes validate the blocks and transactions they see and pass on. Nodes that do are called full nodes.

Miners (or at least mining pools) run nodes like this, picking up transactions their nodes learn about, [assembling them into candidate blocks](https://bitcoin.org/en/developer-reference#getblocktemplate), hashing to find a nonce, and if they succeed, broadcast the resulting block out to their peers using the same P2P network. Miners in this way are not special - people don't directly submit things to miners.

Since recently, an extension to the protocol is used to relay blocks much more efficiently, called [Compact Blocks](https://bitcoincore.org/en/2016/06/07/compact-blocks-faq/). It makes use of the fact that most nodes will already have most of the transactions that are added to a new block.

**Specialized relay networks**

Additional networks exist of groups of nodes that use specialized protocols to very quickly interconnect nodes. One such protocol is [FIBRE](http://bitcoinfibre.org/), which is in fact UDP based and makes heavy use of error correcting codes to avoid roundtrips.

**Electrum**

Ironically, Electrum in your question is a bad example. Electrum is not a P2P client (not even a light one). Instead, it uses a client-server architecture, and connects to specialized servers (which are themselves Bitcoin P2P nodes however).

> **Comments:**
> **username_0**: Thanks for the detailed explanation! What about the online block explorers? Can it be done on client side?  
> **username_2**: I don't understand the question.  
> **username_2**: Oh, nevermind. Block explorers do everything on the server side.  
> **username_0**: I see, thanks! Could you please include it in your answer because I addressed it in the question and someone may look for the info in your answer?  
> **username_2**: Perhaps it's better to move that to a different question; usually you shouldn't ask multiple questions in one thread (as it hurts searchability of answers). I think I could answer the other ones nicely together, but blockexplorers seem pretty unrelated.  
> **username_0**: Ok, then I'll remove it from the question. I included it because I thought there was a way to communicate with the blockchain from a webpage directly (which turned out the be not the case) and so it fitted with other questions.  
> **username_4**: So my full node makes the 8 outgoing connections but I've never seen more than ~20 incoming connections.  Certainly not a few 100.  Do I stink?  Or maybe it's because I'm on an rather slow connection - 75 down/15 up - and the other nodes detect that?  (The machine itself isn't slow or under load so I don't think the machine would be contributing to latency ... just the network connection itself?)  
> **username_4**: (Or maybe it's a config setting I forgot about ...)  
> **username_2**: @username_4 I said "allow up to". If every node makes 8 outgoing connections, on average each will only have 8 incoming connections too. Many nodes only make outgoing though, pushing the number of incoming connections somewhat higher on those that do support them. But in general, no, 20 incoming connections is very healthy. It's only nodes that are fast and reliable and up for very long periods of time (months or more) that gather 100s of connections. Note that more connections is not in any way better for you - once above a certain number, they just slow you down.  

---

### Answer by username_1 on 2018-01-03. Score: 1

I don't know the answers to all questions but let me try to help (most of the above is not verified).

1. <https://bitcoin.org/en/full-node#port-forwarding>
   Bitcoin nodes use ports 8333 to listen to new transactions and blocks.
   Unfortunately in the picture both TCP and UDP port forwarding is selected so I cannot reason which one is used. I assume it is not HTTP as port 8333 is used. EDIT: As per username_2's comment TCP is used.
   (If you have full node with wallet you do the above. If you have lightweight wallet you send your transaction to some full node that does the above process.)
2. EDIT per username_2's comment. The miners (full nodes) listen on port 8333 for inbound connections. Other nodes can establish connection with that node on that port. Also the node itself can establish outbound connections to other nodes that listen for inbound connections on port 8333. It is not necessary to allow for inbound conections. When connection (oubound or inbound) is established between two nodes they exchange transactions and blocks when they a) make transaction, b) mine a block, or c) receive transaction or block from some other node. Each full node holds several connections. What is the specific protocol, and if some common exist I don't know.
3. These websites are running servers that have full bitcoin node (more likely more they have more than one node). This node contains all information about the blockchain and pool of pending transactions (as every full node does). The servers crawl this information and store the data and statistics in their databases. The website is just the frontend that shows this information from these databases.
4. EDIT per username_2's comment. The node (miner) broadcasts the new block to all nodes it has connection with. More commonly the miner mines in pools so it sends the new block to the pool server, which acts as a node and does the above.

> **Comments:**
> **username_0**: Ok, so if I understand this correctly electrum is a light node and only full nodes can broadcast transaction to the network so it needs an intermediary full node. Otherwise your answer is really insightful and I'm only leaving it open to have some more potential visitors who may can expand your answer with the protocol specifics (tcp, ud, http, etc) but I'll mark this as answer later today.  
> **username_2**: Electrum is not a node at all. UDP is not used. The protocol is bidirectional, so who connects to whom doesn't matter. Things are not just announced to outbound connections, but to all connections.  

---

### Answer by username_3 on 2018-01-03. Score: -1

Bitcoin works inline with the standard internet protocols we have. When you make a transaction from your wallet application, your wallet provider which hosts a bitcoin core application will submit the transaction on your behalf and add it to its version of the blockchain. Other miners will then begin to verify(mine) that transaction to make sure that the transaction is legitimate. After the Proof of Work is completed by a particular miner, the mined block is added to the longest most authoritative blockchain. This is then propagated across the network so that all the bitcoin core applications can keep themselves updated with the blockchain. The bitcoin core comes with a JSON RPC which can be used to interact with the blockchain to explore the blocks of transactions. You can look at the API documentation
[here](https://en.bitcoin.it/wiki/API_reference_(JSON-RPC))

> **Comments:**
> **username_0**: As I stated in my question I'm very well aware of the conceptual working of bitcoin and only curious about the technical details which your answer almost entirely lacks. The only (seemingly) useful bit of information is JSON RPC but you didn't provide any details or info on it.  
> **username_3**: Here is a link to the API docs: https://en.bitcoin.it/wiki/API_reference_(JSON-RPC)  

---

