banner
leaf

leaf

It is better to manage the army than to manage the people. And the enemy.

Implementing a simple decentralized network

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 its maintenance.

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 as an independent thread, equivalent to 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 first 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:

image

(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:

image

(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:

image

(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 types. The first type is an initialization request, and after receiving this request, the node will return the local blockchain information. The second type is a 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, broadcasting it to the entire network. The third type is a 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 if it does, it will not be added again). The specific code is as follows:

image

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, as shown in the figure:

image

In addition to the above functions, nodes should also be able to submit a transaction and broadcast it to other nodes in the decentralized network. The code is as follows:

Transaction Submission Function

After completing the above work, you can test the functionality of this blockchain network. 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:

image

Initialize node 1

Then create a node 2 and run it on port 8001, and print the blockchain information of 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 time, output the encrypted digital currency situation of the two nodes. You can see that after node 1 generates the genesis block, it obtains 1 encrypted digital currency, while node 2 does not have any encrypted digital currency, as shown in the figure:

image

Test transaction functionality

Next, test the transaction function. Transfer 0.3 encrypted digital currency from the account of node 1 to the account of node 2 and submit it to the decentralized network. As shown in the figure:

image

Output the blockchain situation of the two nodes again. You can see that both nodes have two blocks, as shown in the figure:

image

At this time, the situation of the encrypted digital currency of the nodes should be that after the account of node 1 transfers 0.3 encrypted digital currency, it becomes 0.7 encrypted digital currency, and the account of node 2 receives 0.3 encrypted digital currency through the transfer and obtains 1 encrypted digital currency through mining, totaling 1.3 encrypted digital currency, as shown in the figure:

image

Print the situation of encrypted digital currency after the transaction

At this point, a simple but relatively complete blockchain network is completed. I hope that readers can learn the implementation method of basic blockchain functions from this Python implementation of the blockchain. Interested readers can improve and optimize it 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.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.