Traditional transaction model
Consider a transfer scenario, Talia (300) transfers 100 yuan to Lindsey(0) wechat, Lindsey(100) transfers 100 yuan to Cooper(300), and Cooper transfers 500 yuan to Merlin (0), How to ensure the correctness of transactions.
Transaction characteristics: With wechat as the intermediate node, the correct conduct of transactions depends on wechat, which keeps all transaction records. All transactions take place in a network centered on wechat. You can only hope wechat doesn’t mess with your balance.
The problem
Without wechat, alipay, the traditional trusted third party, in a completely anonymous decentralized network, how can the aforementioned transaction be properly conducted, how can you send me money?
To solve this problem, in 2008, a man named Satoshi Nakamoto solved the problem with a nine-page paper. The man satoshi Nakamoto used those nine pages to create 1 trillion yuan worth of Bitcoin and the technology that underlies it, blockchain
Bitcoin: A Peer-to-Peer Electronic Cash System
Blockchain and Bitcoin
Without a trusted third party in a decentralized network, the biggest problem is that everyone can’t trust each other, so in the above scenario, every business deal need to broadcast, the purpose is in the network, let everyone know the ins and outs of each a sum of money in a transaction carries an electronic signature in the broadcast, in the network, each node can verify the signature, Verification passes, the transaction is recorded in the ledger. That gives Talia 100 less, Lindsey 100 more. So the question is how do you make sure that I really have 100 pieces? The ledger that all the nodes in the network hold will help them make sure that the ledger is the block, and when you connect all the books together, it’s the block chain.
Block chain
In the Bitcoin system, blockchain records all transactions since the creation of Bitcoin. Now there are about 600,000 blocks, each block records 2,000-3,000 transactions. How much money is in each account, where it comes from, where it is spent, is clearly and transparently recorded. In a blockchain network, everyone holds the same ledger (distributed ledger), and when I transfer $100 to Lindsey, everyone’s ledger starts backtracking to check if I have $100, and if I don’t, the transfer fails. As can be seen, the reliability of accounts is the cornerstone of digital currency, and if the ledger goes wrong, nothing will work. This raises two new questions:
- Who will do the bookkeeping for everyone?
- How to ensure ledger is not falsified?
Who will do the bookkeeping for everyone
If everyone could keep books, each book might record different transactions and the order in which they were made. If someone was cooking the books, it would be even more confusing. It would be impossible to get an open book. So, the bookkeeper needs to be accepted by everyone so that the books written down can be unified, which is also known as the consensus mechanism
The consensus mechanism is the rule that decides who is in charge. In a decentralized world, where everyone’s word counts, the question is who to listen to when no one agrees? So there should be a set of rules that ultimately determine who you should listen to, and those rules are consensus mechanisms. The importance of the consensus mechanism is that it produces the final arbiter, who ultimately does what he says. In other words, consensus mechanism indirectly obtains the result of decision.
In the Bitcoin system, Mr. Nakamoto’s plan is to Work out the questions, and whoever works out the answers first gets the right to keep the books, a mechanism known as POW. The nature of the proof of work is exhaustive, and the more powerful the machine is, the more likely it is to work out the answer.
The calculation process
Understand two important parameters, difficulty value and target value
The difficulty value varies with the network, in order to ensure that a block is generated every 10 minutes under different network conditions
New Difficulty value = Old difficulty value * (last 2016 blocks / 20160 minutes)Copy the code
Proof of work requires a target value. The Target value of bitcoin proof of work is calculated by the following formula:
Target = maximum target/difficulty The biggest target for a constant value: 0 x00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffCopy the code
The size of the target is inversely proportional to the difficulty value. Proof of work in Bitcoin is achieved by the fact that the miners must calculate a block hash value less than the target value.
Specific process:
- Generate Coinbase transactions and form a trade list with all other transactions that are ready to be packaged into the block, using the Merkle Tree algorithm to generate the Merkle Root Hash
- Assemble Merkle Root Hash and other related fields into Block headers, using the 80-byte Block Header as input for proof of work
- Keep changing the random number in the block header, namely the value of nonce, and do double SHA256 operation (SHA256(SHA256(Block_Header)) on the block header after each change, and compare the result value with the target value of the current network. If the value is smaller than the target value, the problem is solved successfully and the proof of work is completed.
But once you figure it out, everyone in the network can instantly verify that you did it right, and if you did, they can link that block back to the ledger and start packing the next block. In this way, everyone in the network has the same ledger, which is updated in real time, and the first node to complete the packaging gets bitcoins in order to give everyone an incentive to do the bookkeeping, a process known as mining. So through the way of reward, estimated that we do bookkeeping, will select the bookkeeping.
How to ensure ledger is not falsified
! [](C:\Users\Administrator\Desktop\block info.png)
To prevent the ledger from being tampered with, each block is recorded with a hash value, also known as a hash pointer, a pointer that moves forward and eventually points to the first creation block, tying all the blocks together. It can be understood as a one-way linked list where each block stores the address of the previous block. Above, the next block address is the data processed by the Bitcoin browser for users to view.
If you modify any inside a block of an arbitrary value, then the hash value of the blocks will change, let the next block pointer failure, so you also need to modify the next block pointer, modify the pointer and causes changes in the hash value of the blocks, and optional need to recalculate the random Numbers, is completed, will also have to modify all subsequent blocks, in turn to effort.
At the same time, because of the existence of electronic signature, can not forge others to their own transfer. And because of historical ledgers, transactions can be traced back and money cannot be conjured up out of thin air, which ensures that the books cannot be falsified.
Question 1: What if two people package a block at the same time?
Because of network delays, it is possible for two miners to dig blocks at the same time. If they participate in the bookkeeping together, it will cause the blockchain to fork. Do we admit it both ways?
Longest chain principle
Of course not, we have to establish the longest chain, the longest chain is the hardest to tamper with. You have to change it all together, of course, the workload is the biggest, which helps maintain system security. The system makes a trade-off based on the longest chain, and which of the new blocks will make the blockchain longer is the longest chain.
When a miner mines, the hash is generated randomly, and the network is delayed, it is possible for two miners to dig blocks at the same time. When this happens, if they keep accounts together, the blockchain forks.
At this point, the system makes a trade-off based on the longest chain principle, which blocks will be recorded as new blocks are created to make the blockchain in which they are located longer. All miners mining on the longest chain helps maintain the uniqueness of the blockchain ledger.
The longest chain in a blockchain is known as the “main chain.” There may be write branches on the side of the main chain, and these branches are often referred to as the standby chain. If the addition of a block causes the standby chain to accumulate more work, the standby chain will be used as the new main chain.
Problem 2: The problem brought by the boss who listens to the boss
51 attack, fake ledger as main chain
Todo: Bitgold to be perfected
Question 3: Since the ledger records transaction data, where did the money for the transaction on the first block come from
Satoshi wrote dead, gave himself 50 coins