How Does Bitcoin Avoid the Double-spending Problem?
2 min read
Because digital currencies are virtual and may be easier to duplicate than actual cash, the "double spending problem" has always been a difficulty for digital currency systems. For example, if one only has $100 in his digital account, one can go to a restaurant and order a $100 set meal, but the $100 is not immediately transferred to the restaurant's account due to network delays or other problems, and then one can immediately buy another pair of shoes with the $100 – An individual transacted $100 twice , which is a common double spending problem. So, how can bitcoin avoid the problem of double-spending?
Timestamp
Timestamp is a transaction on the blockchain network that must be broadcast to the whole network every time it is generated. Within 10 minutes, the miners in charge of packaging the blocks will record all transactions generated on the blockchain into a block. The freshly formed block contains a timestamp, which does not refer to the time of the transaction, but rather to the precise moment when the block was generated, down to the second. The entire blockchain is made up of blocks carrying timestamps that are connected in linear order to form a "chain."
If the scenario described above occurs on the blockchain, what happens is that if one only has $100 worth of bitcoins in an account, one must first broadcast to spend $100 worth of bitcoins to buy a package," and then broadcast to spend $100 worth of bitcoins to buy shoes" to the entire network.
For the entire blockchain network, firstly, these two transactions will not be placed into one block because they are conflicting; secondly, if two transactions are placed into different blocks by two nodes, because of the existence of timestamps, transactions are sequential, so when a node generates a block and broadcasts it to the whole network, only the first transaction can be verified, and the second transaction will be rejected.
So, what happens if two transactions are packed by two distinct nodes and blocks are formed simultaneously? This is the principle of the longest chain.
The principle of the longest chain
In the article ‘What is PoW’, it was explained how different nodes compete for the opportunity to pack a block based on computing power competition. Careful readers may ask who will receive the reward if two nodes calculate the nonce at the same time. In this instance, two parallel blocks will be added to the original blockchain at the same moment, resulting in the blockchain forking.
In fact, due to network delays, the order of transactions received on different nodes is different across the entire blockchain.
Node 1 may, for example, receives a transaction for buying a package first, followed by another transaction for buying shoes; on the other hand, node 2 may receive the transaction for buying shoes first, followed by the transaction for buying a package.
Suppose node 1 and node 2 each packaged the first transaction they received and calculated the input value of the hash at the same time, and a fork was created on the chain, wouldn't there be a double spending problem again?