19

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?

27

The Bitcoin P2P network

The Bitcoin 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 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, 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. 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, 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). Intead, it uses a client-server architecture, and connects to specialized servers (which are themselves Bitcoin P2P nodes however).

  • Thanks for the detailed explanation! What about the online block explorers? Can it be done on client side? – Balázs Sáros Jan 3 '18 at 17:09
  • I don't understand the question. – Pieter Wuille Jan 3 '18 at 17:13
  • Oh, nevermind. Block explorers do everything on the server side. – Pieter Wuille Jan 3 '18 at 17:17
  • 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 Jan 3 '18 at 17:19
  • 1
    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 Jan 3 '18 at 17:29
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 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.)

  2. 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.

  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 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.

  • 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 Jan 3 '18 at 14:45
  • 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 Jan 3 '18 at 14:58
0

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

  • 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 Jan 3 '18 at 14:37
  • Here is a link to the API docs: en.bitcoin.it/wiki/API_reference_(JSON-RPC) – stark49 Jan 3 '18 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.