We can see how popular blockchain is now by looking at the price of bitcoin. However, as a qualified programmer, we should not only pay attention to the value of currency (why I didn't buy bitcoin, T-T), but also pay attention to the essence of technology, the blockchain technology known as the "fourth industrial Revolution". However, many people, including myself, do not know much about this technology. Since you don't understand, it's better to roll one by yourself, and practice makes true knowledge. I just found this article when I read it. Let's build a simple blockchain by ourselves. In short, blockchain is an immutable and orderly chain structure, in which records called blocks are stored. These records can be transactions, files or any data you want.
The important thing is that they are linked together by hash. Here we will implement a blockchain in Python, so we need to install Python first. Here I use python2.7, and I need to install flask and requests libraries. We first create a blockchain class and create two empty lists in the constructor, one for storing our blockchain and the other for storing transactions. Our blockchain class is used to manage the chain. It will store transaction information and some auxiliary methods to add new blocks to the chain. Let's start implementing these methods.
Each block contains the following attributes: index, timestamp (UNIX time), transactions, proof of workload (explained later) and the hash value of the previous block. Here is an example of a block:. Now, the concept of chain should be very clear – Each new block contains the hash value of the previous block. This is important because it ensures the immutability of the whole blockchain: if an attacker destroys a previous block, the hash value of all subsequent blocks will be wrong. Didn't understand? Stop and think about it. This is the core idea behind blockchain.
On New_ After adding a transaction record to the list, the transaction method returns the index of the block to which the record will be added – The next block to be excavated. This will be useful later when users submit transaction records. When our blockchain is instantiated, we need to create a creation block for it (the first block, without the previous block, is similar to the head of the linked list), and add a proof of work to the creation block. The proof of work is the result of mining. We will discuss mining later. The above code should be intuitive. We have almost finished the representation of blockchain.
However, you should be curious about how new blocks are created or dug out. Proof of work (POW) is simply a certificate that can be used to confirm that you have done a certain amount of work. The goal of workload proof is to find a number that can solve a problem. This number is hard to find but easy to prove (this number is the answer to the problem). This is the core idea behind workload proof (commonly known as mining). Suppose there is a problem: multiply an integer x by an integer y, hash the product, and the hash value must end with 0, that is, hash (x*y) =ac23dc… 0
Let's assume that the value of X is 5, and find the value of Y? It is implemented in Python as follows:. The workload proof algorithm used in bitcoin is called hashcash, which is not much different from our simple example above. Miners need to use this algorithm to participate in computing competition in order to obtain the right to create new blocks. Usually, the difficulty of the problem depends on the number of characters that need to be found in the string. After the miner calculates the result, he will get the corresponding reward coin. Tips: if any node in the bitcoin network wants to generate a new block and write it into the blockchain, it must be able to solve the workload proof problem given by the bitcoin network.
The three key elements of this problem are workload proof function, block and difficulty value. The workload proof function is the calculation method of this problem. The block determines the input data of this problem, and the difficulty value determines the amount of calculation required for this problem. Whoever solves the problem first can generate a new block and write it into the blockchain. In order to adjust the difficulty of the algorithm, we can modify the number of zeros that need to be matched. But 4 is enough. You will find that adding a number will greatly increase the calculation time. We will use the pythonflash framework, a lightweight web application framework that can easily map requests to Python functions, which allows us to interact with the blockchain through HTTP requests.
Note that the receiver of the dug new block is the address of our server node. Most of the work we do is to call some methods of the blockchain class. At this point, we can start to interact with our blockchain. After I restart the server and dig two blocks, there are now a total of three blocks (including the creation block), and we can obtain all block information through request. We have a basic blockchain that can accept transactions and mining. But the blockchain system should be decentralized. Since the blockchain is decentralized, how can we ensure that all nodes have the same chain? This is the consistency problem. If we want to run multiple nodes in our blockchain network, we need to implement a consistency algorithm.
Before implementing the consistency algorithm, we need to find a way to let a node know its adjacent nodes. Every node in our network needs to save a record of other nodes. So let's add some interfaces:. As mentioned earlier, conflicts occur between nodes with different chain information. In order to solve the conflict, we define a rule: the longest and effective chain is the chain of authority, in other words, the longest chain in the network is the actual chain. Through this algorithm, we make all nodes in the network consistent. valid_ The chain () method is mainly responsible for checking whether the chain is valid, going through all blocks in the chain, and checking whether the hash value of the block and the workload proof are correct.
resolve_ Conflicts () is responsible for resolving conflicts by going through nearby nodes and passing through valid_ The chain method verifies whether the chain of the node is correct. If the chain of the node is correct and the chain is longer than the chain length of our node, we will replace our chain with this chain. You can run nodes on different machines. Here we start different ports on one machine to represent different nodes. Suppose we have two nodes:. Then dig some blocks in the second node (Port 5001) to ensure that the chain length of this node is longer than that of the first node. Then call the get/nodes/resolve request on the first node, and you can see that the chain of node 1 has been replaced by the consistency algorithm.