Order of Transactions and How Blockchain avoids Double Spend
The fundamental problem with Blockchain is that the order of the blocks doesn’t necessarily mean that was the order in which the transactions were done. Whenever a user initiates a transaction, the transaction goes to an unconfirmed pool. Once they are confirmed and validated they are added to the Blockchain.
This would result in two complications —
- If there is no particular order to transactions, then there could be several branches of the block as plenty of miners would mine transactions and add it to the block in parallel leading to a speculation of which chain blocks is the right one
- Double spending — Using the same transaction reference, one can create two or three transactions as all of them would go to the unconfirmed pool and different miner could mine this and add it to different blocks
Let’s see how Blockchain addresses this.
Blockchain has been designed in a way that every block carries a mathematical puzzle along with. Solving it is imperative in order to add transactions to the block and grow the chain. This puzzle is solved using cryptography and is called nonce.
A block essentially consists of — Unique identifier to the block (this is the result of the mathematical puzzle), previous block reference, the list of transactions and a nonce or random guess.
The cryptographic function that bitcoin Blockchain uses is called SHA256. SHA256 takes the input as characters and converts it into a short digest (a 32bit value). This is a really complex function as the same text with a punctuation change can give a completely different short digest value. Computers would run the entire block.
SHA256(Previous block reference, Transactions, Random guess or Nonce) = Hash Result.
The miners keep guessing this ‘Random guess or Nonce’ so that the output they get is well within a particular target.
SHA256(Previous block reference, Transactions, Random guess or Nonce) = Hash Result <Target Value.
A target value is usually fixed in such a way that the average time for solving is not more than 10 minutes and as the world’s computing power keeps increasing, the target values are changed accordingly so that solving is complex and at the same time not impossible.
Once the hash result is identified, it is the finger print or the unique identifier to the block.
Since solving this puzzle is complex, the probability that two or more miners solve blocks at similar times is rare. Even if they do it and there are two or three branches to the Blockchain, one simply builds on top of the branch they receive thereby growing the branches.
Now there is a tie. This tie is resolved when the second block is mined. If the second block is added to path 1 then, that becomes the confirmed link and the transactions in path 2 simply go back to the unconfirmed pool. The longest network in the Blockchain is usually the trusted link and everybody immediately switches to the longest branch. Hence whenever a block is added, it is good to wait for 10 mins or more to be safe. Since the transactions in the other block went to unconfirmed list, this is a problem in two ways,
- It could lead to double spending as these transactions are still not confirmed
- Users get irritated as their transactions take time to get confirmed
To address the second problem, users usually pay some incentive to get their transactions confirmed first. This is not a lot and is not needed always.
Let us take an example to see how the first problem is addressed.
Mark can have a self-made longest chain that makes the other transaction invalid thereby being put into the unconfirmed pool. But this cannot happen for multiple reasons,
- Mark cannot randomly replace a block in the middle as the block is already referencing the previous block and introducing a new block would not exactly fit
- Mark needs to have more than 50% computing power to keep building on the chain to ensure this is the longest network which is impossible. Mark is racing against other super computers who are trying to mine and solve the blocks. Hence it is not possible for Mark to replace the chain with his self-made chain as that would require immensely large computing power
- Also, a block cannot be solved before the previous block as the previous block’s reference goes into the cryptographic function too. This makes sure that Mark cannot compute the subsequent blocks before only to add them to the chain to create a longest self-made chain
Even though the transactions don’t have order, the problems that result from the lack of order is resolved using complex mathematics and cryptography thereby making Blockchain extremely efficient.