Cryptocurrency mining is the process in which transactions between users are verified and added to the blockchain public ledger. The process of mining is also responsible for introducing new coins into the existing circulating supply and is one of the key elements that allow cryptocurrencies to work as a peer-to-peer decentralized network, without the need for a third party central authority.
Bitcoin is the most popular and well-established example of a mineable cryptocurrency, but it is worth noting that not all cryptocurrencies are mineable. Bitcoin mining is based on a consensus algorithm called Proof of Work.
A miner is a node in the network that collects transactions and organizes them into blocks. Whenever transactions are made, all network nodes receive them and verify their validity. Then, miner nodes gather these transactions from the memory pool and begin assembling them into a block (candidate block).
The first step of mining a block is to individually hash each transaction taken from the memory pool, but before starting the process, the miner node adds a transaction where they send themselves the mining reward (block reward). This transaction is referred to as the coinbase transaction, which is a transaction where coins get created ‘out of thin air’ and, in most cases, is the first transaction to be recorded in a new block.
After every transaction is hashed, the hashes are then organized into something called a Merkle Tree (or a hash tree) - which is formed by organizing the various transaction hashes into pairs and then hashing them. The outputs are then organized into pairs and hashed once again, and the process is repeated until “the top of the tree” is reached. The top of the tree is also called a root hash (or Merkle root) and is basically a single hash that represents all the previous hashes that were used to generate it.
The root hash - along with the hash of the previous block and a random number called nonce - is then placed into the block's header. The block header is then hashed producing an output based on those elements (root hash, previous block's hash, and nonce) plus a few other parameters. The resulting output is the block hash and will serve as the identifier of the newly generated block (candidate block).
In order to be considered valid, the output (block hash) must be less than a certain target value that is determined by the protocol. In other words, the block hash must start with a certain number of zeros.
The target value - also known as the hashing difficulty - is regularly adjusted by the protocol, ensuring that the rate at which new blocks are created remains constant and proportional to the amount of hashing power devoted to the network.
Therefore, every time new miners join the network and competition increases, the hashing difficulty will raise, preventing the average block time from decreasing. In contrast, if miners decide to leave the network, the hashing difficulty will go down, keeping the block time constant even though there is less computational power dedicated to the network.
The process of mining requires miners to keep hashing the block header over and over again, by iterating through the nonce until one in the network miner eventually produces a valid block hash. When a valid hash is found, the founder node will broadcast the block to the network. All other nodes will check if the hash is valid and, if so, add the block into their copy of the blockchain and move on to mining the next block.
However, it sometimes happens that two miners broadcast a valid block at the same time and the network ends up with two competing blocks. Miners start to mine the next block based on the block they received first. The competition between these blocks will continue until the next block is mined based on either one of the competing blocks. The block that gets abandoned is called an orphan block or a stale block. The miners of this block will switch back to mining the chain of the winner block.
While the block reward is granted to the miner who discovers the valid hash first, the probability of finding the hash is equal to the portion of the total mining power on the network. Miners with a small percentage of the mining power stand a very small chance of discovering the next block on their own. Mining pools are created to solve this problem. It means pooling of resources by miners, who share their processing power over a network, to split the reward equally among everyone in the pool, according to the amount of work they contribute to the probability of finding a block.
Stay tuned for more content and don’t forget to check our other videos at Binance Academy!