====== Mining and Consensus ====== What prevents anyone from creating a fake transaction that sends a high amount to himself? Or submitting a block with a higher block reward than normal? To make Bitcoin a fully distributed and trustless platform, all clients have to agree on a set of consensus rules. To have transactions and new blocks propagated on the network, they must conform to these rules. If a peer receives something that violates the rules, it will reject it and won't forward it to other peers. The second element that makes Bitcoin work, is PoW (Proof of Work) which prevents anyone from creating a new block at any time. To submit a new block, it must be proved that a certain amount of work went into building it. Bitcoin uses a mathematical hashing problem as proof of work and the act of solving it is known as mining. ===== Consensus Rules ===== ===== Mining ===== ==== Protocols ==== === getwork === === getblocktemplate === === Stratum === ===== Mining Pools ===== In the early days of Bitcoin, most miners were solo miners. As the difficulty increased it became impossible for smaller miners to solo mine, as it would take them months or years to mine a single block. Mining pools allow multiple miners to combine their efforts by having each miner work on a part of the problem. If any miner in the pool finds a solution, it sends the answer to the pool server and the block reward is distributed between all the miners. This means that miners will get smaller rewards, but at more frequent and predictable intervals. ==== Distributed Mining Pools ==== A problem with mining pools is that when they get too big, big parts of the network hashrate is centered on few parties. In 2020, over 90% of the network hashrate came from 10 mining pools. Although Bitcoin was intended to be a distributed and trustless platform, the mining side of Bitcoin is fairly centralized. To overcome this problem, the idea of distributed, or peer to peer mining pools was introduced. ===== Majority Attack ===== When two miners find a new block at the same time and both submit them to the network, all the peers will create two branches out of the previous block. It is possible that different miners will mine on different branches and a block might be submitted on top of either branch. The branch with the highest accumulated difficulty is considered the **main branch** and the blockchain will take that path (**need to explain better**). It is possible if an attacker controls a sufficient share of the network hashrate (around 50% or more) he can mine blocks in to a different branch, but not immediately submitting them to the network, while a healthy blockchain keeps building up. He can then submit his blocks, which may exclude some transactions in the healthy chain, and forming a new main branch if his branch has a higher total difficulty than the other branch. This would allow the attacker to double-spend his funds by excluding them from the blocks he mines, while the merchant thinks it has been included in the blockchain, at first.