How Does Bitcoin Mining Work?
Bitcoin involves a network of computers around the world that constantly broadcast and relay new transactions to each other. Each computer on this network is a node. Because of Bitcoin's decentralized nature, some nodes can appear or disappear at random times without impacting the network as a whole. No special central nodes exist.
Nodes can be grouped into three categories: those that only broadcast transactions; those that broadcast and relay transactions; and those that broadcast, relay, and create new blocks with transactions. Each type of node requires substantially more computational resources than the previous type. The last of the three includes Bitcoin-mining nodes and requires the heftiest computers.
When a Bitcoin node is launched (i.e., when you launch Bitcoin software on your computer), it connects to other nodes through the Internet to form a somewhat haphazard (but also robust) mesh network. A mesh network has no central nodes that manage traffic. Instead, all nodes equally share the responsibility of propagating information across the network.
Figure 8-2 shows what a small piece of the Bitcoin mesh network might look like.
In this figure, you can see what happens when, for instance, you buy a cup of coffee using bitcoins. Most likely, you would do this from a mobile phone and would be running the most basic type of node, the broadcast-only node (shown as the circle labeled Bat the top of the figure). The transaction steps are as follows:
1. The starting node (your phone) creates a transaction that transfers your coins to the Bitcoin address owned by the coffee shop. This node then immediately sends this transaction to its peers on the mesh network, which are most likely relay nodes.
Figure 8-2: A conceptual drawing of the Bitcoin network. The circles represent nodes, all of which are arbitrarily connected to other nodes via network connections, shown as lines. Transactions and newly mined blocks are continuously broadcast over this network.
2. The relay node simply propagates this transaction to other relay nodes, allowing the transaction to quickly propagate everywhere and to everyone. Actually, it is not quite that simple, since the relay nodes need to be wary of malicious or spammy transactions: If a relay node just forwarded any message, the Bitcoin network would quickly collapse in a flood of junk transactions. Therefore, all the relay nodes check that the transaction is correctly formatted, make sure it has valid signatures, and look through the most current version of the blockchain to ensure the money being spent is verifiably present in the source account of the transaction.
3. If the transaction passes muster, it arrives within seconds at all the mining nodes on the network. These mining nodes add this transaction to a preliminary block, which they will attempt to mine (a process we'll describe in more detail shortly).
4. If the mining is successful, the newly mined block is then broadcast across the network, confirming the block's transactions and giving the miner a mining reward. Although it typically takes a few minutes for a block to be mined, once the block has been found, it will again traverse the entire network within seconds.
Ultimately, every transaction must be recorded on the blockchain. So nodes that only broadcast must link up directly with nodes that mine or indirectly via nodes that relay. Mining nodes may also apply specific arbitrary criteria to transactions, such as favoring those with a larger transaction fee. If the fee is too low, some mining (or relay nodes) may ignore the transaction. However, just because some nodes ignore a transaction doesn't mean others will: As long as the transaction finds a mining node that accepts it, the transaction will eventually be added to the blockchain. Usually, mining nodes collect as many transactions as possible (to collect the most fees). Although each transaction has only a small fee attached to it, many thousands of transactions can be included in a block, and the sum of these fees, which are paid to the miner, can be substantial. But due to memory constraints, the number of transactions that can be included in a block has a maximum limit. For this reason, some miners exclude transactions whose fees are too low (or zero).
Although all mining nodes collect transactions and organize them into blocks, only one of the nodes (the lucky one) adds its block to the block-chain. The fortunate miner then collects the mining reward, which is the sum of the block reward (the newly minted bitcoins in each block) and all the transaction fees for the added block. Nodes that relay transactions and those miners who didn't add a block do not receive any bitcoins from transaction fees or otherwise.
Once the lucky miner is selected, the new block is broadcast to the rest of the network, and all of the other mining nodes stop working on their old block and start working on a new one. So how is the lucky node decided on? It is the node that solves the block by finding a special input to the SHA256 hash function.
-  The maximum limit has generally been increased over time as concerns about memory constraints have lessened. But there will always be a maximum limit.
-  As mentioned before, two or more miners could possibly add a new block at the same time. This causes a split in the blockchain but is resolved by waiting to see which branch grows by yet another block first.