The blockchain network is decentralized, which means that the blockchain is not generated based on a central node, but rather many decentralized nodes participate in maintenance together.
In this section, we will simulate a simple decentralized network based on Python. Multiple nodes in this simulated decentralized network can run on the same computer, but each node uses a different local port number. Each node runs in a separate thread, acting as an independent node. In terms of functionality, these independent nodes run separately and do not affect each other. However, in terms of the blockchain, these nodes cooperate with each other to maintain the correctness of the blockchain, verify blocks, and generate new blocks to extend the length of the entire chain.
(1) Define Nodes
To implement this decentralized network, we need to define a global variable to store all the nodes on the blockchain, and then define the structure of a node. Each node contains a unique port, node name, a unique wallet, and a copy of the blockchain. The code is as follows:
(2) Start Nodes
When each node starts, it initializes the blockchain information and continuously listens on the specified port to process requests from other nodes. The code is as follows:
(3) Initialize the Blockchain
The process of initializing the blockchain is to first determine if there are other nodes in the blockchain network. If there are, it sends an initialization request to request the blockchain information of that node and synchronizes it to the local node. If it is the first node in the network, it needs to initialize a genesis block. The specific code is as follows:
(4) Process Requests
After initializing the blockchain, each node will continue to run and process requests sent by other nodes. The requests received by the node can be divided into three cases. The first case is an initialization request, and after receiving this request, the node will return the local blockchain information. The second case is the broadcast of a new transaction. After receiving this type of request, the node verifies the validity of the transaction. If it is valid, it mines a new block and adds it to the local blockchain, and then broadcasts it to the entire network. The third case is the broadcast of a new block. After receiving this type of request, the node first verifies the validity of the block. If it is valid, it adds it to the local blockchain (in a real network, it may also need to check if this new block already exists in the current blockchain, and not add it again). The specific code is as follows:
Processing requests from other nodes
(5) Broadcast Data
The function defined in the following code, broadcast_new_block, is used to broadcast a new block to the decentralized network. It loops through the entire node list and sends to all nodes except itself. The code is as follows:
In addition to the above functions, a node should also be able to submit a transaction and broadcast it to other nodes in the decentralized network. The code is as follows:
After completing the above work, the functionality of this blockchain network can be tested. First, initialize a node 1 and run this node on port 8000, and print the blockchain information on this node. You can see that this blockchain contains a genesis block, as shown in the figure:
Initialize Node 1
Then create a node 2 and run it on port 8001, and print the blockchain information on this node. You should be able to see that this blockchain also contains a genesis block, which is synchronized from node 1, as shown in Figure 4-66.
At this point, output the cryptocurrency situation of the two nodes. You can see that node 1 has obtained 1 cryptocurrency after generating the genesis block, while node 2 does not have any cryptocurrency, as shown in the figure:
Test the transaction function. Transfer 0.3 cryptocurrency from the account of node 1 to the account of node 2 and submit it to the decentralized network. As shown in the figure:
Test the transaction function
Then output the blockchain situation of the two nodes again. You can see that both nodes have two blocks, as shown in the figure:
At this time, output the cryptocurrency situation of the nodes. Node 1's account should have 0.7 cryptocurrency after transferring 0.3 cryptocurrency, while node 2's account should have 0.3 cryptocurrency from the transfer and 1 cryptocurrency from mining, totaling 1.3 cryptocurrency, as shown in the figure:
Print the cryptocurrency situation after the transaction
At this point, a simple but relatively complete blockchain network is completed. I hope that readers can grasp the implementation method of the basic functions of the blockchain from this Python implementation. Interested readers can improve and optimize based on this prototype, or implement another version of the blockchain prototype based on their familiar programming language. The blockchain-related code implemented in Python has been uploaded to GitHub.