Writing blockchain from 0: explain the basic principle of blockchain with python

It has been more than ten years since the birth of artificial intelligence and blockchain. When these technologies appear, people say they will change the world, but so far, the impact of these two technologies on reality is still limited. Technically, the principle of artificial intelligence is to find rules or patterns from a large amount of data, but what is the technical principle of blockchain? In my opinion, the principle of blockchain has always been in the clouds, shrouded by many metaphysical explanations. Some explain it from economics, some explain it from sociology, and the blockchain explained from the perspective of "humanities" is always too exaggerated. These statements often contain bad intentions.

So I want to go to wucunzhen, We don't have to care about how blockchain "changes the world ", let's discuss it from the perspective of pure technology. In fact, blockchain is the same as artificial intelligence. From the perspective of technology, they all have extremely beautiful design ideas. The beauty of these designs is like Tang poetry and Song Ci, just like Picasso. It would be a pity if we didn't appreciate its beauty. Compared with artificial intelligence, I think blockchain is easier to be popularized in technology Human touch, because the former requires a lot of data and computing power, while the latter can participate as long as we master its technical principles, without too high hardware threshold.

Without much more, let's see how to write the most basic blockchain principles in python code. First, let's look at the data structure of the blockchain. It contains three parts of information. One is used to mark its own id, which is an integer. The other is used to record the id of the previous block, which is also an integer. Since the block is used to record information, it also contains a field, which is represented by history, which is used to record the current information, The biggest function of blockchain is to make this information verifiable and unchangeable. Let's look at the definition of data structure and create a file block first

class Block:
    def __init__(self):
        self.id = None
        self.history = None
        self.parent_id = None

Next, let's look at how blocks form a "chain" and how to record information. Suppose we want to record an event: Zhang San wants to buy three fish with Li Si for 100 yuan, and Li Si gives Zhang San three fish after receiving 100 yuan ", then we can use the following code to record with the blockchain and create the main Py, and then give the following code:

from block import *

block_A = Block()
block_A.id = 1
block_A.history = 'Zhang San wants three fish'

block_B = Block()
block_B.id = 2
block_B.parent_id = block_A.id
block_B.history = 'Zhang San and Li Si buy three fish'

block_C = Block()
block_C.id = 3
block_C.parent_id = block_B.id
block_C.history = 'Zhang San gave Li Si 100 yuan'

block_D = Block()
block_D.id = 3
block_D.parent_id = block_B.id
block_D.history = 'Li Si received three hundred yuan'

block_E = Block()
block_E.id = 3
block_E.parent_id = block_B.id
block_E.history = 'Li Si gives Zhang three fish'

From the code point of view, different blocks pass through parent_id forms a connection between front and back, which is the "chain" in the blockchain, but now there is another serious problem, that is, the information can be changed. Suppose Zhang San wants to come, he puts the block_ The history in E is changed to Li Si giving Zhang San two fish and then settling accounts with Li Si. What should I do, or Li Si wants to default and block_ The history in D is changed to "Li Si received thirty or fifty yuan from Zhang San", and then asked Zhang San for money. What should I do.

In order to ensure that the information is not changed, we need to encrypt or hash the content of each block, so the above code is modified as follows:

# This is a sample Python script.

# Press ⌃R to execute it or replace it with your code.
# Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings.

from block import *
import hashlib
import json

block_A = Block()
block_A.id = 1
block_A.history = 'Zhang San wants three fish'

block_B = Block()
block_B.id = 2
block_B.parent_id = block_A.id
block_B.history = 'Zhang San and Li Si buy three fish'
block_B.parent_hash = hashlib.sha256(json.dumps(block_A.__dict__).encode('utf-8')).hexdigest()

block_C = Block()
block_C.id = 3
block_C.parent_id = block_B.id
block_C.history = 'Zhang San gave Li Si 100 yuan'
block_C.parent_hash = hashlib.sha256(json.dumps(block_B.__dict__).encode('utf-8')).hexdigest()

block_D = Block()
block_D.id = 4
block_D.parent_id = block_C.id
block_D.history = 'Li Si received three hundred yuan'
block_D.parent_hash = hashlib.sha256(json.dumps(block_C.__dict__).encode('utf-8')).hexdigest()

block_E = Block()
block_E.id = 5
block_E.parent_id = block_B.id
block_E.history = 'Li Si gives Zhang three fish'
block_E.parent_hash = hashlib.sha256(json.dumps(block_D__dict__).encode('utf-8')).hexdigest()

With hash, if any block is modified, the hash of the current block and subsequent blocks must be modified, such as Li Siba block_C's history is changed to "Zhang San gives Li 40 or 50 yuan". Then we see that Li Si must block_C to block_ The hash of E has been changed. If the chain is very long, it will be very difficult for Li Si to change it. At the same time, in order to ensure the reliability of information,
Zhang San and Li Si need to send the above blockchain to 100 other people for storage. In this way, it is more difficult for Zhang San or Li Si to default because there are 100 witnesses.

The feature of blockchain information records is that they are only added but not deleted. Therefore, if Zhang San wants to default, he can only add another block based on the above, that is, block_F. The history inside says "Zhang San receives Li Si's two fish", and then sends this block to other 100 people. In order to prevent information confusion caused by arbitrarily adding blocks, there is a special role in the blockchain responsible for adding new blocks to the existing blockchain. After receiving the block data, this role does not consider the parent_ In the case of hash field, serialize the data, and then find a specific string. This string must meet the given requirements, that is, after it is combined with the block serialized data, the calculated hash value must start with five zeros. Let's see what this means from the code:

#proof-of-work
block_F = Block()
block_F.id = 6
block_E.parent_id = block_E.id
block_E.history = 'Li Si gives Zhang three fish'
#Note that we do not set parent here_ Hash field
block_F_serialized = json.dumps(block_F.__dict__).encode('utf-8')
print(block_F_serialized)
for i in range(10000000):
    proof_of_work = str(i).encode('utf-8')
    result = hashlib.sha256(block_F_serialized + proof_of_work).hexdigest()
    if result[:5] == '00000': #The hash result can be added to the public chain only if it starts with 5 zeros
        print(proof_of_work)
        print(result)
        break 
    #Find a specific string and get a return. The so-called mining is to do this

After the above code is run, the result is:

b'{"id": 6, "history": null, "parent_id": null, "parent_hash": null}'
b'553448'
0000034ba1dabbf794212082b47a6bcc98cb33eed86d363993270ca58e243bb9

In other words, the specific string content is "553448", which can make the hash calculated after the new block content is combined with it start with five zeros. The role specially responsible for finding this string for the block is called "miner". This search process is called mining. Once the specific string is found, he can get a return, that is, cryptocurrency.

Now we have realized that it is difficult to modify the data. At the same time, the addition of blocks also needs to pay a certain cost. However, suppose that Li Si is desperate to modify the previously recorded information and is willing to pay all costs. Suppose that there are 1000 data blocks in the current blockchain, he wants to modify the information recorded in the first block, so he modifies the data of the next 999 data blocks, However, the original data is held by others, so the data modified by him will not be adopted. Since the blockchain data is stored in different places, the data may be modified in a certain place, resulting in data inconsistency. Another important task of the blockchain is to reach a consensus in this case.

At the same time, when a new block needs to be added to the public chain, we need to notify everyone of the new block by broadcasting, so there is a problem, that is, some people get the notification earlier, some get the notification later, and you are more likely to receive multiple messages at the same time. Suppose that the last message on the public chain is numbered 5, At this time, you receive two messages at the same time. The message from the East is history: Wang Wu wants to buy a kilo of shrimp with Li Si; The message from the west is history: Li Liu wants to buy two fish with Zhang San, so which message should we use as number 6? At this time, the practice is to wait first. If five messages are sent from the West and only two messages are sent from the East, discard the less messages and add the more messages to the public chain after processing.

One advantage of adding more data to the public chain is that it makes it almost impossible to modify the data. For example, Li Si worked hard for half an hour to modify 999 blocks and then wanted to broadcast them to others, but it is likely that 10000 new blocks will be generated during this period, so the 999 blocks he modified will be discarded, so he can't make any modification. In fact, this process involves a very complex distributed computing theory, which we can't simply implement in code.

The design idea of blockchain is very great. It integrates the crystallization of many wisdom, such as encryption algorithm, distributed algorithm, psychology, economics, etc. because it spans too many fields, which is also the reason for its special "metaphysics". We will only think about it from the perspective of technology to see the algorithm principle adopted by blockchain, At the same time, it also gradually discusses how to carry out application development based on blockchain.

Keywords: Python Blockchain Algorithm

Added by Paul15679 on Mon, 24 Jan 2022 16:25:56 +0200