Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the P2P network of blockchain?

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

The knowledge points of this article "what is the P2P network of blockchain" are not quite understood by most people, so the editor summarizes the following contents, with detailed contents and clear steps, which can be used for reference. I hope you can get something after reading this article. Let's take a look at this "what is the P2P network of blockchain" article.

1P2P network

If we take a simple look at P2P technology, it has a wide range of applications, from streaming media to peer-to-peer communication, from file sharing to collaborative processing.

Similarly, there are many P2P network protocols, the more common are BitTorrent, ED2K, Gnutella, Tor and so on, that is, we often talk about BT tools and electric donkeys.

Many digital currencies, such as Bitcoin and Ethernet Fang, have implemented their own P2P network protocols, but this model is different from the P2P network protocols discussed above, so this lecture mainly focuses on the P2P technology of blockchain technology. that is, the P2P network of Bitcoin and ethernet.

2 network connection and topology

1. Network connection

Except for a few blockchain projects that support UDP protocol, the underlying network protocol used by most blockchain projects is still TCP/IP protocol.

So from the perspective of network protocol, blockchain is actually based on TCP/IP network protocol, which is in the same layer as HTTP protocol and SMTP protocol, that is, the application layer.

In the article "Common misunderstandings of blockchain", we mentioned the saying "whether blockchain will subvert the Internet". If we analyze it carefully, it can only subvert the HTTP protocol at most, not any more.

The mode of interaction with the server represented by the HTTP protocol has been completely broken on the block chain and changed to a complete peer-to-peer topology, which is also the origin of the Web3.0 proposed by Etaifang.

The P2P network of Bitcoin is a very complex structure, taking into account the mining interaction protocol and light nodes within the mining pool. We only discuss P2P network discovery and routing in the full-node scenario.

The P2P network of Bitcoin is based on TCP, and the default communication port of the main network is 8333.

The P2P network of Yi Tai Fang is different from Bitcoin. Yi Tai Fang P2P network is a fully encrypted network, which provides both UDP and TCP connections. The default TCP communication port of the main network is 30303, and the recommended UDP discovery port is 30301.

two。 Topological structure

There are many kinds of P2P network topologies, some are centralized, some are semi-centralized, and some are fully distributed.

The network composed of all Bitcoin nodes is a fully distributed topology, and the transmission process between nodes is more similar to the "flooding algorithm", that is, transactions are generated from a node, then broadcast to neighboring nodes, and the neighboring nodes spread all the way to the whole network.

The interaction mode between the full node and the SPV payment verification client is closer to the semi-centralized topology, that is, the SPV node can randomly select a full node to connect, and this full node will become the agent of the SPV node to help the SPV node broadcast transactions.

3-node discovery

Node discovery is the first step for any blockchain node to access the blockchain P2P network. This is the same as traveling to a strange place alone, if you don't have a map or navigation, you can only drag people nearby to ask for directions. The action of "dragging people nearby to ask for directions" can be understood as node discovery.

Node discovery can be divided into initial node discovery and post-startup node discovery. The initial node discovery means that your whole node has just been downloaded and runs for the first time without any node data. After startup, it is found that the running wallet has been able to follow the network to dynamically maintain available nodes.

1. Initial node discovery

In a bitcoin network, the initial node finds that there are two ways.

The first is called DNS-seed, also known as DNS seed node. DNS is a centralized domain name query service, and Bitcoin community maintainers maintain some domain names.

For example, the domain name seed.bitcoin.sipa.be is maintained by Sipa, the core developer of Bitcoin. If we use nslookup, we will find about 20 A-record IPv4 host addresses.

If we try to connect to port 8333 of a host under the domain name through the nc command, we will find that the connection is successful. The running structure is as follows.

$nc-nvv 149.202.179.35 8333found 0 associationsfound 1 connections: 1: flags=82 outif en0 src 192.168.1.104 port 62125 dst 149.202.179.35 port 8333 rank info not available TCP aux info availableConnection to 149.202.179.35 port 8333 [tcp/*] succeeded!

The second way is to hard-code some addresses in the code, which we call seed nodes (seed-node). When all seed nodes fail, the whole node will try to connect to them.

Used in Ethernet Square, the idea is roughly the same, but also in the code hard-coded (hard-code) some seed nodes to do similar work.

two。 Node discovery after startup

In Bitcoin's network, a node can send its own peer-to-peer list (peer list) to neighboring nodes, so after the initial node is discovered, the first thing your node needs to do is to ask for the list: "make a copy of your node list for me."

So every time it needs to send a protocol message, it will spend a fixed time trying to establish a link with the nodes in the existing node list. If any node can connect before timeout, it does not have to go to DNS seed to get the address. Generally speaking, this possibility is very small, especially when the number of all nodes is very large.

In the Ethernet Fong network, a similar node list (NodeTable) is maintained, but this node list is different from the simple maintenance of bitcoin, it uses a mature algorithm in the P2P network protocol, called Kademlia network, referred to as KAD network.

It uses DHT to locate resources, the full name is Distributed Hash Table, the Chinese name is distributed hash table. The KAD network maintains a routing table that is used to quickly locate target nodes. Because the KAD network is based on the UDP communication protocol, the node discovery of the Ethernet node is based on UDP. If the node is found, the data exchange will switch to the TCP protocol.

3. Blacklist and long connection

The network environment that the public block chain faces is very open, as long as anyone downloads the wallet, opens it and runs it, it will enter the P2P network, which will also bring the possibility of being attacked.

So in the bitcoin code, there will be a piece of control logic, and you can manually remove the nodes you think are suspicious and add them to the forbidden list, while configuring trusted nodes. Of course, the above is not part of the standard protocol of the client, and anyone can implement their own P2P network layer.

There is blacklist processing for accounts on Ethernet Square, but this belongs to the business layer. I haven't found any detailed information, so if you're interested, you can try it yourself.

But generally speaking, the blacklist can also be dealt with through the operating system's firewall, which is not a particularly thorny problem.

4 local area network penetration

We mentioned earlier that the P2P network structure of blockchain is a fully distributed topology. However, nowadays our network environment is made up of local area network and Internet. In other words, when you run a blockchain node in the local area network, it cannot be found in the public network, and the nodes on the public network can only passively accept the connection and cannot initiate the connection actively.

If you can control this LAN, then it is good to say that we only need to configure routing in the VPC network to map the public network IP and port to your IP and port in the LAN.

This condition is very stringent, so is there a solution that can create its own mapping? The answer is: yes, it is NAT technology and UPnP protocol.

NAT technology is very common, and the source NAT is used here, which in short is to replace the source address in the TCP message and map it to the intranet address.

UPnP is the abbreviation of Universal plug and play (Universal Plug and Play). It is mainly used for the smart interconnection of devices, and all devices on the network will immediately know that new devices have joined.

These devices can communicate with each other and can be used or controlled directly. Nothing needs to be set up manually. There is a lot of information about UPnP, so I won't repeat it here. You can search for relevant information on your own.

Both Bitcoin and Ethernet Square use UPnP protocol as a LAN penetration tool. As long as the routing devices in the LAN support NAT gateway function and UPnP protocol, your block chain nodes can be automatically mapped to the public network.

5-node interaction protocol

Once the node has established a connection, the interaction between the nodes follows some specific commands, which are written in the header of the message, and the message body writes the content of the message.

There are two kinds of commands, one is request command, the other is data exchange command.

The first thing a node needs to do when it is connected is called a handshake. The process is similar in Bitcoin and Ethernet Square, which is to greet each other and provide some brief information.

For example, first exchange the version number to see if it is compatible. It's just that Taifang provides symmetrical encryption for the handshake process, while Bitcoin doesn't.

After the handshake, no matter what information you exchange, you need to maintain a long connection. There are two types of PING/PONG messages on Bitcoin, which are obviously designed to maintain the heartbeat of long connections between nodes; in the design of Ethernet Square, the PING/PONG protocol is moved to the process of node discovery.

Request commands are generally divided into initiator requests. For example, the getaddr command in Bitcoin is to obtain the list of available nodes of the other party, while the inv command provides data transmission, and the message body contains a data vector.

We say that the most important function of block chain is synchronous block chain, and synchronous block happens to be the most test of P2P network ability. There are two kinds of block synchronization methods. The first is called HeaderFirst, which provides block head synchronization first, and then obtains the block block from other nodes after synchronization is completed.

The second is called BlockFirst, and this way of block synchronization is relatively simple and rough, that is, getting blocks from other nodes must be complete. The first scheme provides a better interaction process and reduces the burden of the network. These two synchronization methods are directly reflected in the node interaction protocol, and they use completely different command logic.

The above is the content of this article on "what is the P2P network of blockchain". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report