The method by which transactions between users are verified and added to the blockchain public ledger is known as cryptocurrency mining. Mining is also responsible for adding new coins into the existing circulating supply, and it is one of the main components that enable cryptocurrencies to operate as a peer-to-peer decentralized network, without the need for a third party central authority.
Although Bitcoin is the most well-known and well-established example of a mineable cryptocurrency, it is important to note that not all cryptocurrencies are mineable. Bitcoin mining relies on a consensus technique known as Proof of Work.
How does it work?
A miner is a network node that accumulates and organizes transactions into blocks. When a transaction is initiated, all network nodes receive it and validate its authenticity. Then, miner nodes collect these transactions from the memory pool and begin putting them together into a block (candidate block).
The initial stage in mining a block is to hash each transaction taken from the memory pool individually, but before doing so, the miner node inserts a transaction in which they transfer themselves the mining reward (block reward). This is known as the coinbase transaction, which is a transaction in which coins are created ‘out of thin air’ and, in most circumstances, is the first transaction to be recorded in a new block.
After each transaction is hashed, the hashes are grouped into a Merkle Tree (or a hash tree), which is constructed by grouping the various transaction hashes into pairs and then hashing them. The outputs are then paired and hashed again, and the process is repeated until “the top of the tree” is reached. The top of the tree, also known as the root hash (or Merkle root), is essentially a single hash that reflects all of the preceding hashes that were used to produce it.
The root hash, together with the preceding block’s hash and a random integer called nonce, is then appended to the block’s header. The block header is then hashed, and the output is based on those elements (root hash, previous block’s hash, and nonce) as well as a few other criteria. The resulting output is the block hash, which will be used to identify the freshly formed block (candidate block).
The result (block hash) must be smaller than a particular target value defined by the protocol in order to be considered genuine. To put it another way, the block hash must begin with a specific number of zeros.
The protocol adjusts the target value, also known as the hashing difficulty, on a regular basis to ensure that the pace at which new blocks are created remains stable and proportional to the amount of hashing power given to the network.
As a result, as more miners join the network and competition grows, the hashing difficulty rises, keeping the average block time from lowering. In contrast, if miners elect to leave the network, the hashing difficulty decreases, allowing the block time to remain constant despite the fact that less processing power is allocated to the network.
The mining process necessitates miners repeatedly hashing the block header by iterating over the nonce until one of the network miners gets a valid block hash. When a valid hash is discovered, the block is broadcast to the network by the founder node. All other nodes will validate the hash and, if it is, add the block to their copy of the blockchain before proceeding to mine the next block.
However, it is possible for two miners to broadcast a valid block at the same moment, resulting in two competing blocks on the network. Miners begin mining the next block based on the first block they received. The competition between these blocks will continue until either of the competing blocks gets mined as the next block. An orphan block or a stale block is a block that has been abandoned. The miners of this block will return to mining the winner block’s chain.
While the block reward is given to the miner who discovers the proper hash first, the likelihood of finding the hash is proportional to the network’s total mining power. Miners with only a small portion of the mining power have a very slim possibility of locating the next block on their own. Mining pools are formed to address this issue. It refers to the pooling of resources by miners who share their processing capacity across a network in order to split the reward equally among everyone in the pool based on the amount of labor they contribute to the chance of finding a block.