Bitcoin and Blockchain
In this article we will look at the specific real world use of block chain, which is Bitcoin blockchain.
Bitcoin is a peer-to-peer payment system introduced by Satoshi Nakamoto. Payments that are made are recorded in a public ledger / journal. There is no central repository or single administrator and hence can be considered as a first decentralized virtual currency. The underlying implementation of bitcoin is based on blockchain.
Anyone running the bitcoin would have a copy of the global blockchain. The bitcoin software uses this information to verify that every spend is valid, accounted for and no rules are broken.
Each block in the bitcoin blockchain would contain the details of the transactions. Suppose the first block contains the transaction detail “V transferred 100 BTC to X” and then another block contains the transaction detail that “X transferred 40 BTC to Y”, then we would know that X has (100–40 = 60) BTC and Y has 40 BTC. This information can be inferred by examining the blockchain. When the bitcoin nodes accept a block after validating the transactions, the same is appended to the end of the blockchain. The individual nodes in the bitcoin blockchain who accept or reject the transactions are called miners. Each newly created block in the bitcoin blockchain contains the set of transactions that are accepted by the miners. The responsibility of the miners is to ensure that the transactions are valid and in correct order.
Computing and validating the individual bitcoin block requires computing power. As reward for validating the transactions the miners get a debit of 50 BTC called coinbase transaction. As more people join the network and generate new blocks, the number of blocks in the blockchain would increase. With increase in the number of blocks, the computing power required to validate these blocks would also increase. Currently it takes approximately around 10 minutes for a block to be generated. Making a new block is not an easy task. That is why it is impossible to rewrite the blockchain. If I wish to reverse block 50 in a blockchain 100 blocks long, then I would have to reverse block 50, but also reverse the blocks 51, 52… 98,99 and 100, spending all the computing power that went into creating those blocks.
Occasionally though, 2 miners could create a new block at the same time. In such a race condition, it is now a chance depending on which block the next block is built off. Which block is built off is down to mostly luck, timing and externalities (like whether the right people discover the block at the right time). Remember the longest chain rule. Blocks take time to be checked for correctness and so the blockchain is slow. Downloading blocks does not take much time, but it is the checking that takes time. Checking to ensure that the transactions are valid. Transactions in new blocks are checked to ensure they refer to a valid old transaction. This connecting of transactions is the slowdown of all. When bitcoin tries to connect a transaction up with its predecessor, it must search for it in the blockchain. As the blockchain increases from 100 blocks, to 1000 blocks, so too does the number of potential blocks increase from 100 to 1000 that the predecessor transaction may exist in. Searching the entire blockchain for a transaction only becomes worse as the blockchain grows.
In the previous section we saw that the bitcoin transactions are stored in a blockchain. Let us understand the role of bitcoin mining in validating and accepting new blocks in the blockchain.
The primary purpose of mining is to ensure that everyone has a consistent view of the bitcoin data. Since it is a peer-to-peer network it is important to ensure that there is a way to hold the sanctity of the data. One challenge would be to avoid double spending where some user spend the same bitcoin twice. The solution is to mine the outstanding transactions into block of transactions every 10 minutes. Conflicting or incorrect transactions are rejected as part of the mining process.
As part of mining the miners validates the transactions in the blocks and compute the hash. It is easy to produce a hash from collection of data. Bitcoin network makes it difficult to mine the data, else everyone would be hashing the transaction blocks in each second and the bitcoin would be mined very quickly. Bitcoin protocol makes it difficult in computing the hash by introducing what is called “proof of work”. The protocol does not accept any hash. It expects that the hash has certain number of zeroes at the start. Since the miners cannot change the transaction data, they have to tweak the hashing algorithm using nonce, which is the random piece of data used to produce the hash. If the hash does not fit the format then the nonce has to be changed and hash is created again. All the miners in the bitcoin network are trying to do the same and they are rewarded with the 50 BTC when they are able to create the hash the matches the proof of work.
In summary the primary purpose of mining is to enable the bitcoin nodes to reach a secure, tamper resistant consensus in the bitcoin blockchain.
Proof Of Work is a piece of data that is difficult to produce so as to satisfy certain requirements and easy to validate as to whether the data satisfies the specified requirements.
Above diagram gives the flow as to how the proof of work is computed as part of the mining process. Producing the proof of work requires computing power as it involves lots of trial and error before a valid proof of work is computed.
In the above diagram the computed hash must be less than the target value. When the computed hash is not less than the target value, the nonce has to be changed and hash has to be recomputed. The target value defines the difficulty of the mining process. The target value is recalculated or changed every 2016 blocks or approximately two weeks.
When the number of miners is high the block creation goes up, the target value is changed to increase the difficulty level to reduce the rate of block creation.