Transaction
If you create multiple records at the same time to spend your bitcoins, then the system will be messy. In order to solve this kind of problem, Satoshi Nakamoto's solution is to use a timestamp server to solve this kind of problem. The timestamp server here is not a centralized thing. His time is taken from the median of all time connected nodes. It is also justice for the majority. The timestamp server will hash a set of data in the form of blocks, stamp it with a timestamp, and then broadcast the hash. Because the timestamp is unique, it can determine that specific data at this point in time must exist. Each timestamp will incorporate the previous timestamp into its hash, and each subsequent timestamp will enhance the previous timestamp, thus forming a chain.
All nodes receive transaction records and start packaging, but the transaction records packaged by each person may be different, and the timestamp may also be different, so the hash packaged by each node is different. This requires all nodes to reach a consensus, so how to reach a consensus? The solution is PoW, which uses proof of work to grab the right to bookkeeping. Each block is composed of a block header and a list of transactions contained in the block. The size of the deblock header is 80 bytes, 4-byte version number, 32-byte hash of the previous block, 32-byte Merkle root hash, 4-byte time stamp, and 4-byte current difficulty. 4-byte random number. The list of transactions included in the block is appended to the block header. The first transaction is a coinbase transaction, which is a special transaction for miners to obtain rewards and fees.
The 80-byte block header is the input of the proof of work. After the calculation of double SHA256, the random number (nonce) is constantly changed to make the final result less than the target value of the current network, then the problem is solved successfully and the workload is completed.
The calculation formula for the target value of the current network mentioned above is: target value = maximum target value / difficulty value
When a node finds the proof of work, it broadcasts it to the entire network, and then other nodes in the entire network verify it. If all transactions in the block are valid and no other nodes have existed before, then the block is considered valid. After the verification is passed, the node will no longer accept the same block from other nodes, and at the same time, the node will terminate its ongoing calculations that include the same transaction.
Other nodes indicate that they accept the block by moving to the next proof of work and using the hash of this block as the previous hash of the next block.
The node always considers the longest chain as the correct chain, and continues to work and extend it. But if two nodes broadcast different versions of blocks at the same time, the time when other nodes receive the block may be different due to network reasons. Generally, they will work on the block received first, but they will also reserve another block (the block here is a different version) to prevent it from becoming the longest block. Until the next proof of work is found, because the next block uses the previous block's hash as its previous hash. So one of the chains proved to be the longest honest chain. Then the node working on the other branch chain will switch camp and start working for the longest chain.